-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstability_EDAplots.Rmd
299 lines (248 loc) · 11.3 KB
/
Instability_EDAplots.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
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
---
title: "In which there are too many EDA plots"
author: "Matt Tyers"
date: "2024-08-19"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, dpi=300, fig.width = 13, fig.height = 18, results="hide", warning=FALSE, message=FALSE)
```
A sequence of automated plots follow, for all combinations of:
- Variables, both recorded and derived (currently 35 in total!) Categorical variables were also defined for Design CR (less than 1.1, 1.1 to 1.3, greater than 1.3), and for gradient and width differences between reaches 2 and 3 and reaches 4 and 3 (relative differences exceeding 5%).
- V score:
- Interior Channel Width
- Interior Gradient
- Height
- Bank Length
- V score type:
- Numeric & directional, calculated as
$$V_{k[i]}^{(raw)} = log \left(\frac{X_{measured[i]}}{X_{design[i]}}\right)$$
$$V_{k[i]} = \frac{V_{k[i]}^{(raw)}}{SD \left(V_{k[.]}^{(raw)}\right)}$$
- Numeric & absolute, calculated as $V_{k[i]}^{(abs)} = |V_{k[i]}|$
- Categorical & directional, defined as
$$V_{k[i]}^{(cat)} = \left\{\begin{array}{lr} \texttt{"-Negative"}, & \frac{X_{design[i]}}{X_{measured[i]}} > 1.5\\ \texttt{"+Positive"}, & \frac{X_{measured[i]}}{X_{design[i]}} > 1.5\\ \texttt{" Stable"}, & \texttt{else} \end{array}\right.$$
- Categorical & absolute, defined similarly.
Test p-values are reported for all plots, corresponding to ANOVA, simple linear regression, or Chi-squared tests. Plots also include categories for missing data, but missing data were excluded from statistical tests. Test p-values are included for exploratory purposes ONLY, and should not be interpreted as biometrically sound!!
```{r, warning=FALSE, message=FALSE, results="hide", fig.keep="none"}
source("R/2_InstabilityScore_PCA.R")
```
```{r, warning=FALSE, message=FALSE, results="hide", fig.keep="none"}
###### -------- data subsetting, formatting, and creation of new variables
# converting from numeric to factor
Designs$bank_design_type <- as.character(Designs$bank_design_type)
# creating a new dataset where importance is "high"
Designs_high <- Designs[, importancevec %in% "high"]
# -- defining a few new variables -- #
# categorical version of design cr
Designs_high$reach3_design_cr_cat <- cut(Designs_high$reach3_design_cr, c(0,1.1,1.3,3))
dim(Designs_high)
par(mfrow=c(1,1))
# plot(Designs_high)
summary(Designs_high)
# how many missing values?
image(is.na(Designs_high))
colMeans(is.na(Designs_high))
colSums(is.na(Designs_high))
sum(colSums(is.na(Designs_high)) == 0)
sum(colSums(is.na(Designs_high)) <= 1)
# creating a new dataset where importance is "high" or "medium"
Designs_medhigh <- Designs[, importancevec %in% c("medium","high")]
# -- defining a few new variables -- #
# categorical version of design cr
Designs_medhigh$reach3_design_cr_cat <- Designs_high$reach3_design_cr_cat
# inconsistency in gradient & width
aa <- 0.2
# Designs_medhigh$grad_diff_2vs3 <- log((aa+Designs_medhigh$reach_2_gradient)/
# (aa+Designs_medhigh$reach_3_gradient_51))
Designs_medhigh$grad_diff_2vs3 <- ifelse(Designs_medhigh$reach_2_gradient/
Designs_medhigh$reach_3_gradient_51 > 1.05, ">greater",
ifelse(Designs_medhigh$reach_3_gradient_51/
Designs_medhigh$reach_2_gradient > 1.05, "<less",
"=same"))
# plot(Designs_medhigh$grad_diff_2vs3)
# Designs_medhigh$grad_diff_4vs3 <- -log((aa+Designs_medhigh$reach_3_gradient_51)/
# (aa+Designs_medhigh$reach_4gradient))
Designs_medhigh$grad_diff_4vs3 <- ifelse(Designs_medhigh$reach_4gradient/
Designs_medhigh$reach_3_gradient_51 > 1.05, ">greater",
ifelse(Designs_medhigh$reach_3_gradient_51/
Designs_medhigh$reach_4gradient > 1.05, "<less",
"=same"))
# plot(Designs_medhigh$grad_diff_4vs3)
# Designs_medhigh$width_diff_2vs3 <- log(Designs_medhigh$x2_width_straight/
# Designs_medhigh$x3_width_straight)
Designs_medhigh$width_diff_2vs3 <- ifelse(Designs_medhigh$x2_width_straight/
Designs_medhigh$x3_width_straight > 1.05, ">greater",
ifelse(Designs_medhigh$x3_width_straight/
Designs_medhigh$x2_width_straight > 1.05,
"<less","=same"))
# Designs_medhigh$width_diff_4vs3 <- -log(Designs_medhigh$x3_width_straight/
# Designs_medhigh$x4_width_straight)
Designs_medhigh$width_diff_4vs3 <- ifelse(Designs_medhigh$x4_width_straight/
Designs_medhigh$x3_width_straight > 1.05, ">greater",
ifelse(Designs_medhigh$x3_width_straight/
Designs_medhigh$x4_width_straight > 1.05,
"<less","=same"))
dim(Designs_medhigh)
par(mfrow=c(1,1))
# plot(Designs_medhigh)
summary(Designs_medhigh)
# how many missing values?
image(is.na(Designs_medhigh))
colMeans(is.na(Designs_medhigh))
colSums(is.na(Designs_medhigh))
sum(colSums(is.na(Designs_medhigh)) == 0)
sum(colSums(is.na(Designs_medhigh)) <= 1)
## Lots of missing values. Will explore effects each variable separately, then
## only do MLR/tree on dataset with sufficient data.
apply(Designs_medhigh, 2, \(x) length(unique(x)))
# see if any variables are fully repeated (spoiler alert: one is)
repeated <- NULL
for(i in 1:(ncol(Designs_medhigh)-1)) {
for(j in (i+1):ncol(Designs_medhigh)) {
if(i != j) {
if(all(Designs_medhigh[,i]==Designs_medhigh[,j]) & all(is.na(Designs_medhigh[,i])==is.na(Designs_medhigh[,j]))) {
repeated <- c(repeated, j)
}
}
}
}
repeated
Designs_medhigh <- Designs_medhigh[,-repeated]
#### --------------- EDA for all instability scores vs all variables (NUMERIC)
# ## creating a plotting function to plot ALL instability scores vs ALL variables
# magicplot <- function(x, y, main,...) {
# # assuming y is numeric
#
# # pval <- summary(lm(y~x))$coefficients[2,4] WRONG!!
# pval <- anova(lm(y~x))$`Pr(>F)`[1]
#
# if(class(x) %in% c("factor","character")) {
# x[is.na(x)] <- "_NA_"
# thetab <- table(x)
# theplotnames <- boxplot(y~x, plot=FALSE)$names
# namestoplot <- rep(NA, length(theplotnames))
# for(inames in seq_along(namestoplot)) {
# namestoplot[inames] <- paste0(theplotnames[inames], " (n = ",
# sum(x==theplotnames[inames]), ")")
# }
# boxplot(y ~ x,
# main=c(main, paste("Anova pval =", round(pval, 4))),
# xlab="",
# names = namestoplot,
# # names = paste0(names(thetab), " (n = ", thetab, ")"),
# las=2,
# ...=...)
# } else {
# NAval <- min(x, na.rm=TRUE) - 0.2*diff(range(x, na.rm=TRUE))
# x[is.na(x)] <- NAval
# plot(y ~ x,
# main=c(main, paste("Reg pval =", round(pval, 4))),
# xlab="", col=1+(x==NAval),
# ...=...)
# axis(side=1, at=NAval, labels=paste0("NA (n=", sum(x==NAval),")"), las=2)
# }
# }
```
```{r, warning=FALSE, message=FALSE, results="hide", fig.keep="none"}
Vabs_cat <- Vdir_cat <- NA*Vabs
for(i in 1:4) {
# Vabs_cat[,i] <- ifelse(Vabs[,i] < log(1.5), "Stable", "Unstable")
# vscores were standardized by sd, therefore not fully consistent abs vs dir
Vabs_cat[,i] <- ifelse(abs(Vdir[,i]) < log(1.5), "Stable", "Unstable")
Vdir_cat[,i] <- ifelse(Vdir[,i] < -log(1.5), "-Negative",
ifelse(Vdir[,i] > log(1.5), "+Positive", " Stable"))
}
# magicplot_cat <- function(x, y, main, ylab, ...) {
# # assuming y is categorical
#
# if(class(x) %in% c("factor","character")) {
# # mosaicplot with addl NA and sample sizes for columns
# thetable1 <- table(x,y)
# pval <- chisq.test(thetable1)$p.value
# x <- as.character(x)
# x[is.na(x)] <- "_NA_"
# thetable2 <- table(x,y)
# dimnames(thetable2)[[1]] <- paste0(dimnames(thetable2)[[1]],
# " (n = ", table(x),")")
# mosaicplot(thetable2, xlab=main, ylab="",# labs are new
# main=c(ylab, paste("chi^2 pval =", round(pval, 4))), #was main
# ...=...)
#
# } else {
# #
# # NAval <- min(x, na.rm=TRUE) - 0.2*diff(range(x, na.rm=TRUE))
# # x[is.na(x)] <- NAval
# # plot(y ~ x,
# # main=c(main, paste("Reg pval =", round(pval, 4))),
# # xlab="", col=1+(x==NAval),
# # ...=...)
# # axis(side=1, at=NAval, labels=paste0("NA (n=", sum(x==NAval),")"), las=2)
# # pval <- summary(lm(x~y))$coefficients[2,4]
#
# # pval <- summary(lm(y~x))$coefficients[2,4] WRONG!!
# pval <- anova(lm(x~y))$`Pr(>F)`[1]
#
# y <- as.character(y)
# # y[is.na(y)] <- "_NA_"
# thetab <- table(y)
# theplotnames <- boxplot(x~y, plot=FALSE)$names
# namestoplot <- rep(NA, length(theplotnames))
# for(inames in seq_along(namestoplot)) {
# namestoplot[inames] <- paste0(theplotnames[inames], " (n = ",
# sum(y==theplotnames[inames], na.rm=TRUE), ")")
# }
# boxplot(x ~ y,
# main=c(ylab, paste("Anova pval =", round(pval, 4))), # was main
# xlab="",
# ylab=main, # new
# names = namestoplot,
# # names = paste0(names(thetab), " (n = ", thetab, ")"),
# las=2,
# ...=...)
# }
# }
```
```{r}
names(Vdir) <- paste("V", names(Vdir))
names(Vabs) <- paste("V", names(Vabs))
names(Vdir_cat) <- paste("V", names(Vdir_cat))
names(Vabs_cat) <- paste("V", names(Vabs_cat))
```
# Each group is a variable, each row is a V score, each column is a V score type
```{r, results='asis', fig.height=14}
j <- 1 ##### actually do a section for each explanatory variable i
##### then can loop over j again with par(mfrow=c(2,2))
par(mfrow=c(4,4))
par(mar=c(5,6,4,2))
for(i in 1:ncol(Designs_medhigh)) {
cat("## ", names(Designs_medhigh)[i], '\n')
for(j in 1:4) {
magicplot(x=Designs_medhigh[,i],
y=Vdir[,j],
# main=names(Designs_medhigh)[i],
main="Numeric - directional",
ylab=names(Vdir_cat)[j])
magicplot(x=Designs_medhigh[,i],
y=Vabs[,j],
# main=names(Designs_medhigh)[i],
main="Numeric - absolute",
ylab=names(Vdir_cat)[j])
magicplot_cat(x=Designs_medhigh[,i],
y=Vdir_cat[,j],
# main=names(Designs_medhigh)[i],
main="Categorical - directional",
ylab=names(Vdir_cat)[j],
col=c(4,3,2),
horizontal=TRUE)
magicplot_cat(x=Designs_medhigh[,i],
y=Vabs_cat[,j],
# main=names(Designs_medhigh)[i],
main="Categorical - absolute",
ylab=names(Vdir_cat)[j],
col=c(3,2),
horizontal=TRUE)
# cat("\\pagebreak", '\n')
cat('\n','\n')
}
}
```