6

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

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

4

3 回答 3

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}
于 2012-12-09T05:02:37.363 回答
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}

如果这是对人口的描述而不仅仅是样本,我同意不使用 std erros。但是,根据这种推理,您不希望保留 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}
于 2012-12-09T03:55:07.660 回答
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 并作为它的第一个参数 a 传递summary(x),其中 x 是我们的模型。

所以这导致我们到两个地方进行调查: summaryxtable.summary.lm

于 2012-12-09T03:30:36.540 回答