makeGAM: A function to create the formulae needed to fit GAMs in INLA...

View source: R/makeGAM.R

makeGAMR Documentation

A function to create the formulae needed to fit GAMs in INLA in R.

Description

Given the beginning of a formula (e.g. y ~ 0 + Intercept) this function builds the string needed to create GAM models either with or withour the linear terms as well. This can then be returned as a formula (if it is to be used directly) or as a string (if other elements such as random effects are yet to be added).

Usage

makeGAM(
  varnames,
  response = "y",
  invariant = "0 + Intercept",
  linear = TRUE,
  returnstring = TRUE
)

Arguments

varnames

Character vector giving the names of the variables to be used.

response

String giving the name of the response variable

invariant

Any string to be invluded at the beginning of the formula such as identifying the intercept

linear

If TRUE, all variables are included as linear terms. If a character vector, variables in the vector are included as linear terms. If FALSE, no linear terms are included.

returnstring

If TRUE, return formula as a string (which can later be turned into a formula with formula). If FALSE, return a formula object.

Examples

 # GAM formula
 form1 <- makeGAM(c('x1', 'x2'), response = 'y', invariant = '0 + Intercept')
 
 # GAM with additional linear terms
 form2 <- makeGAM(c('x1', 'x2'), response = 'y', invariant = '0 + Intercept', 
                  linear = c('x1', 'x2', 'x3'))
 ## Not run: 
 library(INLA)
 data(Epil)
 observed <- Epil[1:30, 'y']
 Epil <- rbind(Epil, Epil[1:30, ])
 Epil[1:30, 'y'] <- NA
 ## make centered covariates
 formula = y ~ Trt + Age + V4 +
          f(Ind, model="iid") + f(rand,model="iid")
 formula <- makeGAM('Age', invariant = '', linear = c('Age', 'Trt', 'V4'), returnstring = FALSE)
 result = inla(formula, family="poisson", data = Epil, 
               control.predictor = list(compute = TRUE, link = 1))
 ggplot_inla_residuals(result, observed, binwidth = 0.2)
 autoplot(result)
 
## End(Not run)

timcdlucas/INLAutils documentation built on Nov. 29, 2022, 5:41 a.m.