Reputation: 63
I have run 3 regressions with rdrobust, and would like to print some of the values (not all of them in a table, so that each model has a column and the results can be compared side by side.
I tried with stargazer but with no success, same thing goes for modelsummary.
This is what my regression code looks like:
model <- rdrobust::rdrobust(x,
y,
c = cutoffvalue,
kernel = "tri", #default
bwselect = "mserd"
And I'd like to show only the regression estimate, values, bandwidth and kernel in the table.
This is what I tried, but it doesn't give me the values that I want, and also i'ts for one model only. I'd like to have all 3 in the same table.
tidy.rdrobust <- function(model, ...){
ret <- data.frame(term = row.names(model$coef),
estimate = model$coef[, 1],
std.error = model$se[, 1],
p.value = model$pv[, 1])
row.names(ret) <- NULL
ret
}
glance.rdrobust <- function(model, ...){
ret <- data.frame(nobs.left = model$N[1],
kernel = model$kernel,
bwselect = model$bwselect)
ret
}
x <- runif(1000, -1, 1)
y <- 5 + 3 * x + 2 * (x >= 0) + rnorm(1000)
fit <- rdrobust(y, x)
modelsummary(fit)
thanks!
Upvotes: 1
Views: 1659
Reputation: 17725
I asked for clarification in a comment, but here is my best attempt at guessing what you want in the table:
To achieve this, I only modified your glance.rdrobust
method, and I used the statistic
argument of the modelsummary
function.
Load libraries and define custom tidy
and glance
methods to extract information (see documentation) from rdrobust
objects:
library(rdrobust)
library(modelsummary)
tidy.rdrobust <- function(model, ...) {
ret <- data.frame(
term = row.names(model$coef),
estimate = model$coef[, 1],
std.error = model$se[, 1],
p.value = model$pv[, 1]
)
row.names(ret) <- NULL
ret
}
glance.rdrobust <- function(model, ...) {
ret <- data.frame(
Kernel = model$kernel,
Bandwidth = model$bwselect
)
ret
}
Simulate data, estimate 3 models, and store them in a list:
x1 <- runif(1000, -1, 1)
x2 <- runif(1000, -1, 1)
x3 <- runif(1000, -1, 1)
y1 <- 5 + 3 * x1 + 2 * (x1 >= 0) + rnorm(1000)
y2 <- 5 + 3 * x2 + 2 * (x2 >= 0) + rnorm(1000)
y3 <- 5 + 3 * x3 + 2 * (x3 >= 0) + rnorm(1000)
fit1 <- rdrobust(y1, x1)
fit2 <- rdrobust(y2, x2)
fit3 <- rdrobust(y3, x3)
models <- list(fit1, fit2, fit3)
Create a table:
modelsummary(models, statistic = "p.value")
Model 1 | Model 2 | Model 3 | |
---|---|---|---|
Conventional | 2.155 | 2.085 | 2.050 |
(0.000) | (0.000) | (0.000) | |
Bias-Corrected | 2.110 | 2.110 | 1.984 |
(0.000) | (0.000) | (0.000) | |
Robust | 2.110 | 2.110 | 1.984 |
(0.000) | (0.000) | (0.000) | |
Bandwidth | mserd | mserd | mserd |
Kernel | Triangular | Triangular | Triangular |
Upvotes: 2