Title: | Model Temporal Trends |
---|---|
Description: | Provides a coherent interface to multiple modelling tools for fitting trends along with a standardised approach for generating confidence and prediction intervals. |
Authors: | Tim Taylor [aut] , Dirk Schumacher [ctb], Thibaut Jombart [ctb, cre] |
Maintainer: | Thibaut Jombart <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2024-11-01 03:55:14 UTC |
Source: | https://github.com/reconverse/trending |
Generics for accessing model information.
get_result(x, ...) ## Default S3 method: get_result(x, ...) ## S3 method for class 'trending_fit' get_result(x, ...) ## S3 method for class 'trending_predict' get_result(x, ...) ## S3 method for class 'trending_fit_tbl' get_result(x, ...) ## S3 method for class 'trending_predict_tbl' get_result(x, ...) get_warnings(x, ...) ## Default S3 method: get_warnings(x, ...) ## S3 method for class 'trending_fit' get_warnings(x, ...) ## S3 method for class 'trending_predict' get_warnings(x, ...) ## S3 method for class 'trending_fit_tbl' get_warnings(x, ...) ## S3 method for class 'trending_predict_tbl' get_warnings(x, ...) get_errors(x, ...) ## Default S3 method: get_errors(x, ...) ## S3 method for class 'trending_fit' get_errors(x, ...) ## S3 method for class 'trending_predict' get_errors(x, ...) ## S3 method for class 'trending_fit_tbl' get_errors(x, ...) ## S3 method for class 'trending_predict_tbl' get_errors(x, ...) get_fitted_model(x, ...) ## Default S3 method: get_fitted_model(x, ...) ## S3 method for class 'trending_fit' get_fitted_model(x, ...) ## S3 method for class 'trending_fit_tbl' get_fitted_model(x, ...) get_fitted_data(x, ...) ## Default S3 method: get_fitted_data(x, ...) ## S3 method for class 'trending_fit' get_fitted_data(x, ...) ## S3 method for class 'trending_fit_tbl' get_fitted_data(x, ...) get_formula(x, ...) ## Default S3 method: get_formula(x, ...) ## S3 method for class 'trending_model' get_formula(x, ...) ## S3 method for class 'trending_fit' get_formula(x, ...) ## S3 method for class 'trending_fit_tbl' get_formula(x, ...) get_response(x, ...) ## Default S3 method: get_response(x, ...) ## S3 method for class 'trending_model' get_response(x, ...) ## S3 method for class 'trending_fit' get_response(x, ...) ## S3 method for class 'trending_fit_tbl' get_response(x, ...) ## S3 method for class 'trending_prediction' get_response(x, ...) ## S3 method for class 'trending_predict' get_response(x, ...) ## S3 method for class 'trending_predict_tbl' get_response(x, ...) get_predictors(x, ...) ## Default S3 method: get_predictors(x, ...) ## S3 method for class 'trending_model' get_predictors(x, ...) ## S3 method for class 'trending_fit' get_predictors(x, ...) ## S3 method for class 'trending_fit_tbl' get_predictors(x, ...) ## S3 method for class 'trending_prediction' get_predictors(x, ...) ## S3 method for class 'trending_predict' get_predictors(x, ...) ## S3 method for class 'trending_predict_tbl' get_predictors(x, ...) get_estimate(x, ...) ## Default S3 method: get_estimate(x, ...) ## S3 method for class 'trending_prediction' get_estimate(x, ...) ## S3 method for class 'trending_predict' get_estimate(x, ...) ## S3 method for class 'trending_predict_tbl' get_estimate(x, ...)
get_result(x, ...) ## Default S3 method: get_result(x, ...) ## S3 method for class 'trending_fit' get_result(x, ...) ## S3 method for class 'trending_predict' get_result(x, ...) ## S3 method for class 'trending_fit_tbl' get_result(x, ...) ## S3 method for class 'trending_predict_tbl' get_result(x, ...) get_warnings(x, ...) ## Default S3 method: get_warnings(x, ...) ## S3 method for class 'trending_fit' get_warnings(x, ...) ## S3 method for class 'trending_predict' get_warnings(x, ...) ## S3 method for class 'trending_fit_tbl' get_warnings(x, ...) ## S3 method for class 'trending_predict_tbl' get_warnings(x, ...) get_errors(x, ...) ## Default S3 method: get_errors(x, ...) ## S3 method for class 'trending_fit' get_errors(x, ...) ## S3 method for class 'trending_predict' get_errors(x, ...) ## S3 method for class 'trending_fit_tbl' get_errors(x, ...) ## S3 method for class 'trending_predict_tbl' get_errors(x, ...) get_fitted_model(x, ...) ## Default S3 method: get_fitted_model(x, ...) ## S3 method for class 'trending_fit' get_fitted_model(x, ...) ## S3 method for class 'trending_fit_tbl' get_fitted_model(x, ...) get_fitted_data(x, ...) ## Default S3 method: get_fitted_data(x, ...) ## S3 method for class 'trending_fit' get_fitted_data(x, ...) ## S3 method for class 'trending_fit_tbl' get_fitted_data(x, ...) get_formula(x, ...) ## Default S3 method: get_formula(x, ...) ## S3 method for class 'trending_model' get_formula(x, ...) ## S3 method for class 'trending_fit' get_formula(x, ...) ## S3 method for class 'trending_fit_tbl' get_formula(x, ...) get_response(x, ...) ## Default S3 method: get_response(x, ...) ## S3 method for class 'trending_model' get_response(x, ...) ## S3 method for class 'trending_fit' get_response(x, ...) ## S3 method for class 'trending_fit_tbl' get_response(x, ...) ## S3 method for class 'trending_prediction' get_response(x, ...) ## S3 method for class 'trending_predict' get_response(x, ...) ## S3 method for class 'trending_predict_tbl' get_response(x, ...) get_predictors(x, ...) ## Default S3 method: get_predictors(x, ...) ## S3 method for class 'trending_model' get_predictors(x, ...) ## S3 method for class 'trending_fit' get_predictors(x, ...) ## S3 method for class 'trending_fit_tbl' get_predictors(x, ...) ## S3 method for class 'trending_prediction' get_predictors(x, ...) ## S3 method for class 'trending_predict' get_predictors(x, ...) ## S3 method for class 'trending_predict_tbl' get_predictors(x, ...) get_estimate(x, ...) ## Default S3 method: get_estimate(x, ...) ## S3 method for class 'trending_prediction' get_estimate(x, ...) ## S3 method for class 'trending_predict' get_estimate(x, ...) ## S3 method for class 'trending_predict_tbl' get_estimate(x, ...)
x |
An R object. |
... |
Not currently used by any methods. |
Methods are provided for trending_model
, trending_fit
,
trending_fit_tbl
, trending_predict
, and trending_predict_tbl
objects.
get_result()
: the captured output.
get_warnings()
: the captured warnings.
get_errors()
: the captured warnings.
get_fitted_data
: The underlying data used to fit the model.
get_fitted_model()
: The underlying fitted model.
get_formula()
: the formula used to model temporal trends.
get_response()
: the response variable of the underlying model.
get_predictors()
: the predictor variable(s) of the underlying model.
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") fitted_poisson <- fit(poisson_model, dat) get_fitted_model(fitted_poisson) get_formula(fitted_poisson) get_response(fitted_poisson) get_predictors(fitted_poisson)
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") fitted_poisson <- fit(poisson_model, dat) get_fitted_model(fitted_poisson) get_formula(fitted_poisson) get_response(fitted_poisson) get_predictors(fitted_poisson)
fit()
is a generic to fit a specified model.
fit(x, data, ...) ## Default S3 method: fit(x, data, ...)
fit(x, data, ...) ## Default S3 method: fit(x, data, ...)
x |
An R object |
data |
A data frame containing the data to fit. |
... |
Arguments passed to underlying methods. |
The value returned depends on the class of the input argument.
Tim Taylor
fit.trending_model()
and fit.list()
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = poisson) negbin_model <- glm_nb_model(y ~ x) fit(poisson_model, dat) fit(negbin_model, dat) fit(list(poisson_model, negbin_model), dat) fit(list(pm = poisson_model, nm = negbin_model), dat)
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = poisson) negbin_model <- glm_nb_model(y ~ x) fit(poisson_model, dat) fit(negbin_model, dat) fit(list(poisson_model, negbin_model), dat) fit(list(pm = poisson_model, nm = negbin_model), dat)
Fits a list of trending_model objects to the given input data.
## S3 method for class 'list' fit(x, data, ...)
## S3 method for class 'list' fit(x, data, ...)
x |
A list of trending_model objects |
data |
A data frame containing the data to fit. |
... |
Not currently used. |
A trending_fit_tbl
object which is a tibble
subclass with one row for each model and entries:
model_name (optional): If the input is a named list then the name is extracted.
result: the resulting fit from calling the underlying model directly, i.e.
lm_model
: a fitted model object of class lm
glm_model
: a fitted model object of class glm
glm_nb_model
: a fitted model object of class negbin
brm_model
: An object of class brmsfit
NULL
if fitting fails.
warnings: any warnings generated during fitting
errors: any errors generated during fitting
Tim Taylor
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = poisson) negbin_model <- glm_nb_model(y ~ x) fit(list(poisson_model, negbin_model), dat) fit(list(pm = poisson_model, nm = negbin_model), dat)
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = poisson) negbin_model <- glm_nb_model(y ~ x) fit(list(poisson_model, negbin_model), dat) fit(list(pm = poisson_model, nm = negbin_model), dat)
Fits the specified model to the input data
## S3 method for class 'trending_model' fit(x, data, as_tibble = TRUE, ...)
## S3 method for class 'trending_model' fit(x, data, as_tibble = TRUE, ...)
x |
An R object |
data |
A data frame containing the data to fit. |
as_tibble |
Should the output be converted to a tibble subclass. |
... |
Not currently used. |
If as_tibble = FALSE
, then a trending_fit
object is returned.
This is a list subclass with entries:
result: the resulting fit from calling the underlying model directly, i.e.
lm_model
: a fitted model object of class lm
glm_model
: a fitted model object of class glm
glm_nb_model
: a fitted model object of class negbin
brm_model
: An object of class brmsfit
NULL
if fitting fails.
warnings: any warnings generated during fitting
errors: any errors generated during fitting
If as_tibble = TRUE
, a trending_fit_tbl
object which is a
tibble
subclass with one row for each model and
columns 'result', 'warnings' and 'errors' with contents as above.
Tim Taylor
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = poisson) negbin_model <- glm_nb_model(y ~ x) fit(poisson_model, dat) fit(negbin_model, dat) fit(list(poisson_model, negbin_model), dat) fit(list(pm = poisson_model, nm = negbin_model), dat)
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = poisson) negbin_model <- glm_nb_model(y ~ x) fit(poisson_model, dat) fit(negbin_model, dat) fit(list(poisson_model, negbin_model), dat) fit(list(pm = poisson_model, nm = negbin_model), dat)
Adds estimated values and associated confidence and/or prediction intervals to data based on trending_model fit.
## S3 method for class 'list' predict( object, data, name = "estimate", alpha = 0.05, add_ci = TRUE, ci_names = c("lower_ci", "upper_ci"), add_pi = TRUE, pi_names = c("lower_pi", "upper_pi"), simulate_pi = FALSE, sims = 2000, uncertain = TRUE, ... )
## S3 method for class 'list' predict( object, data, name = "estimate", alpha = 0.05, add_ci = TRUE, ci_names = c("lower_ci", "upper_ci"), add_pi = TRUE, pi_names = c("lower_pi", "upper_pi"), simulate_pi = FALSE, sims = 2000, uncertain = TRUE, ... )
object |
A list of |
data |
A |
name |
Character vector of length one giving the name to use for the calculated estimate. |
alpha |
The alpha threshold to be used for prediction intervals, defaulting to 0.05, i.e. 95% prediction intervals are derived. |
add_ci |
Should a confidence interval be added to the output. Default TRUE. |
ci_names |
Names to use for the resulting confidence intervals. |
add_pi |
Should a prediction interval be added to the output. Default TRUE. |
pi_names |
Names to use for the resulting prediction intervals. |
simulate_pi |
Should the prediction intervals for glm models be
simulated. If TRUE, default, |
sims |
The number of simulations to run when simulating prediction intervals for a glm model. |
uncertain |
Only used for glm models and when |
... |
Not currently used. |
A trending_predict_tbl
object which is a
tibble
subclass with one row per model and columns:
result: the input data frame with additional estimates and, optionally,
confidence and or prediction intervals. NULL
if the associated
predict
method fails.
warnings: any warnings generated during prediction.
errors: any errors generated during prediction.
Tim Taylor
predict.trending_model()
, predict.trending_fit()
,
predict.trending_fit_tbl()
,
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") negbin_model <- glm_nb_model(y ~ x) predict(list(poisson_model, negbin_model), dat) predict(list(pm = poisson_model, nm = negbin_model), dat)
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") negbin_model <- glm_nb_model(y ~ x) predict(list(poisson_model, negbin_model), dat) predict(list(pm = poisson_model, nm = negbin_model), dat)
Adds estimated values and associated confidence and/or prediction intervals to trending_fit objects.
## S3 method for class 'trending_fit' predict( object, new_data, name = "estimate", alpha = 0.05, add_ci = TRUE, ci_names = c("lower_ci", "upper_ci"), add_pi = TRUE, pi_names = c("lower_pi", "upper_pi"), simulate_pi = FALSE, sims = 2000, uncertain = TRUE, as_tibble = TRUE, ... )
## S3 method for class 'trending_fit' predict( object, new_data, name = "estimate", alpha = 0.05, add_ci = TRUE, ci_names = c("lower_ci", "upper_ci"), add_pi = TRUE, pi_names = c("lower_pi", "upper_pi"), simulate_pi = FALSE, sims = 2000, uncertain = TRUE, as_tibble = TRUE, ... )
object |
A |
new_data |
A |
name |
Character vector of length one giving the name to use for the calculated estimate. |
alpha |
The alpha threshold to be used for prediction intervals, defaulting to 0.05, i.e. 95% prediction intervals are derived. |
add_ci |
Should a confidence interval be added to the output. Default TRUE. |
ci_names |
Names to use for the resulting confidence intervals. |
add_pi |
Should a prediction interval be added to the output. Default TRUE. |
pi_names |
Names to use for the resulting prediction intervals. |
simulate_pi |
Should the prediction intervals for glm models be
simulated. If TRUE, default, |
sims |
The number of simulations to run when simulating prediction intervals for a glm model. |
uncertain |
Only used for glm models and when |
as_tibble |
Should the output be converted to a tibble subclass. |
... |
Not currently used. |
If as_tibble = FALSE
, a trending_predict
object, which is a list
subclass, with entries:
result: the input data frame with additional estimates and, optionally,
confidence and or prediction intervals. NULL
if the associated
predict
method fails.
warnings: any warnings generated during prediction.
errors: any errors generated during prediction.
If as_tibble = TRUE
, a trending_predict_tbl
object which is a
tibble
subclass with one row per model and columns
'result', 'warnings' and 'errors' with contents as above.
Tim Taylor
predict.trending_fit_tbl()
and predict.trending_model()
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") fitted_poisson <- fit(poisson_model, dat) predict(fitted_poisson) predict(fitted_poisson, as_tibble = FALSE)
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") fitted_poisson <- fit(poisson_model, dat) predict(fitted_poisson) predict(fitted_poisson, as_tibble = FALSE)
Adds estimated values and associated confidence and/or prediction intervals to trending_fit_tbl objects.
## S3 method for class 'trending_fit_tbl' predict( object, new_data, name = "estimate", alpha = 0.05, add_ci = TRUE, ci_names = c("lower_ci", "upper_ci"), add_pi = TRUE, pi_names = c("lower_pi", "upper_pi"), simulate_pi = FALSE, sims = 2000, uncertain = TRUE, ... )
## S3 method for class 'trending_fit_tbl' predict( object, new_data, name = "estimate", alpha = 0.05, add_ci = TRUE, ci_names = c("lower_ci", "upper_ci"), add_pi = TRUE, pi_names = c("lower_pi", "upper_pi"), simulate_pi = FALSE, sims = 2000, uncertain = TRUE, ... )
object |
A |
new_data |
A |
name |
Character vector of length one giving the name to use for the calculated estimate. |
alpha |
The alpha threshold to be used for prediction intervals, defaulting to 0.05, i.e. 95% prediction intervals are derived. |
add_ci |
Should a confidence interval be added to the output. Default TRUE. |
ci_names |
Names to use for the resulting confidence intervals. |
add_pi |
Should a prediction interval be added to the output. Default TRUE. |
pi_names |
Names to use for the resulting prediction intervals. |
simulate_pi |
Should the prediction intervals for glm models be
simulated. If TRUE, default, |
sims |
The number of simulations to run when simulating prediction intervals for a glm model. |
uncertain |
Only used for glm models and when |
... |
Not currently used. |
a trending_predict_tbl
object which is a
tibble
subclass with one row per model and columns
'result', 'warnings' and 'errors' with contents as above.:
result: the input data frame with additional estimates and, optionally,
confidence and or prediction intervals. NULL
if the associated
predict
method fails.
warnings: any warnings generated during prediction.
errors: any errors generated during prediction.
Tim Taylor
predict.trending_fit()
, predict.trending_fit_tbl()
and
predict.trending_model()
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") negbin_model <- glm_nb_model(y ~ x) fitted_tbl <- fit(list(poisson_model, negbin_model), dat) predict(fitted_tbl)
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") negbin_model <- glm_nb_model(y ~ x) fitted_tbl <- fit(list(poisson_model, negbin_model), dat) predict(fitted_tbl)
Adds estimated values and associated confidence and/or prediction intervals to data based on trending_model fit.
## S3 method for class 'trending_model' predict( object, data, name = "estimate", alpha = 0.05, add_ci = TRUE, ci_names = c("lower_ci", "upper_ci"), add_pi = TRUE, pi_names = c("lower_pi", "upper_pi"), simulate_pi = FALSE, sims = 2000, uncertain = TRUE, as_tibble = TRUE, ... )
## S3 method for class 'trending_model' predict( object, data, name = "estimate", alpha = 0.05, add_ci = TRUE, ci_names = c("lower_ci", "upper_ci"), add_pi = TRUE, pi_names = c("lower_pi", "upper_pi"), simulate_pi = FALSE, sims = 2000, uncertain = TRUE, as_tibble = TRUE, ... )
object |
A |
data |
A |
name |
Character vector of length one giving the name to use for the calculated estimate. |
alpha |
The alpha threshold to be used for prediction intervals, defaulting to 0.05, i.e. 95% prediction intervals are derived. |
add_ci |
Should a confidence interval be added to the output. Default TRUE. |
ci_names |
Names to use for the resulting confidence intervals. |
add_pi |
Should a prediction interval be added to the output. Default TRUE. |
pi_names |
Names to use for the resulting prediction intervals. |
simulate_pi |
Should the prediction intervals for glm models be
simulated. If TRUE, default, |
sims |
The number of simulations to run when simulating prediction intervals for a glm model. |
uncertain |
Only used for glm models and when |
as_tibble |
Should the output be converted to a tibble subclass. |
... |
Not currently used. |
If as_tibble = FALSE
, a trending_predict
object, which is a list
subclass, with entries:
result: the input data frame with additional estimates and, optionally,
confidence and or prediction intervals. NULL
if the associated
predict
method fails.
warnings: any warnings generated during prediction.
errors: any errors generated during prediction.
If as_tibble = TRUE
, a trending_predict_tbl
object which is a
tibble
subclass with one row per model and columns
'result', 'warnings' and 'errors' with contents as above.
Tim Taylor
predict.trending_fit()
and predict.trending_fit_tbl()
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") predict(poisson_model, dat) predict(poisson_model, dat, as_tibble = FALSE)
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) dat <- data.frame(x = x, y = y) poisson_model <- glm_model(y ~ x , family = "poisson") predict(poisson_model, dat) predict(poisson_model, dat, as_tibble = FALSE)
These functions wrap various modelling tools to ensure a consistent input for trending functions. They work by capturing the underlying model call and decoupling it from the data specification. This makes it easy to use the same underlying model specification and fitting procedure across different data sets. See details for available model interfaces.
lm_model(formula, ...) glm_model(formula, family = gaussian, ...) glm_nb_model(formula, ...) brm_model(formula, ...)
lm_model(formula, ...) glm_model(formula, family = gaussian, ...) glm_nb_model(formula, ...) brm_model(formula, ...)
formula |
The formula of the model, with the response variable on the
left of a tilde symbol, and predictors on the right hand-side; variable
names used in the formula will need to be matched by columns in the |
... |
Further arguments passed to the underlying models with the
exception of |
family |
Link function to be used for the glm model. |
The following interfaces are available:
lm_model
: interface for linear models implemented in
stats::lm()
.
glm_model
: interface for generalised linear models (GLMs) implemented in
stats::glm()
.
glm_nb_model
: interface for negative binomial generalied linear models
implemented in MASS::glm.nb()
.
brm_model
: interface for Bayesian regression models implemented in
brms::brm()
.
These interfaces will accept the same inputs as the underlying model
functions but do not require, nor will they accept, a data
argument.
Fitting is handled instead by the fit()
generic and associated methods.
A trending_model
object.
Tim Taylor
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) poisson_model <- glm_model(y ~ x , family = "poisson") negbin_model <- glm_nb_model(y ~ x)
x = rnorm(100, mean = 0) y = rpois(n = 100, lambda = exp(1.5 + 0.5*x)) poisson_model <- glm_model(y ~ x , family = "poisson") negbin_model <- glm_nb_model(y ~ x)