estimate_model: Estimate a Linear Model

View source: R/estimate_model.R

estimate_modelR Documentation

Estimate a Linear Model

Description

[Experimental]

Estimates a linear model specified by one or more independent variables. It checks for the presence of the specified independent variables in the dataset and whether the dataset has a sufficient number of observations. Depending on the output parameter, it returns the model's coefficients, t-statistics, residuals, or any combination in a named list.

Usage

estimate_model(data, model, min_obs = 1, output = "coefficients")

Arguments

data

A data frame containing the dependent variable and one or more independent variables.

model

A character that describes the model to be estimated (e.g., "ret_excess ~ mkt_excess + hml + smb").

min_obs

The minimum number of observations required to estimate the model. Defaults to 1.

output

A character vector specifying what to return. Must contain one or more of "coefficients" (default), "residuals", and "tstats". If a single value is provided, the corresponding object is returned directly. If multiple values are provided, a named list is returned.

Value

If output contains a single value: a data frame of coefficients or t-statistics, or a numeric vector of residuals. If output contains multiple values: a named list with the requested elements. Coefficients and t-statistics are returned as data frames with column names corresponding to the model terms. Residuals are returned as a numeric vector of length nrow(data) with NA for rows with missing data or insufficient observations.

See Also

Other estimation functions: estimate_betas(), estimate_fama_macbeth()

Examples

set.seed(42)
data <- data.frame(
  ret_excess = rnorm(100),
  mkt_excess = rnorm(100),
  smb = rnorm(100),
  hml = rnorm(100)
)

# Estimate model with a single independent variable
estimate_model(data, "ret_excess ~ mkt_excess")

# Estimate model with multiple independent variables
estimate_model(data, "ret_excess ~ mkt_excess + smb + hml")

# Estimate model without intercept
estimate_model(data, "ret_excess ~ mkt_excess - 1")

# Calculate residuals
estimate_model(data, "ret_excess ~ mkt_excess + smb + hml",
  output = "residuals"
)

# Return t-statistics
estimate_model(data, "ret_excess ~ mkt_excess + smb + hml",
  output = "tstats"
)

# Return coefficients, t-statistics, and residuals
estimate_model(data, "ret_excess ~ mkt_excess + smb + hml",
  output = c("coefficients", "tstats", "residuals")
)


tidyfinance documentation built on June 1, 2026, 1:06 a.m.