Learn R Programming

sjstats (version 0.14.3)

pred_vars: 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

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

  • 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

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), data = efc)
head(model.frame(m))
head(model_frame(m))

# }

Run the code above in your browser using DataLab