-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmotions_LAP.R
217 lines (183 loc) · 6.64 KB
/
Emotions_LAP.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
# Load necessary libraries
library(openxlsx)
library(psych)
library(foreign)
library(tidyverse)
library(effsize)
library(readxl)
library(car)
library(rcompanion)
library(moments)
library(coin)
# Set working directory
setwd("F:/Google drive data/Plants and IEQ Project/Main experiments/Data analysis")
# Read the data from the Excel file
Data <- read_excel("All data.xlsx", sheet = "Sheet1")
# Convert Group and Survey variables to factors
Data$Group <- factor(Data$Group)
Data$Survey <- factor(Data$Survey)
# Filter data for WoP group and 'LAP' column only for Surveys II and IV
WoP_Data <- Data %>%
filter(Group == "WoP" & (Survey == "II" | Survey == "IV")) %>%
select(Group, Survey, LAP)
# Calculate mean, median, and standard deviation for 'LAP' within WoP group
summary_stats <- WoP_Data %>%
group_by(Survey) %>%
summarize(Mean = mean(LAP, na.rm = TRUE),
Median = median(LAP, na.rm = TRUE),
SD = sd(LAP, na.rm = TRUE))
# Get the means for Surveys II and IV
M1 <- summary_stats$Mean[summary_stats$Survey == "II"]
M2 <- summary_stats$Mean[summary_stats$Survey == "IV"]
# Add mean and residuals to the data frame
WoP_Data <- WoP_Data %>%
mutate(mean = ifelse(Survey == "II", M1, M2),
Residual = LAP - mean)
# Filter out NA values for 'LAP'
valid_data <- WoP_Data %>%
filter(!is.na(LAP))
# Extract data for Wilcoxon test
x <- valid_data$LAP[valid_data$Survey == "II"]
y <- valid_data$LAP[valid_data$Survey == "IV"]
# Perform paired Wilcoxon test if both groups have data
if (length(x) > 0 && length(y) > 0 && length(x) == length(y)) {
wilcox_test <- wilcox.test(x, y, distribution = "asymptotic", alternative = "greater", paired = TRUE)
# Calculate effect size
effect_size <- wilcoxonPairedR(x = c(x, y), g = rep(c("II", "IV"), times = c(length(x), length(y))), ci = TRUE)
# Create a results data frame
results1 <- data.frame(
Column = 'LAP',
Survey = c("II", "IV"),
Median = summary_stats$Median,
Mean = c(mean(x, na.rm = TRUE), mean(y, na.rm = TRUE)),
SD = summary_stats$SD,
p = as.numeric(wilcox_test$p.value),
Z = as.numeric(wilcox_test$statistic),
r = effect_size$r
)
} else {
results1 <- data.frame(
Column = 'LAP',
Survey = c("II", "IV"),
Median = summary_stats$Median,
Mean = c(NA, NA),
SD = summary_stats$SD,
p = NA,
Z = NA,
r = NA
)
}
# Save the results to a CSV file
write.csv(results1, file = "LAP_WoP_Survey_II_vs_IV.csv", row.names = FALSE)
warnings()
######
##LAP_WP_Survey_II_vs_IV
# Filter data for WP group and 'LAP' column only for Surveys II and IV
WP_Data <- Data %>%
filter(Group == "WP" & (Survey == "II" | Survey == "IV")) %>%
select(Group, Survey, LAP)
# Calculate mean, median, and standard deviation for 'LAP' within WoP group
summary_stats <- WP_Data %>%
group_by(Survey) %>%
summarize(Mean = mean(LAP, na.rm = TRUE),
Median = median(LAP, na.rm = TRUE),
SD = sd(LAP, na.rm = TRUE))
# Get the means for Surveys II and IV
M1 <- summary_stats$Mean[summary_stats$Survey == "II"]
M2 <- summary_stats$Mean[summary_stats$Survey == "IV"]
# Add mean and residuals to the data frame
WoP_Data <- WP_Data %>%
mutate(mean = ifelse(Survey == "II", M1, M2),
Residual = LAP - mean)
# Filter out NA values for 'LAP'
valid_data <- WP_Data %>%
filter(!is.na(LAP))
# Extract data for Wilcoxon test
x <- valid_data$LAP[valid_data$Survey == "II"]
y <- valid_data$LAP[valid_data$Survey == "IV"]
# Perform paired Wilcoxon test if both groups have data
if (length(x) > 0 && length(y) > 0 && length(x) == length(y)) {
wilcox_test <- wilcox.test(x, y, distribution = "asymptotic", alternative = "less", paired = TRUE)
# Calculate effect size
effect_size <- wilcoxonPairedR(x = c(x, y), g = rep(c("II", "IV"), times = c(length(x), length(y))), ci = TRUE)
# Create a results data frame
results2 <- data.frame(
Column = 'LAP',
Survey = c("II", "IV"),
Median = summary_stats$Median,
Mean = c(mean(x, na.rm = TRUE), mean(y, na.rm = TRUE)),
SD = summary_stats$SD,
p = as.numeric(wilcox_test$p.value),
Z = as.numeric(wilcox_test$statistic),
r = effect_size$r
)
} else {
results2 <- data.frame(
Column = 'LAP',
Survey = c("II", "IV"),
Median = summary_stats$Median,
Mean = c(NA, NA),
SD = summary_stats$SD,
p = NA,
Z = NA,
r = NA
)
}
# Save the results to a CSV file
write.csv(results2, file = "LAP_WP_Survey_II_vs_IV.csv", row.names = FALSE)
warnings()
######
##LAP_WoP Survey IV vs WP Survey IV
# Filter data for WP group and 'LAP' column only for Surveys IV
Survey_IV_Data <- Data %>%
filter(Survey == "IV" & (Group == "WoP" | Group == "WP")) %>%
select(Survey, Group, LAP)
# Calculate mean, median, and standard deviation for 'LAP' within WoP group
summary_stats <- Survey_IV_Data %>%
group_by(Group) %>%
summarize(Mean = mean(LAP, na.rm = TRUE),
Median = median(LAP, na.rm = TRUE),
SD = sd(LAP, na.rm = TRUE))
# Get the means for Surveys IV WoP and WP
M1 <- summary_stats$Mean[summary_stats$Group == "WoP"]
M2 <- summary_stats$Mean[summary_stats$Group == "WP"]
# Add mean and residuals to the data frame
Survey_IV_Data <- Survey_IV_Data %>%
mutate(mean = ifelse(Group == "WoP", M1, M2),
Residual = LAP - mean)
# Filter out NA values for 'LAP'
valid_data <- Survey_IV_Data %>%
filter(!is.na(LAP))
# Extract data for Wilcoxon test
x <- valid_data$LAP[valid_data$Group == "WoP"]
y <- valid_data$LAP[valid_data$Group == "WP"]
# Perform unpaired Wilcoxon test if both groups have data
if (length(x) > 0 && length(y) > 0 && length(x) == length(y)) {
wilcox_test <- wilcox.test(x, y, distribution = "asymptotic", alternative = "less", paired = FALSE)
# Calculate effect size
effect_size <- wilcoxonR(x = c(x, y), g = rep(c("WoP", "WP"), times = c(length(x), length(y))), ci = TRUE)
# Create a results data frame
results3 <- data.frame(
Column = 'LAP',
Group = c("WoP", "WP"),
Median = summary_stats$Median,
Mean = c(mean(x, na.rm = TRUE), mean(y, na.rm = TRUE)),
SD = summary_stats$SD,
p = as.numeric(wilcox_test$p.value),
Z = as.numeric(wilcox_test$statistic),
r = effect_size$r
)
} else {
results3 <- data.frame(
Column = 'LAP',
Group = c("WoP", "WP"),
Median = summary_stats$Median,
Mean = c(NA, NA),
SD = summary_stats$SD,
p = NA,
Z = NA,
r = NA
)
}
# Save the results to a CSV file
write.csv(results3, file = "LAP_Survey IV WoP vs WP.csv", row.names = FALSE)