Code Monkey home page Code Monkey logo

practical-applications-in-r-for-psychologists's Introduction

Practical Applications in R for Psychologists


Last updated 2023-09-03.

This Github repo contains all lesson files for Practical Applications in R for Psychologists. The goal is to impart students with the basic tools to process data, describe data (w/ summary statistics and plots), and the foundations of building, evaluating and comparing statistical models in R, focusing on linear regression modeling (using both frequentist and Bayesian approaches).

These topics were taught in the graduate-level course Advanced Research Methods for Psychologists (Psych Dep., Ben-Gurion University of the Negev), laying the foundation for the following topic-focused courses:

Notes:

  • This repo contains only materials relating to Practical Applications in R. Though statistics are naturally discussed in many lessons, the focus is generally on the application and not on the theory.
  • Please note that some code does not work on purpose and without warning, to force students to learn to debug.

Setup

You will need:

  1. A fresh installation of R (preferably version 4.1.1 or above).
  2. RStudio IDE (optional, but recommended).
  3. The following packages, listed by lesson:
Lesson Packages
01 intro
02 data wrangling haven, tidyverse, readxl, dplyr, datawizard, summarytools, parameters, psych, finalfit, Hmisc, mice
03 plotting dplyr, ggplot2, ragg, tidyr
04 hypothesis testing and power effectsize, correlation, BayesFactor, dplyr, pwr, ggplot2
05 regression 101 effectsize, parameters, performance, ggeffects, psychTools
06 categorical predictors and model comparison dplyr, parameters, emmeans, ggeffects, bayestestR, performance
07 moderation and curvilinear dplyr, datawizard, parameters, performance, bayestestR, emmeans, ggeffects, ggplot2, modelbased
08 generalized linear models dplyr, parameters, performance, ggeffects, emmeans, marginaleffects
09 assumption checks and violations ggeffects, performance, see, bayesplot, qqplotr, datawizard, permuco, parameters, insight
10 ANOVA afex, emmeans, effectsize, ggeffects, tidyr
11 mediation mediation, tidySEM

(Bold denotes the first lesson in which the package was used.)

You can install all the packages used by running:

# in alphabetical order:

pkgs <- c(
  "afex", "BayesFactor", "bayesplot", "bayestestR", "correlation",
  "datawizard", "dplyr", "effectsize", "emmeans", "finalfit", "ggeffects",
  "ggplot2", "haven", "Hmisc", "insight", "marginaleffects", "mediation",
  "mice", "modelbased", "parameters", "performance", "permuco",
  "psych", "psychTools", "pwr", "qqplotr", "ragg", "readxl", "see",
  "summarytools", "tidyr", "tidySEM", "tidyverse"
)

install.packages(pkgs, repos = c("https://easystats.r-universe.dev", getOption("repos")))
Package Versions

Run on Windows 11 x64 (build 22621), with R version 4.3.1.

The packages used here:

  • afex 1.3-0 (CRAN)
  • BayesFactor 0.9.12-4.4 (CRAN)
  • bayesplot 1.10.0 (CRAN)
  • bayestestR 0.13.1.2 (Local version)
  • correlation 0.8.4 (CRAN)
  • datawizard 0.8.0.7 (Local version)
  • dplyr 1.1.2 (CRAN)
  • effectsize 0.8.5 (Local version)
  • emmeans 1.8.7 (CRAN)
  • finalfit 1.0.6 (CRAN)
  • ggeffects 1.3.0.5 (Github: strengejacke/ggeffects)
  • ggplot2 3.4.3 (CRAN)
  • haven 2.5.3 (CRAN)
  • Hmisc 5.1-0 (CRAN)
  • insight 0.19.3.3 (Github: easystats/insight)
  • marginaleffects 0.13.0 (CRAN)
  • mediation 4.5.0 (CRAN)
  • mice 3.16.0 (CRAN)
  • modelbased 0.8.6 (CRAN)
  • parameters 0.21.1 (CRAN)
  • performance 0.10.4 (CRAN)
  • permuco 1.1.2 (CRAN)
  • psych 2.3.6 (CRAN)
  • psychTools 2.3.6 (CRAN)
  • pwr 1.3-0 (CRAN)
  • qqplotr 0.0.6 (CRAN)
  • ragg 1.2.5 (CRAN)
  • readxl 1.4.3 (CRAN)
  • see 0.8.0.2 (Local version)
  • summarytools 1.0.1 (CRAN)
  • tidyr 1.3.0 (CRAN)
  • tidySEM 0.2.4 (CRAN)
  • tidyverse 2.0.0 (CRAN)

