E Repeated Measures T-test code

Video link: https://youtu.be/A8ASNsQainU

################################################################################
#                        Repeated Measures T-Test
################################################################################

#Call datarium package
library(datarium)

#Assign data set to an object
mice <- mice2

#See what it looks like
head(mice)

#Calculate the difference column
mice$diff <- mice$after - mice$before

#Check your work
head(mice)

#Call the psych package
library(psych)

#Get descriptive statistics
describe(mice$diff)

#Call ggplot
library(ggplot2)

#Generate a boxplot
ggplot(data = mice, aes(x = "", y = diff)) + 
  geom_boxplot() +
  geom_jitter(width = .2)

#SW test on the difference
shapiro.test(mice$diff)

#Make the plot (we called ggplot earlier)
ggplot(data = mice, aes(x = diff)) + 
  geom_histogram()

#Call qqplotr
library(qqplotr)

#Perform QQ plots by group
ggplot(data = mice, mapping = aes(sample = diff)) +
  stat_qq_band(alpha = 0.5, conf = 0.95, bandType = "pointwise", fill = "blue") +
  stat_qq_line(identity = TRUE) +
  stat_qq_point(col = "black") +
  labs(x = "Theoretical Quantiles", y = "Sample Quantiles") + 
  theme_bw()

#Perform detrended QQ plots by group
ggplot(data = mice, mapping = aes(sample = diff)) +
  stat_qq_band(alpha = 0.5, conf = 0.95, bandType = "pointwise", fill = "blue", detrend = TRUE) +
  stat_qq_line(identity = TRUE, detrend = TRUE) +
  stat_qq_point(col = "black", detrend = TRUE) +
  labs(x = "Theoretical Quantiles", y = "Sample Quantiles") + 
  theme_bw()

#Run the repeated measures t-test
t.test(x = mice$after, y = mice$before, paired = TRUE)

#Run repeated measure t-test, before-after
t.test(x = mice$before, y = mice$after, paired = TRUE)

#Call tidyverse to use pipe
library(tidyverse)

#Change to long format
mice_long <- mice[1:3] %>%
  gather(key = "group", value = "weight", before, after)

#Check work
head(mice_long)

#Call the package
library(effectsize)

#Get Cohen's d
cohens_d(data = mice_long, weight ~ group, paired = TRUE)