科学地在texreg或stargazer R中显示格式模型 [英] 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")
以该表为结果
===================================================
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
请问如何将大量数字显示为科学数字,即:3.12e + 07? 我已经尝试过:
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
推荐答案
为此,您可以编写自己的函数以将大数取为科学记数法.
To do this, you can write your own function to take the large numbers and put them into scientific notation.
library(stargazer)
然后,为示例创建大量数据:
set.seed(1)
C <- data.frame("A" = rnorm(10000, 30000, 10000),
"B" = rnorm(10000, 7500, 2500))
调整模型并将stargazer
结果表存储在对象中:
Fit the model and store the stargazer
results table in an object:
fit2 <- lm(A ~ B, data = C)
myResults <- stargazer(fit2, type = "text")
创建一个函数以获取stargazer
表并将大量数字转换为科学计数法. (这不是很灵活,但可以进行简单的修改.目前仅适用于1,000-99,999)
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")
}
}
为您的stargazer
对象提供新功能(fixNumbers
):
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)
这篇关于科学地在texreg或stargazer R中显示格式模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!