Description Usage Arguments Details Value References Examples

View source: R/estimatr_lm_robust.R

This formula fits a linear model, provides a variety of options for robust standard errors, and conducts coefficient tests

1 2 3 4 5 6 7 8 9 10 11 12 13 |

`formula` |
an object of class formula, as in |

`data` |
A |

`weights` |
the bare (unquoted) names of the weights variable in the supplied data. |

`subset` |
An optional bare (unquoted) expression specifying a subset of observations to be used. |

`clusters` |
An optional bare (unquoted) name of the variable that corresponds to the clusters in the data. |

`fixed_effects` |
An optional right-sided formula containing the fixed
effects that will be projected out of the data, such as |

`se_type` |
The sort of standard error sought. If |

`ci` |
logical. Whether to compute and return p-values and confidence intervals, TRUE by default. |

`alpha` |
The significance level, 0.05 by default. |

`return_vcov` |
logical. Whether to return the variance-covariance matrix for later usage, TRUE by default. |

`try_cholesky` |
logical. Whether to try using a Cholesky decomposition to solve least squares instead of a QR decomposition, FALSE by default. Using a Cholesky decomposition may result in speed gains, but should only be used if users are sure their model is full-rank (i.e., there is no perfect multi-collinearity) |

This function performs linear regression and provides a variety of standard
errors. It takes a formula and data much in the same was as `lm`

does, and all auxiliary variables, such as clusters and weights, can be
passed either as quoted names of columns, as bare column names, or
as a self-contained vector. Examples of usage can be seen below and in the
Getting Started vignette.

The mathematical notes in
this vignette
specify the exact estimators used by this function.
The default variance estimators have been chosen largely in accordance with the
procedures in
this manual.
The default for the case
without clusters is the HC2 estimator and the default with clusters is the
analogous CR2 estimator. Users can easily replicate Stata standard errors in
the clustered or non-clustered case by setting ``se_type` = "stata"`

.

The function estimates the coefficients and standard errors in C++, using
the `RcppEigen`

package. By default, we estimate the coefficients
using Column-Pivoting QR decomposition from the Eigen C++ library, although
users could get faster solutions by setting ``try_cholesky` = TRUE`

to
use a Cholesky decomposition instead. This will likely result in quicker
solutions, but the algorithm does not reliably detect when there are linear
dependencies in the model and may fail silently if they exist.

If ``fixed_effects``

are specified, both the outcome and design matrix
are centered using the method of alternating projections (Halperin 1962; Gaure 2013). Specifying
fixed effects in this way will result in large speed gains with standard error
estimators that do not need to invert the matrix of fixed effects. This means using
"classical", "HC0", "HC1", "CR0", or "stata" standard errors will be faster than other
standard error estimators. Be wary when specifying fixed effects that may result
in perfect fits for some observations or if there are intersecting groups across
multiple fixed effect variables (e.g. if you specify both "year" and "country" fixed effects
with an unbalanced panel where one year you only have data for one country).

As with ``lm()``

, multivariate regression (multiple outcomes) will only admit
observations into the estimation that have no missingness on any outcome.

An object of class `"lm_robust"`

.

The post-estimation commands functions `summary`

and `tidy`

return results in a `data.frame`

. To get useful data out of the return,
you can use these data frames, you can use the resulting list directly, or
you can use the generic accessor functions `coef`

, `vcov`

,
`confint`

, and `predict`

. Marginal effects and uncertainty about
them can be gotten by passing this object to
`margins`

from the margins,
or to `emmeans`

in the emmeans package.

Users who want to print the results in TeX of HTML can use the
`extract`

function and the texreg package.

If users specify a multivariate linear regression model (multiple outcomes), then some of the below components will be of higher dimension to accommodate the additional models.

An object of class `"lm_robust"`

is a list containing at least the
following components:

`coefficients` |
the estimated coefficients |

`std.error` |
the estimated standard errors |

`statistic` |
the t-statistic |

`df` |
the estimated degrees of freedom |

`p.value` |
the p-values from a two-sided t-test using |

`conf.low` |
the lower bound of the |

`conf.high` |
the upper bound of the |

`term` |
a character vector of coefficient names |

`alpha` |
the significance level specified by the user |

`se_type` |
the standard error type specified by the user |

`res_var` |
the residual variance |

`N` |
the number of observations used |

`k` |
the number of columns in the design matrix (includes linearly dependent columns!) |

`rank` |
the rank of the fitted model |

`vcov` |
the fitted variance covariance matrix |

`r.squared` |
The
where |

`adj.r.squared` |
The |

`fstatistic` |
a vector with the value of the F-statistic with the numerator and denominator degrees of freedom |

