Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format model display in texreg or stargazer R as scientific

I just ran a statisitical model and i want it to display the results of the model as a table using stargazer. However, the large numbers are displayed in full.

fit2<-lm(A~B,data=C)
stargazer(fit2,type="text")

With this table as result

===================================================
                      Dependent variable:      
                -------------------------------
                               A               
---------------------------------------------------
B                               -0.599             
                                (1.698)            
                          32,126,391.000         
                         (24,004,268.000)        

---------------------------------------------------
 Observations                       5               
R2                               0.040             
Adjusted R2                     -0.280             
Residual Std. Error   31,217,258.000 (df = 3e+00)  
F Statistic            0.124 (df = 1e+00; 3e+00)   
===================================================
Note:               *p<1e-01; **p<5e-02; ***p<1e-02

How do I get the large numbers displayed as scientific i.e: 3.12e+07, please? I have tried:

options("scipen"=-20,"digit"=2)
fit1<-format(lm(A~B,data=C),scientific=T)

This however causes the summary of the model to be distortrd and displayed as a single row. What are the best ways to format the numbers and retain the table structure, please?

                   CO          NO2        SM
Dec 2004 2.750000e+18 1.985136e+15 0.2187433
Jan 2005 2.980000e+18 2.144211e+15 0.1855678
Feb 2005 2.810000e+18 1.586491e+15 0.1764805
Dec 2005 3.010000e+18 1.755409e+15 0.2307153
Jan 2006 3.370000e+18 2.205888e+15 0.2046671
Feb 2006 3.140000e+18 2.084682e+15 0.1834232
Dec 2006 2.940000e+18 1.824735e+15 0.1837391
Jan 2007 3.200000e+18 2.075785e+15 0.1350665
Feb 2007 3.060000e+18 1.786481e+15 0.1179924
Dec 2007 2.750000e+18 1.645800e+15 0.2037340
Jan 2008 3.030000e+18 1.973517e+15 0.1515871
Feb 2008 3.040000e+18 1.753803e+15 0.1289968
Dec 2008 2.800000e+18 1.649315e+15 0.1968024
Jan 2009 3.090000e+18 1.856762e+15 0.1630173
Feb 2009 2.880000e+18 1.610011e+15 0.1446938
Dec 2009 2.660000e+18 1.562971e+15 0.1986012
Jan 2010 2.864333e+18 1.733843e+15 0.1559205
Feb 2010 2.881474e+18 1.469982e+15 0.1397536
Dec 2010 2.730000e+18 1.652751e+15 0.2129476
Jan 2011 3.030000e+18 1.862774e+15 0.1681295
Feb 2011 2.850000e+18 1.658988e+15 0.1531579
like image 831
Joke O. Avatar asked Mar 05 '15 08:03

Joke O.


1 Answers

To do this, you can write your own function to take the large numbers and put them into scientific notation.

First, load the stargazer package:

library(stargazer)

Then, create data with large numbers for the example:

set.seed(1)

C <- data.frame("A" = rnorm(10000, 30000, 10000),
                "B" = rnorm(10000, 7500, 2500))

Fit the model and store the stargazer results table in an object:

fit2 <- lm(A ~ B, data = C) 

myResults <- stargazer(fit2, type = "text")

Create a function to take a stargazer table and convert large numbers into scientific notation. (This is not very flexible but can be with simple modifications. Right now only works for 1,000 - 99,999)

fixNumbers <- function(stargazer.object){

  so <- stargazer.object
  rows <- grep(".*[\\d+],[\\d+].*", so, perl = T)
  for(row in rows){

    # Get number and format into scientific notation
    number <- as.numeric(sub(".*([0-9]{1,2}),([0-9]+\\.?[0-9]*).*", "\\1\\2", so[row], perl = T))
    formatted_num <- sprintf("%.2e", number)
    so[row] <- sub("(.*)[0-9]{1,2},[0-9]+\\.?[0-9]*(.*)", paste0("\\1", formatted_num, "\\2"), so[row], perl = T)
  }

  # Print result
  for(i in 1:length(so)){
    cat(so[i], "\n")
  }
}

Give the new function (fixNumbers) your stargazer object:

fixNumbers(myResults)

-- Here's all the code in one chunk: --

library(stargazer)

set.seed(1)

C <- data.frame("A" = rnorm(10000, 30000, 10000),
                "B" = rnorm(10000, 7500, 2500))

fit2 <- lm(A ~ B, data = C) 

myResults <- stargazer(fit2, type = "text")

fixNumbers <- function(stargazer.object){

  so <- stargazer.object
  rows <- grep(".*[\\d+],[\\d+].*", so, perl = T)
  for(row in rows){

    # Get number and format into scientific notation
    number <- as.numeric(sub(".*([0-9]{1,2}),([0-9]+\\.?[0-9]*).*", "\\1\\2", so[row], perl = T))
    formatted_num <- sprintf("%.2e", number)
    so[row] <- sub("(.*)[0-9]{1,2},[0-9]+\\.?[0-9]*(.*)", paste0("\\1", formatted_num, "\\2"), so[row], perl = T)
  }

  # Print result
  for(i in 1:length(so)){
    cat(so[i], "\n")
  }
}

fixNumbers(myResults)
like image 183
Brigadeiro Avatar answered Oct 21 '22 16:10

Brigadeiro