if(!requireNamespace("sandwich")) {
if(interactive() || is.na(Sys.getenv("_R_CHECK_PACKAGE_NAME_", NA))) {
stop("not all packages required for the example are installed")
} else q() }
## data and model
data("CigaretteDemand", package = "ivreg")
m <- ivreg(log(packs) ~ log(rincome) | log(rprice) | salestax, data = CigaretteDemand)
## summary including diagnostics
summary(m)
## replicate global F test from summary (against null model) "by hand"
m0 <- ivreg(log(packs) ~ 1, data = CigaretteDemand)
anova(m0, m)
## or via linear hypothesis test
car::linearHypothesis(m, c("log(rincome)", "log(rprice)"))
## confidence intervals
confint(m)
## just the Wald tests for the coefficients
library("lmtest")
coeftest(m)
## plug in a heteroscedasticity-consistent HC1 covariance matrix (from sandwich)
library("sandwich")
## - as a function passing additional type argument through ...
coeftest(m, vcov = vcovHC, type = "HC1")
## - as a function without additional arguments
hc1 <- function(object, ...) vcovHC(object, type = "HC1", ...)
coeftest(m, vcov = hc1)
## - as a matrix
vc1 <- vcovHC(m, type = "HC1")
coeftest(m, vcov = vc1)
## in summary() with diagnostics = TRUE use one of the function specifications,
## the matrix is only possible when diagnostics = FALSE
summary(m, vcov = vcovHC, type = "HC1") ## function + ...
summary(m, vcov = hc1) ## function
summary(m, vcov = vc1, diagnostics = FALSE) ## matrix
## in confint() and anova() any of the three specifications can be used
anova(m0, m, vcov = vcovHC, type = "HC1") ## function + ...
anova(m0, m, vcov = hc1) ## function
anova(m0, m, vcov = vc1) ## matrix
Run the code above in your browser using DataLab