-
Notifications
You must be signed in to change notification settings - Fork 3
/
chisquared.R
258 lines (193 loc) · 8.3 KB
/
chisquared.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
252
253
254
255
256
257
258
# likertTable for all three functions should be formatted with question columns
# and demographic (or grouping) columns, each row should be a different
# respondents answer. Cells should be e.g. likert responses (More, etc.) or
# demographic data (e.g. age groups)
## Results table for the addKruskal and addChiSquared functions should be formulated like this:
#
# activityGardensResults <-
# tibble(
# demographicVariable = c(
# "Gender",
# "Marital Status",
# "Age",
# "Housing",
# "Town Type",
# "Governorate",
# "Employment",
# "Work Location",
# "Education",
# "Before Income",
# "After Income"
# ),
# afterGardensRelaxing_CHI = rep(0.0, length(demographicVariable)),
# afterGardensRelaxing_PVAL = rep(0.0, length(demographicVariable)),
# afterGardensRelaxing_CORPVAL = rep(0.0, length(demographicVariable)),
# afterGardensExercise_CHI = rep(0.0, length(demographicVariable)),
# afterGardensExercise_PVAL = rep(0.0, length(demographicVariable)),
# afterGardensExercise_CORPVAL = rep(0.0, length(demographicVariable)),
# afterGardensBirdPhotography_CHI = rep(0.0, length(demographicVariable)),
# afterGardensBirdPhotography_PVAL = rep(0.0, length(demographicVariable)),
# afterGardensBirdPhotography_CORPVAL = rep(0.0, length(demographicVariable))
#
# )
addKruskal <-
function(likertTable,
resultsTable,
questionColumns,
demographicColumns) {
i = min(demographicColumns) # row
j = min(questionColumns) # column
a = 1 # the first row in the resultsTable that should have results assigned.
c = 4 # first column that should have corrected pvalues
# Iterate through each demographic variable
for (i in min(demographicColumns):max(demographicColumns)) {
b = 2 # the first column in the resultsTable that should have results assigned to it
for (j in min(questionColumns):max(questionColumns)) {
# Create a temp table and filter out blanks.
tempTable <- likertTable[, c(j, i)]
tempTable <- tempTable[tempTable[2] != "",]
# perform the kruskal wallis test
tempKruskal <- kruskal.test(tempTable[, 1] ~ tempTable[, 2])
# add statistic value and pvalue to the table.
resultsTable[a, b] <- tempKruskal$statistic
resultsTable[a, b + 1] <- tempKruskal$p.value
# add 2 so next results go in the proper place
b = b + 3
} # end question loop
a = a + 1 # go to next row for next set of demographic info
} # end demographic loop
# add corrected pvalue
z = 1
for (z in 1:length(questionColumns)) {
tempCorrected <-
p.adjust(as.vector(unlist(resultsTable[, c - 1])), method = "holm")
resultsTable[, c] <- tempCorrected
c = c + 3
} # end addition of corrected pvalues
return(resultsTable)
} # end function
addChiSquared <-
function(likertTable,
resultsTable,
questionColumns,
demographicColumns,
minVal = 25) {
i = min(demographicColumns) # row
j = min(questionColumns) # column
a = 1 # the first row in the resultsTable that should have results assigned.
c = 4 # first column that should have corrected pvalues
# Iterate through each demographic variable
for (i in min(demographicColumns):max(demographicColumns)) {
b = 2 # the first column in the resultsTable that should have results assigned to it
for (j in min(questionColumns):max(questionColumns)) {
# Create a temp table and filter out blanks.
tempTable <- likertTable[, c(j, i)]
tempTable <-
tempTable[tempTable[2] != "" & !is.na(tempTable[1]) ,]
# Check to see if any group has less than min responses.
testSize <-
tempTable %>% count(tempTable[1:2]) %>% group_by(across(.cols = 2)) %>% summarise(n = sum(n))
tooSmall <- testSize[testSize[2] < minVal, 1]
print(unlist(tooSmall))
# print(colnames(tempTable)) # use to debug
# Based on this, either assign a "can't be tested"/NA indicator or the values for the chi-squared test.
if (length(setdiff(unique(tempTable[, 2]), tooSmall)) < 2) {
resultsTable[a, b] <- NA
resultsTable[a, b + 1] <- NA
} else {
tempTable <- tempTable[!(tempTable[, 2] %in% tooSmall),]
# perform the chi squared test
tempChi <- chisq.test(tempTable[, 1], tempTable[, 2])
# add statistic value and pvalue to the table.
resultsTable[a, b] <- tempChi$statistic
resultsTable[a, b + 1] <- tempChi$p.value
} # end of else
# Use this if you want to export a Pivot table
# pivot <- pivot_wider(testSize,
# names_from = colnames(testSize[1]),
# values_from = n)
# Clean up
remove(testSize, tooSmall)
# add 2 so next results go in the proper place
b = b + 3
} # end question loop
a = a + 1 # go to next row for next set of demographic info
} # end demographic loop
# add corrected pvalue
z = 1
for (z in 1:length(questionColumns)) {
tempCorrected <-
p.adjust(as.vector(unlist(resultsTable[, c - 1])), method = "holm")
resultsTable[, c] <- tempCorrected
c = c + 3
} # end addition of corrected pvalues
return(resultsTable)
} # end function
# This function needs to compare between categories within a question to see
# which categories are significantly different.
posthocChiSquared <-
function(likertTable,
questionColumn,
demographicColumn,
minVal = 25,
correction = TRUE) {
tempTable <- likertTable[, c(questionColumn, demographicColumn)]
tempTable <-
tempTable[tempTable[2] != "" & !is.na(tempTable[1]) , ]
# Check to see if any group has less than min allowed responses.
testSize <-
tempTable %>% count(tempTable[1:2]) %>% group_by(across(.cols = 2)) %>% summarise(n = sum(n))
tooSmall <- testSize[testSize[2] < minVal, 1]
print(unlist(tooSmall))
# Filter out too small categories
tempTable <- tempTable[!(tempTable[, 2] %in% tooSmall), ]
# and make sure demographic is a factor (this gets lost sometimes)
if (is.factor(tempTable[, 2]) == FALSE) {
tempTable[, 2] <- as.factor(tempTable[, 2])
}
# Make sure that the number of unique is more than two
if (nlevels(tempTable[, 2]) < 3) {
stop(print("Too few levels"))
}
numLevels <- nlevels(tempTable[, 2])
# create the results table
#create matrix with correct number of columns
resultsTable <- matrix(rep(999, times = numLevels ^ 2),
ncol = numLevels,
byrow = TRUE)
#define column names and row names of matrix
tempLevels <- levels(tempTable[, 2])
colnames(resultsTable) <- tempLevels
rownames(resultsTable) <- tempLevels
# for each [i,j] pair of factors add the pvalue for chisquared test
i = 1 # row
j = 1 # column
for (i in 1:numLevels) {
for (j in 1:numLevels) {
if (i != j) {
# subset for i and j levels
testTable <-
tempTable[tempTable[, 2] %in% tempLevels[c(i, j)] ,]
# run test and assign pvalue to i,j spot
resultsTable[i, j] <-
chisq.test(testTable[, 1], testTable[, 2])$p.value
} else {
resultsTable[i, j] <- NA
}
} # end column loop
} # end row loop
# remove the lower triangle--can probably make this a filter but this is easy
resultsTable[lower.tri(resultsTable, diag = FALSE)] <- NA
# correct pvalues
if (correction == TRUE) {
resultsTable <-
matrix(
p.adjust(as.vector(resultsTable), method = 'holm'),
ncol = numLevels,
dimnames = list(tempLevels, tempLevels)
)
}
#convert matrix to a tibble
resultsTable <- as_tibble(resultsTable, rownames = "levels")
return(resultsTable)
} # end function