-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNormalisation.R
797 lines (610 loc) · 26.5 KB
/
Normalisation.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
#Test normality in multiple variables and files, plot distribution, perform Kruskal-Wallis in raw variables and after applying multiple transformations
# The input file is:
# tag, indiv, sex,pop,group,measure1,m2,m3,m4,m5 ... n
# Am001 001, M, A, MA, 04,08,15,16,23 ... 42
# Bm002 002, F, B, FB, 02,71,82,81,82 ... 84
#...etc
# Two first columns must be individual or non-usable data
# Columns with grouping variables (3 in the example, from 3 to 5)
# Columns with your variables
# (In the example tabs and spaces are there only for the data to match the position of the header)
# =====================================================================
# For compatibility with Rscript.exe (only if not loading libraries correctly):
# =====================================================================
if(length(.libPaths()) == 1){
# We're in Rscript.exe
possible_lib_paths <- file.path(Sys.getenv(c('USERPROFILE','R_USER')),
"R","win-library",
paste(R.version$major,
substr(R.version$minor,1,1),
sep='.'))
indx <- which(file.exists(possible_lib_paths))
if(length(indx)){
.libPaths(possible_lib_paths[indx[1]])
}
# CLEAN UP
rm(indx,possible_lib_paths)
}
#install.packages("ggplot2")
library(ggplot2)
#install.packages("ggfortify")
library(ggfortify)
#install.packages("ggpubr")
library(ggpubr)
#install.packages("ggpmisc")
library(ggpmisc)
#install.packages("npsurv") # not available anymore
#library(npsurv) # not available anymore
#install.packages("fitdistrplus")
library(fitdistrplus)
#install.packages("broom")
library(broom)
#install.packages("psych")
library(psych)
#trying this packages for standardisation and normalisations
#install.packages("caret")
library("caret")
#install.packages("heatmaply")
library("heatmaply")
### TRY TO LOOP THROUGH ALL YOUR FILES AND VARIABLES
rm(list = ls()) #Remove all objects
cat("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") # I like clean spaces when trying the same analysis with slight changes or differents datasets
#Check working directory
getwd()
setwd("D:/Dropbox/MOSKY/CURRO/PODARCIS/Measurements/nat_pop_Iva/pheno-env/jun21_RDAnew/residuals_norm/norm/bysex")
## FILE LIST, INPUT PATH AND PATTERN
filelist <- list.files(path = ".", pattern = ".csv$") #Change "." for the path of a directory with your data files "folder/subdirectory/here"; You can also make pattern more specific to filter out some files: "_final_version.csv"
filelist #check that all the fileas are alright
#open one file to check
wrk <- read.delim(filelist[[1]], sep = ',', header =TRUE) #comma separated file with column headers
str(wrk) #check structure of the file
head(wrk) #check file
#Individual tags if any must be in the left most column
#Number of Columns with Stuff (to ignore) -including individual tags and factor/grouping variables, before the first numerical variable to analyze-
otherstuff=5
# CHECK A FILE
coltags <- names(wrk)
maxcol <- ncol (wrk) #number of columns
lastgrup = otherstuff #column with the last grouping variable
firstvar = lastgrup + 1 # column with the first variable
allvar = maxcol - lastgrup
varnames = coltags[c(firstvar:maxcol)]
varnum = length(varnames)
cat ("\n\nFile checked has", varnum, "variables:", varnames, "\n will be tested for Normality\n\n")
################## RUN EVERYTHING BELLOW IF IT SEEMS ALRIGHT ########################
#warnings()
rawtime = Sys.time()
hour<- sub(".* ", "", rawtime)
onlydate<-sub(" .*", "", rawtime)
printime <- gsub(":", "-", hour)
numfiles = length(filelist) # extract how many files there are
myrange = c(1:numfiles) #range from 1 to number of files
file1 = paste("Normality_", onlydate, "_", printime, "_out.txt", sep="")
cat ( "Shapiro test of Normality p-values\n", "File_name\tvariable\ttransformation\tvalue_added\tShapiroW pval\tNormality?", sep="\n", file=file1, append=FALSE)
#k=1
#generate an empty data frame for the summary table
summarytable = data.frame(matrix("NA", ncol = 3, nrow = 1))
names(summarytable) <- c("File", "Variable", "Normal transformations")
rown=1
for (k in myrange) {
csvfile <- filelist[[k]] #choose one of the file names in each loop
# OPEN INPUT FILE
cat("Processing ", csvfile, "\n") #Print on screen
measurespod <- read.delim(filelist[[k]], sep = ',', header =TRUE)
refrow = c()
coltags <- names(measurespod)
#coltags
maxcol <- ncol (measurespod) #number of columns
lastgrup = otherstuff #column with the last grouping variable
firstvar = lastgrup + 1 # column with the first variable
allvar = maxcol - lastgrup
varnames = coltags[c(firstvar:maxcol)]
#varnames
varrange = c(firstvar:maxcol)
#varrange
varnum = length(varnames)
#extract the variables to test
#i=15
for (i in varrange) {
normlist="NONE"
varname <- coltags[i]
cat(varname, sep="\n")
#extract the variable
variable <- as.vector(measurespod[[i]])
#variable
#if there is negative values calculate how much you should add to all of them for some transformations
minim <- min(variable)
minim
if(minim < -0.1) {
toadd=0.1
negmin=abs(minim)+toadd #minimum negative value, this will added to all the values, so there is no negative values
#negmin
} else if (minim < 0) {
toadd=0.01
negmin=abs(minim)+toadd #minimum negative value, this will added to all the values, so there is no negative values
#negmin
} else { negmin = 0 }
#negmin
#toadd
#variable
##### SHAPIRO-WILK
#raw
#perform test
testout <- shapiro.test (((variable)))
#declare if something was added to the variable
added=0
#save if it is significantly different from Normal
pval = round(testout$p.value, digits=6)
if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
normlist="no-transf"
}
#isnormal
#declare which transformation was used with the data
transformed = "no-transf"
Nans = "no"
#print table
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
plotname = paste(csvfile, "_", varname, "-distrb-plot.png", sep="")
png(plotname, units="in", width=10, height=7, res=300)
descdist(variable, boot=1000)
dev.off()
#log
transformed = "nat log"
added=negmin
#check if there are missing
checktrans = any(is.na(log(variable+negmin)))
if(checktrans==FALSE) {
testout <- shapiro.test ((log(variable+negmin)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#log10
transformed = "log 10"
added=negmin
#check if there are missing
checktrans = any(is.na(log10(variable+negmin)))
if(checktrans==FALSE) {
testout <- shapiro.test ((log10(variable+negmin)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#squareroot
added=negmin
transformed = "sqr root"
#check if there are missing
checktrans = any(is.na(((variable+negmin)^(1/2))))
if(checktrans==FALSE) {
testout <- shapiro.test (((variable+negmin)^(1/2)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#cubic root
added=negmin
transformed = "cubic root"
#check if there are missing
checktrans = any(is.na(((variable+negmin)^(1/3))))
if(checktrans==FALSE) {
testout <- shapiro.test (((variable+negmin)^(1/3)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#4th root
added=negmin
transformed = "4th root"
#check if there are missing
checktrans = any(is.na(((variable+negmin)^(1/4))))
if(checktrans==FALSE) {
checktrans = any(is.na(((variable+negmin)^(1/4))))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#fifth root
added=negmin
transformed = "5th root"
#check if there are missing
checktrans = any(is.na(((variable+negmin)^(1/5))))
if(checktrans==FALSE) {
testout <- shapiro.test (((variable+negmin)^(1/5)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
##exponential
added=0
transformed = "exp"
#check if there are missing
checktrans = any(is.na(exp(variable)))
checknums = length(unique(exp(variable)))
if (checknums < 2) { checktrans = TRUE }
if(checktrans==FALSE) {
testout <- shapiro.test ((exp(variable)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#logistic
added=negmin
transformed = "logistic"
#check if there are missing
checktrans = any(is.na(logistic(variable+negmin)))
checknums = length(unique(logistic(variable)))
if (checknums < 2) { checktrans = TRUE }
if(checktrans==FALSE) {
testout <- shapiro.test ((logistic(variable)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#power of two
added=negmin
transformed = "pwr2"
#check if there are missing
checktrans = any(is.na(((variable+negmin)^2)))
if(checktrans==FALSE) {
testout <- shapiro.test (((variable)^2))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#power of three
added=negmin
transformed = "pwr3"
#check if there are missing
checktrans = any(is.na(((variable+negmin)^3)))
if(checktrans==FALSE) {
testout <- shapiro.test (((variable)^3))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
##power of four
added=negmin
transformed = "pwr4"
#check if there are missing
checktrans = any(is.na(((variable+negmin)^4)))
if(checktrans==FALSE) {
testout <- shapiro.test (((variable)^4))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#power of five
added=negmin
transformed = "pwr5"
#check if there are missing
checktrans = any(is.na(((variable+negmin)^5)))
if(checktrans==FALSE) {
testout <- shapiro.test (((variable)^5))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#gamma
added=0
transformed = "gamma"
#check if there are missing
checktrans = any(is.na(gamma(variable)))
checknums = length(unique(gamma(variable)))
if (checknums < 2) { checktrans = TRUE }
if(checktrans==FALSE) {
testout <- shapiro.test ((gamma(variable)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#sin
added=0
transformed = "sin"
#check if there are missing
checktrans = any(is.na(sin(variable)))
if(checktrans==FALSE) {
testout <- shapiro.test ((sin(variable)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#asin
added=0
transformed = "asin"
#check if there are missing
checktrans = any(is.na(asin(variable)))
if(checktrans==FALSE) {
testout <- shapiro.test ((asin(variable)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#cos
added=0
transformed = "cos"
#check if there are missing
checktrans = any(is.na(cos(variable)))
if(checktrans==FALSE) {
testout <- shapiro.test ((cos(variable)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#tan
added=0
transformed = "tan"
#check if there are missing
checktrans = any(is.na(tan(variable)))
if(checktrans==FALSE) {
testout <- shapiro.test ((tan(variable)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#scaled sin
added=0
transformed = "std sin"
#check if there are missing
checktrans = any(is.na(sin(scale((variable), center=TRUE, scale=TRUE))))
if(checktrans==FALSE) {
testout <- shapiro.test (sin(scale((variable), center=TRUE, scale=TRUE)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#scaled cos
added=0
transformed = "std asin"
#check if there are missing
checktrans = any(is.na(asin(scale((variable), center=TRUE, scale=TRUE))))
if(checktrans==FALSE) {
testout <- shapiro.test (asin(scale((variable), center=TRUE, scale=TRUE)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#scaled cos
added=0
transformed = "std cos"
#check if there are missing
checktrans = any(is.na(cos(scale((variable), center=TRUE, scale=TRUE))))
if(checktrans==FALSE) {
testout <- shapiro.test (cos(scale((variable), center=TRUE, scale=TRUE)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
#scaled tan
added=0
transformed = "std tan"
#check if there are missing
checktrans = any(is.na(tan(scale((variable), center=TRUE, scale=TRUE))))
if(checktrans==FALSE) {
testout <- shapiro.test (tan(scale((variable), center=TRUE, scale=TRUE)))
checkshapir = any(is.na(testout$p.value))
checkshapir
pval = round(testout$p.value, digits=6)
if (checkshapir == TRUE) {
cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE)
} else if (pval < 0.05) {
isnormal="NO"
} else {
isnormal="YES!"
if (normlist=="NONE") { normlist = transformed } else { normlist = c(normlist, transformed) }
}
#isnormal
cat(paste(csvfile, varname, transformed, added, pval, isnormal, sep="\t"), sep="\n", file=file1, append=TRUE)
} else { cat(paste(csvfile, varname, transformed, added, "#N/A!", "#N/A!", sep="\t"), sep="\n", file=file1, append=TRUE) }
printlist = paste(normlist, collapse=" / ")
addvector=c(csvfile, varname, printlist)
summarytable[rown,] <- addvector
rown=rown+1
}
cat ("\nAll variables in ", csvfile, " analyzed.\n\n", sep ="")
}
summaryfile = paste("SummaryTable", onlydate, "_", printime, "_Normality.txt", sep="")
write.table(summarytable, file=summaryfile, sep="\t", quote=FALSE, row.names=FALSE)
cat ("\n\nALL FILES ANALYZED!\n\n\n")