practical-applications-in-r-for-psychologists's People

Contributors

mattansb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

practical-applications-in-r-for-psychologists's Issues

Changes to 2021

Each lesson should have a story driving the data operations...

1. Intro to R

(2 lessons)

Lesson 1

  • Operations
  • types
  • lists
  • sub-setting, pulling, replacing
  • (Don't forget to assign)

Lesson 2

  • refresher
  • for ()
  • if () and ifelse()
  • functions
  • [ ] apply (or purrr::map()) (optional)

2. Data wrangling

(2-3 lessons)

Explain how we normally work in R
image

Lesson 3

  • data frames
  • importing
  • dplyr and co
  • joining and making data long / wide
  • exporting (csv, rds, even sav)

Lesson 4

  • describing data
  • summarizing data
    • reliability (psych)
  • Dealing with outliers (optional)
  • Dealing with missing data (optional)

3. Plotting

(1 lesson)

Lesson 5

  • ggplot2 <3

4. NHST

(1 lesson)

Lesson 6

  • htest-functions + correlations (psych)
  • Bayesian alts
  • effectsizes
  • power (optional)
    • simulation...

5. Modelling

(5 lesson)
All models will have accompanying plots with ggeffects.

Lesson 7

  • linear regression
  • parameters
  • standardized coefficients ("beta")
  • predict() + newdata=
  • plot

Lesson 8

  • dummy
  • plot
  • emmeans and contrasts
  • comparing models
    • indices of fit
    • Bayesian

Lesson 9

  • moderation
    • What is this y ~ a + b*c / y ~ (a + b)*c / y ~ a*b*c
    • the parameters and also who cares about them?
    • centering
  • simple slopes (by cat / cont) with emmeans
  • plot with ggeffects (+ with interactions?)
  • curvilinear / poly

Lesson 10

  • (rm-)ANOVA
    • long data
  • interaction
    • Link to more....

Lesson 11

6. Assumptions

(1 lesson)

For fixed effect linear models...

Lesson 12

  • assumption checks
    • Replace effects with ggeffects?
  • permutation test + basic bootstrap

7. Other topics (optional)

  • mediation (optional) (move to SEM?)
  • Factor analyses? (already in SEM and partially in ML)

Bayes

# install.packages("BAS")
data(UScrime, package = "MASS")

UScrime[, -2] <- log(UScrime[, -2])

head(UScrime)

library(BAS)
crime.ZS <- bas.lm(y ~ Time + Prob + Ineq + Pop + GDP,
                   
                   data = UScrime,
                   
                   # include.always = ~ Time,
                   
                   # initprobs = c(0.1, 0.1),
                   modelprior = uniform(),
                   # modelprior = Bernoulli(),
                   
                   
                   force.heredity = TRUE,
                   prior = "ZS-null")

library(BayesFactor)
BFs <- regressionBF(y ~ Time + Prob + Ineq + Pop + GDP,
                    data = UScrime)
BFs
BF_bas <- summary(crime.ZS, n.models = 10)[5,-1]
BF_bas/BF_bas[4]


crime.ZS$model
coef(crime.ZS, n.models = 8)


bayestestR::bf_inclusion(BFs)

tableone

library(tableone)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

CreateTableOne(data = iris, strata = "Species")
#>                           Stratified by Species
#>                            setosa        versicolor    virginica     p     
#>   n                          50            50            50                
#>   Sepal.Length (mean (SD)) 5.01 (0.35)   5.94 (0.52)   6.59 (0.64)   <0.001
#>   Sepal.Width (mean (SD))  3.43 (0.38)   2.77 (0.31)   2.97 (0.32)   <0.001
#>   Petal.Length (mean (SD)) 1.46 (0.17)   4.26 (0.47)   5.55 (0.55)   <0.001
#>   Petal.Width (mean (SD))  0.25 (0.11)   1.33 (0.20)   2.03 (0.27)   <0.001
#>   Species (%)                                                        <0.001
#>      setosa                  50 (100.0)     0 (  0.0)     0 (  0.0)        
#>      versicolor               0 (  0.0)    50 (100.0)     0 (  0.0)        
#>      virginica                0 (  0.0)     0 (  0.0)    50 (100.0)        
#>                           Stratified by Species
#>                            test
#>   n                            
#>   Sepal.Length (mean (SD))     
#>   Sepal.Width (mean (SD))      
#>   Petal.Length (mean (SD))     
#>   Petal.Width (mean (SD))      
#>   Species (%)                  
#>      setosa                    
#>      versicolor                
#>      virginica


mtcars %>% 
  mutate(across(c(cyl, am, vs, gear, carb), ordered)) %>% 
  CreateTableOne(data = ., strata = "cyl") %>% 
  print(nonnormal = c("hp", "drat"))
#>                      Stratified by cyl
#>                       4                     6                      
#>   n                       11                     7                 
#>   mpg (mean (SD))      26.66 (4.51)          19.74 (1.45)          
#>   cyl (%)                                                          
#>      4                    11 (100.0)             0 (  0.0)         
#>      6                     0 (  0.0)             7 (100.0)         
#>      8                     0 (  0.0)             0 (  0.0)         
#>   disp (mean (SD))    105.14 (26.87)        183.31 (41.56)         
#>   hp (median [IQR])    91.00 [65.50, 96.00] 110.00 [110.00, 123.00]
#>   drat (median [IQR])   4.08 [3.81, 4.16]     3.90 [3.35, 3.91]    
#>   wt (mean (SD))        2.29 (0.57)           3.12 (0.36)          
#>   qsec (mean (SD))     19.14 (1.68)          17.98 (1.71)          
#>   vs = 1 (%)              10 ( 90.9)             4 ( 57.1)         
#>   am = 1 (%)               8 ( 72.7)             3 ( 42.9)         
#>   gear (%)                                                         
#>      3                     1 (  9.1)             2 ( 28.6)         
#>      4                     8 ( 72.7)             4 ( 57.1)         
#>      5                     2 ( 18.2)             1 ( 14.3)         
#>   carb (%)                                                         
#>      1                     5 ( 45.5)             2 ( 28.6)         
#>      2                     6 ( 54.5)             0 (  0.0)         
#>      3                     0 (  0.0)             0 (  0.0)         
#>      4                     0 (  0.0)             4 ( 57.1)         
#>      6                     0 (  0.0)             1 ( 14.3)         
#>      8                     0 (  0.0)             0 (  0.0)         
#>                      Stratified by cyl
#>                       8                       p      test   
#>   n                       14                                
#>   mpg (mean (SD))      15.10 (2.56)           <0.001        
#>   cyl (%)                                     <0.001        
#>      4                     0 (  0.0)                        
#>      6                     0 (  0.0)                        
#>      8                    14 (100.0)                        
#>   disp (mean (SD))    353.10 (67.77)          <0.001        
#>   hp (median [IQR])   192.50 [176.25, 241.25] <0.001 nonnorm
#>   drat (median [IQR])   3.12 [3.07, 3.22]      0.001 nonnorm
#>   wt (mean (SD))        4.00 (0.76)           <0.001        
#>   qsec (mean (SD))     16.77 (1.20)            0.002        
#>   vs = 1 (%)               0 (  0.0)          <0.001        
#>   am = 1 (%)               2 ( 14.3)           0.013        
#>   gear (%)                                     0.001        
#>      3                    12 ( 85.7)                        
#>      4                     0 (  0.0)                        
#>      5                     2 ( 14.3)                        
#>   carb (%)                                     0.007        
#>      1                     0 (  0.0)                        
#>      2                     4 ( 28.6)                        
#>      3                     3 ( 21.4)                        
#>      4                     6 ( 42.9)                        
#>      6                     0 (  0.0)                        
#>      8                     1 (  7.1)

Created on 2021-02-22 by the reprex package (v1.0.0)

mediation

specify the levels of treat! (defaults to 0,1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.