簡體   English   中英

如何從R / RSweave / LaTeX上的lm的xtable()輸出中刪除“標准錯誤”列

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

我目前正在對人口數據進行一些數據分析,因此報告參數系數表中的標准誤差實際上並沒有統計意義。 我做了一些搜索,無法找到任何方法來自定義xtable輸出以刪除它。 誰能指出我正確的方向?

非常感謝,我沒有輕易發布; 如果這是明顯的事情,我為浪費時間而道歉!

在我(其他)整個冗長的回答之后......這也有效:

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}

使用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}

看看str(summary(Model1))我們看到$coefficientsStd. Error 我們要刪除的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM