Debian Codegraph @

R/S3-methods.R

raw
1 #----------------------------------------------
2 # get "working" variance-covariance matrix
3 #----------------------------------------------
4
5 targetVariance <- function(obj, cluster) UseMethod("targetVariance")
6
7 #' @export
8
9 targetVariance.default <- function(obj, cluster) {
10 matrix_list(rep(1, length(cluster)), cluster, "both")
11 }
12
13 #----------------------------------------------
14 # get weighting matrix
15 #----------------------------------------------
16
17 weightMatrix <- function(obj, cluster) UseMethod("weightMatrix")
18
19 #' @export
20
21 weightMatrix.default <- function(obj, cluster) {
22 weights <- weights(obj)
23 if (is.null(weights)) {
24 weights <- w_scale <- 1
25 } else {
26 weights <- weights[weights > 0]
27 w_scale <- mean(weights)
28 weights <- weights / w_scale
29 }
30 W <- rep(weights, length.out = length(cluster))
31 W_list <- matrix_list(W, cluster, "both")
32 attr(W_list, "w_scale") <- w_scale
33 W_list
34 }
35
36 #----------------------------------------------
37 # get X matrix
38 #----------------------------------------------
39
40 model_matrix <- function(obj) UseMethod("model_matrix")
41
42 #' @export
43
44 model_matrix.default <- function(obj) {
45 model_matrix <- model.matrix(obj)
46
47 w <- obj$weights
48 if (is.null(w) || all(pos_wts <- w > 0)) {
49 return(model_matrix)
50 } else {
51 return(model_matrix[pos_wts > 0,,drop=FALSE])
52 }
53 }
54
55 #----------------------------------------------
56 # get augmented design matrix
57 #----------------------------------------------
58
59 augmented_model_matrix <- function(obj, cluster, inverse_var, ignore_FE) UseMethod("augmented_model_matrix")
60
61 #' @export
62
63 augmented_model_matrix.default <- function(obj, cluster, inverse_var, ignore_FE) {
64 NULL
65 }
66
67 #----------------------------------------------
68 # get residuals
69 #----------------------------------------------
70
71 residuals_CS <- function(obj) UseMethod("residuals_CS")
72
73 #' @export
74
75 residuals_CS.default <- function(obj) {
76 w <- obj$weights
77 if (is.null(w) || all(pos_wts <- w > 0)) {
78 residuals(obj)
79 } else {
80 residuals(obj)[pos_wts]
81 }
82 }
83
84 #----------------------------------------------
85 # get coefficient estimates
86 #----------------------------------------------
87
88 coef_CS <- function(obj) UseMethod("coef_CS")
89
90 #' @export
91
92 coef_CS.default <- function(obj) {
93 coef(obj)
94 }
95
96 #----------------------------------------------
97 # get bread matrix
98 #----------------------------------------------
99
100 # bread matrices imported from sandwich package or elsewhere
101 #' @importFrom sandwich bread
102
103 get_bread <- function(obj) bread(obj)
104
105 v_scale <- function(obj) UseMethod("v_scale")
106
107 #' @export
108
109 v_scale.default <- function(obj) {
110 nobs(obj)
111 }