View source: R/estimate_model.R
| estimate_model | R Documentation |
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.
estimate_model(data, model, min_obs = 1, output = "coefficients")
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.,
|
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 |
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.
Other estimation functions:
estimate_betas(),
estimate_fama_macbeth()
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")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.