specify_mean_model: Specify the mean model for the data generating process.

View source: R/modeling_phrases.R

specify_mean_modelR Documentation

Specify the mean model for the data generating process.

Description

A wrapper for lm and glm which allows the user to specify the form of the model for the mean response of the data generating process.

Usage

specify_mean_model(data, formula, family, ...)

Arguments

data

an optional data frame containing the variables appearing in formula.

formula

an object of class "formula": a symbolic description of the form of the mean response model. Allows some notation not typical in base R.

family

a description of the error distribution and link function to be used in the model. See glm for details. If missing (default), a linear model is used.

...

additional arguments to pass to lm or glm.

Details

specify_mean_model provides a single interface to both lm and glm with two noticable differences:

  1. The intercept is not specified by default.

  2. The notation "constant" can be used in the formula statement in order to specify terms which do not require parameters, which are typically specified by offset() in base R. The difference is that "constant" allows scalar terms.

If family is not specified, then lm is used to fit the model. Otherwise, glm is used to fit the model.

Value

an objet of type lm or glm depending on whether family is specified.

Examples

# specify a linear model
fit_reg <- specify_mean_model(mpg ~ 1 + hp, data = mtcars)
summary(fit_reg)

# intercept is not included by default
fit_0_intercept <- specify_mean_model(mpg ~ hp, data = mtcars)
summary(fit_0_intercept)

# use 'constant()' to prevent parameter from being fit
fit_restricted <- specify_mean_model(mpg ~ constant(3) + hp, data = mtcars)
summary(fit_restricted)

# specify a logistic regression model
fit_logistic <- specify_mean_model(am ~ 1 + hp,
  family = binomial,
  data = mtcars)
summary(fit_logistic)

# using piping
mtcars |> specify_mean_model(mpg ~ 1 + hp)


reyesem/IntroAnalysis documentation built on March 29, 2025, 3:29 p.m.