Overview

2017-10-06

psycho for R: an Overview

Installation

# Do this once (uncomment if needed)
# install.packages("devtools") 
# library(devtools)
# devtools::install_github("https://github.com/neuropsychology/psycho.R")

# Load psycho (at the beginning of every script)
library(psycho)

General Workflow

The package mainly revolves around the psychobject. Main functions from the package return this type, and the analyze() function transforms other R objects (for now, only stan_lmer type) into psychobjects. Then, 4 functions can be applied on a psychobject: summary(), print(), plot() and values().

Examples

Correlation Table and Plot

library(psycho)

df <- iris

cor <- psycho::correlation(df)

cortable <- print(cor)

# You can save it using write.csv(cortable, "correlation_table.csv")
# Or diplay it using View(cortable)

kable(cortable)
Sepal.Length Sepal.Width Petal.Length
Sepal.Length NA NA NA
Sepal.Width -0.12 NA NA
Petal.Length 0.87*** -0.43*** NA
Petal.Width 0.82*** -0.37*** 0.96***
# You can also plot it
cor$plot()

Normalize/Z-score/Scale

library(psycho)
library(tidyverse)

# Normalize all numeric variables
df <- iris %>% 
  psycho::normalize()

summary(df)
##        Species    Sepal.Length       Sepal.Width       Petal.Length    
##  setosa    :50   Min.   :-1.86378   Min.   :-2.4258   Min.   :-1.5623  
##  versicolor:50   1st Qu.:-0.89767   1st Qu.:-0.5904   1st Qu.:-1.2225  
##  virginica :50   Median :-0.05233   Median :-0.1315   Median : 0.3354  
##                  Mean   : 0.00000   Mean   : 0.0000   Mean   : 0.0000  
##                  3rd Qu.: 0.67225   3rd Qu.: 0.5567   3rd Qu.: 0.7602  
##                  Max.   : 2.48370   Max.   : 3.0805   Max.   : 1.7799  
##   Petal.Width     
##  Min.   :-1.4422  
##  1st Qu.:-1.1799  
##  Median : 0.1321  
##  Mean   : 0.0000  
##  3rd Qu.: 0.7880  
##  Max.   : 1.7064

Assess

library(psycho)

results <- psycho::assess(124, mean=100, sd=15)

# Print it
print(results)
## [1] "The participant (score = 124) is positioned at 1.6 standard deviations from the mean (M = 100, SD = 15). The participant's score is greater than 94.51 % of the general population."
# Plot it
plot(results)

Bayesian Mixed Linear Model

library(psycho)
library(rstanarm)

# Create dataframe
df <- data.frame(Participant = as.factor(rep(1:50,each=2)), Condition = base::rep_len(c("A", "B"), 100), V1 = rnorm(100, 30, .2), V2 = runif(100, 3, 5))
df <- psycho::normalize(df)

# Show dataframe
kable(head(df))
Participant Condition V1 V2
1 A 0.8173544 -0.1834544
1 B -0.5505681 -1.6831122
2 A -1.2760210 -0.0120930
2 B -0.2900019 0.3333527
3 A 1.5021987 1.0535220
3 B -1.1998550 -0.7126512
# Fit bayesian mixed model
fit <- rstanarm::stan_lmer(V1 ~ Condition / V2 + (1|Participant), data=df)
results <- psycho::analyze(fit)

# Print summary
kable(summary(results))
Variable MPE Median MAD Mean SD CI_lower CI_higher
(Intercept) 55.575 -0.0176665 0.1375705 -0.0184814 0.1450662 -0.3127607 0.2708144
ConditionB 55.825 0.0274426 0.1875246 0.0223625 0.1957412 -0.3862145 0.4069334
ConditionA:V2 86.825 0.1654395 0.1451932 0.1647602 0.1467532 -0.1231931 0.4546875
ConditionB:V2 86.200 -0.1584952 0.1465662 -0.1582316 0.1454779 -0.4416015 0.1235225
# Show text
print(results)
## [1] "We fitted a Markov Chain Monte Carlo [type] model to predict[Y] with [X] (formula =~).Priors were set as follow: [INSERT INFO ABOUT PRIORS]."                                                                         
## [2] "We fitted a Markov Chain Monte Carlo [type] model to predict[Y] with [X] (formula =V1).Priors were set as follow: [INSERT INFO ABOUT PRIORS]."                                                                        
## [3] "We fitted a Markov Chain Monte Carlo [type] model to predict[Y] with [X] (formula =Condition/V2 + (1 | Participant)).Priors were set as follow: [INSERT INFO ABOUT PRIORS]."                                          
## [4] "Concerning the effect of (Intercept), there is a probability of 55.57% that its coefficient is between -0.61 and 0 (Median = -0.018, MAD = 0.14, Mean = -0.018, SD = 0.15, 95% CI [-0.31, 0.27])."                    
## [5] "Concerning the effect of ConditionB, there is a probability of 55.83% that its coefficient is between 0 and 0.8 (Median = 0.027, MAD = 0.19, Mean = 0.022, SD = 0.2, 95% CI [-0.39, 0.41])."                          
## [6] "Concerning the interaction effect between ConditionA and V2, there is a probability of 86.83% that its coefficient is between 0 and 0.75 (Median = 0.17, MAD = 0.15, Mean = 0.16, SD = 0.15, 95% CI [-0.12, 0.45])."  
## [7] "Concerning the interaction effect between ConditionB and V2, there is a probability of 86.2% that its coefficient is between -0.79 and 0 (Median = -0.16, MAD = 0.15, Mean = -0.16, SD = 0.15, 95% CI [-0.44, 0.12])."
# Plot effects
plot(results)