-
Notifications
You must be signed in to change notification settings - Fork 0
/
subjectiveDistanceANOVA.R
251 lines (162 loc) · 7.5 KB
/
subjectiveDistanceANOVA.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
library("rstatix")
library(ggplot2)
#library(compute.es)
library(lsr)
library(effsize)
library(ini)
library(rstudioapi)
library(stringr)
totalParticipants = 24
# Read the config file
currentFolder <- str_sub(getSourceEditorContext()$path, 1, -27) # -27 to remove the file name
configPath <- paste(currentFolder, "/config.ini", sep = "")
config <- read.ini(configPath, encoding = "UTF-8")
# Access and print the values
print(config)
#read xlsx file
data = openxlsx::read.xlsx(config$FILES$distanceDataXLSXFile)
data = data[1:totalParticipants,]
#踢人
data <- subset(data, subjectNumber != "3",subjectNumber != "14")
# 可能要踢王俊佑
sdData <- gather(data = data, key = types, value = distance, expectedAverageNumDistance, sNumDistance, fNumDistance, expectedAverageActDistance, sActDistance, fActDistance)
sdData
class(sdData$distance)
sdData$distance = as.numeric(sdData$distance)
class(sdData$distance)
# One-way repeated measures ANOVA all: significant
anova_result <- sdData %>% anova_test(distance ~ types + Error(subjectNumber/types))
anova_result
summary(anova_result)
#data <- subset(data, !(subjectNumber %in% c("3", "4")))
#One-way in num: significant
sdDataNum <- gather(data = data, key = types, value = distance, expectedAverageNumDistance, sNumDistance, fNumDistance)
sdDataNum$distance = as.numeric(sdDataNum$distance)
sdDataNum$subjectNumber = as.numeric(sdDataNum$subjectNumber)
sdDataNum = sdDataNum[,c("subjectNumber","types","distance")]
#str(sdDataNum)
anova_result_num <- sdDataNum %>% anova_test(distance ~ types + Error(subjectNumber/types))
anova_result_num
#effect size of ANOVA
class(anova_result_num)
anova_result_num[3]
#rstatix::eta_squared(anova_result_num)
#rstatix::eta_squared(anova_result_num$ANOVA)
eta_squared_g <- 0.017 # Replace with your actual value
f_cohen <- sqrt(eta_squared_g / (1 - eta_squared_g))
f_cohen
#plot
# Create a new data frame with the mean and standard error for each condition
sdDataNum$types <- factor(sdDataNum$types)
means <- aggregate(distance ~ types + subjectNumber, sdDataNum, mean)
se <- aggregate(distance ~ types + subjectNumber, sdDataNum, sd) #/ sqrt(length(sdDataNum$subjectNumber))
se$distance <- se$distance/ sqrt(length(sdDataNum$subjectNumber))
means$se <- se$distance
# Plot the data
numSubLabels <- c("expected", "fast", "slow")
ggplot(means, aes(x = types, y = distance)) +
geom_point(size = 0.5) +
geom_line(aes(group = subjectNumber)) +
geom_errorbar(aes(ymin = distance - sd(distance)/sqrt(length(distance)),
ymax = distance + sd(distance)/sqrt(length(distance))),
width = 0.2)+
scale_x_discrete(labels = numSubLabels) + # Change x-axis labels
labs(x = "Types", y = "Subjective Distance") +
theme_classic()
# Perform pairwise t-tests and adjust for multiple comparisons using the Holm method
posthoc_results_num <- sdDataNum %>%
pairwise_t_test(distance ~ types,
paired = TRUE,
p.adjust.method = "holm")
# View the pairwise comparison results
posthoc_results_num
# significant for average VS f and average VS s
paired_t_eff <- sdDataNum %>% cohens_d(distance ~ types, paired = TRUE)
paired_t_eff
# Assuming d_cohen is your Cohen's d value and n is the number of paired observations
d_cohen <- 1.18 # Replace with your actual value
n <- 23 # Replace with your actual value
DZ <- d_cohen / sqrt(n)
DZ
#One-way in act: significant!
sdDataAct <- gather(data = data, key = types, value = distance, expectedAverageActDistance, sActDistance, fActDistance)
sdDataAct$distance = as.numeric(sdDataAct$distance)
sdDataAct = sdDataAct[,c("subjectNumber","types","distance")]
anova_result_act <- sdDataAct %>% anova_test(distance ~ types + Error(subjectNumber/types))
anova_result_act
#plot
# Create a new data frame with the mean and standard error for each condition
sdDataAct$types <- factor(sdDataAct$types)
means <- aggregate(distance ~ types + subjectNumber, sdDataAct, mean)
se <- aggregate(distance ~ types + subjectNumber, sdDataAct, sd) #/ sqrt(length(sdDataAct$subjectNumber))
se$distance <- se$distance/ sqrt(length(sdDataAct$subjectNumber))
means$se <- se$distance
# Plot the data
numSubLabels <- c("expected", "fast", "slow")
ggplot(means, aes(x = types, y = distance)) +
geom_point(size = 0.5) +
geom_line(aes(group = subjectNumber)) +
geom_errorbar(aes(ymin = distance - sd(distance)/sqrt(length(distance)),
ymax = distance + sd(distance)/sqrt(length(distance))),
width = 0.2)+
scale_x_discrete(labels = numSubLabels) +
labs(x = "Types", y = "Subjective Distance") +
theme_classic()
# Perform one-way repeated measures ANOVA on the data
anova_result_act <- sdDataAct %>% anova_test(distance ~ types + Error(subjectNumber/types))
# Perform pairwise t-tests and adjust for multiple comparisons using the Holm method
posthoc_results_act <- sdDataAct %>%
pairwise_t_test(distance ~ types,
paired = TRUE,
p.adjust.method = "holm")
# View the pairwise comparison results
posthoc_results_act
# significant for f VS s
paired_t_eff <- sdDataAct %>% cohens_d(distance ~ types, paired = TRUE)
paired_t_eff
#realdata: not a good idea
realData <- gather(data = data, key = realTypes, value = realNumDistance, mu, sNumPrac, fNumPrac)
#$mu <- as.numeric(realData$mu)
#realData$sNumPrac <- as.numeric(realData$sNumPrac)
realData$realNumDistance <- as.numeric(realData$realNumDistance)
realData <- subset(realData, subjectNumber != "3",subjectNumber != "14")
#realData2 <- subset(realData)
anovaRealResult <- realData %>% anova_test(realNumDistance ~ realTypes + Error(subjectNumber/realTypes))
anovaRealResult
#t-test between mu and sNumPrac
t.test(data$sNumPrac, mu = 1.9444444)
cohen.d(data$sNumPrac, mu = 1.9444444, f=NA)
#t-test between mu and fNumPrac
t.test(data$fNumPrac, mu = 1.9444444)
cohen.d(data$fNumPrac, f = NA, mu = 1.9444444)
#t-test between sNumPrac and fNumPrac
t.test(data$sNumPrac, data$fNumPrac, paired = TRUE)
#cohensD(data$sNumPrac, data$fNumPrac)
cohen.d(data$sNumPrac, data$fNumPrac, paired = TRUE)
realData2 <- gather(data = data, key = realTypes, value = realNumDistance, sNumPrac, fNumPrac)
realData2 <- subset(realData2, subjectNumber != "3", subjectNumber != "14")
realData2$realNumDistance <- as.numeric(realData2$realNumDistance)
realData2 = realData2[,c("subjectNumber","realTypes","realNumDistance")]
#plot
# Create a new data frame with the mean and standard error for each condition
realData2$realTypes <- factor(realData2$realTypes)
means <- aggregate(realNumDistance ~ realTypes + subjectNumber, realData2, mean)
se <- aggregate(realNumDistance ~ realTypes + subjectNumber, realData2, sd) #/ sqrt(length(realData$subjectNumber))
se$realNumDistance <- se$realNumDistance/ sqrt(length(realData2$subjectNumber))
means$se <- se$realNumDistance
# Plot the data
tTestLabel <- c("fast", "slow")
ggplot(means, aes(x = realTypes, y = realNumDistance)) +
geom_point(size = 0.5) +
geom_line(aes(group = subjectNumber)) +
geom_errorbar(aes(ymin = realNumDistance - sd(realNumDistance)/sqrt(length(realNumDistance)),
ymax = realNumDistance + sd(realNumDistance)/sqrt(length(realNumDistance))),
width = 0.2)+
geom_hline(yintercept = 1.94444, linetype = "dotted") +
scale_x_discrete(labels = tTestLabel) +
labs(x = "Types", y = "Objective Distance") +
theme_classic()
# correlation between subjective distance and objective distance in number conditions
print(data$sNumPrac)
cor(data$sNumDistance, data$sNumPrac)
cor(data$fNumDistance, data$fNumPrac)