Skip to contents

apm_mod() generates a list of models characterized by a basic model formulas and other options (e.g., lags, families, etc.) that are supplied to apm_pre(). These values are completely crossed to create a grid of model specifications, and multiple sets of model specifications can be combined using c() (see Examples).

Usage

apm_mod(
  formula_list,
  family = "gaussian",
  lag = 0L,
  diff_k = 0L,
  log = FALSE,
  time_trend = 0L,
  fixef = FALSE,
  identiy_only_log = TRUE
)

Arguments

formula_list

a list of model formulas with the outcome on the left side and predictions (or just an intercept) on the right side.

family

a list of family specifications; see family() for allowable options. These will eventually be passed to glm() when fitting the models in apm_pre(). "negbin" can also be supplied to request a negative binomial model with a log link fit using MASS::glm.nb(). Default is "gaussian" to specify a linear model.

lag

a vector of integers indicating the desired outcome lags to be used as predictors. For example, a lag value of 3 means the outcome lagged once, twice, and three times will be included as predictors. Default is 0 for no lags.

diff_k

a vector of integers indicating the desired outcome lag to be used a an offset For example, a diff_k value of 1 means the prior time point's outcome will be included as an offset, equivalent to using the outcome minus its corresponding lag as the outcome of the corresponding model. Default is 0 for no lags. Any models with a diff_k value less than a lag value will be removed automatically. When used with a family with a log link, the lags are automatically log-transformed; an error will be thrown by apm_pre() if nonpositive values are present in the outcome.

log

a logical vector indicating whether the outcome should be log-transformed. Default is FALSE to use the original outcome. When lag or diff_k are greater than 0, the outcome lags will also be log-transformed if TRUE. When the family has a log link and diff_k is greater than zero, the lag in the offset will be log transformed.

time_trend

a vector of integers indicating the desired powers to be included in a time trend. For example, a time_trend value of 2 means the time variable and its square will be included as predictors in the model. A value of 0 (the default) means time is not included as a predictor.

fixef

a logical vector indicating whether unit fixed effects should be included as predictors. Default is FALSE to omit unit fixed effects.

identiy_only_log

logical; whether to omit any models in which log is TRUE but the link in the family specification corresponds to something other than "identity". Default is TRUE, and this should probably not be changed.

Value

An apm_models object, which is a list containing the full cross (less any omitted combinations) of the model features specified in the arguments, with each combination a list. These have a print() method and can be combined using c(). Each model is named automatically, but these can be set manually using names() as well. Models can be removed by setting their value to NULL; see Examples.

See also

Examples

data("ptpdata")

# Combination of 8 models: 1 baseline formulas,
# 2 families, 2 lags, 2 time trends
models1 <- apm_mod(crude_rate ~ 1,
                   family = list("gaussian", "quasipoisson"),
                   time_trend = 0:1,
                   lag = 0:1, fixef = TRUE)
models1
#> - Model 1: FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 2: FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 3: AR(1) + FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 4: AR(1) + FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 5: linear trend + FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 6: linear trend + FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 7: linear trend + AR(1) + FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 8: linear trend + AR(1) + FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes

# Add a single other model with a square time trend
models2 <- apm_mod(crude_rate ~ 1,
                   family = "gaussian",
                   time_trend = 2,
                   fixef = FALSE)
models2
#> - Model 1: quadratic trend
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: quadratic
#> unit fixed effects: no

(models <- c(models1, models2))
#> - Model 1: FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 2: FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 3: AR(1) + FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 4: AR(1) + FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 5: linear trend + FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 6: linear trend + FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 7: linear trend + AR(1) + FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 8: linear trend + AR(1) + FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 9: quadratic trend (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: quadratic
#> unit fixed effects: no

# Remove a model
models[[4]] <- NULL
models
#> - Model 1: FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 2: FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 3: AR(1) + FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: none
#> unit fixed effects: yes
#> 
#> - Model 4: linear trend + FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 5: linear trend + FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 6: linear trend + AR(1) + FE (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 7: linear trend + AR(1) + FE (Quasipoisson)
#> crude_rate ~ 1
#> family: quasipoisson(link = "log")
#> outcome lag: 1
#> outcome diff: none
#> log outcome: no
#> time trend: linear
#> unit fixed effects: yes
#> 
#> - Model 8: quadratic trend (Gaussian)
#> crude_rate ~ 1
#> family: gaussian(link = "identity")
#> outcome lag: none
#> outcome diff: none
#> log outcome: no
#> time trend: quadratic
#> unit fixed effects: no