-
Notifications
You must be signed in to change notification settings - Fork 0
/
Covariates.Rmd
148 lines (128 loc) · 6.67 KB
/
Covariates.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
---
title: "R Notebook"
output: html_notebook
editor_options:
chunk_output_type: console
---
This file documents where the covariate files were downloaded and how they were
prepared for modeling. Preparation involves masking with shapefile for the sub-Saharan Africa (making values outside of sub-Saharan Africa becomes NA), cropping according to sub-Saharan Africa, resampling such that grid asters are spatially aligned. `tidy_raster` function to mask, (dis)aggregate, and resample relevant raster
### Resample covariates such that they are aligned on the grid level
Some of the covariates (e.g., population per pixel or ppp) were reused from those used in the typhoid mapping.
raster::resample function is used
```{r}
library(raster)
#reference for resampling
refraster = readRDS("data/from_tf_mapping/ppp_20km_af_2020_20220929.rds") #
fls = list.files(path="data/from_tf_mapping/", pattern=".*.rds$", full.names=T)
covars = vector('list', length(fls))
for (i in seq_along(fls)) {
cat("i =", i, ", ")
covars[[i]] <- raster::resample(readRDS(fls[[i]]), refraster, method='bilinear')
}
# save them as .tif files
for (i in seq_along(fls)) {
covname = gsub(".*mapping/|_2022.*", "", fls[[i]])
writeRaster(covars[[i]], paste0("data/covars/", covname, "_", tstamp(), ".tif"))
}
```
### Plasmodium falciparum incidence and prevalence
The data were downloaded from:
https://data.malariaatlas.org/maps?layers=Malaria:202206_Global_Pf_Parasite_Rate&extent=-19196088.839679208,-8881205.592462437,20878927.84589928,11072813.132231852
Global Pf Parasite Rate represents the Proportion of Children 2 to 10 years of age showing, on a given year, detectable Plasmodium falciparum parasite
Global Pf Incidence Rate represents the Number of newly diagnosed Plasmodium falciparum cases per 1,000 population, on a given year
tidy_raster function to mask, (dis)aggregate, and resample relevant raster
```{r}
# Used for masking
afss_adm0 <- readRDS("data/africa_sub_Sahara_adm0_shp.rds")
fls = list.files(path="data/", pattern=".*Pf.*.tif$", full.names=T)
covars2 = vector('list', length(fls))
for (i in seq_along(fls)) { # first file duplicate
# rst = raster(fls[[i]])
# covars2[[i]] = tidy_raster(rst=raster(fls[[i]]), extent=afss_adm0, ref=refraster)
# covname = gsub(".*data/|.tif", "", fls[[i]])
covname = gsub(".*data/|_([0-9]+).tif", "", fls[[i]])
year = gsub('.*_([0-9]+).*','\\1', fls[[i]])
fname = paste0("data/covars/", covname, "_af_", year, "_", tstamp(), ".tif")
print(fname)
writeRaster(covars2[[i]], fname)
}
```
### Improved sanitation estimates
Access to improved sanitation facilities and relying on unimproved sanitation facilities. Although there is a separate variable, access to non-piped improved sanitation facilities, we did not include as it is a subset of the variable, access to improved sanitation facilities, which has easier interpretation.
```{r}
library(raster)
refraster = readRDS("data/from_tf_mapping/ppp_20km_af_2020_20220929.rds")
fls = list.files(path="data/", pattern="S_UNIMP_PERCENT|S_IMP_PERCENT", full.names=T)
covars = vector('list', length(fls))
for (i in seq_along(fls)) {
cat("i =", i, ", ")
rst = raster(fls[i])
covars[[i]] <- tidy_raster(rst, func="mean", ref=refraster)
# covars[[i]] <- raster::resample(readRDS(fls[[i]]), refraster, method='bilinear')
}
for (i in seq_along(fls)) {
# covname = gsub(".*data/|_([0-9]+).tif", "", fls[[i]])
covname = gsub(".*data/|_([0-9]+)_([0-9]+)|_([0-9]+)_Y2020.*.TIF", "", fls[[i]])
year = gsub('^data.*_MEAN_|_Y2020.*', "", fls[[i]])
fname = paste0("data/covars/", covname, "_20km_af_", year, "_", tstamp(), ".tif")
print(fname)
writeRaster(covars[[i]], fname)
}
```
### RasterBrick of covariates by year
Creates RasterBrick of covariates to make it easier for modelling
```{r}
elev = raster("data/covars/elevation_20km_africa.rds_20230314.tif")
motor_traveltime_healthcase = raster("data/covars/motorized_travel_time_to_healthcare_20km_af_20230314.tif")
walk_traveltime_healthcase = raster("data/covars/walking_travel_time_to_healthcare_20km_af_20230314.tif")
traveltime_city = raster("data/covars/travel_time_cities_20km_af_20230314.tif")
distance_to_water = raster("data/covars/distance_to_water_20km_af_20230314.tif")
## load the covars files by year
years = 2000:2020
for (yr in years) {
pat = paste0(".*af_", yr, ".*tif$")
# fls = list.files(path="data/covars/", pattern=".*af_2000.*tif$", full.names=T)
fls = list.files(path="data/covars/", pattern=pat, full.names=T)
# fn = list.files(path="data/covars/", pattern=".*af_2000.*tif$")
# the following covariates are absent for 2018-2020 and we used the 2017 instead
if (yr >= 2018){
fls = c(fls[1:2]
, "data/covars/annual_mean_temperature_20km_af_2017_20230314.tif"
, "data/covars/annual_rainfall_20km_af_2017_20230314.tif"
, "data/covars/improved_water_20km_af_2017_20230314.tif"
, "data/covars/open_defecation_20km_af_2017_20230314.tif"
, "data/covars/piped_sanitation_20km_af_2017_20230314.tif"
, "data/covars/piped_water_20km_af_2017_20230314.tif"
, fls[3]
,"data/covars/prev_HIV_adults_20km_af_2017_20230314.tif"
, "data/covars/prev_stunting_20km_af_2017_20230314.tif"
, "data/covars/surface_water_20km_af_2017_20230314.tif"
, "data/covars/underweight_20km_af_2017_20230314.tif"
, "data/covars/wasting_20km_af_2017_20230314.tif")
}
cat("year =", yr, ", #fls =", length(fls), "\n")
covars = vector('list', (length(fls)+5))
for (i in seq_along(fls)){
covars[[i]] = raster(fls[[i]])
}
covars[[i+1]] = elev
covars[[i+2]] = motor_traveltime_healthcase
covars[[i+3]] = walk_traveltime_healthcase
covars[[i+4]] = traveltime_city
covars[[i+5]] = distance_to_water
# covstack = raster::stack(covars)
#
nms = lapply(fls, function(x) gsub('data/covars/|_20230314.tif$', "", x))
# names(covstack) = c(unlist(nms), "elevation", "motor_traveltime_healthcare", "walk_traveltime_healthcare", "traveltime_city")
# ## add the covars that are not dependent by
# terra::writeRaster(covstack, paste0("data/covars/covars_", yr, ".tif"), overwrite=TRUE)
# r = terra::rast(paste0("data/covars/covars_", yr, ".tif"))
covbrick = raster::brick(covars)
names(covbrick)= c(unlist(nms), "elevation", "motor_traveltime_healthcare", "walk_traveltime_healthcare", "traveltime_city", "distance_to_water")
# native raster format (.grd) retains the names for each RasterLayer while .tif does not
writeRaster(covbrick, filename=paste0("data/covars/covars_brick_", yr, "_", tstamp(), ".grd"), format="raster", overwrite=TRUE, options=c("INTERLEAVE=BAND","COMPRESS=LZW"))
}
# infile = brick(paste0("data/covars/covars_", yr, ".grd"))
# infile
# infile[[2]]
```