Skip to contents

Format formula data for use with stan

Usage

enw_formula_as_data_list(formula, prefix, drop_intercept = FALSE)

Arguments

formula

The output of enw_formula().

prefix

A character string indicating variable label to use as a prefix.

drop_intercept

Logical, defaults to FALSE. Should the intercept be included as a fixed effect or excluded. This is used internally in model modules where an intercept must be present/absent.

Value

A list defining the model formula. This includes:

  • prefix_fintercept: Is an intercept present for the fixed effects design matrix.

  • prefix_fdesign: The fixed effects design matrix

  • prefix_fnrow: The number of rows of the fixed design matrix

  • prefix_findex: The index linking design matrix rows to observations

  • prefix_fnindex: The length of the index

  • prefix_fncol: The number of columns (i.e effects) in the fixed effect design matrix (minus 1 if an intercept is present).

  • prefix_rdesign: The random effects design matrix

  • prefix_rncol: The number of columns (i.e random effects) in the random effect design matrix (minus 1 as the intercept is dropped).

  • prefix_arima_present: 1 if the formula contains an arima() term, 0 otherwise.

  • prefix_arima_T, prefix_arima_G: ARIMA series length and group count.

  • prefix_arima_p, prefix_arima_d, prefix_arima_q: ARIMA orders.

  • prefix_arima_flat_idx: per-observation column-major index into a (T x G) ARIMA residual matrix, used by Stan to gather residuals with to_vector(eps)[flat_idx].

  • prefix_arima_n_obs: length of the lookup vectors.

Examples

f <- enw_formula(~ 1 + (1 | cyl), mtcars)
enw_formula_as_data_list(f, "mtcars")
#> $mtcars_fintercept
#> [1] 1
#> 
#> $mtcars_fnrow
#> [1] 3
#> 
#> $mtcars_findex
#>  [1] 1 1 2 1 3 1 3 2 2 1 1 3 3 3 3 3 3 2 2 2 2 3 3 3 3 2 2 2 3 1 3 2
#> 
#> $mtcars_fnindex
#> [1] 32
#> 
#> $mtcars_fncol
#> [1] 3
#> 
#> $mtcars_rncol
#> [1] 1
#> 
#> $mtcars_fdesign
#>   cyl4 cyl6 cyl8
#> 1    0    1    0
#> 3    1    0    0
#> 5    0    0    1
#> 
#> $mtcars_rdesign
#>   fixed cyl
#> 1     0   1
#> 2     0   1
#> 3     0   1
#> attr(,"assign")
#> [1] 1 2
#> 
#> $mtcars_arima_present
#> [1] 0
#> 
#> $mtcars_arima_T
#> [1] 0
#> 
#> $mtcars_arima_G
#> [1] 0
#> 
#> $mtcars_arima_p
#> [1] 0
#> 
#> $mtcars_arima_d
#> [1] 0
#> 
#> $mtcars_arima_q
#> [1] 0
#> 
#> $mtcars_arima_n_obs
#> [1] 0
#> 
#> $mtcars_arima_flat_idx
#> integer(0)
#> 
#> $mtcars_gp_present
#> [1] 0
#> 
#> $mtcars_gp_T
#> [1] 0
#> 
#> $mtcars_gp_G
#> [1] 0
#> 
#> $mtcars_gp_M
#> [1] 0
#> 
#> $mtcars_gp_type
#> [1] 0
#> 
#> $mtcars_gp_nu
#> [1] 0
#> 
#> $mtcars_gp_d
#> [1] 0
#> 
#> $mtcars_gp_L
#> [1] 0
#> 
#> $mtcars_gp_n_obs
#> [1] 0
#> 
#> $mtcars_gp_PHI
#> <0 x 0 matrix>
#> 
#> $mtcars_gp_flat_idx
#> integer(0)
#> 

# A missing formula produces the default list
enw_formula_as_data_list(prefix = "missing")
#> $missing_fintercept
#> [1] 0
#> 
#> $missing_fnrow
#> [1] 0
#> 
#> $missing_findex
#> numeric(0)
#> 
#> $missing_fnindex
#> [1] 0
#> 
#> $missing_fncol
#> [1] 0
#> 
#> $missing_rncol
#> [1] 0
#> 
#> $missing_fdesign
#> numeric(0)
#> 
#> $missing_rdesign
#> numeric(0)
#> 
#> $missing_arima_present
#> [1] 0
#> 
#> $missing_arima_T
#> [1] 0
#> 
#> $missing_arima_G
#> [1] 0
#> 
#> $missing_arima_p
#> [1] 0
#> 
#> $missing_arima_d
#> [1] 0
#> 
#> $missing_arima_q
#> [1] 0
#> 
#> $missing_arima_n_obs
#> [1] 0
#> 
#> $missing_arima_flat_idx
#> integer(0)
#> 
#> $missing_gp_present
#> [1] 0
#> 
#> $missing_gp_T
#> [1] 0
#> 
#> $missing_gp_G
#> [1] 0
#> 
#> $missing_gp_M
#> [1] 0
#> 
#> $missing_gp_type
#> [1] 0
#> 
#> $missing_gp_nu
#> [1] 0
#> 
#> $missing_gp_d
#> [1] 0
#> 
#> $missing_gp_L
#> [1] 0
#> 
#> $missing_gp_n_obs
#> [1] 0
#> 
#> $missing_gp_PHI
#> <0 x 0 matrix>
#> 
#> $missing_gp_flat_idx
#> integer(0)
#>