User Tools

Site Tools


r:repeated_measure_anova

This is an old revision of the document!


# Data preparation
# Wide format
# install.packages("datarium")
# library(datarium)

data("selfesteem", package = "datarium")
head(selfesteem, 3)

# Gather columns t1, t2 and t3 into long format
# Convert id and time into factor variables

# for %>% function, dplyr packages needed
# install.packages("dplyr")
# library(dplyr)

# for convert_as_factor function, rstatix needed
# install.packages("rstatix")
# library(rstatix)

selfesteem <- selfesteem %>% 
  gather(key = "time", value = "score", t1, t2, t3) %>% 
  convert_as_factor(id, time)
head(selfesteem, 3)

# get_summary_stats(group_by(selfesteem, time), 
# score, type = "mean_sd")
# the above is the same as the below
selfesteem %>%
  group_by(time) %>%
  get_summary_stats(score, type = "mean_sd")


res.aov <- anova_test(
  data = selfesteem, 
  dv = score, 
  wid = id, 
  within = time)
get_anova_table(res.aov)

# ges = generalized effect size
# F
# (2,18)

###################################################
###################################################
###################################################

# data
df <- data.frame(patient=rep(1:5, each=4),
                 drug=rep(1:4, times=5),
                 response=c(30, 28, 16, 34,
                            14, 18, 10, 22,
                            24, 20, 18, 30,
                            38, 34, 20, 44,
                            26, 28, 14, 30))

#view data
df
write.csv(df, file="rep.meas.anova.csv")


#fit repeated measures ANOVA model
df$drug <- factor(df$drug)
df$patient <- factor(df$patient)

# Error(patient) = patient error should be isolated
m.aov <- aov(response ~ drug 
             + Error(patient), 
             data = df)
#view model summary
summary(m.aov)

# check probability level (pr)
1 - pf(24.75886525, 3, 12)

# A one-way repeated measures ANOVA was conducted 
# on five individuals to examine the effect that 
# four different drugs had on response time.

# Results showed that the type of drug used lead 
# to statistically significant differences in 
# response time (F(3, 12) = 24.76, p < 0.001).


# the second
movrev <- data.frame(reviewer=rep(1:5, each=3),
                 movie=rep(1:3, times=5),
                 score=c(88, 84, 92,
                            76, 78, 90,
                            78, 94, 95,
                            80, 83, 88, 
                            82, 90, 99))

#view data
movrev
write.csv(movrev, file="rep.meas.anova.mov.rev.csv")

movrev$movie <- factor(movrev$movie)
movrev$reviewer <- factor(movrev$reviewer)

# Error(reviewer) = reviewer error should be isolated
# The above is the same as Error(reviewer/movie)
m.aov <- aov(score ~ movie 
             + Error(reviewer), 
             data = movrev)
#view model summary
summary(m.aov)

# pairwise.t.test(movrev$score, movrev$movie, paired = T, p.adjust.method = "bonf")
attach(movrev)
pairwise.t.test(score, movie, paired = T, p.adjust.method = "bonf")
# or
with(movrev, 
     pairwise.t.test(score, movie, 
                     paired = T, 
                     p.adjust.method = "bonf"))



r/repeated_measure_anova.1715123562.txt.gz · Last modified: 2024/05/08 08:12 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki