1 Initialize R

options(contrasts=c("contr.sum","contr.poly")) # IMPORTANT!
load(file=url('http://pnb.mcmaster.ca/bennett/psy710/datasets/L4-learning.rda'))

The options command sets up R to define ANOVA effects using the sum-to-zero constraint.

Answer all of the following questions and submit your answers as a script file on Avenue 2 Learn. Make sure to begin your script file with the following lines:

# PSYCH 710 Lab 4 Homework
# Script File
# SEP-2023
# Your Name: <<Your name here>>
# Student ID: <<Your ID here>>
# Collaborators: <<Names of your collaborators here>>

Also, make sure that text that is not an R command is preceded by a comment symbol (#). For example, you can insert questions or comments among your commands like this:

# The following command doesn't work... not sure why...
# ttest(x=g1,y=g2) # was trying to do a t test

2 Perceptual Learning Experiment

Performance in most perceptual and cognitive tasks improves with practice, but it remains unclear how precisely how performance changes as a function of practice. Theory 1 holds that improvement is a linear function of the number of practice trials. Theory 2 holds that learning is an accelerating non-linear function of practice (see Figure 1).

Figure 1. Hypothetical effects of practice on performance.

Figure 1. Hypothetical effects of practice on performance.

An experiment examined the effect of the number of practice trials on response accuracy in a pattern identification task. Participants completed a practice session on Day 1 and a test session on Day 2. The number of practice trials varied across groups; all participants the same number of test trials on Day 2. The dependent variable was response accuracy (proportion correct) on Day 2; the independent variable was the number of practice trials on Day 1. The data are stored in the data frame learning which contains the ordered factor trials, which indicates the number of practice trials, and the numeric variable, accuracy, which holds response accuracy on Day 2.

sapply(learning,class)
## $trials
## [1] "ordered" "factor" 
## 
## $accuracy
## [1] "numeric"
summary(learning)
##  trials      accuracy     
##  t10:25   Min.   :0.0100  
##  t20:25   1st Qu.:0.2654  
##  t30:25   Median :0.4012  
##  t40:25   Mean   :0.4097  
##  t50:25   3rd Qu.:0.5671  
##  t60:25   Max.   :0.8849

2.1 Questions

When answering the following questions, you may use an alpha of 0.05 and assume (unless stated otherwise) that the data satisfy the assumptions of normality and homogeneity of variance.

  1. Plot average response accuracy as a function of practice trials.
yAccuracy <- with(learning,tapply(accuracy,trials,mean)) # get mean accuracy for each condition
xTrials <- seq(10,60,10)
plot(x=xTrials,
     y=yAccuracy,
     type="p",
     ylim=c(0,1),
     xlim=c(10,60),
     xlab="Trials",
     ylab="Accuracy",
     cex=2,
     cex.axis=1.5,
     cex.lab=2,
     pch=19)
# following sections are useful but not necessary
# add error bars:
ySD <-  with(learning,tapply(accuracy,trials,sd)) # get SD for each condition
yN <-  with(learning,tapply(accuracy,trials,length)) # get N for each condition
ySEM <- ySD/sqrt(yN) # calculate standard error of mean for each condition
segments(x0=xTrials,x1=xTrials,y0=yAccuracy-ySEM,y=yAccuracy+ySEM) # ± 1 SEM
abline(lm(yAccuracy~xTrials),lty=2) # add regression line

  1. Use ANOVA to examine the effect of practice trials on response accuracy. Your answer should include the ANOVA table and a conclusion regarding the null hypothesis evaluated by the omnibus \(F\).
# Use ANOVA to examine the effect of practice trials on response accuracy. 
# Your answer should include the ANOVA table and a conclusion regarding the 
# null hypothesis evaluated by the omnibus F. 

learn.aov.01 <- aov(accuracy~trials,data=learning)
summary(learn.aov.01)
##              Df Sum Sq Mean Sq F value   Pr(>F)    
## trials        5  2.568  0.5136   16.38 8.91e-13 ***
## Residuals   144  4.516  0.0314                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# The effect of trials was significant, therefore I reject the null hypothesis that
# all group means are equal.
  1. Calculate a measure of effect size and association strength for trials.
library(effectsize)
cohens_f(learn.aov.01) # effect size
## # Effect Size for ANOVA
## 
## Parameter | Cohen's f |      95% CI
## -----------------------------------
## trials    |      0.75 | [0.58, Inf]
## 
## - One-sided CIs: upper bound fixed at [Inf].

omega_squared(learn.aov.01) # association strength
## # Effect Size for ANOVA
## 
## Parameter | Omega2 |       95% CI
## ---------------------------------
## trials    |   0.34 | [0.22, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
  1. Explain why the results of your ANOVA do not distinguish between Theories 1 and 2.
# The omnibus F looks for ANY difference among the group means,
# whereas Theories 1 and 2 predict SPECIFIC differences among the group means
  1. Present the results of a linear contrast (or several contrasts) that might help you distinguish Theories 1 and 2. You may assume that these contrasts are planned.
# There are several possible ways of answering this question.
# I will perform a trend analysis and look for the existence of a linear
# trend of accuracy across trials (which is predicted by both theories) and
# a non-linear trend (which is predicted by theory 2). The next analysis finds
# a significant linear trend and a non-significant quadratic trend.
summary(learn.aov.01,split=list(trials=list(L=1,Q=2)))
##              Df Sum Sq Mean Sq F value   Pr(>F)    
## trials        5  2.568  0.5136  16.378 8.91e-13 ***
##   trials: L   1  2.415  2.4149  77.006 4.47e-15 ***
##   trials: Q   1  0.015  0.0153   0.489    0.486    
## Residuals   144  4.516  0.0314                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# We can also perform the analysis with emmeans
library(emmeans)
learn.emm <- emmeans(learn.aov.01,specs="trials")
contrast(learn.emm,method = "poly")
##  contrast  estimate    SE  df t.ratio p.value
##  linear       2.600 0.296 144   8.775  <.0001
##  quadratic   -0.227 0.325 144  -0.699  0.4856
##  cubic       -0.832 0.475 144  -1.750  0.0822
##  quartic      0.163 0.187 144   0.871  0.3852
##  degree 5     0.425 0.562 144   0.756  0.4511
# The next analysis looks for **any** non-linear trend. The F test is not significant.
# So our analysis finds a significant linear trend but no significant non-linear trend.
# Note that one limitation of our analysis is that we may simply lack sufficient power
# to detect the non-linear trend.
summary(learn.aov.01,split=list(trials=list(L=1,nonLin=2:4)))
##                   Df Sum Sq Mean Sq F value   Pr(>F)    
## trials             5  2.568  0.5136  16.378 8.91e-13 ***
##   trials: L        1  2.415  2.4149  77.006 4.47e-15 ***
##   trials: nonLin   3  0.135  0.0451   1.437    0.235    
## Residuals        144  4.516  0.0314                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##  There is a significant linear trend and no evidence of a nonlinear trend and therefore I think these results are more consistent with the predictions of Theory 1 than Theory 2. Note that we cannot conclude that there is no nonlinear trend: we may simply have failed to detect a true nonlinear trend. If we wanted to pursue this further, we could perform an equivalence test on the nonlinear trend to see if it is equivalent zero.
  1. Re-evaluate your contrasts assuming that you decided to perform them after looking at the data.
# Re-evaluate your contrasts assuming that you decided to perform 
# them *after* looking at the data.

trendWeights <- contrasts(learning$trials) # get weights
( wLin <- trendWeights[,".L"] ) # linear trend weights
## [1] -0.5976143 -0.3585686 -0.1195229  0.1195229  0.3585686  0.5976143
( wQuad <- trendWeights[,".Q"] ) # quadratic trend weights
## [1]  0.5455447 -0.1091089 -0.4364358 -0.4364358 -0.1091089  0.5455447
df.trials <- length(levels(learning$trials)) - 1 # degrees of freedom
library(emmeans)
learn.emm <- emmeans(learn.aov.01,specs="trials")
lin.post.hoc <- contrast(learn.emm,method=list(linear=wLin,quad=wQuad) )
summary(lin.post.hoc,adjust="scheffe",scheffe.rank=df.trials) 
##  contrast estimate     SE  df t.ratio p.value
##  linear     0.3108 0.0354 144   8.775  <.0001
##  quad      -0.0248 0.0354 144  -0.699  0.9924
## 
## P value adjustment: scheffe method with rank 5

# The linear trend is significant.