Learn R Programming

tidyfinance (version 0.4.3)

estimate_model: Estimate Model Coefficients

Description

[Experimental]

This function estimates the coefficients of 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. It returns the model's coefficients as either a numeric value (for a single independent variable) or a data frame (for multiple independent variables).

Usage

estimate_model(data, model, min_obs = 1)

Value

A data frame with a row for each coefficient and column names corresponding to the independent variables.

Arguments

data

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

model

A character that describes the model to estimate (e.g. "ret_excess ~ mkt_excess + hmb + sml").

min_obs

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

See Also

stats::lm() for details on the underlying linear model fitting used.

Examples

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

Run the code above in your browser using DataLab