Fitting rainfall profiles
Fitting-rainfall-profiles.Rmd
library(umbrella)
Below is an example workflow for using the umbrella package to fit rainfall profiles for use in malariasimulation.
First you’ll need to download the rasters and extract the raw data.
There are some helper function to download global daily rainfall rasters
in the package. You can use the get_urls()
function to
create the URLs to get daily rainfall rasters for a given year. After
that the download_raster()
can be used to download the
rasters to your device. Beware that the set of unzipped rasters for a
whole year will take up quite a bit of memory (each raster is about 2.2
MB).
Once you have you set of rasters you can load them and extract the
data. For this I recommend getting a sf
shape file with the
boundaries of the area you would like to extract the data for. The R
package terra
is a great way to extract the data, something
like:
my_raster_files <- c("rainfall1.tif", "rainfall2.tif")
my_raster <- terra::rast(my_raster_files)
my_sf <- load("my_sf.rds")
extract_data <- terra::extract(my_raster, my_sf, fun = mean)
Once you’ve extracted and tidied the output, you might have somethig like:
head(rainfall)
#> day rainfall
#> 1 1 0.00
#> 2 2 0.00
#> 3 3 0.00
#> 4 4 0.28
#> 5 5 0.17
#> 6 6 0.03
Lets fit a simple seasonal profile to this dataset, and a profile with an imposed minimum rainfall floor.
fit1 <- fit_fourier(rainfall = rainfall$rainfall, t = rainfall$day, floor = 0)
predict_1 <- fourier_predict(coef = fit1$coefficients, t = 1:365, floor = 0)
fit2 <- fit_fourier(rainfall = rainfall$rainfall, t = rainfall$day, floor = 1)
predict_2 <- fourier_predict(coef = fit1$coefficients, t = 1:365, floor = 1)
plot(rainfall, xlab = "Day", ylab = "Rainfall", pch = 19)
lines(predict_1, col = "deeppink", lwd = 2)
lines(predict_2, col = "dodgerblue", lwd = 2, lty = 2)