gpt4 book ai didi

r - 从 mtable/outreg-type 表中省略一些系数

转载 作者:行者123 更新时间:2023-12-03 18:15:23 26 4
gpt4 key购买 nike

我一直在运行一堆不同的回归模型,现在想将它们的估计值放入 LaTeX 表中。为了使不同的规范具有可比性,我想使用 outreg 的那种表格。来自 rockchalk包裹或mtable来自 memisc生产,即其中不同的模型显示在列中,来自这些模型的参数估计显示在适当的行中。这就是我所拥有的:

df <- data.frame(x=rnorm(20),
z=rnorm(20),
group=gl(5,4,20,labels=paste('group',rep(1:5))))
df$y = 5 + 2*df$x + 5*df$z + rep(c(3.2,5,6.2,8.2,5),each=4) + rnorm(20)

model1 <- lm(y ~ x + z + factor(group),data=df)
model2 <- lm(y ~ x + factor(group),data=df)
model3 <- lm(y ~ x + z,data=df)

library(memisc)

reg.table <- mtable("Model 1"=model1,"Model 2"=model2,"Model 3"=model3,
summary.stats=c("sigma","R-squared","F","p","N"))

toLatex(reg.table)

这工作得很好,但我有一个大约有 200 个级别和相应大量系数的因子。我想要做的是从表中省略与该因子相关的系数,或者(为了奖励积分!)以简单的"is"或“否”来表明模型中使用了该因子。所以,我理想的输出是这样的:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %
%
% Calls:
% Model 1: lm(formula = y ~ x + z + factor(group), data = df)
% Model 2: lm(formula = y ~ x + factor(group), data = df)
% Model 3: lm(formula = y ~ x + z, data = df)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
\begin{tabular}{lcD{.}{.}{7}cD{.}{.}{7}cD{.}{.}{7}}
\toprule
&&\multicolumn{1}{c}{Model 1} && \multicolumn{1}{c}{Model 2} && \multicolumn{1}{c}{Model 3}\\
\midrule
(Intercept) & & 8.315^{***} && 4.235 && 10.338^{***}\\
& & (0.537) && (3.276) && (0.468) \\
x & & 1.976^{***} && 2.398 && 1.858^{***}\\
& & (0.238) && (1.530) && (0.443) \\
z & & 5.389^{***} && && 5.359^{***}\\
& & (0.226) && && (0.463) \\
group & & yes && yes && no \\
\midrule
sigma & & 0.929 && 5.981 && 2.092 \\
R-squared & & 0.984 && 0.265 && 0.891 \\
F & & 129.485 && 1.009 && 69.306 \\
p & & 0.000 && 0.448 && 0.000 \\
N & & 20 && 20 && 20 \\
\bottomrule
\end{tabular}

这可能吗?

最佳答案

只需选择前三个系数就非常简单:

reg.table$coefficients <- reg.table$coefficients[,,1:3,,drop=FALSE]
toLatex(reg.table)

“奖金”问题(即添加一个手工构建的第 4 行描述“组”)需要更多的工作:
## Select the first three coeffients + one to be modified
reg.table$coefficients <- reg.table$coefficients[,,1:4,,drop=FALSE]

## Make a copy of all the coefficients, and in the copy, modify the 4th
j <- reg.table$coefficients
j[,,4,] <- c("yes", "", "yes", "", "no", "")
dimnames(j)[[3]][4] <- "group"

## Put the modified coefficients back into `reg.table`
reg.table$coefficients <- j

等等
toLatex(reg.table)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Calls:
% Model 1: lm(formula = y ~ x + z + factor(group), data = df)
% Model 2: lm(formula = y ~ x + factor(group), data = df)
% Model 3: lm(formula = y ~ x + z, data = df)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{tabular}{lcD{.}{.}{7}cD{.}{.}{7}cD{.}{.}{7}}
\toprule
&&\multicolumn{1}{c}{Model 1} && \multicolumn{1}{c}{Model 2} && \multicolumn{1}{c}{Model 3}\\
\midrule
(Intercept) & & 8.830^{***} && 9.846^{**} && 10.342^{***} \\
& & (0.626) && (3.272) && (0.442) \\
x & & 2.047^{***} && 1.765 && 1.937^{***} \\
& & (0.244) && (1.276) && (0.319) \\
z & & 5.138^{***} && && 4.847^{***} \\
& & (0.267) && && (0.372) \\
group & & yes && yes && no \\
& & && && \\
\midrule
sigma & & 1.204 && 6.310 && 1.812 \\
R-squared & & 0.975 && 0.270 && 0.927 \\
F & & 85.576 && 1.033 && 107.717 \\
p & & 0.000 && 0.436 && 0.000 \\
N & & 20 && 20 && 20 \\
\bottomrule
\end{tabular}

编辑:

这是我更喜欢的版本。它解决了下面 OP 的第一条评论,并使用了 abind() (如 rbind() 用于数组)将组信息添加到数组中,我发现它更清晰:
library(abind)

j <- reg.table$coefficients

groupFac <- array(c("yes", "", "yes", "", "no", ""), dim=c(2,1,3))
nonGroupFacs <- which(!grepl("group", dimnames(j)[[3]]))
j <- j[,,nonGroupFacs,,drop=FALSE]
j <- abind(j, groupFac, along=3)
dimnames(j)[[3]][length(nonGroupFacs)+1] <- "group"

reg.table$coefficients <- j

toLatex(reg.table)

关于r - 从 mtable/outreg-type 表中省略一些系数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11040380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com