Skip to contents

Update data and formula for fitting cglmm model

Usage

update_formula_and_data(
  data,
  formula,
  family = "gaussian",
  quietly = TRUE,
  dispformula = ~1,
  ziformula = ~0
)

Arguments

data

input data for fitting cglmm model.

formula

model formula, specified by user including amp_acro().

family

the model family.

quietly

controls whether messages from amp_acro are displayed. TRUE by default

dispformula

The formula specifying the dispersion model

ziformula

The formula specifying the zero-inflation model

Value

Returns a list.

Examples

# Use vitamind data but add a "patient" identifier used as a random effect
vitamind2 <- vitamind
vitamind2$patient <- sample(
  LETTERS[1:5],
  size = nrow(vitamind2), replace = TRUE
)

# Use update_formula_and_data() to perform wrangling steps of cglmm()
# without yet fitting the model
data_and_formula <- update_formula_and_data(
  data = vitamind2,
  formula = vit_d ~ X + amp_acro(time,
    group = "X",
    period = 12
  )
)

# print formula from above
data_and_formula$newformula
#> vit_d ~ X + X:main_rrr1 + X:main_sss1
#> <environment: 0x55f4b2faa3c8>

# fit model while adding random effect to cosinor model formula.
mod <- fit_model_and_process(
  obj = data_and_formula,
  formula = update.formula(
    data_and_formula$newformula, . ~ . + (1 | patient)
  )
)

mod
#> 
#>  Conditional Model 
#> 
#>  Raw formula: 
#> vit_d ~ X + (1 | patient) + X:main_rrr1 + X:main_sss1 
#> 
#>  Raw Coefficients: 
#>              Estimate
#> (Intercept)  29.68979
#> X1            1.90186
#> X0:main_rrr1  0.93079
#> X1:main_rrr1  6.51029
#> X0:main_sss1  6.20099
#> X1:main_sss1  4.81846
#> 
#>  Transformed Coefficients: 
#>             Estimate
#> (Intercept) 29.68979
#> [X=1]        1.90186
#> [X=0]:amp    6.27046
#> [X=1]:amp    8.09947
#> [X=0]:acr    1.42181
#> [X=1]:acr    0.63715
mod$fit # printing the `glmmTMB` model within shows Std.Dev. of random effect
#> Formula:          vit_d ~ X + (1 | patient) + X:main_rrr1 + X:main_sss1
#> Data: newdata
#>       AIC       BIC    logLik  df.resid 
#> 1248.0316 1274.4181 -616.0158       192 
#> Random-effects (co)variances:
#> 
#> Conditional model:
#>  Groups   Name        Std.Dev. 
#>  patient  (Intercept) 0.0005457
#>  Residual             5.2653118
#> 
#> Number of obs: 200 / Conditional model: patient, 5
#> 
#> Dispersion estimate for gaussian family (sigma^2): 27.7 
#> 
#> Fixed Effects:
#> 
#> Conditional model:
#>  (Intercept)            X1  X0:main_rrr1  X1:main_rrr1  X0:main_sss1  
#>      29.6898        1.9019        0.9308        6.5103        6.2010  
#> X1:main_sss1  
#>       4.8185