0

I use stargazer to create a table for multiple models. They are actually the same model but the first is based on all observations, while the other drop different observations respectively. All variables are named the same, so what surprises me is that when I export the table to Latex, two lines, one for a dummy variable and another for an interaction term, are duplicated.

What is really strange is that I cannot replicate the results, but I will post a minimal working example nonetheless. Perhaps you can help me based on my description alone.

This is the code for my MWE:

library(tibble)
library(stargazer)

df <- as_tibble(data.frame(first = rnorm(100, 50), second = rnorm(100, 30), third = rnorm(100, 100), fourth = c(rep(0, 50), rep(1, 50))))

model.1 <- lm(first ~ second + third + fourth + third*fourth, data = df)
model.2 <- lm(first ~ second + third + fourth + third*fourth, data = df[!rownames(df) %in% "99",])
stargazer(model.1, model.2)

I will now post the Latex output includes the error that I am trying to fix (with this snippet it seems to work just fine).

Table with weird cell content

What I would like to have, of course is the code as produced by this snippet (I feel very stupid for not being able to reproduce it):

Table working normally

Tea Tree
  • 882
  • 11
  • 26

1 Answers1

1

you could take a look at the names of your model's coefficients using coefficients(). Mare sure they are identical, i.e. identical(names(model.1), names(model.2)) Then use stargazer's keep statement to make sure you get the coefficients you want,

Here with the example above keeping selected variables;

coefficients(model.1)
#>  (Intercept)       second        third       fourth third:fourth 
#>  57.27352606   0.02674072  -0.08236250  20.23596216  -0.20288137 

coefficients(model.2)
#>  (Intercept)       second        third       fourth third:fourth 
#>  57.06149556   0.03305134  -0.08214812  20.85087288  -0.20885718

identical(names(model.1), names(model.2))
#> [1] TRUE

I'm using the type = "text" to make it more friendly to SO, but I guess it's the same with LaTeX,

stargazer(model.1, model.2, type = "text", keep=c("third","third:fourth"))
#> 
#> =========================================================
#>                              Dependent variable:         
#>                     -------------------------------------
#>                                     first                
#>                            (1)                (2)        
#> ---------------------------------------------------------
#> third                     -0.082             -0.082      
#>                          (0.166)            (0.167)      
#>                                                          
#> third:fourth              -0.203             -0.209      
#>                          (0.222)            (0.223)      
#>                                                          
#> ---------------------------------------------------------
#> Observations               100                 99        
#> R2                        0.043              0.044       
#> Adjusted R2               0.002              0.004       
#> Residual Std. Error  1.044 (df = 95)    1.047 (df = 94)  
#> F Statistic         1.056 (df = 4; 95) 1.089 (df = 4; 94)
#> =========================================================
#> Note:                         *p<0.1; **p<0.05; ***p<0.01

but it might be hard to rule out that it's a local issue if we cannot find a way to reproduce your issue.

Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • OK, what actually fixed my problem was to re-run the whole script. But I am certain that your solution would have provided me with the answer if that had not worked. Thank you! – Tea Tree Feb 04 '18 at 19:09