我使用texreg并行显示回归,包括SUR系统和systemfit,但我有一些格式约束/首选项。我希望能用一种方法来舍入系数,同时把合适度的值舍入到更多的位数。至少现在我也会显示置信区间而不是标准错误,所以这些不一定是这里的一个因素。我还需要以HTML格式输出,而不是LaTeX格式。
我的问题类似于emagar's ,但是那里的答案主要是基于LaTeX表,而我目前需要使用LaTeX表,因此,出于这个原因和其他原因,这个问题的答案不足以适用于我的问题。
我使用的实际函数实际上是knitreg (因为这是在R中),但是通常将它作为htmlreg来处理。
texreg::knitreg(
l=list(ln(...), systemfit_object),
<various formatting>,
digits=?
)我怀疑有几个潜在的工作,我已经在这个项目中做了一些复杂的其他用途。显然,如果可能的话,我更喜欢一些更简单的东西,但实际上我的主要偏好是能够为此主要使用R-based代码。
发布于 2022-05-20 08:23:02
如果您很高兴使用我的huxtable包,这非常简单:
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, model2Huxtable表将自动以正确的格式打印在针织文档中,因此您只需计算对象。
另一种选择是使用管道样式:
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顺便说一句,如果您喜欢texreg输出样式,那么您可以在该包中使用huxtablereg,然后按照上面的方式设置number_format。
发布于 2022-05-18 21:10:35
一种方法是map2两个列表(模型之一,所需的数字之一),通过cbind对matrixreg的第二列(=系数)进行cbind输出,然后将它们表示为所需的输出格式:
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.下面的代码显示了(用于其他场合,,但因为我在.)如何修改每个结果类的输出数字(受this SO thread启发)。因此,如果要选择每个结果类的数字(例如,用4打印systemfit()结果,用6位数字打印lm()结果),则可以:
..。检查“工厂默认值”,如:systemfit:::print.systemfit (请注意三个冒号),它显示数字的数量是函数参数(formals)的一部分。
## > systemfit:::print.systemfit
## function (x, digits = max(3, getOption("digits") - 1), ...)
## {
## etc...。然后将“工厂版本”提升到您的全球环境中:
print.systemfit <- systemfit:::print.systemfit..。并将会话的正式数字设置如下:
formals(print.systemfit)$digits <- 10..。看看它是否有效:
## > 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! :-)https://stackoverflow.com/questions/71947231
复制相似问题