r - 通过汽车包获得的 MANOVA 对象的 xtable

标签 r xtable r-car manova

我想知道如何获取通过汽车包获得的 MANOVA 对象的 xtable。这是 MWE:

library(xtable)
library(car)
MANOVA <- Anova(lm(cbind(Al, Fe, Mg, Ca, Na) ~ Site, data=Pottery))
xtable(MANOVA)

Error in UseMethod("xtable") : no applicable method for 'xtable' applied to an object of class "Anova.mlm"

最佳答案

问题是 xtable 有一个针对 anova 类对象的方法,但是 car 包的 Anova函数返回 anova.mlm 类的对象。我在 https://stat.ethz.ch/pipermail/r-help/2009-June/201478.html 找到了修复程序:

library(xtable)
library(car)

# Create some example data
Pottery <- data.frame(
  "Al" = rnorm(10),
  "Fe" = rnorm(10),
  "Mg" = rnorm(10),
  "Ca" = rnorm(10),
  "Na" = rnorm(10),
  "Site" = sample(LETTERS[1:2], 10, replace = TRUE))

# Create a custom function handling `anova.mlm` objects
xtable.Anova.mlm <- function (x, ...) {
  test <- x$test
  repeated <- x$repeated
  ntests <- length(x$terms)
  tests <- matrix(NA, ntests, 4)
  if (!repeated)
    SSPE.qr <- qr(x$SSPE)
  for (term in 1:ntests) {
    eigs <- Re(eigen(qr.coef(if (repeated) qr(x$SSPE[[term]]) else
      SSPE.qr,
      x$SSP[[term]]), symmetric = FALSE)$values)
    tests[term, 1:4] <- switch(test, Pillai = stats:::Pillai(eigs,
      x$df[term], x$error.df), Wilks = stats:::Wilks(eigs,
        x$df[term], x$error.df), `Hotelling-Lawley` = stats:::HL(eigs,
          x$df[term], x$error.df), Roy = stats:::Roy(eigs,
            x$df[term], x$error.df))
  }
  ok <- tests[, 2] >= 0 & tests[, 3] > 0 & tests[, 4] > 0
  ok <- !is.na(ok) & ok
  tests <- cbind(x$df, tests, pf(tests[ok, 2], tests[ok, 3],
    tests[ok, 4], lower.tail = FALSE))
  rownames(tests) <- x$terms
  colnames(tests) <- c("Df", "test stat", "approx F", "num Df",
    "den Df", "Pr(>F)")
  tests <- structure(as.data.frame(tests), heading = paste("\nType ",
    x$type, if (repeated)
      " Repeated Measures", " MANOVA Tests: ", test, " test
statistic",
    sep = ""), class = c("anova", "data.frame"))
  #    print(tests)
  #    invisible(x)
  xtable(tests)
}


MANOVA <- Anova(lm(cbind(Al, Fe, Mg, Ca, Na) ~ Site, data=Pottery))

xtable(MANOVA)

% latex table generated in R 2.15.2 by xtable 1.7-1 package
% Tue Jun 11 20:49:51 2013
\begin{table}[ht]
\centering
\begin{tabular}{lrrrrrr}
\hline
& Df & test stat & approx F & num Df & den Df & Pr($>$F) \\ 
\hline
Site & 1 & 0.61 & 1.24 & 5 & 4 & 0.4288 \\ 
\hline
\end{tabular}
\end{table}

关于r - 通过汽车包获得的 MANOVA 对象的 xtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17006890/

相关文章:

python - 使用 RPy 和 xtable 从 R 摘要生成 LaTeX 表

r - 在椭圆协方差图上获得椭圆的顶点(由 `car::ellipse`创建)

r - 包车无法加载,nlme版本错误

r - 使用Docker镜像运行旧版本的R

删除中央白色圆圈?

r - 使用 toLatex() 或 xtable() 将 mlogit 摘要结果导出到 Latex

r - 来自 read.csv 的数据摘要的水平 xtable

r - 通过提取相似的列名跨列应用函数

r - 如何向条形图添加重要性指示(星号)?

重新编码字符串时,Car 包中的重新编码返回意外符号