# February 7, 2024 library(tidyverse) library(lmSupport) # a concrete example of a three-group design threeGroups <- tibble(group = factor(c(rep("image", 10), rep("rhyme", 10), rep("control", 10))), memory = c(15, 16, 6, 7, 13, 10, 13, 9, 12, 19, 16, 8, 5, 10, 9, 13, 16, 3, 8, 12, 11, 4, 8, 2, 10, 6, 2, 5, 7, 5)) threeGroups %>% group_by(group) %>% summarise(M = mean(memory)) # dummy codes threeGroups <- threeGroups %>% mutate(D1 = case_when(group == "image" ~ 1, group == "rhyme" ~ 0, group == "control" ~ 0), D2 = case_when(group == "image" ~ 0, group == "rhyme" ~ 1, group == "control" ~ 0)) # the dummy-code model modelDummy <- lm(memory ~ D1 + D2, threeGroups) summary(modelDummy) # notice that if you use the existing factor (that is, "group") as a predictor, # the results are the same as the dummy-coded model; R dummy codes factors by # default modelDefault <- lm(memory ~ group, threeGroups) summary(modelDefault) summary(modelDummy) # notice further that REGARDLESS of whether you use contrast or dummy or some # other coding scheme for groups, the "model F" (which has as its compact model # an intercept-only model) is identical summary(modelDummy)$fstatistic # F = 6.29 summary(modelDefault)$fstatistic # F = 6.29 # let's use some strange numbers for the groups threeGroups <- threeGroups %>% mutate(strange1 = case_when(group == "image" ~ 2, group == "rhyme" ~ 7, group == "control" ~ 1), strange2 = case_when(group == "image" ~ 3, group == "rhyme" ~ 11, group == "control" ~ 4)) modelStrange <- lm(memory ~ strange1 + strange2, threeGroups) summary(modelStrange)$fstatistic # F = 6.29 # the pairwise.t.test function is handy for getting p-values, but useless for # getting t and df values; let's do the latter first summary(modelDummy) # t = 3.48 for I vs C; t = 2.32 for for R vs C # getting the third t-test takes two steps threeGroups$group <- relevel(threeGroups$group, ref = "image") # changes the reference group summary(lm(memory ~ group, threeGroups)) # t = 1.16 for R vs I pairwise.t.test(x = threeGroups$memory, # the DV g = threeGroups$group, # IV p.adjust.method = "none") # method of adjustment (see ?p.adjust) pairwise.t.test(x = threeGroups$memory, # the DV g = threeGroups$group, # IV p.adjust.method = "bonferroni") # method of adjustment (see ?p.adjust)