rlatexoutputregressionmodelsummary

Export latex table from summary of rddensity function in R


I am interested in exporting the summary of the rddensity function in latex in R. I've tried to use modelsummary without sucess, and capturing output also hasn't worked out. My objective is to export the summary() when I analyze the estimations from rddensity() function.

The example code is here:

library(rdrobust)
library(rddensity)
set.seed(42)
x <- rnorm(2000, mean = -0.5)
rdd <- rddensity(X = x, vce = "jackknife")
summary(rdd)

And the output I want to convert into a table (and desirable in a latex file) is shown here:

Manipulation testing using local polynomial density estimation.

Number of obs =       2896
Model =               unrestricted
Kernel =              triangular
BW method =           estimated
VCE method =          jackknife

c = 41                Left of c           Right of c          
Number of obs         1913                983                 
Eff. Number of obs    589                 549                 
Order est. (p)        2                   2                   
Order bias (q)        3                   3                   
BW est. (h)           5.572               5.994               

Method                T                   P > |T|             
Robust                0.8779              0.38                


P-values of binomial tests (H0: p=0.5).

Window Length / 2          <c     >=c    P>|T|
0.300                      31      22    0.2717
0.600                      58      37    0.0396
0.900                      89      63    0.0422
1.200                     123      96    0.0787
1.500                     151     139    0.5184
1.800                     177     184    0.7522
2.100                     197     219    0.3032
2.400                     225     243    0.4320
2.700                     254     265    0.6607
3.000                     291     287    0.9007

So I would like to have the previous information generated in a latex file.

Thanks.


Solution

  • You are going to have to do some manual wrangling. For modelsummary to work it needs a tidy or glance method to help it build tables. In this case it may not be that straightforward to define those since you are doing falsification checks. Instead, it may be easier to write a function to extract all that info and make use tinytable which is one of modelsummary's dependencies.

    library(tibble)
    library(dplyr)
    library(tinytable)
    library(rddensity)
    library(rdrobust)
    
    
    data("rddensity_senate")
    
    rddsenate = as.data.frame(margin)
    
    senate = rddensity(X = rddsenate$margin)
    
    extract_info = \(object, ...){
      
      summary_stats = tibble(
        nobs = nrow(object),
        model = object$opt$fitselect,
        kernel = object$opt$kernel,
        bw_method = object$opt$bwselect, 
        vce_method = object$opt$vce,
        nobs_left_of_c = object$N$left,
        nobs_right_of_c = object$N$right,
        effective_n_left = object$N$eff_left,
        effective_n_right = object$N$eff_right,
        order_est_left = object$opt$p,
        order_est_right = object$opt$p,
        order_bias_left = object$opt$q,
        order_bias_right = object$opt$q,
        bw_est_left = object$x$h$left,
        bw_est_right = object$x$h$right
      )
    
      binom_data = tibble(pvals = object$bino$pval,
                          left_n = object$bino$LeftN,
                          right_n = object$bino$RightN,
                          `Window Length/2` =  object$bino$LeftWindow)
      
      summary_stats = bind_cols(summary_stats, binom_data)
      
      return(summary_stats)
    }
    
    make_nice = extract_info(senate) 
    
    tt(make_nice) |> 
      save_tt(output = 'examp.tex')
    

    You will have to adjust your latex preamble to include

    \usepackage{tabularray}
    \usepackage{float}
    \usepackage{graphicx}
    \usepackage[normalem]{ulem}
    \UseTblrLibrary{booktabs}
    \NewTableCommand{\tinytableDefineColor}[3]{\definecolor{#1}{#2}{#3}}
    \newcommand{\tinytableTabularrayUnderline}[1]{\underline{#1}}
    \newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}}