-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheda.Rmd
535 lines (394 loc) · 13.1 KB
/
eda.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
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
---
title: "Exploratory Data Analysis"
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
echo = TRUE,
error = TRUE,
comment = "")
```
# Rationale
Exploratory data analysis is important for understanding your data, checking for data issues/errors, and checking assumptions for different statistical models.
LOOK AT YOUR DATA—this is one of the most overlooked steps in data analysis!
# Preamble
## Install Libraries
```{r}
#install.packages("remotes")
#remotes::install_github("DevPsyLab/petersenlab")
```
## Load Libraries
```{r, message = FALSE, warning = FALSE}
library("petersenlab")
library("car")
library("vioplot")
library("ellipse")
library("nlme")
library("effects")
library("corrplot")
library("ggplot2")
library("psych")
library("tidyverse")
library("purrr")
library("naniar")
library("mvnormtest")
library("ggExtra")
library("XICOR")
```
# Simulate Data
```{r}
set.seed(52242)
n <- 1000
ID <- rep(1:100, each = 10)
predictor <- rbeta(n, 1.5, 5) * 100
outcome <- predictor + rnorm(n, mean = 0, sd = 20) + 50
predictorOverplot <- sample(1:50, n, replace = TRUE)
outcomeOverplot <- predictorOverplot + sample(1:75, n, replace = TRUE)
categorical1 <- sample(1:5, size = n, replace = TRUE)
categorical2 <- sample(1:5, size = n, replace = TRUE)
mydata <- data.frame(
ID = ID,
predictor = predictor,
outcome = outcome,
predictorOverplot = predictorOverplot,
outcomeOverplot = outcomeOverplot,
categorical1 = categorical1,
categorical2 = categorical2)
mydata[sample(1:n, size = 10), "predictor"] <- NA
mydata[sample(1:n, size = 10), "outcome"] <- NA
mydata[sample(1:n, size = 10), "predictorOverplot"] <- NA
mydata[sample(1:n, size = 10), "outcomeOverplot"] <- NA
mydata[sample(1:n, size = 30), "categorical1"] <- NA
mydata[sample(1:n, size = 70), "categorical2"] <- NA
```
# Descriptive statistics
```{r}
round(data.frame(psych::describe(mydata)), 2)
```
## Sample
- Check the sample size (*N*)
- Is the sample size in the data the expected sample size?
Are there cases (participants) that are missing?
Are there cases that should not be there?
- Here is the sample size:
```{r}
length(unique(mydata$ID))
```
- Check the extent of missingness
- How much data are missing in the model variables—including the predictor, outcome, and covariates?
- Here are the proportion of missing data in each variable:
```{r}
map(mydata, ~mean(is.na(.))) %>% t %>% t
```
## Distribution
- Frequencies
- Examine the frequencies of categorical variables:
```{r}
mydata %>%
select(categorical1, categorical2) %>%
sapply(function(x) table(x, useNA = "always")) %>%
t()
```
## Central Tendency
- Mean
```{r}
round(colMeans(mydata, na.rm = TRUE), 2)
round(apply(mydata, 2, function(x) mean(x, na.rm = TRUE)), 2)
mydata %>%
summarise(across(everything(),
.fns = list(mean = ~ mean(., na.rm = TRUE)))) %>%
round(., 2)
```
- Median
```{r}
round(apply(mydata, 2, function(x) median(x, na.rm = TRUE)), 2)
mydata %>%
summarise(across(everything(),
.fns = list(median = ~ median(., na.rm = TRUE)))) %>%
round(., 2)
```
- Mode
```{r}
round(apply(mydata, 2, function(x) Mode(x, multipleModes = "mean")), 2)
mydata %>%
summarise(across(everything(),
.fns = list(mode = ~ Mode(., multipleModes = "mean")))) %>%
round(., 2)
```
Compute all of these measures of central tendency:
```{r}
mydata %>%
summarise(across(everything(),
.fns = list(mean = ~ mean(., na.rm = TRUE),
median = ~ median(., na.rm = TRUE),
mode = ~ Mode(., multipleModes = "mean")),
.names = "{.col}.{.fn}")) %>%
round(., 2) %>%
pivot_longer(cols = everything(),
names_to = c("variable","index"),
names_sep = "\\.") %>%
pivot_wider(names_from = index,
values_from = value)
```
## Dispersion
- Standard deviation
- Observed minimum and maximum (vis-à-vis possible minimum and maximum)
- Skewness
- Kurtosis
Compute all of these measures of dispersion:
```{r}
mydata %>%
summarise(across(everything(),
.fns = list(SD = ~ sd(., na.rm = TRUE),
min = ~ min(., na.rm = TRUE),
max = ~ max(., na.rm = TRUE),
skewness = ~ skew(., na.rm = TRUE),
kurtosis = ~ kurtosi(., na.rm = TRUE)),
.names = "{.col}.{.fn}")) %>%
round(., 2) %>%
pivot_longer(cols = everything(),
names_to = c("variable","index"),
names_sep = "\\.") %>%
pivot_wider(names_from = index,
values_from = value)
```
Consider transforming data if skewness > |0.8| or if kurtosis > |3.0|.
## Summary Statistics
Add summary statistics to the bottom of correlation matrices in papers:
```{r}
cor.table(mydata, type = "manuscript")
summaryTable <- mydata %>%
summarise(across(everything(),
.fns = list(n = ~ length(na.omit(.)),
missingness = ~ mean(is.na(.)) * 100,
M = ~ mean(., na.rm = TRUE),
SD = ~ sd(., na.rm = TRUE),
min = ~ min(., na.rm = TRUE),
max = ~ max(., na.rm = TRUE),
skewness = ~ skew(., na.rm = TRUE),
kurtosis = ~ kurtosi(., na.rm = TRUE)),
.names = "{.col}.{.fn}")) %>%
pivot_longer(cols = everything(),
names_to = c("variable","index"),
names_sep = "\\.") %>%
pivot_wider(names_from = index,
values_from = value)
summaryTableTransposed <- summaryTable[-1] %>%
t() %>%
as.data.frame() %>%
setNames(summaryTable$variable) %>%
round(., digits = 2)
summaryTableTransposed
```
## Distribution Plots
See [here](figures.html) for resources for creating figures in R.
### Histogram
#### Base R
```{r}
hist(mydata$outcome)
```
#### `ggplot2`
```{r}
ggplot(mydata, aes(x = outcome)) +
geom_histogram(color = 1)
```
### Histogram overlaid with density plot and rug plot
#### Base R
```{r}
hist(mydata$outcome, prob = TRUE)
lines(density(mydata$outcome, na.rm = TRUE))
rug(mydata$outcome)
```
#### `ggplot2`
```{r}
ggplot(mydata, aes(x = outcome)) +
geom_histogram(aes(y = after_stat(density)), color = 1) +
geom_density() +
geom_rug()
```
### Density Plot
#### Base R
```{r}
plot(density(mydata$outcome, na.rm = TRUE))
```
#### `ggplot2`
```{r}
ggplot(mydata, aes(x = outcome)) +
geom_density()
```
### Box and whisker plot (boxplot)
#### Base R
```{r}
boxplot(mydata$outcome, horizontal = TRUE)
```
#### `ggplot2`
```{r}
ggplot(mydata, aes(x = outcome)) +
geom_boxplot()
```
### Violin plot
#### Base R
```{r}
vioplot(na.omit(mydata$outcome), horizontal = TRUE)
```
#### `ggplot2`
```{r}
ggplot(mydata, aes(x = "", y = outcome)) +
geom_violin()
```
# Bivariate Associations
For more advanced scatterplots, see [here](figures.html#marginalDistributions).
## Correlation Coefficients
### Pearson Correlation
```{r}
cor(mydata, use = "pairwise.complete.obs")
cor.test( ~ predictor + outcome, data = mydata)
cor.table(mydata)
```
### Spearman Correlation
```{r}
cor(mydata, use = "pairwise.complete.obs", method = "spearman")
cor.test( ~ predictor + outcome, data = mydata, method = "spearman")
cor.table(mydata, correlation = "spearman")
```
### Xi ($\xi$)
Xi ($\xi$) is an index of the degree of dependence between two variables, which is useful as an index of nonlinear correlation.
Chatterjee, S. (2021). A new coefficient of correlation. *Journal of the American Statistical Association, 116*(536), 2009-2022. https://doi.org/10.1080/01621459.2020.1758115
```{r}
calculateXI(
mydata$predictor,
mydata$outcome)
```
## Scatterplot
### Base R
```{r}
plot(
mydata$predictor,
mydata$outcome)
abline(lm(
outcomeOverplot ~ predictorOverplot,
data = mydata,
na.action = "na.exclude"))
```
### `ggplot2`
```{r}
ggplot(mydata, aes(x = predictor, y = outcome)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE)
```
## Scatterplot with Marginal Density Plot
```{r}
scatterplot <-
ggplot(mydata, aes(x = predictor, y = outcome)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE)
```
```{r}
densityMarginal <- ggMarginal(
scatterplot,
type = "density",
xparams = list(fill = "gray"),
yparams = list(fill = "gray"))
```
```{r}
print(densityMarginal, newpage = TRUE)
```
## High Density Scatterplot
```{r}
ggplot(mydata, aes(x = predictorOverplot, y = outcomeOverplot)) +
geom_point(position = "jitter", alpha = 0.3) +
geom_density2d()
smoothScatter(mydata$predictorOverplot, mydata$outcomeOverplot)
```
## Data Ellipse
```{r}
mydata_nomissing <- na.omit(mydata[,c("predictor","outcome")])
dataEllipse(mydata_nomissing$predictor, mydata_nomissing$outcome, levels = c(0.5, .95))
```
## Visually Weighted Regression
```{r, message = FALSE, results = "hide"}
vwReg(outcome ~ predictor, data = mydata)
```
# Basic inferential statistics
## Tests of Normality
### Shapiro-Wilk test of normality
The Shapiro-Wilk test of normality does not accept more than 5000 cases because it will reject the hypothesis that data come from a normal distribution with even slight deviations from normality.
```{r}
shapiro.test(na.omit(mydata$outcome)) #subset to keep only the first 5000 rows: mydata$outcome[1:5000]
```
### Test of multivariate normality
```{r}
mydata %>%
na.omit %>%
t %>%
mshapiro.test
```
## Statistical decision tree
https://upload.wikimedia.org/wikipedia/commons/7/74/InferentialStatisticalDecisionMakingTrees.pdf (archived at https://perma.cc/L2QR-ALFA)
## Tests of systematic missingness (i.e., whether missingness on a variable depends on other variables)
- Generally test:
- Whether data are consistent with a missing completely at random (MCAR) pattern—Little's MCAR Test
- Whether outcome variable(s) differ as a function of any model variables (predictors and covariates) and as a function of any key demographic characteristics (e.g., sex, ethnicity, socioeconomic status)
- Whether focal predictor variable(s) differ as a function of any model variables (including outcome variable) and as a function of any key demographic characteristics
- For instance:
- Whether males are more likely than girls to be missing scores on the dependent variable
- Whether longitudinal attrition is greater in lower socioeconomic status families
- If missingness differs systematically as a function of other variables, you can include that variable as a control variable in models, and/or can include that variable in multiple imputation to inform imputed scores for missing values
### Little's MCAR Test
```{r}
mcar_test(mydata)
```
## Multivariate Associations
### Correlation Matrix
#### Pearson Correlations
```{r}
cor.table(mydata[,c("predictor","outcome","predictorOverplot","outcomeOverplot")])
cor.table(mydata[,c("predictor","outcome","predictorOverplot","outcomeOverplot")], type = "manuscript")
cor.table(mydata[,c("predictor","outcome","predictorOverplot","outcomeOverplot")], type = "manuscriptBig")
```
#### Spearman Correlations
```{r}
cor.table(mydata[,c("predictor","outcome","predictorOverplot","outcomeOverplot")], correlation = "spearman")
cor.table(mydata[,c("predictor","outcome","predictorOverplot","outcomeOverplot")], type = "manuscript", correlation = "spearman")
cor.table(mydata[,c("predictor","outcome","predictorOverplot","outcomeOverplot")], type = "manuscriptBig", correlation = "spearman")
```
#### Partial Correlations
Examine the associations among variables controlling for a covariate (`outcomeOverplot`).
```{r}
partialcor.table(mydata[,c("predictor","outcome","predictorOverplot")], z = mydata[,c("outcomeOverplot")])
partialcor.table(mydata[,c("predictor","outcome","predictorOverplot")], z = mydata[,c("outcomeOverplot")], type = "manuscript")
partialcor.table(mydata[,c("predictor","outcome","predictorOverplot")], z = mydata[,c("outcomeOverplot")], type = "manuscriptBig")
```
### Correlogram
```{r}
corrplot(cor(mydata[,c("predictor","outcome","predictorOverplot","outcomeOverplot")], use = "pairwise.complete.obs"))
```
### Scatterplot matrix
```{r}
scatterplotMatrix(~ predictor + outcome + predictorOverplot + outcomeOverplot, data = mydata, use = "pairwise.complete.obs")
```
### Pairs panels
```{r}
pairs.panels(mydata[,c("predictor","outcome","predictorOverplot","outcomeOverplot")])
```
## Effect Plots
### Multiple Regression Model
```{r}
multipleRegressionModel <- lm(outcome ~ predictor + predictorOverplot,
data = mydata,
na.action = "na.exclude")
allEffects(multipleRegressionModel)
plot(allEffects(multipleRegressionModel))
```
### Multilevel Regression Model
```{r}
multilevelRegressionModel <- lme(outcome ~ predictor + predictorOverplot, random = ~ 1|ID,
method = "ML",
data = mydata,
na.action = "na.exclude")
allEffects(multilevelRegressionModel)
plot(allEffects(multilevelRegressionModel))
```
# Session Info
```{r, class.source = "fold-hide"}
sessionInfo()
```