-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-explore.Rmd
174 lines (119 loc) · 3.2 KB
/
1-explore.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
---
title: "1-explore"
author: "bernard-liew"
date: "2021-03-29"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
---
# Package
```{r, include = FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
```{r}
# Helper
library (tidyverse)
library (skimr)
library (DataExplorer)
library (janitor)
library (rsample)
# Import
library (rio)
# Missing
library (VIM)
library (naniar)
library (mice)
library (NADIA)
```
Model for Improvement of neckpain: filter by VAS_neckpain_0 > 1.5 AND 0 missing values for all the obseravtions who entered into the full model.
Model for Improvement of armpain: filter by VAS_armpain_0 > 1.5 AND 0 missing values for all the obseravtions who entered into the full model.
Model for Improvement of disability: filter by disability_0 >= 7 AND 0 missing values for all the obseravtions who entered into the full model.
# Import
```{r}
df <- import ("data/neck_pain_database_3001.xlsx", sheet = "DATA")
keys <- import ("data/neck_pain_database_3001.xlsx", sheet = "LEGEND")
```
# Tidy
## Convert factors
```{r}
var_as_factors <- keys[["Type of variable"]] == "Factor"
df <- df %>%
mutate_if (var_as_factors, ~.x %>%
as.character() %>% as.factor()) %>%
rename (imp_np = improvem_NECKpain,
imp_ap = improvem_ARMpain,
imp_dis = improvem_DISAB)
```
## Explore data
```{r}
skim (df)
table (df$improvem_NECKpain, df$improvem_ARMpain, df$improvem_DISAB)
```
```{r}
visdat::vis_dat(df, sort_type = TRUE)
visdat::vis_miss(df, cluster = TRUE)
```
## Missingness
```{r}
aggr(df)
barMiss(df %>%
select (imp_np, imp_dis))
spineMiss(df %>%
select (imp_np, imp_dis))
spineMiss(df %>%
select (imp_np, imp_ap))
```
```{r}
np <- df %>%
select (-c(imp_ap, imp_dis)) %>%
purrr::discard(~sum(is.na(.x))/length(.x)* 100 >= 50) %>%
rename (outcome = imp_np)
ap <- df %>%
select (-c(imp_np, imp_dis)) %>%
purrr::discard(~sum(is.na(.x))/length(.x)* 100 >= 50)%>%
rename (outcome = imp_ap)
dis <- df %>%
select (-c(imp_np, imp_ap)) %>%
purrr::discard(~sum(is.na(.x))/length(.x)* 100 >= 60)%>%
rename (outcome = imp_dis)
df_list <- list (np = vector ("list"),
ap = vector ("list"),
dis = vector ("list"))
df_list$np$orig <- np
df_list$ap$orig <- ap
df_list$dis$orig <- dis
```
```{r}
for (n in seq_along (df_list)) {
set.seed(123)
df_split <- initial_split(df_list[[n]]$orig, prop = 0.80, strata = outcome)
train <- training(df_split)
test <- testing(df_split)
df_list[[n]]$train <- train
df_list[[n]]$test <- test
}
# Save the split information for an 80/20 split of the data
```
```{r}
for (n in seq_along (df_list)) {
train_impute <- mice (df_list[[n]]$train)
df_list[[n]]$train_imp <- complete (train_impute)
test_impute <- mice.reuse(train_impute,
df_list[[n]]$test)
df_list[[n]]$test_imp <- test_impute[[1]]
}
# train_impute <- mice (train)
#
# train2 <- complete (train_impute)
# test_impute <- mice.reuse(train_impute,
# test)
# test2 <- test_impute[[1]]
#
dat <- list (data = df,
df_list = df_list)
```
## Save
```{r}
saveRDS(dat,
"output/df.RDS")
```