`weighted` |
whether or not weights were applied |

`call` |
the original function call |

`fitted.values` |
the matrix of predicted means |

We also return `terms`

and `contrasts`

, used by `predict`

. If `fixed_effects`

are specified, then we return `proj_fstatistic`

, `proj_r.squared`

, and `proj_adj.r.squared`

, which are model fit statistics that are computed on the projected model (after demeaning the fixed effects).

Abadie, Alberto, Susan Athey, Guido W Imbens, and Jeffrey Wooldridge. 2017. "A Class of Unbiased Estimators of the Average Treatment Effect in Randomized Experiments." arXiv Pre-Print. https://arxiv.org/abs/1710.02926v2.

Bell, Robert M, and Daniel F McCaffrey. 2002. "Bias Reduction in Standard Errors for Linear Regression with Multi-Stage Samples." Survey Methodology 28 (2): 169-82.

Gaure, Simon. 2013. "OLS with multiple high dimensional category variables." Computational Statistics \& Data Analysis 66: 8-1. doi: 10.1016/j.csda.2013.03.024

Halperin, I. 1962. "The product of projection operators." Acta Scientiarum Mathematicarum (Szeged) 23(1-2): 96-99.

MacKinnon, James, and Halbert White. 1985. "Some Heteroskedasticity-Consistent Covariance Matrix Estimators with Improved Finite Sample Properties." Journal of Econometrics 29 (3): 305-25. doi: 10.1016/0304-4076(85)90158-7.

Pustejovsky, James E, and Elizabeth Tipton. 2016. "Small Sample Methods for Cluster-Robust Variance Estimation and Hypothesis Testing in Fixed Effects Models." Journal of Business & Economic Statistics. Taylor & Francis. doi: 10.1080/07350015.2016.1247004.

Samii, Cyrus, and Peter M Aronow. 2012. "On Equivalencies Between Design-Based and Regression-Based Variance Estimators for Randomized Experiments." Statistics and Probability Letters 82 (2). doi: 10.1016/j.spl.2011.10.024.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | ```
library(fabricatr)
dat <- fabricate(
N = 40,
y = rpois(N, lambda = 4),
x = rnorm(N),
z = rbinom(N, 1, prob = 0.4)
)
# Default variance estimator is HC2 robust standard errors
lmro <- lm_robust(y ~ x + z, data = dat)
# Can tidy() the data in to a data.frame
tidy(lmro)
# Can use summary() to get more statistics
summary(lmro)
# Can also get coefficients three ways
lmro$coefficients
coef(lmro)
tidy(lmro)$estimate
# Can also get confidence intervals from object or with new 1 - `alpha`
lmro$conf.low
confint(lmro, level = 0.8)
# Can recover classical standard errors
lmclassic <- lm_robust(y ~ x + z, data = dat, se_type = "classical")
tidy(lmclassic)
# Can easily match Stata's robust standard errors
lmstata <- lm_robust(y ~ x + z, data = dat, se_type = "stata")
tidy(lmstata)
# Easy to specify clusters for cluster-robust inference
dat$clusterID <- sample(1:10, size = 40, replace = TRUE)
lmclust <- lm_robust(y ~ x + z, data = dat, clusters = clusterID)
tidy(lmclust)
# Can also match Stata's clustered standard errors
lm_robust(
y ~ x + z,
data = dat,
clusters = clusterID,
se_type = "stata"
)
# Works just as LM does with functions in the formula
dat$blockID <- rep(c("A", "B", "C", "D"), each = 10)
lm_robust(y ~ x + z + factor(blockID), data = dat)
# Weights are also easily specified
dat$w <- runif(40)
lm_robust(
y ~ x + z,
data = dat,
weights = w,
clusters = clusterID
)
# Subsetting works just as in `lm()`
lm_robust(y ~ x, data = dat, subset = z == 1)
# One can also choose to set the significance level for different CIs
lm_robust(y ~ x + z, data = dat, alpha = 0.1)
# We can also specify fixed effects
# Speed gains with fixed effects are greatests with "stata" or "HC1" std.errors
tidy(lm_robust(y ~ x + z, data = dat, fixed_effects = ~ blockID, se_type = "HC1"))
## Not run:
# Can also use 'margins' or 'emmeans' package if you have them installed
# to get marginal effects
library(margins)
lmrout <- lm_robust(y ~ x + z, data = dat)
summary(margins(lmrout))
# Can output results using 'texreg'
library(texreg)
texreg(lmrout)
# Using emmeans to obtain covariate-adjusted means
library(emmeans)
fiber.rlm <- lm_robust(strength ~ diameter + machine, data = fiber)
emmeans(fiber.rlm, "machine")
## End(Not run)
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.