Search code examples
rhtml-tabler-markdownstargazertexreg

Is there a reasonable way with texreg or similar in R (making html tables) to round coefficients to different digits than goodness-of-fit measures?


I am using texreg to show regressions side-by-side, including SUR systems with systemfit, but I have some formatting constraints/preferences. I would like to be able to round the coefficients one way, while rounding the goodness-of-fit measures to more digits. For now at least I will also be displaying confidence intervals instead of standard errors, so those are not necessarily a factor here. I also need to output this in HTML and not LaTeX format.

My issue is similar to emagar's How to show only coefficients rounded to whole numbers in LaTeX tables?, but the answer there was largely based around LaTeX tables, while I currently need to be using HTML tables, so the answer to that question, for this and other reasons, is not sufficiently applicable to my issue.
The actual function I am using to do this is actually knitreg (since this is in R Markdown), but have generally been treating it as htmlreg.

    texreg::knitreg(
    l=list(ln(...), systemfit_object), 
    <various formatting>, 
    digits=?
    )

I suspect that there are several potential work-arounds, and I have already done some complex in this project for other purposes. Obviously, though, I would prefer something simpler, if possible, but really my main preference would be to able to use primarily R-based code for this.


Solution

  • If you're happy using my huxtable package, this is very simple:

    library(huxtable)
    r1 <- lm(Sepal.Width ~ Sepal.Length, iris)
    r2 <- lm(Sepal.Width ~ Sepal.Length + Petal.Width, iris)
    h <- huxreg(r1, r2)
    h
                  ────────────────────────────────────────────────────
                                          (1)              (2)        
                                   ───────────────────────────────────
                    (Intercept)           3.419 ***        1.926 ***  
                                         (0.254)          (0.321)     
                    Sepal.Length         -0.062            0.289 ***  
                                         (0.043)          (0.066)     
                    Petal.Width                           -0.466 ***  
                                                          (0.072)     
                                   ───────────────────────────────────
                    N                   150              150          
                    R2                    0.014            0.234      
                    logLik              -86.732          -67.781      
                    AIC                 179.464          143.563      
                  ────────────────────────────────────────────────────
                    *** p < 0.001; ** p < 0.01; * p < 0.05.           
    
    Column names: names, model1, model2
    
    # rows 9-11, columns 2-3 are the model statistics
    # display them to one decimal place:
    number_format(h)[9:11, 2:3] <- 1 
    h
                  ────────────────────────────────────────────────────
                                          (1)              (2)        
                                   ───────────────────────────────────
                    (Intercept)           3.419 ***        1.926 ***  
                                         (0.254)          (0.321)     
                    Sepal.Length         -0.062            0.289 ***  
                                         (0.043)          (0.066)     
                    Petal.Width                           -0.466 ***  
                                                          (0.072)     
                                   ───────────────────────────────────
                    N                   150              150          
                    R2                    0.0              0.2        
                    logLik              -86.7            -67.8        
                    AIC                 179.5            143.6        
                  ────────────────────────────────────────────────────
                    *** p < 0.001; ** p < 0.01; * p < 0.05.           
    
    Column names: names, model1, model2
    

    Huxtable tables will automatically print to the right format in knitr documents, so you can just evaluate the object.

    An alternative is to use pipe style:

    huxreg(r1, r2) |>
      set_number_format(9:11, -1, 1)
                  ────────────────────────────────────────────────────
                                          (1)              (2)        
                                   ───────────────────────────────────
                    (Intercept)           3.419 ***        1.926 ***  
                                         (0.254)          (0.321)     
                    Sepal.Length         -0.062            0.289 ***  
                                         (0.043)          (0.066)     
                    Petal.Width                           -0.466 ***  
                                                          (0.072)     
                                   ───────────────────────────────────
                    N                   150              150          
                    R2                    0.0              0.2        
                    logLik              -86.7            -67.8        
                    AIC                 179.5            143.6        
                  ────────────────────────────────────────────────────
                    *** p < 0.001; ** p < 0.01; * p < 0.05.           
    
    Column names: names, model1, model2
    

    Incidentally, if you like the texreg output style, then you can use huxtablereg in that package, then set number_format as above.