knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The goal of yahtsee is to provide tools around fitting hierarchical time series models for data commonly found in malaria.
You can install the released version of yahtsee from GitHub with:
# install.packages("remotes") remotes::install_github("njtierney/yahtsee")
This package requires INLA
, to install it you can run:
install_yahtsee_deps()
Or alternatively:
options( repos = c( INLA = "https://inla.r-inla-download.org/R/testing", CRAN = "https://cloud.r-project.org/" ) ) install.packages("INLA")
m <- fit_hts( # inputs are the levels of hierarchy, in order of decreasing size formula = pr ~ avg_lower_age + hts(who_region, who_subregion, country), .data = malaria_africa_ts, family = "gaussian", special_index = month_num )
This is a model with an AR1 process for each of these subregions using the hts()
component in the formula. The inputs are the levels of hierarchy, in order of decreasing size.
We then provide the data, likelihood family (in this case "gaussian", but all INLA likelihoods are available).
We specify the time component at the moment using the special_index
argument (this will be removed later once we resolve a couple of bugs to do with the data).
The equivalent model fitted with inlabru
could look like the following. (Note that for this code to work you would need to perform various data transformations, so this code cannot be run as-is).
inlabru::bru( formula = pr ~ avg_lower_age + Intercept + who_region(month_num, model = "ar1", group = .who_region_id, constr = FALSE) + who_subregion(month_num, model = "ar1", group = .who_subregion_id, constr = FALSE) + country(month_num, model = "ar1", group = .country_id, constr = FALSE), family = "gaussian", data = malaria_africa_ts, options = list( control.compute = list(config = TRUE), control.predictor = list(compute = TRUE, link = 1) ) )
Which would be equivalent to the following INLA code
INLA::inla( formula = pr ~ avg_lower_age + Intercept + f(month_num_1, model = "ar1", group = .who_region_id, constr = FALSE) + f(month_num_2, model = "ar1", group = .who_subregion_id, constr = FALSE) + f(month_num_3, model = "ar1", group = .country_id, constr = FALSE), family = "gaussian", data = malaria_africa_ts, options = list( control.compute = list(config = TRUE), control.predictor = list(compute = TRUE, link = 1) ) )
Using yahtsee
's fit_hts
function, we now do not need to think about the following:
month_num
)group
argument requires a special index variable of a group to be made (.who_subregion_id
)inlabru
in options to help get the appropriate data back.This package was built to help make is simpler to create specific type of hierarchical time series models, for modelling packages like INLA
, inlabru
, and mgcv
.
Specifically we had the following goals:
gam
and other modelling software.tsibble
data structure to store the time
component of the time series so the model is aware of the time component and it doesn't need to be specifiedlibrary(yahtsee) # for nice table printing library(tibble)
There are two example datasets.
who_regions
, containing region information from WHO databasewho_regions
malaria_africa_ts
- containing malaria prevalence data, extracted using the malariaAtlas
R package - https://cran.r-project.org/web/packages/malariaAtlas/vignettes/overview.htmlmalaria_africa_ts
Please note that the yahtsee project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.