1

# import data
d <- read.csv("http://whlevine.hosted.uark.edu/psyc5143/ps10.csv")

# make sure factors are factors
d$id <- as.factor(d$id)
d$A <- as.factor(d$A)

# what coding is R using for factor A?
contrasts(d$A)
# dummy coding

# useful info: condition means
d %>% group_by(A) %>% summarise(M = mean(DV))

# fitting the model treating the data as between-subjects
modelBetween <- lm(DV ~ A, d)

# getting some SS values
anova(modelBetween)

SS_between <- 154.8
SS_within <- 158.8
SS_total <- SS_between + SS_within

# another way to get these
modelC <- lm(DV ~ 1, d)
modelEffectSizes(modelC)

modelA <- lm(DV ~ 1 + A, d)
modelEffectSizes(modelA)

modelCompare(modelC, modelA)

\(SS_{between}\) = 154.8. \(SS_{within}\) = 158.8. \(SS_{total}\) = 313.6.

2

# a & b

# probably the easiest way to do the ANOVA
ezANOVA(data = d,
                dv = DV,
                wid = id,
                within = A,
                detailed = TRUE)$ANOVA

# the SSs
SS_persons <- 141.6   # this is SSd for the intercept
SS_A <- 154.8         # this is SSn for the IV
SS_resid <- 17.2      # this is SSd for the Iv

# checking equality
all.equal(SS_persons + SS_resid, SS_within) # this function checks for the
                                            # approximate equality of two things
  1. \(SS_{A}\) = 154.8. \(SS_{persons}\) = 141.6. \(SS_{error}\) = 17.2.

  2. \(SS_{between}\) = \(SS_{A}\). \(SS_{within}\) from #1 = \(SS_{persons}\) + \(SS_{error}\) from #2.

  3. The denominator for \(F\) in #2 is much smaller than in #1 because \(SS_{persons}\) has been removed from it. It cost a few \(df\), but the \(F\)-ratio increase by more than a factor of 6!

3

modelLMEM <- lmer(DV ~ A + (1|id), d)
anova(modelLMEM)

\(F\) = 36 again!