# setup

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(apaTables)
library(rstatix)
## 
## Attaching package: 'rstatix'
## 
## The following object is masked from 'package:stats':
## 
##     filter
library(report)
library(emmeans)
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
library(lme4)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
# cannot use lm() for a within-subjects ANOVA, use lmer() instead

Betting Behavior

Creating the Dataset

dat <- tribble(~participant, ~condition1, ~condition2, ~condition3,
               "A", 1, 4, 7, 
               "B", 4, 8, 6, 
               "C", 2, 7, 9,
               "D", 1, 5, 6
               )

dat_long <- dat %>% 
  pivot_longer(cols = c(condition1, condition2, condition3),
               names_to = "condition",
               values_to = "score")

dat_long
## # A tibble: 12 × 3
##    participant condition  score
##    <chr>       <chr>      <dbl>
##  1 A           condition1     1
##  2 A           condition2     4
##  3 A           condition3     7
##  4 B           condition1     4
##  5 B           condition2     8
##  6 B           condition3     6
##  7 C           condition1     2
##  8 C           condition2     7
##  9 C           condition3     9
## 10 D           condition1     1
## 11 D           condition2     5
## 12 D           condition3     6

Mean & SD

dat_long %>% 
  group_by(condition) %>% 
  summarise(mean = mean(score),
            sd = sd(score))
## # A tibble: 3 × 3
##   condition   mean    sd
##   <chr>      <dbl> <dbl>
## 1 condition1     2  1.41
## 2 condition2     6  1.83
## 3 condition3     7  1.41

Creating the Model

model <- lmer(score ~ condition + (1 | participant),
              data = dat_long)

Normality: Shapiro-Wilk test

dat_long %>% 
  group_by(condition) %>% 
  summarise("S-W Stat" = shapiro_test(score)$statistic,
            "p-value" = shapiro_test(score)$p.value)
## # A tibble: 3 × 3
##   condition  `S-W Stat` `p-value`
##   <chr>           <dbl>     <dbl>
## 1 condition1      0.827     0.161
## 2 condition2      0.950     0.714
## 3 condition3      0.827     0.161

Sphericity

anova_test(data = dat_long,
           dv = score,
           wid = participant,
           within = condition,
           type = 3,
           effect.size = "pes",
           detailed = TRUE)
## ANOVA Table (type III tests)
## 
## $ANOVA
##        Effect DFn DFd SSn SSd    F     p p<.05   pes
## 1 (Intercept)   1   3 300  12 75.0 0.003     * 0.962
## 2   condition   2   6  56  10 16.8 0.003     * 0.848
## 
## $`Mauchly's Test for Sphericity`
##      Effect    W    p p<.05
## 1 condition 0.36 0.36      
## 
## $`Sphericity Corrections`
##      Effect  GGe     DF[GG] p[GG] p[GG]<.05   HFe     DF[HF] p[HF] p[HF]<.05
## 1 condition 0.61 1.22, 3.66 0.017         * 0.808 1.62, 4.85 0.007         *
library(ez)

aov_w <- ezANOVA(data = dat_long,
                 dv = score,
                 wid = participant,
                 within = condition,
                 detailed = TRUE)
## Warning: Converting "participant" to factor for ANOVA.
## Warning: Converting "condition" to factor for ANOVA.
aov_w
## $ANOVA
##        Effect DFn DFd SSn SSd    F           p p<.05       ges
## 1 (Intercept)   1   3 300  12 75.0 0.003239037     * 0.9316770
## 2   condition   2   6  56  10 16.8 0.003478309     * 0.7179487
## 
## $`Mauchly's Test for Sphericity`
##      Effect    W    p p<.05
## 2 condition 0.36 0.36      
## 
## $`Sphericity Corrections`
##      Effect       GGe      p[GG] p[GG]<.05       HFe       p[HF] p[HF]<.05
## 2 condition 0.6097561 0.01668231         * 0.8082192 0.007460345         *
aov_result <- aov(score ~ condition +
                    Error(participant/condition), data=dat_long)

summary(aov_result)
## 
## Error: participant
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals  3     12       4               
## 
## Error: participant:condition
##           Df Sum Sq Mean Sq F value  Pr(>F)   
## condition  2     56  28.000    16.8 0.00348 **
## Residuals  6     10   1.667                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

ANOVA Table

apa.ezANOVA.table(aov_w, filename = "assignment_1_pt2_anova.doc")
## 
## 
## ANOVA results
##  
## 
##    Predictor df_num df_den Epsilon SS_num SS_den     F    p ges
##  (Intercept)   1.00   3.00         300.00  12.00 75.00 .003 .93
##    condition   1.22   3.66    0.61  56.00  10.00 16.80 .017 .72
## 
## Note. df_num indicates degrees of freedom numerator. df_den indicates degrees of freedom denominator. 
## Epsilon indicates Greenhouse-Geisser multiplier for degrees of freedom, 
## p-values and degrees of freedom in the table incorporate this correction.
## SS_num indicates sum of squares numerator. SS_den indicates sum of squares denominator. 
## ges indicates generalized eta-squared.
## 

Pairwise Comparisons

emmeans(model,
        pairwise ~ condition,
        adjust = "bonferroni")
## $emmeans
##  condition  emmean    SE   df lower.CL upper.CL
##  condition1      2 0.782 7.48    0.175     3.82
##  condition2      6 0.782 7.48    4.175     7.82
##  condition3      7 0.782 7.48    5.175     8.82
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95 
## 
## $contrasts
##  contrast                estimate    SE df t.ratio p.value
##  condition1 - condition2       -4 0.913  6  -4.382  0.0140
##  condition1 - condition3       -5 0.913  6  -5.477  0.0046
##  condition2 - condition3       -1 0.913  6  -1.095  0.9460
## 
## Degrees-of-freedom method: kenward-roger 
## P value adjustment: bonferroni method for 3 tests