【问题标题】:R: t.test output for LaTexR:LaTex 的 t.test 输出
【发布时间】:2014-02-02 13:07:38
【问题描述】:

我想知道是否有任何函数可以将t.test 的输出重定向到LaTeX。像这样的东西

library(xtable)
xtable(t.test(extra ~ group, data = sleep))

【问题讨论】:

  • Hmisc::latex 是一种选择
  • 感谢@baptiste 的评论。我使用了Hmisc::latex,但它引发了以下错误:Error in UseMethod("latex") : no applicable method for 'latex' applied to an object of class "htest"
  • 奇怪,它对我有用
  • 不适用于Hmisc_3.12-2
  • 请修改您的问题并调整标题以反映这一点。

标签: r statistics knitr sweave reproducible-research


【解决方案1】:

这就是诀窍。

library(schoRsch)
library(xtable)

T.Test <- t.test(extra ~ group, data = sleep)
xtable(
  t_out(toutput=T.Test, n.equal = TRUE, welch.df.exact = TRUE, welch.n = NA,
    d.corr = TRUE, print = TRUE)
    )



                      Test                            Results
1 Welch Two Sample t-test: t(17.78) = -1.86, p = .079, d = NA
% latex table generated in R 3.1.1 by xtable 1.7-3 package
% Mon Aug 25 21:01:13 2014
\begin{table}[ht]
\centering
\begin{tabular}{rll}
  \hline
 & Test & Results \\ 
  \hline
1 & Welch Two Sample t-test: & t(17.78) = -1.86, p = .079, d = NA \\ 
   \hline
\end{tabular}
\end{table}

【讨论】:

    【解决方案2】:

    您可以使用带有knitr::kable 函数的简单 S3 方法:

    as.data.frame.htest <- function(x) {
        x <- unclass(x)
        names <- c("statistic", "estimate", "parameter", "p.value")
        x <- x[names]
        x <- x[!sapply(x, is.null)]
        for (i in seq_along(x)) {
            if (!is.null(names(x[[i]])))
                names(x)[i] <- names(x[[i]])
        }
        as.data.frame(x, stringsAsFactors = FALSE)
    }
    
    knitr::kable(t.test(extra ~ group, data = sleep), format = "latex")
    

    注意:有关htest 类结构的更多详细信息,请参阅str(t.test(extra ~ group, data = sleep))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2013-04-07
      • 2014-03-17
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多