如何从R / Sweave / LaTeX中的lm的xtable()输出中删除“标准错误”列

时间:2012-12-09 03:27:49

标签: r latex xtable

我目前正在对人口数据进行一些数据分析,因此报告参数系数表中的标准误差实际上并没有统计意义。我做了一些搜索,无法找到任何方法来自定义xtable输出以删除它。谁能指出我正确的方向?

非常感谢,我没有轻易发布;如果这是明显的事情,我为浪费时间而道歉!

3 个答案:

答案 0 :(得分:5)

所以在我(其他)整个冗长的答案之后...这也有效:

xtable(summary(model1)$coefficients[,c(1,3,4)])

或者更一般地说:

sm <- summary(SomeModel)
SE.indx <- which(colnames(sm$coefficients) == "Std. Error")   # find which column is Std. Error (usually 2nd)
sm$coefficients <- sm$coefficients[, -SE.indx]  # Remove it
xtable(sm$coefficients)   # call xtable on just the coefficients table

结果:

% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Sun Dec  9 00:01:46 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
  \hline
 & Estimate & t value & Pr($>$$|$t$|$) \\ 
  \hline
(Intercept) & 29.80 & 30.70 & 0.00 \\ 
  crim & -0.31 & -6.91 & 0.00 \\ 
  age & -0.09 & -6.50 & 0.00 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

答案 1 :(得分:2)

使用help(lm)中的第一个示例:

 xtable(as.matrix(coef(lm.D9)))

% latex table generated in R 2.15.2 by xtable 1.7-0 package
% Sat Dec  8 19:53:09 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rr}
  \hline
 & x \\ 
  \hline
(Intercept) & 5.03 \\ 
  groupTrt & -0.37 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

如果这是对人口的描述而不仅仅是样本,我同意不使用标准错误。但是,通过这种推理,您不希望留下p值或t统计量。这就是我只包括系数的原因。仅从摘要系数矩阵中删除标准错误列:

xtable( coef(summary(lm.D9))[,-2] )

% latex table generated in R 2.15.2 by xtable 1.7-0 package
% Sat Dec  8 21:02:17 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
  \hline
 & Estimate & t value & Pr($>$$|$t$|$) \\ 
  \hline
(Intercept) & 5.03 & 22.85 & 0.00 \\ 
  groupTrt & -0.37 & -1.19 & 0.25 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

答案 2 :(得分:0)

查看str(summary(Model1)),我们发现$coefficients具有我们要删除的Std. Error值。

lesserSummary <- function(x) {
## returns same as summary(x), but with "Std. Error" remove from coefficients. 
##    and class of object is "modifiedSummary"

  # grab the summary
  sm <- summary(x)

  # find which column is std error
  SE.indx <- which(colnames(sm$coefficients) == "Std. Error")

  # remove it 
  sm$coefficients <- sm$coefficients[, -SE.indx]

  # give it some class
  class(sm) <- "modifiedSummary"

  # return it
  sm
}


xtable.modifiedSummary <- 
function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, ...)  {
# x is a modifiedSummary object
# This function is a modification of xtable:::xtable.summary.lm
# Key Difference is simply the number of columns that x$coef is expected to have
#   (Here 3.  Originally 4)  

    x <- data.frame(x$coef, check.names = FALSE)
    class(x) <- c("xtable", "data.frame")
    caption(x) <- caption
    label(x) <- label
    align(x) <- switch(1 + is.null(align), align, c("r", "r", "r", "r"))
    digits(x) <- switch(1 + is.null(digits), digits, c(0, 4, 2, 4))
    display(x) <- switch(1 + is.null(display), display, c("s", "f", "f", "f"))
    return(x)
}


xtable_mod <- function(x) {
  # Wrapper function to xtable.modified summary, calling first lesserSummary on x
  xtable(lesserSummary(x))
}    

实施例

xtable_mod(model1)

% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Sat Dec  8 23:44:54 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
  \hline
 & Estimate & t value & Pr($>$$|$t$|$) \\ 
  \hline
(Intercept) & 29.8007 & 30.70 & 0.0000 \\ 
  crim & -0.3118 & -6.91 & 0.0000 \\ 
  age & -0.0896 & -6.50 & 0.0000 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}


<小时/>

以下是为达成上述结论所采取的步骤。

您可以修改对xtable的调用,但首先需要跟进它: 首先看一下xtable的来源:

xtable
# function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, 
#     display = NULL, ...) 
# {
#     UseMethod("xtable")
# }
# <environment: namespace:xtable>

我们看到它只是打电话给UseMethod()。那么让我们看看哪些方法可用:

methods(xtable)
#  [1] xtable.anova*           xtable.aov*             xtable.aovlist*        
#  [4] xtable.coxph*           xtable.data.frame*      xtable.glm*            
#  [7] xtable.lm*              xtable.matrix*          xtable.prcomp*         
# [10] xtable.summary.aov*     xtable.summary.aovlist* xtable.summary.glm*    
# [13] xtable.summary.lm*      xtable.summary.prcomp*  xtable.table*          
# [16] xtable.ts*              xtable.zoo*      

有几个。请注意,带有星号*的那些不可见。

调用的方法由我们调用的对象的类xtable决定。

假设我们的输出是Model1我们看一下它的类:'

class(Model1)
# [1] "lm"

因此,我们要查看的来源是xtable.lm

xtable.lm
# Error: object 'xtable.lm' not found

错误?没错,它是不可见的。所以我们使用包含三重冒号的包名称。 注意:请务必阅读帮助文件中的通知?“:::”

xtable:::xtable.lm
# function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, 
# display = NULL, ...) 
# {
#     return(xtable.summary.lm(summary(x), caption = caption, label = label, 
#         align = align, digits = digits, display = display))
# }
# <environment: namespace:xtable>   

我们注意到xtable.lm调用xtable.summary.lm并将第一个参数作为summary(x)传递,其中x是我们的模型。

因此,我们将我们带到两个地方进行调查:summaryxtable.summary.lm