/
deltaMethod-delta-generic.R
162 lines (162 loc) · 3.52 KB
/
deltaMethod-delta-generic.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#' Delta Method (Generic Object Input)
#'
#' Calculates delta method sampling variance-covariance matrix
#' for a function of parameters
#' using a numerical Jacobian.
#'
#' @author Ivan Jacob Agaloos Pesigan
#'
#' @return Returns an object
#' of class `deltamethod` which is a list with the following elements:
#' \describe{
#' \item{call}{Function call.}
#' \item{args}{Function arguments.}
#' \item{coef}{Estimates.}
#' \item{vcov}{Sampling variance-covariance matrix.}
#' \item{jacobian}{Jacobian matrix.}
#' \item{fun}{Function used ("DeltaGeneric").}
#' }
#'
#' @param object R object.
#' Fitted model object with `coef` and `vcov` methods
#' that return a named vector of
#' estimated parameters and sampling variance-covariance matrix,
#' respectively.
#' @param def List of character strings.
#' A list of defined functions of parameters.
#' The string should be a valid R expression when parsed
#' and should result a single value when evaluated.
#' @param theta Numeric vector.
#' Parameter values when the null hypothesis is true.
#' @param alpha Numeric vector.
#' Significance level/s.
#' @param z Logical.
#' If `z = TRUE`,
#' use the standard normal distribution.
#' If `z = FALSE`,
#' use the t distribution.
#' @param df Numeric.
#' Degrees of freedom if `z = FALSE`.
#'
#' @examples
#' object <- glm(
#' formula = vs ~ wt + disp,
#' family = "binomial",
#' data = mtcars
#' )
#' def <- list("exp(wt)", "exp(disp)")
#' DeltaGeneric(
#' object = object,
#' def = def,
#' alpha = 0.05
#' )
#' @export
#' @family Delta Method Functions
#' @keywords deltaMethod
DeltaGeneric <- function(object,
def,
theta = 0,
alpha = c(0.05, 0.01, 0.001),
z = TRUE,
df = NULL) {
if (!z) {
if (is.null(df)) {
stop(
paste0(
"Please provide a value for the argument `df`.\n",
"Otherwise, set `z = TRUE`.\n"
)
)
}
}
args <- list(
object = object,
def = def,
theta = theta,
alpha = alpha,
z = z,
df = df
)
## function
func <- function(coef,
def) {
env <- list2env(
as.list(coef)
)
sapply(
X = def,
FUN = function(i) {
return(
eval(
parse(text = i),
envir = env
)
)
}
)
}
## identify coefficients used and do delta only for them
defs_exp <- lapply(
X = def,
FUN = function(x) {
parse(text = x)
}
)
def_vars <- unique(
unlist(
sapply(
X = defs_exp,
FUN = all.vars
)
)
)
## def to be used as names
def_vec <- def
dim(def_vec) <- NULL
coef <- stats::coef(object)[def_vars]
vcov <- stats::vcov(object)[def_vars, def_vars]
k <- length(coef)
j <- numDeriv::jacobian(
func = func,
x = coef,
def = def
)
if (k == 1) {
# univariate
vcov <- as.vector(vcov)
vcov <- matrix(
data = j^2 * vcov,
nrow = 1,
ncol = 1
)
} else {
# multivariate
vcov <- j %*% vcov %*% t(j)
}
est <- func(
coef = coef,
def = def
)
def <- do.call(
what = "rbind",
args = def
)
dim(def) <- NULL
names(est) <- def
colnames(vcov) <- rownames(vcov) <- def
out <- list(
call = match.call(),
args = args,
est = est,
vcov = vcov,
jacobian = j,
fun = "DeltaGeneric"
)
class(out) <- c(
"deltamethod",
class(out)
)
return(
out
)
}