
Robust / heteroskedasticity-consistent variance for a fitted model
Source:R/causal.R
morie_causal_robust_se.RdThin wrapper around sandwich variance estimators. Returns the requested variance-covariance matrix and the corresponding robust standard errors. Supports HC0-HC5 (cross-section), HAC (time-series), and clustered (one-way) variance.
Arguments
- model
A fitted model object (typically from
stats::lmorstats::glm) compatible withsandwich::vcovHC(),sandwich::vcovHAC(), orsandwich::vcovCL().- type
One of
"HC0","HC1","HC2","HC3","HC4","HC4m","HC5","HAC", or"CL"(clustered). Default"HC3"(Long-Ervin small-sample default).- cluster
Optional one-sided formula or vector identifying the cluster variable (required when
type = "CL").- ...
Additional arguments forwarded to the chosen sandwich estimator.
Value
Named list with elements vcov (variance matrix),
se (named numeric vector of robust SEs), type,
and n_coef.
Details
Hard-errors if sandwich is not installed – the HC sandwich algebra is well-tested upstream and re-implementing it inline would be both lengthy and error-prone.
References
Zeileis A, Koll S, Graham N (2020). Various Versatile Variances: An Object-Oriented Implementation of Clustered Covariances in R. Journal of Statistical Software, 95(1), 1-36.
Examples
set.seed(1)
d <- data.frame(x = rnorm(30), y = rnorm(30))
m <- stats::lm(y ~ x, data = d)
n <- 80L
x <- arima.sim(model = list(ar = 0.6), n = n)
y <- as.numeric(x) + rnorm(n, sd = 0.2)
morie_causal_robust_se(m, type = "HC3")
#> $vcov
#> (Intercept) x
#> (Intercept) 0.02518345 -0.01119561
#> x -0.01119561 0.03404757
#>
#> $se
#> (Intercept) x
#> 0.1586929 0.1845198
#>
#> $type
#> [1] "HC3"
#>
#> $n_coef
#> [1] 2
#>