简体   繁体   English

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

[英]How to remove “Standard Error” column from xtable() output of an lm on R/RSweave/LaTeX

I'm currently doing some data analysis on population data, so reporting the standard errors in the tables of parameter coefficients just doesn't really make statistical sense. 我目前正在对人口数据进行一些数据分析,因此报告参数系数表中的标准误差实际上并没有统计意义。 I've done a fair bit of searching and can't find any way to customize the xtable output to remove it. 我做了一些搜索,无法找到任何方法来自定义xtable输出以删除它。 Can anyone point me in the right direction? 谁能指出我正确的方向?

Thanks a lot, I didn't post this lightly; 非常感谢,我没有轻易发布; if it's something obvious, I apologize for having wasted time! 如果这是明显的事情,我为浪费时间而道歉!

so after my (other) whole long-winded answer... this works too: 在我(其他)整个冗长的回答之后......这也有效:

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

Or more generically: 或者更一般地说:

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

Results: 结果:

% 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}

Using the first example in help(lm): 使用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}

I agreed with not using std erros if this were descriptions of a population and not just a sample. 如果这是对人口的描述而不仅仅是样本,我同意不使用标准错误。 By that reasoning, however, you would not want to leave in p-values or t-statistics. 但是,通过这种推理,您不希望留下p值或t统计量。 That was the reason I only included the coefficients. 这就是我只包括系数的原因。 To remove the standard error column only from the summary coefficient matrix: 要仅从摘要系数矩阵中删除标准错误列:

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}

Looking at str(summary(Model1)) we see that $coefficients has the Std. Error 看看str(summary(Model1))我们看到$coefficientsStd. Error Std. Error value we want to remove. 我们要删除的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))
}    

EXAMPLE: 例:

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}




Below are the steps taken to arrive at the above conclusion. 以下是为达成上述结论所采取的步骤。

You can likely modify the call to xtable, but you first need to follow it down a bit: start by looking at the source for xtable: 您可以修改对xtable的调用,但首先需要跟进它:首先查看xtable的源代码:

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

We see that it simply has a call to UseMethod() . 我们看到它只是调用了UseMethod() So lets see which methods are available: 那么让我们看看哪些方法可用:

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*      

There are several. 有几个。 Note that the ones with an asterisk * are non-visible. 请注意,带星号*的那些是不可见的。

The method called is determined by the class of the object we are calling xtable on. 调用的方法由我们调用xtable的对象的类决定。

Let's say our output is Model1 We take a look at its class: ' 假设我们的输出是Model1我们来看看它的类:'

class(Model1)
# [1] "lm"

So the source we want to look at is xtable.lm . 所以我们要看的源是xtable.lm

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

Error? 错误? That's right, it is non-visible. 没错,它是不可见的。 So we use the package name with triple-colons. 所以我们使用包含三重冒号的包名称。 Note: please be sure to read the notice in the help file ?":::" 注意:请务必阅读帮助文件中的通知?“:::”

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>   

We notice that xtable.lm calls xtable.summary.lm and passes as its first argument a summary(x) , where x is our model. 我们注意到xtable.lm调用xtable.summary.lm并将第一个参数传递给summary(x) ,其中x是我们的模型。

So that leads us to two place to investigate: summary and xtable.summary.lm 因此,我们将我们带到两个地方进行调查: summaryxtable.summary.lm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM