# January 31, 2024 library(tidyverse) d <- tibble(group = factor(c(rep("exercise", 16), rep("control", 16))), stride = c(24, 25, 22, 24, 26, 17, 21, 22, 22, 19, 24, 23, 23, 28, 25, 23, 26, 23, 20, 23, 20, 16, 21, 17, 18, 23, 16, 20, 25, 19, 17, 16)) d %>% group_by(group) %>% summarise(mean(stride)) # try +1/2 and -1/2 d <- d %>% mutate(groupHalfs = ifelse(group == "exercise", 0.5, -0.5)) modelBest <- lm(stride ~ groupHalfs, d) modelSummary(modelBest) ggplot(d, aes(groupHalfs, stride)) + geom_point() + geom_jitter(width = 0.1) + geom_smooth(method = "lm", se = F) + xlim(-1, 1) + geom_point(aes(x = -0.5, y = 20), color = "red", size = 5) + geom_point(aes(x = 0.5, y = 23), color = "red", size = 5) + geom_vline(xintercept = 0) # try dummy coding (setting the control group to 0) d <- d %>% mutate(groupDummy = ifelse(group == "control", 0, 1)) modelDummy <- lm(stride ~ groupDummy, d) modelSummary(modelDummy) ggplot(d, aes(groupDummy, stride)) + geom_point() + geom_jitter(width = 0.1) + geom_smooth(method = "lm", se = F) + xlim(-1, 2) + geom_point(aes(x = 0, y = 20), color = "red", size = 5) + geom_point(aes(x = 1, y = 23), color = "red", size = 5) + geom_vline(xintercept = 0) # stuff need for laborious calculation of CI # F-crit for 1, 30 df Fcrit <- qf(p = .95, df1 = 1, df2 = 30) # variance of X varX <- var(d$groupHalfs) var(d$groupDummy) # hey, it's the same; ponder why this is! # CI half-width CIhalf <- sqrt((Fcrit*8.8)/(31*varX)) # easier! confint(modelBest) # pooled variance var1 <- var(d$stride[d$group == "exercise"]) var2 <- var(d$stride[d$group == "control"]) s2p <- (15*10.6667 + 15*6.9333) / 30 s2p_better <- (15*var1 + 15*var2) / 30 # SE of the difference between two independent means SE <- sqrt(s2p/16 + s2p/16) # t statistic t <- (23 - 20)/SE # more quickly! t.test(stride ~ group, data = d, var.equal = T) # Cohen's d (23 - 20)/sqrt(s2p) # a three-group design threeGroups <- tibble(Y = c(2, 4, 6, 8, 10, 5, 6, 8, 10, 11, 10, 13, 14, 15, 18), group = factor(c(rep("group1", 5), rep("group2", 5), rep("group3", 5)))) # the case_when function will be introduced in drill next week; but it's very # useful for assigning numerical values to a grouping variable with more than # two levels ... but the syntax is tricky (I often forget commas or parentheses) threeGroups <- threeGroups %>% mutate(X = case_when(group == "group1" ~ 0, group == "group2" ~ 1, group == "group3" ~ 2)) ggplot(threeGroups, aes(X, Y)) + geom_point() + geom_smooth(method = "loess", se = F)