Home > Blockchain >  How to change digits of numbers in the export_summs() table
How to change digits of numbers in the export_summs() table

Time:09-04

I get the regression output table using export-summs() installed in the package called jtools and huxtable.

My code is: export_summs(model1, model2, scale = TRUE) and

          ────────────────────────────────────────────────────
                                Model 1          Model 2      
                           ───────────────────────────────────
            (Intercept)            0.28 **          0.00      
                                  (0.10)           (0.02)     
            educ                   0.09 ***                   
                                  (0.01)                      
            exper                  0.00 *                     
                                  (0.00)                      
            tenure                 0.02 ***                   
                                  (0.00)                      
            xtilde                                  0.09 ***  
                                                   (0.01)     
                           ───────────────────────────────────
            N                    526              526         
            R2                     0.32             0.23      
          ────────────────────────────────────────────────────
            *** p < 0.001; ** p < 0.01; * p < 0.05.           

Is there any way to make all the digits 3 decimal numbers?

CodePudding user response:

You can pass a number_format string directly:

library(jtools)

model1 <- lm(drat ~ wt   hp, mtcars)
model2 <- lm(drat ~ hp, mtcars)

export_summs(model1, model2, scale = TRUE, number_format = "%.4g")
#> ----------------------------------------------------------------------------
#>                                   Model 1                  Model 2          
#>                          ---------------------------------------------------
#>   (Intercept)                          3.597 ***                 3.597 ***  
#>                                       (0.06852)                 (0.08586)   
#>   wt                                  -0.3937 ***                           
#>                                       (0.09254)                             
#>   hp                                   0.01942                  -0.2399 **  
#>                                       (0.09254)                 (0.08724)   
#>                          ---------------------------------------------------
#>   N                                   32                        32          
#>   R2                                   0.5083                    0.2014     
#> ----------------------------------------------------------------------------
#>   All continuous predictors are mean-centered and scaled by 1               
#>   standard deviation.  *** p < 0.001; ** p < 0.01; * p < 0.05.              
#> 
#> Column names: names, Model 1, Model 2

CodePudding user response:

Assign the result of your export_summs() call to an object, and manipulate the format of specific rows and columns using huxtable::set_number_format(). Here is an example:

  1. Some fake data and a couple of models
set.seed(123)
df = data.frame(x=rnorm(100), y = rnorm(100), g = sample(c(1:3),100, replace=T))
model1 = lm(y~x,data=df)
model2 = lm(y~x factor(g), data=df)
  1. Assigning the result of jtools::export_summs() to an object; this object is of class huxtable
model_table = jtools::export_summs(model1, model2, scale=T)
  1. Now, use a fixed format (i.e. the "f" in "%.3f") for all the numbers in rows 2 through 9 and columns 2 through 3
huxtable::set_number_format(model_table,2:9,2:3,"%.3f")

Output:

        ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                       Model 1                                     Model 2                   
                                                    ─────────────────────────────────────────────────────────────────────────────────────────
          (Intercept)                                                                   -0.108                                       0.073   
                                                                                        (0.097)                                     (0.169)  
          x                                                                             -0.048                                      -0.035   
                                                                                        (0.098)                                     (0.098)  
          `factor(g)`2                                                                                                              -0.175   
                                                                                                                                    (0.235)  
          `factor(g)`3                                                                                                              -0.373   
                                                                                                                                    (0.241)  
                                                    ─────────────────────────────────────────────────────────────────────────────────────────
          N                                                                            100                                         100       
          R2                                                                             0.00                                        0.03    
        ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
          All continuous predictors are mean-centered and scaled by 1 standard deviation.  *** p < 0.001; ** p < 0.01; * p < 0.05.           

Column names: names, Model 1, Model 2
  • Related