rstargazer

In the Stargazer package in R, how can one add a line separator for the resulting output table?


I have two regressions to report in stargazer in R, with add.lines() adding a predicted table at the end. My table currently looks like:

enter image description here

but I want to add a line right below "Predicted Values on x Values", so that it is its own row like we have in the "Observations" row. Is there a way to do this?

Code to generate regression data:

x <- 1:100
y <- rnorm(100, 4*x, 5)
mod1 <- lm(y ~ x)
mod2 <- lm(y ~ 1)
se1 <- summary(mod1)$coef[2,2]
se2 <- summary(mod2)$coef[1,2]
mod1.pred1 <- predict(mod1, newdata=data.frame(x=1))
mod2.pred1 <- predict(mod2, newdata=data.frame(x=1))
mod1.pred2 <- predict(mod1, newdata=data.frame(x=2))
mod2.pred2 <- predict(mod2, newdata=data.frame(x=2))

Stargazer output with table:

stargazer(mod1, mod2, se = list(se1, se2), out="Results.html", notes="Two Models", keep.stat="n", type = "text", 
      table.layout ="ldmc#-t-s-a=n",
      add.lines = list(
        c("Predicted Values on x Values"),
        c("", "", "", ""), # add empty list element
        c("Predict when x=1",mod1.pred1,mod2.pred1),
        c("Predict when x=2",mod1.pred2,mod2.pred2)),
      add.lines.separator = c(1) # add separator after fourth element)

Solution

  • Assuming I understood correctly that you want to add a horizontal rule below the words 'Predicted Values on x Values':

    option 1:

    Edit the html output file

    star = readLines("Results.html")
    newline.pos = grep('Predicted Values on x Values', star)
    star = c(star[1:newline.pos], star[newline.pos -1], star[-(1:newline.pos)])
    writeLines(star, "Results.html")
    

    enter image description here

    option 2:

    Edit the html before it is save to an html file. For this, instead of type = "text" you specify type = "html" to match your output file format.

    star = stargazer(mod1, mod2, se = list(se1, se2), 
                     notes="Two Models", keep.stat="n", type = "html", 
                table.layout ="ldmc#-t-s-a=n",
                add.lines = list(
                  c("Predicted Values on x Values"),
                  c("", "", "", ""), # add empty list element
                  c("Predict when x=1",mod1.pred1,mod2.pred1),
                  c("Predict when x=2",mod1.pred2,mod2.pred2)),
                add.lines.separator = c(1) # add separator after fourth element)
    )
                
    newline.pos = grep('Predicted Values on x Values', star)
    star = c(star[1:newline.pos], star[newline.pos -1], star[-(1:newline.pos)])
    writeLines(star, "Results.html")
    

    option 3:

    Insert the horizontal rule into your list of add.lines:

    stargazer(mod1, mod2, se = list(se1, se2), 
                     out="Results.html", notes="Two Models", keep.stat="n", type = "html", 
                table.layout ="ldmc#-t-s-a=n",
                add.lines = list(
                  c("Predicted Values on x Values"),
                  c('<tr><td colspan="3" style="border-bottom: 1px solid black"></td></tr><tr>'),
                  c("", "", "", ""), # add empty list element
                  c("Predict when x=1",mod1.pred1,mod2.pred1),
                  c("Predict when x=2",mod1.pred2,mod2.pred2)),
                add.lines.separator = c(1) # add separator after fourth element)
    )