-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary.rd.R
201 lines (177 loc) · 6.69 KB
/
summary.rd.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#' Summarize the Regression Discontinuity
#'
#' \code{summary.rd} is a \code{summary} method for class \code{"rd"}
#' It is based on \code{summary.RD} function in the "rdd" package.
#'
#' @method summary rd
#'
#' @param object An object of class \code{"rd"}, usually a result of a call to \code{\link{rd_est}}.
#' @param level A numeric value between 0 and 1 specifying the confidence level for confidence intervals. The default is 0.95.
#' @param digits A non-negative integer specifying the number of digits to display.
#' The default is \code{max(3, getOption("digits") - 3)}.
#' @param ... Additional arguments passed to \code{summary}..
#'
#' @return \code{summary.rd} returns a list which has the following components:
#' \item{coefficients}{A matrix containing bandwidths, number of observations, estimates,
#' SEs, confidence intervals, z-values and p-values for each estimated bandwidth.}
#'
#' @references Drew Dimmery (2016). rdd: Regression Discontinuity Estimation. R package
#' version 0.57. https://CRAN.R-project.org/package=rdd
#'
#' @importFrom stats residuals
#'
#' @include rd_est.R
#'
#' @export
summary.rd <- function(object, level = 0.95, digits = max(3, getOption("digits") - 3), ...) {
call.copy <- object$call
if ("data" %in% names(call.copy) && length(call.copy$data) > 1) {
call.copy$data <- "(.)"
}
if ("subset" %in% names(call.copy) && length(call.copy$subset) > 1) {
call.copy$subset <- "(.)"
}
cat("\n")
cat("Call:\n")
# print(object$call)
print(call.copy)
cat("\n")
if (is.null(object$call$est.itt)){
est.itt = 'FALSE'
}else{
if (object$call$est.itt == 'T' || object$call$est.itt == 'TRUE'){
est.itt = 'TRUE'
}else{
est.itt = 'FALSE'
}
}
cat("ITT used:\n")
cat(est.itt)
cat("\n\n")
cat("Type:\n")
cat(object$type, "\n\n")
# If the model wasn't included in the output, we need to get it
# mod <- FALSE
# if ("model" %in% names(object$call))
# mod <- object$call$model
# if (mod == 'F' || !mod) {
# object$call$model <- TRUE
# object$call$verbose <- FALSE
# object <- eval.parent(object$call)
# }
# if ("est.cov" %in% names(object$call) && eval(object$call$est.cov)) {
# k <- 1 length(object$call$cov)
# } else {
# k <- 1
# }
m <- length(object$est)
n <- length(object$obs)
k <- m / n
obs <- vector(length = n)
if (object$type == "sharp") {
for (i in 1:n) obs[i] <- length(residuals(object$model[[i]]))
} else {
for (i in 1:n) obs[i] <- length(residuals(object$model$iv[[i]]))
}
# Need to get this to give at least as much as stata does in fuzzy designs
stars <- vector(length = n)
for (i in 1:n) {
stars[i] <- if (is.na(object$p[i]))
" " else if (object$p[i] < 0.001)
"***" else if (object$p[i] < 0.01)
"**" else if (object$p[i] < 0.05)
"*" else if (object$p[i] < 0.1)
"." else " "
}
# out <- matrix(c(rep(object$bw, each = k), rep(object$obs, each = k),
# object$est, object$se, object$z, object$p), nrow = m)
# rownames(out) <- names(object$est)
alpha <- 1 - level
lower.CL <- object$est - qnorm(1-alpha/2) * object$se
upper.CL <- object$est qnorm(1-alpha/2) * object$se
if (object$impute) {
out <- matrix(c(rep(object$bw, each = k), rep(object$obs, each = k),
object$est, object$se, lower.CL, upper.CL, object$z, object$df, object$p), nrow = m)
colnames(out) <- c("Bandwidth", "Observations", "Estimate", "Std. Error",
"lower.CL", "upper.CL",
"t value", "df", "Pr(>|t|)")
} else {
out <- matrix(c(rep(object$bw, each = k), rep(object$obs, each = k),
object$est, object$se, lower.CL, upper.CL, object$z, object$p), nrow = m)
colnames(out) <- c("Bandwidth", "Observations", "Estimate", "Std. Error",
"lower.CL", "upper.CL",
"z value", "Pr(>|z|)")
}
rownames(out) <- names(object$est)
# if there are estimates of covariates, display in a separate table
splitcov = FALSE
if (!is.null(object$cov) && !is.null(object$call$est.cov)){
if (object$call$est.cov == 'T' || object$call$est.cov == 'TRUE'){
splitcov = TRUE
}
}
outmat = cbind(apply(out, 2, function(x) format(x, digits = digits)), " " = stars)
if (splitcov){
outmat.est = outmat[seq(1, nrow(outmat), by = 2),]
outmat.cov = outmat[seq(2, nrow(outmat), by = 2),]
out = list(out[seq(1, nrow(out), by = 2),], out[seq(2, nrow(out), by = 2),])
cat("Estimates:\n")
print.default(outmat.est, quote = FALSE, print.gap = 2, right = FALSE)
cat("---\n")
cat("Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\n")
cat("Estimates of covariates:\n")
print.default(outmat.cov, quote = FALSE, print.gap = 2, right = FALSE)
cat("---\n")
cat("Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\n")
}else{
cat("Estimates:\n")
print.default(outmat, quote = FALSE, print.gap = 2, right = FALSE)
cat("---\n")
cat("Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\n")
}
cat("Confidence interval used: ", level, "\n\n")
out <- list(coefficients = out)
# class(out) <- "summary.rd"
# fstat <- matrix(NA, nrow = n, ncol = 4)
# if (object$type == "sharp") {
# for (i in 1:n) {
# fstat[i, ] <- tryCatch({
# summ <- summary(object$model[[i]])
# c(summ$fstatistic[[1]],
# summ$fstatistic[[2]],
# summ$fstatistic[[3]],
# pf(summ$fstatistic[[1]],
# summ$fstatistic[[2]],
# summ$fstatistic[[3]]))
# },
# error = function(e) {
# rep(NA, 4)
# }
# )
# }
# # use two-sided f test to calculate the p-value,
# # which can be used for testing equality of variance
# fstat[, 4] <- 2 * apply(cbind(fstat[, 4], 1 - fstat[, 4]), 1, min)
# } else {
# for (i in 1:n) {
# fstat[i, ] <- tryCatch({
# summ <- summary(object$model$iv[[i]])
# c(summ$waldtest[1],
# summ$waldtest[3],
# summ$waldtest[4],
# summ$waldtest[2])
# },
# error = function(e) {
# rep(NA, 4)
# }
# )
# }
# }
# colnames(fstat) <- c("F", "Num. DoF", "Denom. DoF", "p")
# rownames(fstat) <- names(object$bw)
# cat("F-statistics:\n")
# print.default(apply(fstat, 2, function(x) format(x, digits = digits)), quote = FALSE,
# print.gap = 2, right = FALSE)
# out$fstat <- fstat
return(invisible(out))
}