Learn R Programming

MLBC (version 0.2.1)

ols: 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, ...)

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

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

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

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

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

Run this code
# 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)

Run the code above in your browser using DataLab