-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonParametric.r
121 lines (93 loc) · 3.66 KB
/
nonParametric.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
library(ARTool)
library(tidyr)
library(rcompanion)
library(rstatix)
library(ini)
library(rstudioapi)
library(stringr)
# Read the config file
currentFolder <- str_sub(getSourceEditorContext()$path, 1, -17) # -17 to remove the file name
print(currentFolder)
configPath <- paste(currentFolder, "/config.ini", sep = "")
config <- read.ini(configPath, encoding = "UTF-8")
# Access and print the values
# print(config)
#read csv file
rgCalcData <- read.csv(config$FILES$rgCalcResultsCSVFile, header = TRUE, sep = ",")
print(rgCalcData)
#extract (s or f) and (num or act) respectively as two variables
speed = c(); numOrAct = c()
for (i in rgCalcData$type) {
speed = append(speed, substr(i, 1, 1))
numOrAct = append(numOrAct, substr(i, 2, 4))
}
sortedDataR = data.frame(rgCalcData$subject, speed, numOrAct, rgCalcData$R)
sortedDataAll = data.frame(rgCalcData[1], speed, numOrAct, rgCalcData[,c(4:60)])
colnames(sortedDataR)[1] <- "subject"
colnames(sortedDataR)[4] <- "R"
#踢掉董
rgCalcData <- subset(rgCalcData, subject != "3")
sortedDataR <- subset(sortedDataR, subject != "3")
#art analysis and effect size
resultRand <- art(R ~ factor(speed) * factor(numOrAct) + Error(factor(subject)), data = sortedDataR)
result <- anova(resultRand)
print(result, verbose = TRUE)
result$eta.sq.part = with(result, `Sum Sq`/(`Sum Sq` + `Sum Sq.res`))
result
#The interaction is significant and both main effects are significant.
#Use the non-parametric method to calculate the simple main effects
#speed
speedS = subset(sortedDataR, speed == "s")
speedF = subset(sortedDataR, speed == "f")
speedS = speedS[,c("subject","R")]
speedF = speedF[,c("subject","R")]
#numOrAct
num = subset(sortedDataR, numOrAct == "num")
act = subset(sortedDataR, numOrAct == "act")
num = num[,c("subject","R")]
act = act[,c("subject","R")]
#simple main effect
#speed
speedS = speedS[order(speedS$subject),]
speedF = speedF[order(speedF$subject),]
speedS = speedS[,c("R")]
speedF = speedF[,c("R")]
speed = cbind(speedS, speedF)
speed = as.data.frame(speed)
speed = gather(data = speed, key = speed, value = R, speedS, speedF)
speed$speed = factor(speed$speed)
speed
#numOrAct
num = num[order(num$subject),]
act = act[order(act$subject),]
num = num[,c("R")]
act = act[,c("R")]
numOrAct = cbind(num, act)
numOrAct = as.data.frame(numOrAct)
numOrAct = gather(data = numOrAct, key = numOrAct, value = R, num, act)
numOrAct$numOrAct = factor(numOrAct$numOrAct)
numOrAct
#use wilcoxon test to do multiple comparison
#speed
multiSpeed = wilcox.test(sortedDataR[sortedDataR$speed == "s",]$R, sortedDataR[sortedDataR$speed == "f",]$R, paired = TRUE)
multiSpeed
multiNumOrAct = wilcox.test(sortedDataR[sortedDataR$numOrAct == "num",]$R, sortedDataR[sortedDataR$numOrAct == "act",]$R, paired = TRUE)
multiNumOrAct
#effect size
effsizeSpeed <- wilcox_effsize(sortedDataR, R ~ speed, paired = TRUE, conf.level = 0.95)
effsizeSpeed
library(ggplot2)
library(plyr)
# Calculate bootstrapped standard errors for each participant
se_df <- ddply(sortedDataR, .(subject, speed, numOrAct), summarise, se = sd(R)/sqrt(length(R)))
se_df$se[is.na(se_df$se)] <- 0 # Replace NAs with zeros
# Merge the standard error data with the original data
sortedDataR <- merge(sortedDataR, se_df, by = c("subject", "speed", "numOrAct"))
# Plot the data with error bars
ggplot(sortedDataR, aes(x = numOrAct, y = R, color = speed, group = interaction(speed, subject))) +
geom_point(size = 3, position = position_dodge(width = 0.8)) +
geom_errorbar(aes(ymin = R - se, ymax = R + se), position = position_dodge(width = 0.8), width = 0.2) +
facet_wrap(~subject, scales = "free") +
labs(x = "numOrAct", y = "R", color = "speed") +
theme_classic() +
theme(legend.position = "bottom")