-
Notifications
You must be signed in to change notification settings - Fork 0
/
normality.R
executable file
·218 lines (158 loc) · 16.5 KB
/
normality.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
normality <- function(data, subject, analyte, decimal){
if(nrow(data) > 0){
library(nortest)
resultStep1 = NULL
resultStep2 = NULL
analyteValue = data$value
loganalytevalue = log(analyteValue)
logdata = data
logdata$value = loganalytevalue
replicate = as.factor(data[,"subject"])
nreplicate = length(levels(replicate))
nameanalyte = analyte
sw1 = tapply(analyteValue, replicate, shapiro.test)
unlist1 = data.frame(matrix(unlist(sw1), nrow=nreplicate, ncol=4, byrow=T))
unlist1$X2 = as.numeric(as.character(unlist1$X2))
pval1 = unlist1[,2]
nreject1 = table(pval1>0.05)[[1]]
if(nreject1 == length(pval1)){
naccept1 = 0
}else{
naccept1 = table(pval1>0.05)[[2]]
}
normalityRate = naccept1/nrow(unlist1)
if(normalityRate > 0.50){ # if data are normally distributed, then it uses original data, if not it applies a logarithmic transformation and uses this transformed-data for the rest of the calculations
test = "Shapiro-Wilk"
naccept = naccept1
nreject = nreject1
normalityRate = round(normalityRate*100,decimal)
resultStep1 = cbind.data.frame(Step1 = "On set of results from each individual",Test = test, "# of acceptance of normality" = naccept, "# of rejection normality" = nreject, "Normality rate" = paste0("%",normalityRate), Comment = paste0("The data is assumed to be normal on set of results from each individual for ", nameanalyte) )
rownames(resultStep1) = NULL
# cat("\n----------------------------------------------------------------------------------\nSTEP 1. NORMALITY TEST:-ON SET OF RESULTS FROM EACH INDIVIDUAL")
# cat("\n----------------------------------------------------------------------------------\nShapiro-Wilk's normality test is applied")
# cat("\n----------------------------------------------------------------------------------\nThe data is assumed to be normal on set of results from each individual for", nameanalyte,"\n----------------------------------------------------------------------------------")
# cat("\nNumber of normally distributed subjectervations", naccept1,"\n----------------------------------------------------------------------------------")
# cat("\nNumber of non-normally distributed subjectervations", nreject1,"\n----------------------------------------------------------------------------------")
#
meanval = tapply(analyteValue, replicate, mean)
sw2 = shapiro.test(meanval)
pval2 = sw2$p.value
if (pval2 >= 0.05){
resultStep2 = cbind.data.frame(Step2 = "On mean values of subjects", Test = "Shapiro-Wilk", "Test statistic" = round(sw2$statistic,decimal),
"p value" = ifelse(pval2<0.001, "<0.001", round(pval2,3)), Comment = paste0("The data is assumed to be normal on mean values of subjects for ", nameanalyte),
"Final result" = "Analyses can be continued by nested ANOVA using the original data.")
rownames(resultStep2) = NULL
# cat("\n----------------------------------------------------------------------------------\nSTEP 2. NORMALITY TEST:-ON MEAN VALUES OF SUBJECTS")
# cat("\n----------------------------------------------------------------------------------\nShapiro-Wilk's normality test is applied","\n----------------------------------------------------------------------------------")
# cat("\nThe data is assumed to be normal on mean values of subjects for", nameanalyte,"\n----------------------------------------------------------------------------------")
# cat("\nAnalyses can be continued by nested ANOVA","\n----------------------------------------------------------------------------------")
} else {
ks1 = nortest::lillie.test(meanval)
pval3 = ks1$p.value
if (pval3 >= 0.05){
resultStep2 = cbind.data.frame(Step2 = paste0("On mean values of subjects. Normality assumption is violated on mean values of subjects for ", nameanalyte, ". Kolmogorov-Smirnov's normality test is applied. The data is assumed to be normal on mean values of subjects for ", nameanalyte), Test = "Kolmogorov-Smirnov", "Test statistic" = round(ks1$statistic,decimal),
"p value" = ifelse(pval3<0.001, "<0.001", round(pval3,3)), Comment = paste0("The data is assumed to be normal on mean values of subjects for ", nameanalyte),
"Final result" = "Analyses can be continued by nested ANOVA using the original data.")
# cat("\n----------------------------------------------------------------------------------\nSTEP 2. NORMALITY TEST:-ON MEAN VALUES OF SUBJECTS")
# cat("\n----------------------------------------------------------------------------------\nShapiro-Wilk's normality test is applied","\n----------------------------------------------------------------------------------")
# cat("\nNormality assumption is violated on mean values of subjects for", nameanalyte,"\n----------------------------------------------------------------------------------")
# cat("\nKolmogorov-Smirnov's normality test is applied", "\n----------------------------------------------------------------------------------")
# cat("\nThe data is assumed to be normal on mean values of subjects for", nameanalyte,"\n----------------------------------------------------------------------------------")
# cat("\nAnalyses can be continued by nested ANOVA","\n----------------------------------------------------------------------------------")
} else {
logmeanval = log(meanval)
sw3 = shapiro.test(logmeanval)
pval4 = sw3$p.value
if (pval4 >= 0.05){
resultStep2 = cbind.data.frame(Step2 = paste0("On mean values of subjects (normality assumption is violated on mean values of subjects for ",
nameanalyte, ". Kolmogorov-Smirnov's normality test is applied. Normality assumption is violated again on mean values of subjects for ", nameanalyte, ". Logarithmic transformation is applied. The data is assumed to be normal on mean values of subjects for ", nameanalyte),
Test = "Kolmogorov-Smirnov", "Test statistic" = round(sw3$statistic,decimal),
"p value" = ifelse(pval4<0.001, "<0.001", round(pval4,3)), Comment = paste0("The data is assumed to be normal on mean values of subjects for ", nameanalyte),
"Final result" = "Analyses can be continued by nested ANOVA using the log-transformed data.")
#
# cat("\n----------------------------------------------------------------------------------\nSTEP 2. NORMALITY TEST:-ON MEAN VALUES OF SUBJECTS")
# cat("\n----------------------------------------------------------------------------------\nShapiro-Wilk's normality test is applied","\n----------------------------------------------------------------------------------")
# cat("\nNormality assumption is violated on mean values of subjects for ", nameanalyte,"\n----------------------------------------------------------------------------------")
# cat("\nKolmogorov-Smirnov's normality test is applied", "\n----------------------------------------------------------------------------------")
# cat("\nNormality assumption is violated again on mean values of subjects for ", nameanalyte,"\n----------------------------------------------------------------------------------")
# cat("\nLogarithmic transformation is applied", "\n----------------------------------------------------------------------------------")
# cat("\nThe data is assumed to be normal on mean values of subjects for", nameanalyte,"\n----------------------------------------------------------------------------------")
# cat("\nAnalyses can be continued by nested ANOVA","\n----------------------------------------------------------------------------------")
} else{
resultStep2 = cbind.data.frame(Step2 ="On mean values of subjects", "Final comment" = paste0("Shapiro-Wilk's normality test is applied. Normality assumption is violated on mean values of subjects for ", nameanalyte,
". Kolmogorov-Smirnov's normality test is applied. Normality assumption is violated again on mean values of subjects for ", nameanalyte,
". Logarithmic transformation is applied. Normality assumption is violated again on mean values of subjects for ", nameanalyte, ". The data is not normally distributed for ", nameanalyte,
". Analyses should not be continued by nested ANOVA.")
)
}
}
}
}else {
sw6 = tapply(loganalytevalue, replicate, shapiro.test)
unlist2 = data.frame(matrix(unlist(sw6), nrow=nreplicate, ncol=4, byrow=T))
unlist2$X2 = as.numeric(as.character(unlist2$X2))
pval6 = unlist2[,2]
nreject2 = table(pval6>0.05)[[1]]
if(length(pval6) == nreject2){
naccept2 = 0
}else{
naccept2 = table(pval6>0.05)[2]
}
names(naccept2) = names(nreject2) = NULL
normalityRate = naccept2/nrow(unlist2)
if(naccept2 > nreject2){
meanval = tapply(analyteValue, replicate, mean)
logmeanval = log(meanval)
sw7 = shapiro.test(logmeanval)
pval7 = sw7$p.value
normalityRate = round(normalityRate*100,decimal)
if (pval7 >= 0.05){
resultStep1 = cbind.data.frame(Step1 = "On set of results from each individual", Warning = "Logarithmic transformation is applied",
Test = "Kolmogorov-Smirnov", "# of acceptance of normality" = naccept2,
"# of rejection normality" = nreject2, "Normality rate" = paste0("%",normalityRate), Comment = paste0("The data is assumed to be normal on set of results from each individual for ", nameanalyte) )
rownames(resultStep1) = NULL
# cat("\n----------------------------------------------------------------------------------\nSTEP 1. NORMALITY TEST:-ON SET OF RESULTS FROM EACH INDIVIDUAL")
# cat("\n----------------------------------------------------------------------------------\nShapiro-Wilk's normality test is applied")
# cat("\n----------------------------------------------------------------------------------\nNormality assumption is violated on set of results from each individual for", nameanalyte,"\n----------------------------------------------------------------------------------")
# cat("\nNumber of normally distributed subjectervations", naccept1,"\n----------------------------------------------------------------------------------")
# cat("\nNumber of non-normally distributed subjectervations", nreject1,"\n----------------------------------------------------------------------------------")
# cat("\nLogarithmic transformation is applied", "\n----------------------------------------------------------------------------------")
# cat("\nNumber of normally distributed subjectervations", naccept2,"\n----------------------------------------------------------------------------------")
# cat("\nNumber of non-normally distributed subjectervations", nreject2,"\n----------------------------------------------------------------------------------")
# cat("\nThe data is assumed to be normal on set of results from each individual for", nameanalyte,"\n----------------------------------------------------------------------------------")
resultStep2 = cbind.data.frame(Step2 = "On mean values of subjects", Warning = "Logarithmic transformation is applied", Test = "Shapiro-Wilk", "Test statistic" = round(sw7$statistic,2),
"p value" = ifelse(pval7<0.001, "<0.001", round(pval7,3)), Comment = paste0("The data is assumed to be normal on mean values of subjects for ", nameanalyte),
"Final result" = "Analyses can be continued by nested ANOVA using the log-transformed data.")
rownames(resultStep2) = NULL
# cat("\n----------------------------------------------------------------------------------\nSTEP 2. NORMALITY TEST:-ON MEAN VALUES OF SUBJECTS")
# cat("\n----------------------------------------------------------------------------------\nShapiro-Wilk's normality test is applied","\n----------------------------------------------------------------------------------")
# cat("\nThe data is assumed to be normal on mean values of subjects for", nameanalyte,"\n----------------------------------------------------------------------------------")
# cat("\nAnalyses can be continued by nested ANOVA","\n----------------------------------------------------------------------------------")
} else {
normalityRate = round(100*(naccept1/(naccept1+nreject1)),decimal)
resultStep1 = cbind.data.frame(Step1 = "On set of results from each individual", Warning = "Logarithmic transformation is applied", Test = "Shapiro-Wilk",
"# of acceptance of normality" = naccept1, "# of rejection normality" = nreject1, "Normality rate" = paste0("%",normalityRate), Comment = paste0("The data is assumed to be normal on set of results from each individual for ", nameanalyte) )
rownames(resultStep1) = NULL
resultStep2 = cbind.data.frame(Step2 ="On mean values of subjects", "Final comment" = paste0("Shapiro-Wilk's normality test is applied to the trasnformed data. Normality assumption is violated again on mean values of subjects for ", nameanalyte,
". The data is not normally distributed for ", nameanalyte,
". Analyses should not be continued by nested ANOVA."))
}
} else {
normalityRate = round(100*(naccept1/(naccept1+nreject1)),decimal)
resultStep1 = cbind.data.frame(Step1 = "On set of results from each individual", Warning = "Logarithmic transformation is applied", Test = "Shapiro-Wilk", "# of acceptance of normality" = naccept1,
"# of rejection normality" = nreject1, "Normality rate" = paste0("%",normalityRate),
Comment = paste0("The data is not normally distributed for ", nameanalyte), "Final result" = "Analyses should not be continued by nested ANOVA")
rownames(resultStep1) = NULL
resultStep2 = cbind.data.frame(Note = "Analyses should not be continued by nested ANOVA")
rownames(resultStep2) = NULL
}
}
}else{
resultStep1 = cbind.data.frame(Note = "There is no observation left after removing outliers. Therefore, the analysis is terminated.")
rownames(resultStep1) = NULL
resultStep2 = cbind.data.frame(Note = "There is no observation left after removing outliers. Therefore, the analysis is terminated.")
rownames(resultStep2) = NULL
}
normalityResult = list(Step1 = resultStep1, Step2 = resultStep2)
return(normalityResult)
}