-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2 - Wilcoxon with Boxes.Rmd
248 lines (186 loc) · 8.78 KB
/
2 - Wilcoxon with Boxes.Rmd
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
---
title: "Bits of Code"
author: "Amir Nakar"
date: "10/1/2020"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, include=FALSE}
load(file = "~/MorK.2020/REnviron.RData")
#### Load the data ####
library(openxlsx)
library(reshape2)
library(ggplot2)
library(Hmisc)
library(dplyr)
library(car)
library(caret)
library(readxl)
library(ggsignif)
library(tidyr)
library(vegan)
library(purrr)
library(egg)
```
```{r message=FALSE}
# Significance testing:
# We will use the wilcoxon Ranked Sum Test as a non-parametric test:
colnames(data)
#### Function explained ####
# This function will work as follows:
# Input: two variable names from your data set
# Output: Boxplot with Wilcoxon Rank Sum Test statistic printed on it
# Notes:
# 1. You can ignore the warnings:
# No id variables; using all as measure variables
# Removed 2 rows containing non-finite values
# It's just the way it handles NA lines. Which you have
# 2. Box plots:
# The middle line is the median
#
# The lower and upper hinges correspond to the first and third quartiles
#
# The whiskers extend from the hinge to the furthest value but no further than 1.5 * IQR from the hinge
# (where IQR is the inter-quartile range, or distance between the first and third quartiles).
# Outliers: Points beyond the whisker limit
wilcox.plus.boxes <- function(var1, var2) { # Defines the function requiring two parameters
data.for.function = data[,c(var1,var2)] # creates a dataframe from your data which is only the two parameters you chose
colnames(data.for.function) = c(var1,var2) # Gives the relevant columns names (for later)
varname = gsub("\\..*","",var1)
levs = c("New","Old")
result = wilcox.test( # Calculates the wilcoxon test between the two variables
data.for.function[,1],
data.for.function[,2]
)
mean.diff = mean(data.for.function[,1], na.rm = T)-mean(data.for.function[,2],na.rm = T)
melted = melt(data.for.function) # Transforms the data for ggplot
plot = ggplot( # This is a very long function to create the plot
melted, aes(x=variable,y=value,fill=variable)) + # Defines the releant data
geom_boxplot()+ # Defines the plot type as Box Plot
theme(panel.background = element_rect(fill="white")) + # Makes it visually clean
labs(x = varname,y="Time [Mins]") + # Adds X, Y axis labels
theme(panel.grid.major = element_line(colour = "gray")) + # adds gray lines on the Y axis
theme(panel.grid.minor = element_line(colour = "gray")) + # does the same
guides(fill=FALSE) + # Removes the legend (not needed for this)
labs( # Startes to create the complicated titles
title = paste("Comparison of", varname, sep = " "), # Main title: Comparison between the two variables
subtitle = paste( # Subtitle: gives the different statistics pasted together
"Result of the Wilcoxon Rank Sum Test: \ntest statistic = ",
result$statistic, "\n p-value = ",
formatC(result$p.value, format = "g", digits = 2),
"\n mean difference = ", round(mean.diff, digits = 1), "minutes", sep = " " )
) +
scale_x_discrete(breaks = c(var1,var2), labels=c("New","Old")) +
geom_signif(comparisons = list(c(var1, var2)), # Creates a significance star when the pvalue<0.05
test="wilcox.test", map_signif_level = c("*"=0.05))
plot # Saves the final plot
}
names = colnames(data) # This is for my own reference to know which variables need to be tabulated
# Calculations:
# Each Q is a question from your list
Q1 = wilcox.plus.boxes(names[33],names[32]) #SUM
Q2 = wilcox.plus.boxes(names[19],names[18]) # Appointments1
Q3 = wilcox.plus.boxes(names[27],names[26]) # Mirshm
Q4 = wilcox.plus.boxes(names[29],names[28]) # Payments
Q5 = wilcox.plus.boxes(names[31],names[30]) # Update
Q6 = wilcox.plus.boxes(names[21],names[20]) # Appointments 2
Q7 = wilcox.plus.boxes(names[25],names[24]) # Medical File
Q8 = wilcox.plus.boxes(names[23],names[22]) # Blood Test
```
```{r message=FALSE}
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q8
FIG3 = ggarrange(Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8, ncol = 4)
```
# Calculating Wilcoxon either one or two way for report:
``` {r}
colnames(data)
wilcox.test( # Calculates the wilcoxon test between the two variables
data[,22],
data[,23], alternative = "less")
```
# And now for all of them:
```{r message=FALSE}
# Significance testing:
# We will use the wilcoxon Ranked Sum Test as a non-parametric test:
colnames(data)
#### Function explained ####
# This function will work as follows:
# Input: two variable names from your data set
# Output: Boxplot with Wilcoxon Rank Sum Test statistic printed on it
# Notes:
# 1. You can ignore the warnings:
# No id variables; using all as measure variables
# Removed 2 rows containing non-finite values
# It's just the way it handles NA lines. Which you have
# 2. Box plots:
# The middle line is the median
#
# The lower and upper hinges correspond to the first and third quartiles
#
# The whiskers extend from the hinge to the furthest value but no further than 1.5 * IQR from the hinge
# (where IQR is the inter-quartile range, or distance between the first and third quartiles).
# Outliers: Points beyond the whisker limit
wilcox.plus.boxes <- function(var1, var2) { # Defines the function requiring two parameters
data.for.function = data[,c(var1,var2)] # creates a dataframe from your data which is only the two parameters you chose
colnames(data.for.function) = c(var1,var2) # Gives the relevant columns names (for later)
varname = gsub("\\..*","",var1)
levs = c("New","Old")
result = wilcox.test( # Calculates the wilcoxon test between the two variables
data.for.function[,1],
data.for.function[,2], alternative = "less"
)
mean.diff = mean(data.for.function[,1], na.rm = T)-mean(data.for.function[,2],na.rm = T)
melted = melt(data.for.function) # Transforms the data for ggplot
plot = ggplot( # This is a very long function to create the plot
melted, aes(x=variable,y=value,fill=variable)) + # Defines the releant data
geom_boxplot()+ # Defines the plot type as Box Plot
theme(panel.background = element_rect(fill="white")) + # Makes it visually clean
labs(x = varname,y="Time [Mins]") + # Adds X, Y axis labels
theme(panel.grid.major = element_line(colour = "gray")) + # adds gray lines on the Y axis
theme(panel.grid.minor = element_line(colour = "gray")) + # does the same
guides(fill=FALSE) + # Removes the legend (not needed for this)
labs( # Startes to create the complicated titles
title = paste("Comparison of", varname, sep = " "), # Main title: Comparison between the two variables
subtitle = paste( # Subtitle: gives the different statistics pasted together
"Result of the Wilcoxon Rank Sum Test: \ntest statistic = ",
result$statistic, "\n p-value = ",
formatC(result$p.value, format = "g", digits = 2),
"\n mean difference = ", round(mean.diff, digits = 1), "minutes", sep = " " )
) +
scale_x_discrete(breaks = c(var1,var2), labels=c("New","Old")) +
geom_signif(comparisons = list(c(var1, var2)), # Creates a significance star when the pvalue<0.05
test="wilcox.test", map_signif_level = c("*"=0.05))
plot # Saves the final plot
}
names = colnames(data) # This is for my own reference to know which variables need to be tabulated
# Calculations:
# Each Q is a question from your list
Q1alt = wilcox.plus.boxes(names[33],names[32]) #SUM
Q2alt = wilcox.plus.boxes(names[19],names[18]) # Appointments1
Q3alt = wilcox.plus.boxes(names[27],names[26]) # Mirshm
Q4alt = wilcox.plus.boxes(names[29],names[28]) # Payments
Q5alt = wilcox.plus.boxes(names[31],names[30]) # Update
Q6alt = wilcox.plus.boxes(names[21],names[20]) # Appointments 2
Q7alt = wilcox.plus.boxes(names[25],names[24]) # Medical File
Q8alt = wilcox.plus.boxes(names[23],names[22]) # Blood Test
```
```{r message=FALSE}
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q8
FIG3 = ggarrange(Q1alt,Q2alt,Q3alt,Q4alt,Q5alt,Q6alt,Q7alt,Q8alt, ncol = 4)
```