-
Notifications
You must be signed in to change notification settings - Fork 3
/
00-data-preprocessing.qmd
221 lines (186 loc) · 4 KB
/
00-data-preprocessing.qmd
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
---
title: "Data preprocessing"
author: "Diego Villa"
format: html
editor: visual
---
```{r message=FALSE, warning=FALSE}
library(readr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)
library(skimr)
library(tibble)
```
# Data loading
Create csv. file path.
```{r infile-path, echo=TRUE, message=FALSE, warning=FALSE}
# use here package
raw_data_path <- file.path("../data/raw/")
infile_name <- "malaria-monthly-cases-district-loreto"
infile <- paste(infile_name, ".csv", sep = "")
infile_path <- file.path(raw_data_path, infile)
```
```{r outfile_path}
processed_data_path <- file.path("../data/processed/")
outfile_name <- "malaria-monthly-cases-district-loreto"
outfile <- paste("pro_", outfile_name, ".csv", sep = "")
outfile_path <- file.path(processed_data_path, outfile)
```
Define data types for columns.
```{r column-names, message=FALSE, warning=FALSE}
# Column names
col_names <- c(
"district",
"year",
"month",
"falciparum",
"vivax",
"aet",
"prcp",
"q",
"soilm",
"tmax",
"tmin",
"water_deficit",
"loss",
"loss_km2",
"cum_loss_km2",
"diag",
"enviro",
"nets",
"workers",
"pamafro",
"pop2015",
"province",
"region",
"id_district"
)
```
```{r column-types}
# Column types
col_types <-
readr::cols(
district = col_character(),
year = col_integer(),
month = col_integer(),
falciparum = col_integer(),
vivax = col_integer(),
aet = col_double(),
prcp = col_double(),
q = col_double(),
soilm = col_double(),
tmax = col_double(),
tmin = col_double(),
water_deficit = col_double(),
loss = col_double(),
loss_km2 = col_double(),
cum_loss_km2 = col_double(),
diag = col_integer(),
enviro = col_integer(),
nets = col_integer(),
workers = col_integer(),
pamafro = col_integer(),
pop2015 = col_integer(),
province = col_character(),
region = col_character(),
id_district = col_character()
)
```
Reading csv. file.
```{r read-file, message=FALSE, warning=FALSE}
dataset <-
readr::read_csv(
file = infile_path,
col_names = col_names,
col_types = col_types,
skip = 1,
locale = locale(encoding = "UTF-8")
)
```
## Data validation
Inspect data.
```{r inspect, include=FALSE}
# View(dataset)
```
```{r}
head(dataset)
```
Check data structure.
```{r data-structure, include=FALSE, message=FALSE, warning=FALSE}
str(dataset)
```
Summarize
```{r summary}
skimr::skim(dataset)
```
```{r message=FALSE, warning=FALSE}
# TODO Potential function
cat_cols <- c()
for (col in names(dataset)) {
if (is.character(dataset[[col]])) {
cat_cols <- c(cat_cols, col)
}
}
for (col in cat_cols) {
cat(
"-",
col,
length(unique(dataset[[col]])),
"\n",
unique(dataset[[col]]),
"\n"
)
}
```
# Data preparation
```{r replace-province}
processed_dataset <- tibble::tibble(dataset)
processed_dataset$province <- replace(
dataset$province,
dataset$province == "DATEN DEL MARAÑON",
"DATEM DEL MARAÑON"
)
```
```{r check-replace}
unique(processed_dataset$province)
```
Create a reporting date features in datetime format using `year` and `month` columns.
```{r make-datetime, message=FALSE, warning=FALSE}
processed_dataset$dttm <- lubridate::make_datetime(
year = dataset$year,
month = dataset$month,
day = 1L
)
```
```{r encode-dttm}
processed_dataset$time <- as.numeric(processed_dataset$dttm)
```
Column subset for analysis
```{r}
col_analysis <- c(
"district",
"year",
"month",
"falciparum",
"vivax",
"aet",
"prcp",
"q",
"soilm",
"tmax",
"tmin",
"pop2015",
"province",
"region",
"dttm",
"time"
)
processed_dataset <- processed_dataset[, col_analysis]
```
Inspect processed data set
```{r}
head(processed_dataset)
```
# Export processed data
```{r}
readr::write_csv(processed_dataset, outfile_path)
```