1

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!

Vincent
  • 15,809
  • 7
  • 37
  • 39
dilly
  • 63
  • 1
  • 7
  • Can you please clarify the question? What *specific* information do you want in the table? What "values" do you want, specifically? When you say "bandwidth", is that something like `mserd`? Do you want to include p values or to omit uncertainty estimates altogether? To summarize multiple models side by side in `modelsummary`, you can just put all your models in a list. See the package documentation. – Vincent Jun 03 '21 at 14:55

1 Answers1

2

I asked for clarification in a comment, but here is my best attempt at guessing what you want in the table:

  1. Multiple models side-by-side
  2. Model estimates
  3. p values in parentheses below estimates
  4. Kernel type at the bottom of hte table
  5. Bandwidth selection at the bottom of 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
Vincent
  • 15,809
  • 7
  • 37
  • 39
  • thanks so much! This is what I'm looking for, with the only difference that in the first column I would also like the standard error to show up. Also, I'd only want the robust estimate and not the conventional and bias-corrected ones. Do you have any leads on how I could do this? – dilly Jun 03 '21 at 15:58
  • Also, for the three models (let's call them M1, M2 and M3), which I have already estimated with rdrobust: how exactly do I include them in a list? I don't really get the bit of code where you use the xs and ys (where you wrote "Simulate data, estimate 3 models, and store them in a list:"). thanks! – dilly Jun 03 '21 at 16:02
  • 1
    The relevant line of code to "include models in a list" is `models <- list(fit1, fit2, fit3)` – Vincent Jun 03 '21 at 16:03
  • 1
    To choose whether to display standard errors or p.values or estimates, you can use the `estimate` and `statistic` arguments in the `modelsummary` function. Both arguments are very well documented with lots of additional examples on the website. – Vincent Jun 03 '21 at 16:05
  • 1
    To omit rows, look at the `coef_omit` and `gof_omit` arguments in `modelsummary`. Again, this is well-documented and there are many examples on the website. – Vincent Jun 03 '21 at 16:05
  • 1
    Glad it works! Feel free to accept the answer if it answers your needs (no pressure). – Vincent Jun 03 '21 at 17:18
  • I just did:) also have another follow up question: is there any way I can display the number of observations? when running the get_estimates() function, the number of obvs doesn't come out as an option – dilly Jun 03 '21 at 19:16
  • 1
    this is in `glance.rdrobust`. You initial function actually did show the number of observations, but I removed it because you did not mention wanting that in your initial question. – Vincent Jun 03 '21 at 19:39