Skip to content

Commit

Permalink
Merge pull request #51 from neuropsychology/dev
Browse files Browse the repository at this point in the history
0.2.3 - CRAN
  • Loading branch information
DominiqueMakowski authored May 11, 2018
2 parents df464f5 + 587e6a8 commit a316879
Show file tree
Hide file tree
Showing 11 changed files with 743 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: psycho
Type: Package
Title: Efficient and Publishing-Oriented Workflow for Psychological Science
Version: 0.2.2
Version: 0.2.3
Authors@R: c(
person("Dominique",
"Makowski",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ importFrom(stats,qnorm)
importFrom(stats,quantile)
importFrom(stats,rchisq)
importFrom(stats,rnorm)
importFrom(stats,runif)
importFrom(stats,sd)
importFrom(stats,terms)
importFrom(stats,update)
importFrom(stats,var)
importFrom(stringr,str_squish)
importFrom(stringr,str_to_title)
importFrom(tibble,rownames_to_column)
importFrom(utils,combn)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `crawford.test` now computes the Bayesian version
- `crawford.test` has been renamed to `crawford.test.freq`
### New functions / parameters
- Added `i_am_cheating` parameter to `correlation` to prevent p-hacking
- Added `percentile` and `percentile_to_z` functions
- Added `as.data.frame` method for density objects
- Added Crawford-Garthwaite (2007) Bayesian test for single-case analysis
Expand Down
4 changes: 2 additions & 2 deletions R/assess.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ assess <- function(patient, mean = 0, sd = 1, n = NULL, controls = NULL, CI=95,
warning("Multiple scores were provided. Returning a list of results.")
}
results <- list()
for (i in patient) {
results[i] <- crawford.test(patient, controls, mean, sd, n, CI, treshold, iter, color_controls, color_CI, color_score, color_size, alpha_controls, alpha_CI)
for (i in 1:length(patient)) {
results[[i]] <- crawford.test(patient[i], controls, mean, sd, n, CI, treshold, iter, color_controls, color_CI, color_score, color_size, alpha_controls, alpha_CI)
return(results)
}
} else {
Expand Down
22 changes: 20 additions & 2 deletions R/correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#' "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none"). See
#' \link[stats]{p.adjust} for details about why to use "holm" rather than
#' "bonferroni").
#' @param i_am_cheating Set to TRUE to run many uncorrected correlations.
#'
#' @return output
#'
Expand All @@ -37,18 +38,20 @@
#'
#' @author \href{https://dominiquemakowski.github.io/}{Dominique Makowski}
#'
#' @importFrom stats na.omit p.adjust cor
#' @importFrom stats na.omit p.adjust cor runif
#' @importFrom psych corr.test
#' @importFrom ggplot2 theme element_text
#' @importFrom stringr str_to_title
#' @import ggcorrplot
#' @import ppcor
#' @import dplyr
#' @export
correlation <- function(df,
df2 = NULL,
type = "full",
method = "pearson",
adjust = "holm") {
adjust = "holm",
i_am_cheating = FALSE) {

# Processing
# -------------------
Expand All @@ -60,6 +63,21 @@ correlation <- function(df,
if (is.null(df2) == FALSE) {
df2 <- df2[, sapply(df2, is.numeric)]
}

# P-fishing prevention
if (ncol(df) > 10 && adjust == "none" && i_am_cheating == FALSE) {
warning("We've detected that you are running a lot (> 10) of correlation tests without adjusting the p values. To help you in your p-fishing, we've added some interesting variables: You never know, you might find something significant!\nTo deactivate this, change the 'i_am_cheating' argument to TRUE.")
df_complete <- dplyr::mutate_all(df, dplyr::funs_("replace(., is.na(.), 0)"))
df$Local_Air_Density <- svd(df_complete)$u[, 1]
df$Reincarnation_Cycle <- runif(nrow(df), max = 100)
df$Communism_Level <- -1 * svd(df_complete)$u[, 2]
df$Alien_Mothership_Distance <- rnorm(nrow(df), mean = 50000, sd = 5000)
df$Schopenhauers_Optimism <- svd(df_complete)$u[, 3]
df$Hulks_Power <- runif(nrow(df), max = 10)
}



# Compute r coefficients
if (type == "full") {
corr <- psych::corr.test(df, y = df2, use = "pairwise", method = method, adjust = "none")
Expand Down
107 changes: 107 additions & 0 deletions docs/_posts/preparation/correlation.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: "Formatted and Flexible Correlation Tables"
layout: post
output:
html_document:
df_print: paged
toc: yes
md_document:
toc: yes
variant: markdown_github
author: "Dominique Makowski"
date: "`r Sys.Date()`"
tags:
- r
- psychology
- neuroscience
- neuropsychology
---


```{r message=FALSE, warning=FALSE, include=FALSE}
library(knitr)
```

# Another *correlation* function?!

Yes, the `correlation` function from the [`psycho`](https://github.com/neuropsychology/psycho.R) package.

```{r fig.align='center', message=FALSE, warning=FALSE, comment=NA, results='hide'}
library(psycho)
library(tidyverse)
cor <- psycho::affective %>%
correlation()
```

This function automatically select numeric variables and run a correlation analysis. It returns a `psychobject`.


# A table

We can then extract a **formatted table** that can be saved and pasted into reports and manuscripts.

```{r fig.align='center', message=FALSE, warning=FALSE, comment=NA, results='hide'}
summary(cor)
# write.csv(summary(cor), "myformattedcortable.csv")
```
```{r echo=FALSE, fig.align='center', message=FALSE, warning=FALSE, comment=NA, results='markup'}
knitr::kable(summary(cor))
```

# A Plot

It integrates a **plot** done with [`ggcorplot`](https://github.com/kassambara/ggcorrplot).

```{r, eval = TRUE, results='markup', fig.align='center'}
plot(cor)
```


# A print

It also includes a **pairwise correlation printing** method.

```{r fig.align='center', message=FALSE, warning=FALSE, comment=NA, results='markup'}
print(cor)
```

# Options

You can also cutomize the **type** (pearson, spearman or kendall), the **p value correction method** (holm (default), bonferroni, fdr, none...) and run **partial, semi-partial or glasso** correlations.


```{r fig.align='center', message=FALSE, warning=FALSE, comment=NA, results='hide'}
psycho::affective %>%
correlation(method = "pearson", adjust="bonferroni", type="partial") %>%
summary()
```
```{r echo=FALSE, fig.align='center', message=FALSE, warning=FALSE, comment=NA, results='markup'}
psycho::affective %>%
correlation(method = "pearson", adjust="bonferroni", type="partial") %>%
summary() %>%
knitr::kable()
```

# Fun with *p*-hacking

In order to **prevent people for running many uncorrected correlation tests** (promoting *p*-hacking and result-fishing), we included the `i_am_cheating` parameter. If FALSE (default), the function will help you finding interesting results!

```{r, eval = TRUE, results='hide', fig.align='center'}
df_with_11_vars <- data.frame(replicate(11, rnorm(1000)))
cor <- correlation(df_with_11_vars, adjust="none")
summary(cor)
```
```{r echo=FALSE, fig.align='center', message=FALSE, warning=FALSE, comment=NA, results='markup'}
knitr::kable(summary(cor)[,1:11])
```


**As we can see, Schopenhauer's Optimism is strongly related to many variables!!!**


# Credits

This package was useful? You can cite [`psycho`](https://github.com/neuropsychology/psycho.R) as follows:

- Makowski, (2018). *The psycho Package: an Efficient and Publishing-Oriented Workflow for Psychological Science*. Journal of Open Source Software, 3(22), 470. https://doi.org/10.21105/joss.00470
588 changes: 588 additions & 0 deletions docs/_posts/preparation/correlation.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/_posts/preparation/package_downloads.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ blogposts_names <- list.files("../") %>%
events <- data.frame(date = c("2018-01-10", "2018-02-05", "2018-03-21"),
label = c("JOSS Review", "JOSS Publication", "Blog Creation"),
color = c("orange", "green", "blue")) %>%
color = c("#2196F3", "#4CAF50", "#FF9800")) %>%
mutate_all(as.character) %>%
bind_rows(
data.frame(date = blogposts_dates,
label = blogposts_names,
color = "red") %>%
color = "#E91E63") %>%
mutate_all(as.character)
) %>%
mutate(date = lubridate::ymd(date)) %>%
Expand Down
4 changes: 3 additions & 1 deletion man/correlation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test-assess.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ test_that("It works", {
)

testthat::expect_equal(x$values$p, 0.18, tol = 0.02)

x <- assess(
patient = c(10, 12),
mean = 8,
sd = 2,
verbose=FALSE
)

testthat::expect_equal(x[[1]]$values$p, 0.16, tol = 0.05)
})
8 changes: 8 additions & 0 deletions tests/testthat/test-odds_to_probs.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ context("odds_to_probs")
test_that("Correct", {
testthat::expect_equal(odds_to_probs(-1.6), 0.17, tolerance = 0.01)
testthat::expect_equal(odds_to_probs(-1.6, log = F), 2.66, tolerance = 0.01)

testthat::expect_equal(
ncol(odds_to_probs(
psycho::affective,
subset=c("Life_Satisfaction"),
except=c("Sex"))),
8)
})

0 comments on commit a316879

Please sign in to comment.