Skip to content

Instantly share code, notes, and snippets.

@BioSciEconomist
Created November 14, 2021 22:27
Show Gist options
  • Save BioSciEconomist/b674d323b867b0da4483dfe97ed117ea to your computer and use it in GitHub Desktop.
Save BioSciEconomist/b674d323b867b0da4483dfe97ed117ea to your computer and use it in GitHub Desktop.
Example calculations of VIFs and robust SEs
# *-----------------------------------------------------------------
# | 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