regress: Linear regression

View source: R/models.R

regressR Documentation

Linear regression

Description

Fits an ordinary least-squares model. R4VN compact syntax allows models such as regress(y, c.age, i.sex, i.sex*i.treatment) without ~ or +.

Usage

regress(
  y,
  ...,
  vars = NULL,
  data = NULL,
  noconstant = FALSE,
  vce = c("model", "robust", "cluster"),
  cluster = NULL,
  weights = NULL,
  subset = NULL,
  ref = NULL,
  standardized = FALSE,
  vif = FALSE,
  diagnosis = FALSE,
  level = 0.95,
  digits = 3,
  p_digits = 3,
  show = TRUE,
  console = FALSE
)

Arguments

y

Formula or numeric outcome variable.

...

Predictors or model terms when y is not a formula.

vars

Optional model terms written as vars(...).

data

Data frame or NULL for active data.

noconstant

Fit without an intercept.

vce

Model-based, HC1 robust, or cluster-robust covariance.

cluster

Cluster variable used when vce = "cluster".

weights

Optional non-negative analytic weights.

subset

Optional logical subset expression.

ref

Optional named list of factor reference levels.

standardized

Also display standardized coefficients for numeric columns.

vif

Also display coefficient-level variance inflation factors.

diagnosis

Logical; if TRUE, append model-diagnostic tables. For linear regression these include residual normality, a Breusch-Pagan heteroscedasticity test, standardized/studentized residuals, leverage, Cook's distance, DFFITS, COVRATIO, influential observations, and collinearity diagnostics. Default FALSE.

level

Confidence level.

digits, p_digits

Decimal places.

show

Logical; open the formatted result in the Viewer. Default TRUE.

console

Logical; also print the traditional result in the Console. Default FALSE.

Details

Compact model prefixes are c.x for continuous, i.x for categorical, b2.x for the second factor level as reference, and ib2.x for value/level 2 as reference. Use * for main effects plus interaction and : for interaction only.

Ordinary linear regression models should be compared with the usual nested F test rather than lrtest().

Value

An object of class r4vn_stat, returned invisibly. Its sections component contains the formatted model summary, ANOVA, coefficient table, and any requested standardized-coefficient or VIF tables. In raw, model is the fitted lm object, vcov is the covariance matrix, coefficients contains coefficient-level estimates and tests, overall contains the overall model test, and vce and model.terms record the covariance estimator and fitted terms.

Examples

d <- data.frame(score = c(60, 65, 68, 72, 75, 80, 77, 70),
                age = c(20, 25, 30, 35, 40, 45, 50, 55),
                bmi = c(20, 22, 24, 23, 26, 28, 27, 25),
                sex = factor(rep(c("Female", "Male"), 4)))

regress(score, age, bmi, i.sex, data = d, show = FALSE)
regress(score, vars = vars(c.age, c.bmi, i.sex), data = d, show = FALSE)
# Full model diagnostics
m <- regress(score, c.age, c.bmi, i.sex, data = d, diagnosis = TRUE, show = FALSE)
m$sections$`Model diagnosis`
m$sections$`Influence diagnostics`
# Postestimation diagnostics can also be generated as variables
usedf(d)
regress(score, c.age, c.bmi, i.sex, diagnosis = FALSE, show = FALSE)
predict(newvar = stdres, type = "standardized", show = FALSE)
predict(newvar = cooksd, type = "cooksd", show = FALSE)

R4VN documentation built on Sept. 30, 2026, 5:13 p.m.