Created
November 14, 2021 22:27
-
-
Save BioSciEconomist/b674d323b867b0da4483dfe97ed117ea to your computer and use it in GitHub Desktop.
Example calculations of VIFs and robust SEs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *----------------------------------------------------------------- | |
# | PROGRAM NAME: ex basic econometrics.R | |
# | DATE: 11/14/21 | |
# | CREATED BY: MATT BOGARD | |
# | PROJECT FILE: | |
# *---------------------------------------------------------------- | |
# | PURPOSE: example calculate VIFs and robust SEs | |
# *---------------------------------------------------------------- | |
library(car) | |
df = read.csv("/Users/mattbogard/Google Drive/Data/lalonde.csv") | |
names(df) | |
# | |
# vif | |
# | |
vif(lm(treat ~ age + nodegree + re74 + re75, data=df)) | |
# | |
# robust standard errors | |
# | |
# https://stats.stackexchange.com/questions/117052/replicating-statas-robust-option-in-r | |
library(foreign) | |
library(sandwich) | |
library(lmtest) | |
model <- lm(treat ~ age + nodegree + re74 + re75, data=df) | |
summary(model) #non-robust | |
# check that "sandwich" returns HC0 | |
coeftest(model, vcov = sandwich) # robust; sandwich | |
coeftest(model, vcov = vcovHC(model, "HC0")) # robust; HC0 | |
# check that the default robust var-cov matrix is HC3 | |
coeftest(model, vcov = vcovHC(model)) # robust; HC3 | |
coeftest(model, vcov = vcovHC(model, "HC3")) # robust; HC3 (default) | |
# reproduce the Stata default | |
coeftest(model, vcov = vcovHC(model, "HC1")) # robust; HC1 (Stata default) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment