Learn R Programming

sjstats (version 0.17.4)

link_inverse: Access information from model objects

Description

Several functions to retrieve information from model objects, like variable names, link-inverse function, model frame, model family etc., in a tidy and consistent way.

Usage

link_inverse(x, multi.resp = FALSE, mv = FALSE)

model_family(x, multi.resp = FALSE, mv = FALSE)

model_frame(x, fe.only = TRUE)

pred_vars(x, ...)

# S3 method for default pred_vars(x, fe.only = FALSE, ...)

# S3 method for glmmTMB pred_vars(x, fe.only = FALSE, zi = FALSE, disp = FALSE, ...)

# S3 method for MixMod pred_vars(x, fe.only = FALSE, zi = FALSE, ...)

re_grp_var(x)

grp_var(x)

resp_val(x)

resp_var(x, combine = TRUE)

var_names(x)

Arguments

x

A fitted model; for var_names(), x may also be a character vector.

mv, multi.resp

Logical, if TRUE and model is a multivariate response model from a brmsfit object or of class stanmvreg, then a list of values (one for each regression) is returned.

fe.only

Logical, if TRUE and x is a mixed effects model, model_frame() returns the model frame for fixed effects only, and pred_vars() returns only fixed effects terms. Note that the default for model_frame() is fe.only = TRUE, while for pred_vars() the default is fe.only = FALSE.

...

Currently not used.

zi

Logical, if TRUE and model has a zero-inflation-formula, the variable(s) used in this formula are also returned.

disp

Logical, if TRUE and model is of class glmmTMB and has a dispersion-formula, the variable(s) used in the dispersion-formula are also returned.

combine

Logical, if TRUE and the response is a matrix-column, the name of the response matches the notation in formula, and would for instance also contain patterns like "cbind(...)". Else, the original variable names from the matrix-column are returned. See 'Examples'.

Value

For pred_vars() and resp_var(), the name(s) of the response or predictor variables from x as character vector. resp_val() returns the values from x's response vector. re_grp_var() returns the group factor of random effects in mixed models, or NULL if x has no such random effects term (grp_var() is an alias for re_grp_var()). link_inverse() returns, if known, the inverse link function from x; else NULL for those models where the inverse link function can't be identified. model_frame() is similar to model.frame(), but should also work for model objects that don't have a S3-generic for model.frame(). var_names() returns the "cleaned" variable names, i.e. things like s() for splines or log() are removed. model_family() returns a list with information about the model family (see 'Details').

Details

model_family() returns a list with information about the model family for many different model objects. Following information is returned, where all values starting with is_ are logicals.

  • is_bin: family is binomial (but not negative binomial)

  • is_pois: family is either poisson or negative binomial

  • is_negbin: family is negative binomial

  • is_count: model is a count model (i.e. family is either poisson or negative binomial)

  • is_beta: family is beta

  • is_logit: model has logit link

  • is_linear: family is gaussian

  • is_ordinal: family is ordinal or cumulative link

  • is_categorical: family is categorical link

  • is_zeroinf: model has zero-inflation component

  • is_multivariate: model is a multivariate response model (currently only works for brmsfit objects)

  • is_trial: model response contains additional information about the trials

  • link.fun: the link-function

  • family: the family-object

model_frame() slighty differs from model.frame(), especially for spline terms and matrix-variables created with cbind() (for example in binomial models, where the response is a combination of successes and trials) . Where model.frame() returns a matrix for splines, model_frame() returns the data of the original variable and uses the same column name as in the data-argument from the model-function. This makes it easier, for instance, to get data that should be used as new data in predict(). For matrix-variables created with cbind(), model_frame() returns the original variable as matrix and additionally each column as own variable. See 'Examples'.

Examples

Run this code
# NOT RUN {
data(efc)
fit <- lm(neg_c_7 ~ e42dep + c161sex, data = efc)

pred_vars(fit)
resp_var(fit)
resp_val(fit)

link_inverse(fit)(2.3)

# example from ?stats::glm
counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12)
outcome <- gl(3, 1, 9)
treatment <- gl(3, 3)
m <- glm(counts ~ outcome + treatment, family = poisson())

link_inverse(m)(.3)
# same as
exp(.3)

outcome <- as.numeric(outcome)
m <- glm(counts ~ log(outcome) + as.factor(treatment), family = poisson())
var_names(m)

# model.frame and model_frame behave slightly different
library(splines)
m <- lm(neg_c_7 ~ e42dep + ns(c160age, knots = 2), data = efc)
head(model.frame(m))
head(model_frame(m))

library(lme4)
data(cbpp)
cbpp$trials <- cbpp$size - cbpp$incidence
m <- glm(cbind(incidence, trials) ~ period, data = cbpp, family = binomial)
head(model.frame(m))
head(model_frame(m))

resp_var(m, combine = TRUE)
resp_var(m, combine = FALSE)

# get random effects grouping factor from mixed models
library(lme4)
data(sleepstudy)
m <- lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)
re_grp_var(m)

# get model predictors, with and w/o dispersion formula
# }
# NOT RUN {
library(glmmTMB)
data("Salamanders")
m <- glmmTMB(
  count ~ spp + cover + mined + poly(DOP, 3) + (1 | site),
  ziformula = ~spp + mined,
  dispformula = ~DOY,
  data = Salamanders,
  family = nbinom2
)

pred_vars(m)
pred_vars(m, fe.only = TRUE)
pred_vars(m, disp = TRUE)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab