R学习笔记-单因素重复测量方差分析
R语言之重复测量方差分析——ezANOVA的使用与解析 - 知乎
单因素重复测量方差分析(One-Way Repeated Measures ANOVA)——R软件实现 - 梦特医数通
### 清空environment
rm(list = ls())
### 加载包
if (!require("tidyverse")) install.packages("tidyverse")
library(tidyverse)#用于数据清理、操作、可视化和分析
if (!require("conflicted")) install.packages("conflicted")
library(conflicted)#让 R 遇到冲突时抛出错误,并让您明确选择要调用的函数
library(readxl)
if (!require("ez")) install.packages("ez")#用于方差分析
library(ez)
### 读取数据
file_path <- "D:/LLYdata/EEG_EMG_expdata/behavior/result/MATLAB/analysis_data.xlsx"
data <- read_excel(file_path, range = cell_rows(1:25))#读取excel的第1到25行的所有列
### 设置数据
# 选择需要的四列并转换为长格式
data_long <- data %>%
rstatix::select(goRT_uni_gocorrect, goRT_i_clgocorrect, goRT_i_crgocorrect, goRT_onlygocorrect) %>%
pivot_longer(cols = everything(), names_to = "condition", values_to = "RT") %>%
mutate(subject = rep(1:(nrow(data)), each = 4)) # 为每个被试添加一个唯一的标识符
# 计算描述性统计
means <- c(mean(data$goRT_uni_gocorrect), mean(data$goRT_i_clgocorrect), mean(data$goRT_i_crgocorrect), mean(data$goRT_onlygocorrect))
sds <- c(sd(data$goRT_uni_gocorrect), sd(data$goRT_i_clgocorrect), sd(data$goRT_i_crgocorrect), sd(data$goRT_onlygocorrect))
ns <- rep(nrow(data), 4)
# 生成结果数据框
result <- data.frame(
Condition = c("goRT_uni_gocorrect", "goRT_i_clgocorrect", "goRT_i_crgocorrect", "goRT_onlygocorrect"),
Mean = means,
SD = sds,
N = ns
)
# 使用 ezANOVA 进行单因素重复测量方差分析
anova_results <- ezANOVA(
data = data_long,
dv = RT, # 因变量
wid = subject, # 受试者
within = condition, # 重复测量因素
detailed = TRUE
)
# 成对比较
AOV<-aov(RT ~ condition,data_long) #检验不同时间之间的差异
# 打开文件以写入
sink(output_file)
# 添加自定义文本
cat("###Condition (uni/i_cl/i_cr/onlygo) 的重复测量方差分析###\n\n")
# 描述性结果
cat("Condition\tMean\tSD\tN\n")
for (i in 1:nrow(result)) {
cat(paste(result$Condition[i], "\t",
round(result$Mean[i], 2), "\t",
round(result$SD[i], 2), "\t",
result$N[i], "\n"))
}
# 方差分析结果
print(anova_results)
# 事后两两比较结果
TukeyHSD(AOV,p.adjust.methods="bonferroni")
# 关闭文件
sink()
# 保留的变量
keep_vars <- c("data", "file_path", "output_file")
# 获取当前环境中的所有变量
all_vars <- ls()
# 找出需要删除的变量
vars_to_remove <- setdiff(all_vars, keep_vars)
# 删除不需要的变量
rm(list = vars_to_remove)
rm(list = ls())#清空environment
### 加载包
if (!require("tidyverse")) install.packages("tidyverse")
library(tidyverse)#用于数据清理、操作、可视化和分析
if (!require("conflicted")) install.packages("conflicted")
library(conflicted)#让 R 遇到冲突时抛出错误,并让您明确选择要调用的函数
library(readxl)
library(ez)
### 读取数据
file_path <- "D:/LLYdata/EEG_EMG_expdata/behavior/result/MATLAB/analysis_data.xlsx"
data <- read_excel(file_path, range = cell_rows(1:25))#读取excel的第1到25行的所有列
# 设置结果输出文件路径
output_file <- "D:/LLYdata/EEG_EMG_expdata/behavior/result/R/results.txt"
################################################################
###go RT: Condition (uni/i_cl/i_cr/onlygo) 的重复测量方差分析###
################################################################
# 计算描述性统计
means <- c(mean(data$goRT_uni_gocorrect), mean(data$goRT_i_clgocorrect), mean(data$goRT_i_crgocorrect), mean(data$goRT_onlygocorrect))
sds <- c(sd(data$goRT_uni_gocorrect), sd(data$goRT_i_clgocorrect), sd(data$goRT_i_crgocorrect), sd(data$goRT_onlygocorrect))
ns <- rep(nrow(data), 4)
# 生成结果数据框
result <- data.frame(
Condition = c("goRT_uni_gocorrect", "goRT_i_clgocorrect", "goRT_i_crgocorrect", "goRT_onlygocorrect"),
Mean = means,
SD = sds,
N = ns
)
# 选择需要的四列并转换为长格式
data_long <- data %>%
rstatix::select(goRT_uni_gocorrect, goRT_i_clgocorrect, goRT_i_crgocorrect, goRT_onlygocorrect) %>%
pivot_longer(cols = everything(), names_to = "condition", values_to = "RT") %>%
mutate(subject = rep(1:(nrow(data)), each = 4)) # 为每个被试添加一个唯一的标识符
# 使用 ezANOVA 进行单因素重复测量方差分析
anova_results <- ezANOVA(
data = data_long,
dv = RT, # 因变量
wid = subject, # 受试者
within = condition, # 重复测量因素
detailed = TRUE
)
# 成对比较
AOV<-aov(RT ~ condition,data_long) #检验不同时间之间的差异
# 打开文件以写入
sink(output_file)
# 添加自定义文本
cat("###Condition (uni/i_cl/i_cr/onlygo) 的重复测量方差分析###\n\n")
# 描述性结果
cat("Condition\tMean\tSD\tN\n")
for (i in 1:nrow(result)) {
cat(paste(result$Condition[i], "\t",
round(result$Mean[i], 2), "\t",
round(result$SD[i], 2), "\t",
result$N[i], "\n"))
}
# 方差分析结果
print(anova_results)
# 事后两两比较结果
TukeyHSD(AOV,p.adjust.methods="bonferroni")
# 关闭文件
sink()