【问题标题】:Forcing NAs to be shown in regression output from xtable强制 NA 在 xtable 的回归输出中显示
【发布时间】:2014-02-05 17:15:51
【问题描述】:

xtable 默认从回归输出中删除 NA 值。我想强制这些系数显示在输出中,以及它们的NA 值。 NA.stringprint.xtable 有一个参数,我认为它会提供此功能,但它至少对 lm 对象没有任何作用。

例如,

df <- data.frame(A=1:3, B=2:4, C=2:4)
reg <- lm(A ~ B + C, df)
print(xtable(reg), NA.string = "Foo") 

创建以下输出

% latex table generated in R 3.0.2 by xtable 1.7-1 package
% Wed Feb  5 11:04:09 2014
\begin{table}[ht]
\centering
\begin{tabular}{rrrrr}
   \hline
 & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\ 
  \hline
(Intercept) & -1.0000 & 0.0000 & -Inf & 0.0000 \\ 
   B & 1.0000 & 0.0000 &  Inf & 0.0000 \\ 
   \hline
\end{tabular}
\end{table}

而我想要输出的是

% latex table generated in R 3.0.2 by xtable 1.7-1 package
% Wed Feb  5 11:04:09 2014
\begin{table}[ht]
\centering
\begin{tabular}{rrrrr}
   \hline
 & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\ 
  \hline
(Intercept) & -1.0000 & 0.0000 & -Inf & 0.0000 \\ 
   B & 1.0000 & 0.0000 &  Inf & 0.0000 \\ 
   C & NA & NA & NA & NA \\ %%% This is the new line
   \hline
\end{tabular}
\end{table}

【问题讨论】:

  • 我没用过xtable,所以猜测。也许看看xtable(reg) 的结构,看看你是否可以操纵它来添加一个值“NA”的元素C,类似于现有的任何包含“B”的东西。
  • 如果您查看 summary(reg)$coefficients 您会看到这是打印的表格,它不包括 C 行;然而 print(summary(reg)) 显示 NA 行。
  • 我宁愿不直接编辑xtable对象。

标签: r xtable


【解决方案1】:

这是一个非常古老的问题,但我发现它是因为我遇到了类似的问题。

xtable 文档建议将“NA.string”作为适当的参数。因此,您将添加 NA.string="NA" 以在输出中打印 NA,因为默认值为空格。

请注意,如果由于某种原因您无法使其工作,但您只有少量 NA,您实际上可以手动编辑输出(通过在每行的相邻 & 符号之间插入 NA)。

【讨论】:

    【解决方案2】:

    我希望有一个参数我遗漏或没有正确使用。也许我正在寻找的东西是不可能的。我想我将手动创建一个包含所有NA 系数和xtable 的数据框。

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 2019-10-31
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多