Debian Codegraph @

R/glm.R

raw
1 #-------------------------------------
2 # vcovCR with defaults
3 #-------------------------------------
4
5 #' Cluster-robust variance-covariance matrix for a glm object.
6 #'
7 #' \code{vcovCR} returns a sandwich estimate of the variance-covariance matrix
8 #' of a set of regression coefficient estimates from an \code{\link{glm}} object.
9 #'
10 #' @param cluster Expression or vector indicating which observations belong to
11 #' the same cluster. Required for \code{glm} objects.
12 #' @param target Optional matrix or vector describing the working
13 #' variance-covariance model used to calculate the \code{CR2} and \code{CR4}
14 #' adjustment matrices. If a vector, the target matrix is assumed to be
15 #' diagonal. If not specified, the target is taken to be the estimated variance function.
16 #' @inheritParams vcovCR
17 #'
18 #' @return An object of class \code{c("vcovCR","clubSandwich")}, which consists
19 #' of a matrix of the estimated variance of and covariances between the
20 #' regression coefficient estimates.
21 #'
22 #' @seealso \code{\link{vcovCR}}
23 #'
24 #' @examples
25 #'
26 #' if (requireNamespace("geepack", quietly = TRUE)) {
27 #'
28 #' data(dietox, package = "geepack")
29 #' dietox$Cu <- as.factor(dietox$Cu)
30 #' weight_fit <- glm(Weight ~ Cu * poly(Time, 3), data=dietox, family = "quasipoisson")
31 #' V_CR <- vcovCR(weight_fit, cluster = dietox$Pig, type = "CR2")
32 #' coef_test(weight_fit, vcov = V_CR, test = "Satterthwaite")
33 #'
34 #' }
35 #'
36 #' @export
37
38 vcovCR.glm <- function(obj, cluster, type, target = NULL, inverse_var = NULL, form = "sandwich", ...) {
39 if (missing(cluster)) stop("You must specify a clustering variable.")
40 if (is.null(inverse_var)) inverse_var <- is.null(target)
41 vcov_CR(obj, cluster = cluster, type = type,
42 target = target, inverse_var = inverse_var, form = form)
43 }
44
45 # coef()
46 # nobs()
47
48 #-----------------------------------------------
49 # Model matrix
50 #-----------------------------------------------
51
52 #' @export
53
54 model_matrix.glm <- function(obj) {
55 X <- model.matrix(obj)
56 eta <- obj$linear.predictors
57 dmu_deta <- obj$family$mu.eta
58 d <- dmu_deta(eta)
59 d * X
60 }
61
62 #-------------------------------------
63 # residuals
64 #-------------------------------------
65
66 #' @export
67
68 residuals_CS.glm <- function(obj) {
69 residuals(obj, type = "response")
70 }
71
72 #-----------------------------------------------
73 # Get (model-based) working variance matrix
74 #-----------------------------------------------
75
76 #' @export
77
78 targetVariance.glm <- function(obj, cluster) {
79 mu <- fitted.values(obj)
80 var_fun <- obj$family$variance
81 v <- var_fun(mu)
82 w <- weights(obj, type = "prior")
83 matrix_list(v / w, cluster, "both")
84 }
85
86 #-------------------------------------
87 # Get weighting matrix
88 #-------------------------------------
89
90 #' @export
91
92 weightMatrix.glm <- function(obj, cluster) {
93 mu <- fitted.values(obj)
94 var_fun <- obj$family$variance
95 v <- var_fun(mu)
96 w <- weights(obj, type = "prior")
97 matrix_list(w / v, cluster, "both")
98 }
99
100 #---------------------------------------
101 # Get bread matrix and scaling constant
102 #---------------------------------------
103
104 # bread.glm() is in sandwich package
105
106 #' @export
107
108 v_scale.glm <- function(obj) {
109 if (substr(obj$family$family, 1, 17) %in% c("poisson", "binomial", "Negative Binomial")) {
110 dispersion <- 1
111 } else {
112 wres <- as.vector(residuals(obj, "working")) * weights(obj, "working")
113 dispersion <- sum(wres^2)/sum(weights(obj, "working"))
114 }
115 as.vector(sum(summary(obj)$df[1:2])) * dispersion
116 }