Debian Codegraph @

R/ivreg.R

raw
1 #-------------------------------------
2 # vcovCR with defaults
3 #-------------------------------------
4
5 #' Cluster-robust variance-covariance matrix for an ivreg object.
6 #'
7 #' \code{vcovCR} returns a sandwich estimate of the variance-covariance matrix
8 #' of a set of regression coefficient estimates from an ivreg object fitted
9 #' from the \CRANpkg{AER} package or the \CRANpkg{ivreg} package.
10 #'
11 #' @param cluster Expression or vector indicating which observations belong to
12 #' the same cluster. Required for \code{ivreg} objects.
13 #' @param target Optional matrix or vector describing the working
14 #' variance-covariance model used to calculate the \code{CR2} and \code{CR4}
15 #' adjustment matrices. If a vector, the target matrix is assumed to be
16 #' diagonal. If not specified, the target is taken to be an identity matrix.
17 #' @param inverse_var Not used for \code{ivreg} objects.
18 #' @inheritParams vcovCR
19 #'
20 #' @details For any "ivreg" objects fitted via the \code{\link[ivreg]{ivreg}}
21 #' function from the \CRANpkg{ivreg} package, only traditional 2SLS
22 #' regression method (method = "OLS") is supported.
23 #' clubSandwich currently cannot support robust-regression methods such as
24 #' M-estimation (method = "M") or MM-estimation (method = "MM").
25 #'
26 #' @return An object of class \code{c("vcovCR","clubSandwich")}, which consists
27 #' of a matrix of the estimated variance of and covariances between the
28 #' regression coefficient estimates.
29 #'
30 #' @seealso \code{\link{vcovCR}}
31 #'
32 #' @examples
33 #'
34 #' if (requireNamespace("AER", quietly = TRUE)) withAutoprint({
35 #'
36 #' library(AER)
37 #' data("CigarettesSW")
38 #' Cigs <- within(CigarettesSW, {
39 #' rprice <- price/cpi
40 #' rincome <- income/population/cpi
41 #' tdiff <- (taxs - tax)/cpi
42 #' })
43 #'
44 #' iv_fit_AER <- AER::ivreg(log(packs) ~ log(rprice) + log(rincome) |
45 #' log(rincome) + tdiff + I(tax/cpi), data = Cigs)
46 #' vcovCR(iv_fit_AER, cluster = Cigs$state, type = "CR2")
47 #' coef_test(iv_fit_AER, vcov = "CR2", cluster = Cigs$state)
48 #'
49 #' })
50 #'
51 #' pkgs_available <-
52 #' requireNamespace("AER", quietly = TRUE) &
53 #' requireNamespace("ivreg", quietly = TRUE)
54 #'
55 #' if (pkgs_available) withAutoprint ({
56 #'
57 #' data("CigarettesSW")
58 #' Cigs <- within(CigarettesSW, {
59 #' rprice <- price/cpi
60 #' rincome <- income/population/cpi
61 #' tdiff <- (taxs - tax)/cpi
62 #' })
63 #' iv_fit_ivreg <- ivreg::ivreg(log(packs) ~ log(rprice) + log(rincome) |
64 #' log(rincome) + tdiff + I(tax/cpi), data = Cigs)
65 #' vcovCR(iv_fit_ivreg, cluster = Cigs$state, type = "CR2")
66 #' coef_test(iv_fit_ivreg, vcov = "CR2", cluster = Cigs$state)
67 #' })
68 #'
69 #' @export
70
71 vcovCR.ivreg <- function(obj, cluster, type, target = NULL, inverse_var = FALSE, form = "sandwich", ...) {
72 if (missing(cluster)) stop("You must specify a clustering variable.")
73 if (inverse_var != FALSE) stop("Unfortunately, the inverse_var option is not available for ivreg models.")
74 if (!is.null(obj$method) && obj$method %in% c("M", "MM")) stop("clubSandwich does not currently support ivreg models estimated using method = 'M' or method = 'MM'.")
75 vcov_CR(obj, cluster = cluster, type = type,
76 target = target, inverse_var = inverse_var, form = form)
77 }
78
79 # residuals_CS()
80 # coef()
81 # targetVariance()
82 # weightMatrix()
83 # v_scale()
84
85 #----------------------------------------------
86 # get X matrix
87 #----------------------------------------------
88
89 #' @export
90
91 model_matrix.ivreg <- function(obj) {
92 model_matrix <- model.matrix(obj, component = "projected")
93
94 w <- obj$weights
95 if (is.null(w) || all(pos_wts <- w > 0)) {
96 return(model_matrix)
97 } else {
98 return(model_matrix[pos_wts > 0,,drop=FALSE])
99 }
100
101 }
102
103 #---------------------------------------
104 # Get bread matrix and scaling constant
105 #---------------------------------------
106
107 # bread.ivreg() is in AER package
108 # use default v_scale()