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.

pred_vars(x)

resp_var(x)

resp_val(x)

link_inverse(x)

model_frame(x, fe.only = TRUE)

model_family(x)

var_names(x)

Arguments

x

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

fe.only

Logical, if TRUE (default) and x is a mixed effects model, returns the model frame for fixed effects only.

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. 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_logit: model has logit link

  • is_linear: family is gaussian

  • is_linear: family is gaussian

  • is_ordinal: family is ordinal or cumulative link

  • is_zeroinf: model has zero-inflation component

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

  • link.fun: the link-function

  • family: the family-object

model_frame() slighty differs from model.frame(), especially for spline terms. 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(). See 'Examples'.

Examples

data(efc) fit <- lm(neg_c_7 ~ e42dep + c161sex, data = efc) pred_vars(fit)
#> [1] "e42dep" "c161sex"
resp_var(fit)
#> [1] "neg_c_7"
resp_val(fit)
#> [1] 12 20 11 10 12 19 15 11 15 10 28 18 13 18 16 13 11 11 13 17 11 9 8 14 11 #> [26] 23 11 15 11 25 9 15 20 9 10 19 8 17 16 17 14 14 16 19 17 15 16 19 17 10 #> [51] 14 14 9 12 25 17 22 13 15 7 19 11 15 13 11 14 17 7 15 11 19 10 10 20 10 #> [76] 12 15 7 13 12 16 10 15 15 15 25 11 10 11 14 10 10 13 10 11 18 14 12 10 9 #> [101] 13 14 10 10 13 13 12 12 18 7 13 14 11 16 15 15 9 17 17 22 16 14 9 13 9 #> [126] 17 17 9 13 14 12 18 7 10 12 20 12 14 12 10 12 11 14 11 13 10 12 12 10 9 #> [151] 15 12 11 14 16 18 11 11 14 14 12 10 9 12 8 10 11 10 11 13 7 10 11 12 15 #> [176] 10 16 13 20 7 12 17 14 10 12 9 7 16 13 14 8 8 20 7 15 7 9 14 11 12 #> [201] 11 12 18 8 13 16 8 13 14 11 8 12 24 11 11 13 9 13 20 12 16 15 20 10 12 #> [226] 12 12 11 10 9 10 8 10 12 10 9 11 7 9 11 11 12 11 14 12 12 19 12 15 11 #> [251] 17 8 13 11 10 8 10 19 10 18 8 11 9 10 13 11 9 9 8 9 8 8 11 9 10 #> [276] 12 9 17 20 12 7 9 7 8 8 14 7 10 8 16 9 16 13 8 20 16 9 9 8 15 #> [301] 16 19 8 12 17 12 14 11 9 11 9 8 10 8 10 15 13 8 10 10 12 14 12 7 8 #> [326] 16 9 16 7 8 13 9 7 9 9 8 17 7 8 9 7 10 10 11 18 9 10 13 8 12 #> [351] 9 7 10 8 7 7 12 12 9 8 10 18 16 11 15 10 9 9 12 18 12 13 17 9 8 #> [376] 7 16 12 14 15 10 9 17 17 21 17 17 15 9 12 12 22 11 14 11 9 8 12 13 13 #> [401] 9 10 12 9 11 13 11 17 10 18 10 16 10 10 14 11 11 10 11 8 15 12 10 13 13 #> [426] 13 12 9 13 10 15 18 11 14 11 12 12 14 15 8 10 9 7 8 18 7 7 7 11 8 #> [451] 11 11 16 13 14 14 7 9 7 17 7 10 9 9 7 12 14 7 10 20 7 8 9 11 10 #> [476] 14 7 8 8 10 8 12 10 14 11 8 11 17 10 22 8 9 19 11 18 16 18 15 19 10 #> [501] 13 15 7 8 22 8 20 19 10 7 25 9 11 7 11 9 8 12 9 20 7 12 9 9 8 #> [526] 10 8 17 12 9 9 8 7 8 9 17 17 8 9 9 10 9 7 8 27 25 14 28 16 11 #> [551] 15 7 9 7 7 8 13 19 15 14 20 20 14 10 11 15 7 14 11 13 16 13 10 17 10 #> [576] 12 11 7 8 15 13 11 7 18 17 12 18 17 13 10 19 7 8 10 18 17 19 8 12 10 #> [601] 14 10 13 9 8 8 9 15 11 7 8 11 21 8 11 10 10 11 10 11 9 13 17 9 8 #> [626] 8 9 13 14 14 9 12 8 11 10 11 11 10 10 10 12 13 7 8 12 8 8 13 10 12 #> [651] 16 8 10 13 10 9 10 12 11 9 10 9 13 10 9 10 8 7 8 7 7 9 8 11 9 #> [676] 10 12 11 7 16 12 10 8 12 23 10 10 18 13 12 18 9 12 13 9 7 10 7 8 17 #> [701] 11 14 11 23 14 8 7 15 8 12 9 15 17 13 13 10 20 10 11 25 10 12 10 12 10 #> [726] 8 14 8 18 8 15 11 12 10 7 10 13 14 7 7 14 11 11 11 9 7 15 9 9 18 #> [751] 8 15 7 8 13 8 8 9 7 7 9 8 8 13 10 11 13 11 8 12 8 9 16 11 19 #> [776] 12 12 9 10 10 9 13 7 11 13 10 10 13 9 14 15 15 9 10 8 8 9 9 9 9 #> [801] 9 13 9 12 14 12 8 10 7 22 18 16 13 15 24 11 14 12 11 10 7 10 10 12 10 #> [826] 7 9 16 14 12 9 10 8 9 7 8 10 9 8 10 10 7 11 8 10 11 14 7 8 10 #> [851] 10 11 11 8 8 9 11 7 7 8 9 9 7 13 15 11 24 8 9 7 10 15 18 22 18 #> [876] 9 11 14 7 9 17 23 12 13 15 8 8 14 10 10
link_inverse(fit)(2.3)
#> [1] 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)
#> [1] 1.349859
# same as exp(.3)
#> [1] 1.349859
outcome <- as.numeric(outcome) m <- glm(counts ~ log(outcome) + as.factor(treatment), family = poisson()) var_names(m)
#> [1] "counts" "outcome" "treatment"
# model.frame and model_frame behave slightly different library(splines) m <- lm(neg_c_7 ~ e42dep + ns(c160age), data = efc) head(model.frame(m))
#> neg_c_7 e42dep 1 #> 1 12 3 0.4291237 #> 2 20 3 0.4065382 #> 3 11 3 0.7001492 #> 4 10 4 0.5759292 #> 5 12 4 0.3274891 #> 6 19 4 0.4291237
head(model_frame(m))
#> neg_c_7 e42dep c160age #> 1 12 3 0.4291237 #> 2 20 3 0.4065382 #> 3 11 3 0.7001492 #> 4 10 4 0.5759292 #> 5 12 4 0.3274891 #> 6 19 4 0.4291237