R/lm_robust.R

Defines functions .robustSE .clusterSE

Documented in .clusterSE .robustSE

#' Calculating Heteroskedasticity-robust Standard Errors
#' 
#' @param X model matrix
#' @param e residual vector
#' @param type type of the sandwich estimator. Must be one of HC0, HC1, HC2, HC3, or HC4.
#' @details The different types of robust estimators differ in their degrees-of-freedom corrections for finite sample bias. See vignette for a detailed description.
#' @return returns heteroskedasticity robust variance-covariance matrix
#' @export
.robustSE = function(X, e, type = "HC1") {
    
    # get type of vcov
    type = match.arg(type, paste0("HC", 0:4), several.ok = FALSE)
    
    switch(type,
           HC0 = robust_HC0(X, e),
           HC1 = robust_HC1(X, e),
           HC2 = robust_HC2(X, e),
           HC3 = robust_HC3(X, e),
           HC4 = robust_HC4(X, e)
    )
    
}
    
#' Calculating Cluster-robust Standard Errors
#' 
#' @param X model matrix
#' @param e residual vector
#' @param clust integer vector indicating cluster membership
#' @param type type of the sandwich estimator. Must be one of HC0 or HC1.
#' @return returns cluster-robust variance-covariance matrix
#' @export
.clusterSE = function(X, e, clust, type = "HC1") {
    
    # get type of vcov
    type = match.arg(type, paste0("HC", 0:1), several.ok = FALSE)
    
    switch(type,
           HC0 = cluster_HC0(X, e, clust),
           HC1 = cluster_HC1(X, e, clust)
    )
    
}
    
    
baruuum/jars documentation built on Nov. 3, 2019, 2:06 p.m.