-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-tests.R
More file actions
176 lines (121 loc) · 6.32 KB
/
03-tests.R
File metadata and controls
176 lines (121 loc) · 6.32 KB
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
#########################################################################################
### Book:
### Visualization and Imputation of Missing Values
### Springer Publishing, 2013
### Chapter 2: Distribution, pre-analysis of missing values and data quality.
### Copyright: Matthias Templ
### University of Applied Sciences and Arts Northwestern Switzerland (FHNW)
### Licence: GPL3
## ----matrixplot2var, message=FALSE, warning=FALSE, fig.cap="Matrixplot of log10(Dream) and log10(BodyWgt) of the sleep data set. Missing are in red while any other value of the data matrix is in grey, whereby the grey scale depends on the magnitude of the value.", tidy=FALSE----
library(VIM)
data(sleep, package = "VIM")
matrixplot(log10(sleep[, c("Dream", "BodyWgt")]),
sortby = "BodyWgt")
## ----ttest-------------------------------------------------------------------------------------------
sleep$mDream <- factor(is.na(sleep$Dream))
table(sleep$mDream)
## ----ttest2, tidy = FALSE, echo = TRUE, eval=FALSE---------------------------------------------------
## t.test(BodyWgt ~ mDream, data = sleep)
## ----ttest2show, tidy = FALSE, echo=FALSE, eval=TRUE-------------------------------------------------
t.test(BodyWgt ~ mDream, data = sleep)
## ----wilcoxtest, echo=TRUE---------------------------------------------------------------------------
wilcox.test(BodyWgt ~ mDream, data = sleep)
## ----glmresprint, eval = TRUE, echo=FALSE, warning=FALSE, message=FALSE, tidy=FALSE------------------
x <- na.omit(sleep[, c("BodyWgt","BrainWgt","mDream")])
x <- sleep[, c("BodyWgt","BrainWgt","mDream")]
res <- glm(mDream ~ Span + Gest + log(BodyWgt) + log(BrainWgt) +
Danger + Exp + Pred,
data = sleep,
family = binomial(),
na.action = "na.omit")
summary(res)
## ----sleeppatternsoutput, echo = FALSE, tidy=FALSE---------------------------------------------------
data(sleep, package = "VIM")
summary(aggr(sleep[, 1:7], plot = FALSE))
# or alternatively with a lot of more information
# (we only display a part of the information)
s <- robCompositions::missPatterns(sleep[, 1:7])
s$groups
## ----pattern6----------------------------------------------------------------------------------------
s$groups$`0:0:1:1:0:0:0`
## ----obs6--------------------------------------------------------------------------------------------
sleep[s$groups$`0:0:1:1:0:0:0`, 1:7]
## ----mu6, tidy=FALSE---------------------------------------------------------------------------------
means_pattern6 <- colMeans(sleep[s$groups$`0:0:1:1:0:0:0`,
c(1:2,5:7)])
means_pattern6
## ----cvest, message=FALSE, warning=FALSE-------------------------------------------------------------
library(monomvn)
cv_est <- monomvn(sleep[, 1:7])
#cv_est <- mlest(sleep[, 1:7])
means_full <- cv_est$mu
means_full
cv_full <- cv_est$S
cv_full
## ----littletest, message=FALSE, warning=FALSE, results='hide', eval = TRUE---------------------------
library(BaylorEdPsych)
p.val <- LittleMCAR(sleep[, 1:7])$p.val
## ----littletestres, message=FALSE, warning=FALSE-----------------------------------------------------
p.val
## ----testlittle, message=FALSE, warning=FALSE, cache = FALSE, tidy=FALSE-----------------------------
createMVNdata <- function(out = FALSE){
s <- diag(5)
s[upper.tri(s)] <- 0.9
s[lower.tri(s)] <- 0.9
x <- data.frame(MASS::mvrnorm(100, mu = rep(10, 5),
Sigma = s))
for(i in 1:5){ # 10% missing values with MCAR
x[sample(1:100, 10), i] <- NA
}
if(out){ # optionally outliers
x[1,] <- 1000 * x[1,]
x[2,1] <- 1000 * x[2, ]
}
return(x)
}
## ----testlittleres, message=FALSE, warning=FALSE, cache = FALSE--------------------------------------
set.seed(123)
BaylorEdPsych::LittleMCAR(createMVNdata())$p.val
## ----testlittle2, message=FALSE, warning=FALSE, cache = TRUE, results='hide', tidy=FALSE-------------
sizeOfTest <- mean(replicate(500,
try(LittleMCAR(createMVNdata())$p.val)) < 0.05)
## ----sizeOfTest, testlittle3-------------------------------------------------------------------------
sizeOfTest
## ----logtest2, message=FALSE, warning=FALSE, cache = TRUE, results='hide', tidy=FALSE----------------
sizeOfTest_log <- mean(replicate(1000,
try(LittleMCAR(log(createMVNdata()))$p.val)) < 0.05)
## ----logtest3----------------------------------------------------------------------------------------
sizeOfTest_log
## ----logtest4, message=FALSE, warning=FALSE, cache = TRUE, results='hide', tidy=FALSE----------------
sizeOfTest_out <- mean(replicate(1000,
try(LittleMCAR(log(createMVNdata(out=TRUE)))$p.val)) < 0.05)
## ----logtest5----------------------------------------------------------------------------------------
sizeOfTest_out
## ----missmechfirstoutput, cache = TRUE, echo=FALSE, eval=TRUE----------------------------------------
options(width=50)
library(MissMech)
TestMCARNormality(sleep[, c(1:4, 6:7)])
## ----missmech0, message=FALSE, warning=FALSE, cache = TRUE, results='hide', tidy=FALSE---------------
sizeOfTest_out <- mean(replicate(1000,
try(TestMCARNormality(createMVNdata())$pvalcomb)) < 0.05)
## ----missmech02--------------------------------------------------------------------------------------
sizeOfTest_out
## ----missmech00, message=FALSE, warning=FALSE, cache = TRUE, results='hide', tidy=FALSE--------------
sizeOfTest_out <- mean(replicate(1000,
try(TestMCARNormality(createMVNdata(),
method = "Nonparametric",
imputation.method = "Dist.Free")$pnormality)) < 0.05)
## ----missmech002-------------------------------------------------------------------------------------
sizeOfTest_out
## ----missmech1, message=FALSE, warning=FALSE, cache = TRUE, results='hide', tidy=FALSE---------------
sizeOfTest_out <- mean(replicate(500,
try(TestMCARNormality(log(createMVNdata()))$pvalcomb)) < 0.05)
## ----missmech2---------------------------------------------------------------------------------------
sizeOfTest_out
## ----missmech3, message=FALSE, warning=FALSE, cache = TRUE, results='hide', tidy=FALSE---------------
sizeOfTest_out <- mean(replicate(500,
try(TestMCARNormality(log(createMVNdata()),
method = "Nonparametric",
imputation.method = "Dist.Free")$pnormality)) < 0.05)
## ----missmech4---------------------------------------------------------------------------------------
sizeOfTest_out