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.

2

There are 2 answers

8
dash2 On BEST ANSWER

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.

0
AudioBubble On

One approach would be to map2 two lists (one of models, one of desired digits), reduce the output by cbinding the second column of matrixreg (= the coefficients), and afterwards kableing them to the desired output format:

library(systemfit)
library(purrr)
library(kableExtra)

## generate some example models,
## e.g. fit3sls and fit2sls (used below):
example(systemfit)


## map2 and reduce:
map2(list(fit3sls, fit2sls), ## list of models
     list(2, 3), ## list of desired output digits per model
     function(MODEL, DIGITS) matrixreg(MODEL, digits = DIGITS)
     ) %>%
  ## cbind 2nd columns (coefficients) of upstream matrixreg results:
  reduce(~ cbind(.x, .y[,2])) %>%
  as.data.frame %>% ## convert to dataframe for kable
  kable(format = 'html') ## or 'latex' etc.

Below code shows (for other occasions, but since I were at it ...) how to modify the output digits per result class (inspired by this SO thread). So, if you were to choose digits per result class (e.g. print systemfit() results with 4, and lm() results with 6 digits) you could:

... inspect the "factory default" like this: systemfit:::print.systemfit (note the threefold colon) which reveals that the number of digits is part of the function's arguments (formals)

## > systemfit:::print.systemfit
## function (x, digits = max(3, getOption("digits") - 1), ...) 
## {
## etc.

... then hoist the "factory version" into your global environment:

print.systemfit <- systemfit:::print.systemfit

... and set the digits formal for the session like this:

formals(print.systemfit)$digits <-  10

... see if it works:

## > example(systemfit)
## ...
Coefficients:
demand_(Intercept)       demand_price      demand_income supply_(Intercept) 
     99.8954229115      -0.3162988049       0.3346355982      58.2754312020 
      supply_price   supply_farmPrice       supply_trend 
      0.1603665957       0.2481332947       0.2483023473 
## ...
## it works! :-)