Title: | Yet Another Hierachical Time Series Extension and Expansion |
---|---|
Description: | An opinionated approach to building hierarchical time series models in R using INLA and inlabru. |
Authors: | Nicholas Tierney [aut, cre] , Aarathy Babu [aut], Mitchell O'Hara-Wild [ctb] |
Maintainer: | Nicholas Tierney <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.0.9000 |
Built: | 2024-11-20 03:35:29 UTC |
Source: | https://github.com/njtierney/yahtsee |
Some modelling software needs group data as an index. This means instead of
c("AU", "AU", "UK", "UK", "NZ")
you need c(1, 1, 2, 2, 3)
,
corresponding to each of the groups. This function makes it simpler to add
that information to a dataframe.
add_group_id(.data, group)
add_group_id(.data, group)
.data |
data.frame |
group |
group to add the ID for |
data.frame
with additional column named ".group_id", where "group" is
named after the variable you would like to add the group ID for.
malaria_africa_ts %>% add_group_id(country)
malaria_africa_ts %>% add_group_id(country)
Create object of class "hts_inla"
as_hts_inla(x, formula)
as_hts_inla(x, formula)
x |
model from inlabru |
formula |
formula passed to |
hts_inla model
internal
Nicholas Tierney
The augment()
function returns the data used in the model
## S3 method for class 'hts_inla' augment(x, newdata = NULL, exponentiate = FALSE, ...)
## S3 method for class 'hts_inla' augment(x, newdata = NULL, exponentiate = FALSE, ...)
x |
object of class "hts_inla" |
newdata |
new data to pass to prediction? Default is NULL |
exponentiate |
default FALSE. Whether to exponentiate predictions |
... |
extra arguments to pass to |
dataframe with column names of original data, as well as extra columns, ".fitted" and ".resid".
hts_example_model augment(hts_example_model)
hts_example_model augment(hts_example_model)
Helper function to show the available likelihoods that can be used
available_likelihoods()
available_likelihoods()
list of the available likelihoods
available_likelihoods() # code taken from INLA::inla.models()$likelihood
available_likelihoods() # code taken from INLA::inla.models()$likelihood
create formula to pass to inlabru::bru
build_bru_formula(formula, .data, special_index)
build_bru_formula(formula, .data, special_index)
formula |
formula with an hts() term |
.data |
tsibble data |
special_index |
special index term |
formula
Nicholas Tierney
bru_form <- build_bru_formula( formula = pr ~ avg_lower_age + hts(who_subregion, country), .data = malaria_africa_ts, special_index = month_num ) bru_form
bru_form <- build_bru_formula( formula = pr ~ avg_lower_age + hts(who_subregion, country), .data = malaria_africa_ts, special_index = month_num ) bru_form
Create data to be used when fitting a model with fit_hts
create_hts_data(.data, formula)
create_hts_data(.data, formula)
.data |
tsibble |
formula |
formula with hts() term |
data.frame
data_w_groups <- create_hts_data( .data = malaria_africa_ts, formula = pr ~ avg_lower_age + hts(who_subregion, country) ) data_w_groups
data_w_groups <- create_hts_data( .data = malaria_africa_ts, formula = pr ~ avg_lower_age + hts(who_subregion, country) ) data_w_groups
Fit a hierarchical time series model
fit_hts( formula, .data, special_index, family, verbose = TRUE, keep_data = TRUE, ... )
fit_hts( formula, .data, special_index, family, verbose = TRUE, keep_data = TRUE, ... )
formula |
formula must contain fixed effect terms and |
.data |
a |
special_index |
The variable that represents time in your data |
family |
character. |
verbose |
default TRUE, whether to display message |
keep_data |
attach the data used to fit the model to the final model object? |
... |
list of options to pass to |
inlabru model
To weight your observations, say when using empirical logit, you must
provide a vector of variances, for each observation, which represents
the uncertainty of measurement. You can do this, provided the
family = "gaussian"
, add the argument, scale = vec
, to fit_hts
,
where vec
is a vector of variances for each observation.
## Not run: m <- fit_hts( #inputs are the levels of hierarchy, in order of decreasing size formula = pr ~ avg_lower_age + hts(who_subregion, country), .data = malaria_africa_ts, family = "gaussian", special_index = month_num ) ## End(Not run)
## Not run: m <- fit_hts( #inputs are the levels of hierarchy, in order of decreasing size formula = pr ~ avg_lower_age + hts(who_subregion, country), .data = malaria_africa_ts, family = "gaussian", special_index = month_num ) ## End(Not run)
hts_inla
model objectThis returns the fitted
## S3 method for class 'hts_inla' fitted(object, exponentiate = FALSE, ...)
## S3 method for class 'hts_inla' fitted(object, exponentiate = FALSE, ...)
object |
hts_inla object |
exponentiate |
default FALSE |
... |
extra arguments. Currently not used. |
head(fitted(hts_example_model))
head(fitted(hts_example_model))
Check if formula variables are present in data
formula_vars_in_data(data, formula)
formula_vars_in_data(data, formula)
data |
a |
formula |
formula |
formula_vars_in_data( data = malaria_africa_ts, formula = pr ~ dates )
formula_vars_in_data( data = malaria_africa_ts, formula = pr ~ dates )
Glance: A single row summary of a model fit
## S3 method for class 'hts_inla' glance(x, ...)
## S3 method for class 'hts_inla' glance(x, ...)
x |
object of class "hts_inla" |
... |
extra arguments to pass to |
dataframe with column names: deviance information criterion, wakaike information criteriot, log marginal-likelihood (integration), log marginal-likelihood (Gaussian).
hts_example_model glance(hts_example_model)
hts_example_model glance(hts_example_model)
Build hts call
hts_builder(.data, new_index, ...)
hts_builder(.data, new_index, ...)
.data |
tsibble |
new_index |
(temporary) new index we are using whilr working out model kinks |
... |
the levels of hierarchy, in order of decreasing size |
constructed expression of ar1 terms to pass to inla
# # hts_builder(malaria_africa_ts, who_region, who_subregion, country)
# # hts_builder(malaria_africa_ts, who_region, who_subregion, country)
Example model for use in testing and examples
data(hts_example_model)
data(hts_example_model)
An object of class hts_inla
(inherits from bru
, iinla
, inla
) of length 14.
Currently this just installs the latest version of INLA
install_yahtsee_deps()
install_yahtsee_deps()
nothing
## Not run: install_yahtsee_deps() ## End(Not run)
## Not run: install_yahtsee_deps() ## End(Not run)
Data containing information on malaria prevalence in Africa. Data was
extracted using the malariaAtlas
package. If using this data, please
cite:
data(malaria_africa_ts)
data(malaria_africa_ts)
An object of class tbl_ts
(inherits from tbl_df
, tbl
, data.frame
) with 1046 rows and 15 columns.
Pfeffer, D.A., Lucas, T.C., May, D., Harris, J., Rozier, J., Twohig, K.A., Dalrymple, U., Guerra, C.A., Moyes, C.L., Thorn, M., Nguyen, M., et al. 2018. malariaAtlas: an R interface to global malariometric data hosted by the Malaria Atlas Project. Malaria Journal, 17(1), p.352.
Format: a 'tsibble' data frame with 1,117 observations on the following 15 variables.
who_region
A character of WHO region, the largest region size.
who_subregion
A character of WHO subregion, the second largest region size.
country
A character of countries, the smallest region size.
country_id
A three letter character code adhering to the standard, ISO 3166-1 alpha-3. For more detail see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3.
continent_id
A character of continent name
year
integer of year data recorded
month
integer of month data recorded
date
date class of year+month recorded. Defaults to day = 1 as day was not provided.
lower_age
lower age range
upper_age
upper age range
examined
integer, number of people examined
positive
numeric, number of positive cases
pr
positive rate - given from positive/examined
species
Mosquito species
method
method of measurement
Predict from hts_inla model
## S3 method for class 'hts_inla' predict( object, newdata = NULL, type = c("link", "response", "terms"), se.fit = FALSE, dispersion = NULL, terms = NULL, na.action = na.pass, ... )
## S3 method for class 'hts_inla' predict( object, newdata = NULL, type = c("link", "response", "terms"), se.fit = FALSE, dispersion = NULL, terms = NULL, na.action = na.pass, ... )
object |
a fitted object of class inheriting from |
newdata |
optionally, a data frame in which to look for variables with which to predict. If omitted, the fitted linear predictors are used. |
type |
the type of prediction required. The default is on the scale of the linear predictors; the alternative ‘"response"’ is on the scale of the response variable. Thus for a default binomial model the default predictions are of log-odds (probabilities on logit scale) and ‘type = "response"’ gives the predicted probabilities. The ‘"terms"’ option returns a matrix giving the fitted values of each term in the model formula on the linear predictor scale. The value of this argument can be abbreviated. |
se.fit |
logical switch indicating if standard errors are required. |
dispersion |
the dispersion of the GLM fit to be assumed in computing the standard errors. If omitted, that returned by ‘summary’ applied to the object is used. |
terms |
with ‘type = "terms"’ by default all terms are returned. A character vector specifies which terms are to be returned |
na.action |
function determining what should be done with missing values in ‘newdata’. The default is to predict ‘NA’. |
...further |
arguments passed to or from other methods. |
currently borrowing parameters + descriptions from predict.glm
## Not run: predict(hts_example_model) ## End(Not run)
## Not run: predict(hts_example_model) ## End(Not run)
prediction_data(model_data, key, index, ...)
prediction_data(model_data, key, index, ...)
model_data |
data used in the model |
key |
new key |
index |
new index |
... |
any other variables you want to create an example dataset of |
Create prediction data that has the same index and key as the model data for use in prediction.
tsibble
object with the same index and key as model_data
.
date_range <- clock::date_build(2019, 2, 1:5) date_range countries <- c("Ethiopia", "Tanzania") countries prediction_data( model_data = malaria_africa_ts, key = countries, index = date_range )
date_range <- clock::date_build(2019, 2, 1:5) date_range countries <- c("Ethiopia", "Tanzania") countries prediction_data( model_data = malaria_africa_ts, key = countries, index = date_range )
The tidy()
function provides a data frame of the mean, standard deviation,
quantiles
## S3 method for class 'hts_inla' tidy(x, ...)
## S3 method for class 'hts_inla' tidy(x, ...)
x |
object of class "hts_inla" |
... |
extra arguments to pass to |
dataframe with column names: "variable", "mean", "sd", and quantiles for 0.025, 0.5, and 0.975, as well as mode and kld
hts_example_model tidy(hts_example_model)
hts_example_model tidy(hts_example_model)
Data containing information on countries and WHO designated regions and ISO 3166-1 alpha-3 country code. From largest to smallest area it goes: "who_region", "who_subregion", then "country".
data(who_regions)
data(who_regions)
An object of class tbl_df
(inherits from tbl
, data.frame
) with 110 rows and 4 columns.
Format: a data frame with 110 observations on the following 4 variables.
who_region
A character of WHO region, the largest region size.
who_subregion
A character of WHO subregion, the second largest region size.
country
A character of countries, the smallest region size.
country_iso_code
A three letter character code adhering to the standard, ISO 3166-1 alpha-3. For more detail see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3.
Currently this just checks for INLA
yahtsee_sitrep()
yahtsee_sitrep()
message on availability of system requirements
## Not run: yahtsee_sitrep() ## End(Not run)
## Not run: yahtsee_sitrep() ## End(Not run)