ols: Ordinary Least Squares (OLS) regression

View source: R/OLS.R

olsR Documentation

Ordinary Least Squares (OLS) regression

Description

Ordinary Least Squares regression with support for both formula and array-based interfaces. This function provides a unified interface for fitting linear models using either R formulas with data frames or raw matrices.

Usage

ols(Y, X = NULL, data = parent.frame(), se = TRUE, intercept = FALSE, ...)

## Default S3 method:
ols(Y, X, data = parent.frame(), se = TRUE, intercept = FALSE, ...)

## S3 method for class 'formula'
ols(Y, X = NULL, data = parent.frame(), se = TRUE, intercept = TRUE, ...)

Arguments

Y

numeric response vector, or a one-sided formula

X

numeric design matrix (if Y is numeric)

data

data frame (if Y is a formula)

se

logical; return heteroskedastic-robust standard errors?

intercept

logical; include an intercept term?

...

unused

Value

An object of class mlbc_fit and mlbc_ols with:

  • coef: coefficient estimates

  • vcov: variance-covariance matrix

  • sXX: scaled cross-product X'X / n

Usage Options

Option 1: Formula Interface

  • Y: A one-sided formula (e.g., y ~ x1 + x2)

  • data: A data frame containing the variables referenced in the formula

Option 2: Array Interface

  • Y: Response variable vector

  • X: Design matrix of covariates

Examples

# Load the remote work dataset
data(SD_data)

# Formula interface
fit1 <- ols(log(salary) ~ wfh_wham + soc_2021_2 + employment_type_name,
            data = SD_data)
summary(fit1)

# Array interface
Y <- log(SD_data$salary)
X <- model.matrix(~ wfh_wham + soc_2021_2, data = SD_data)
fit2 <- ols(Y, X[, -1], intercept = TRUE)  # exclude intercept column
summary(fit2)


MLBC documentation built on Aug. 8, 2025, 7:31 p.m.

Related to ols in MLBC...