'How to do repeated measures with group by time?
Introduction
I followed this guide awhile back and returned to it in order to do my own repeated measures ANOVA: https://www.datanovia.com/en/lessons/repeated-measures-anova-in-r/
Script
So far I have this script and dont have any immediate problems there:
#### Local R data ####
hwk1 <- structure(list(group = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), format.spss = "F8.2", class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`stress management` = 1,
control = 2)), time1 = structure(c(39, 46, 32, 29, 24, 35, 42,
51, 44, 32, 31, 48, 46, 47, 39, 30, 35, 40, 46, 58, 47, 39, 36,
54), format.spss = "F8.2"), time2 = structure(c(37, 42, 32, 33,
22, 32, 33, 47, 41, 37, 38, 44, 48, 45, 35, 31, 33, 38, 46, 52,
44, 41, 39, 55), format.spss = "F8.2"), time3 = structure(c(36,
38, 34, 31, 18, 29, 43, 50, 36, 38, 39, 39, 47, 41, 38, 33, 37,
38, 51, 55, 46, 43, 41, 48), format.spss = "F8.2")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -24L))
#### Load libraries ####
library(tidyverse)
library(correlation)
library(rstatix)
library(ggpubr)
#### Pivot data ####
pivo_hwk1 <- hwk1 %>%
gather(key = "time",
value = "stress",
time1,
time2,
time3) %>%
convert_as_factor(group,
time)
#### Descriptives ####
pivo_hwk1 %>%
group_by(group,
time) %>%
get_summary_stats(stress,
type = "mean_sd")
#### Simple boxplot of means ####
bxp <- ggboxplot(pivo_hwk1,
x="time",
y="stress",
color = "group",
add = "point",
palette = "lancet")
bxp
#### Check outliers ####
pivo_hwk1 %>%
group_by(group,
time) %>%
identify_outliers()
#### Normality: Shapiro ####
pivo_hwk1 %>%
group_by(group,
time) %>%
shapiro_test(stress)
#### Normality: QQ ####
ggqqplot(pivo_hwk1,
x="stress",
color = "group",
palette = "lancet",
title = "QQ Plot of Group x Time",
subtitle = "Group 1 = Treatment, Group 2 = Control")+
facet_wrap(~time)+
theme_bw()+
theme(plot.title = element_text(face = "bold"),
plot.background = element_rect(fill = "steelblue"),
axis.text = element_text(color = "black"))
Problem
Well I tried to run the actual repeated measures ANOVA but I'm confused on how to enter it into the anova_test command:
#### RMANOVA ####
res.aov <- anova_test(
data = pivo_hwk1,
dv = stress,
wid = group,
within = c(treatment,
time))
get_anova_table(res.aov)
The res.aov bit doesnt work, and it appears clear that the reason is the wid portion. However, I'm not sure what I should do to modify this, as every combination of the variables I have tried from the dataset doesn't seem to work. Any advice would be great!
EDIT:
Looks like I was able to get the ANOVA to run with this code:
# Read the data:
hwk1_raw <- read_csv("C:/Users/DELL/Dropbox/My PC (DESKTOP-SUOCLVS)/Desktop/RM II ASSIGN I/hwk1_raw.csv")
# Create ID values:
hwk1_raw$id <- c(1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,
18,19,20,21,22,23,24)
# Add ID value as character:
hwk1_raw$id <- as.character(hwk1_raw$id)
# Pivot data:
pivo_raw <- hwk1_raw %>%
pivot_longer(
cols = 2:4,
names_to = "time",
values_to = "stress"
)
# Running ANOVA:
res.aov <- anova_test(
data = pivo_raw,
wid = id,
dv = stress,
within = time,
between = group
)
res.aov
However, I think I'm still having issues figuring how how to do the pairwise comparisons. As far as I can tell, there should only be one pair, so it should be something like this:
pivo_raw %>%
pairwise_t_test(
data = pivo_raw,
formula = stress ~ group,
paired = T,
p.adjust.method = "bonferroni")
But it just spits out this:
Error in t.test.default(x = numeric(0), y = numeric(0), paired = TRUE) : not enough 'x' observations
Can someone suggest what to do from here?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
