R的summary.lm对象的LaTeX输出-在表外显示信息时[关闭]


10

在我看来,这是基本知识,但似乎无法在线找到解决方案,因此我想知道自己可能会缺少什么。

我希望将lm摘要对象的输出包括在Sweave(.Rnw)文档中。我可以按原样输出summary.lm,也可以使用xtable / Hmisc包(通过xtable或latex命令)。是否有类似xtable的东西,它也提供了表外部可用的摘要信息?(,F统计等...?)R2


为什么要关闭它?应该继续吗?
Tal Galili

4
我认为它可以保留在这里,这个问题对统计学家而言比对程序员而言更为重要。
mpiktas 2011年

根据该论坛的当前标准,这绝对是题外话。
变形虫

Answers:


6

看一下包apsrtable。您可以根据需要调整输出,然后汇总几个模型而不是一个。


嗨,mpiktas。感谢您的答复。我对显示几个表格的兴趣不大-希望简单地总结一个模型,但这看起来“不错”。我可以打开代码并执行此操作,但令我惊讶的是它之前没有做过……
Tal Galili

1
@Tal我已对此响应+1,因为我很确定有一种方法可以实现您想要的目标,尽管我没有花时间来深入研究软件包选项(我曾经用它来显示您所说的几种模型)。
chl

2

我放弃并玩了代码以产生类似的东西。虽然不是最漂亮的东西。如果有人想改进它-我很乐意使用您的代码。

print.summary.lm.xtable <- function (x, digits = max(3, getOption("digits") - 3), symbolic.cor = x$symbolic.cor, 
    signif.stars = getOption("show.signif.stars"), ...) 
{

if(!require(xtable)) stop("This function requires the package 'xtable' - please make sure you get it")


cat("\\begin{verbatim}")

    cat("\nCall:\n", paste(deparse(x$call), sep = "\n", collapse = "\n"), 
        "\n\n", sep = "")
    resid <- x$residuals
    df <- x$df
    rdf <- df[2L]
    cat(if (!is.null(x$w) && diff(range(x$w))) 
        "Weighted ", "Residuals:\n", sep = "")
    if (rdf > 5L) {
        nam <- c("Min", "1Q", "Median", "3Q", "Max")
        rq <- if (length(dim(resid)) == 2L) 
            structure(apply(t(resid), 1L, quantile), dimnames = list(nam, 
                dimnames(resid)[[2L]]))
        else {
            zz <- zapsmall(quantile(resid), digits + 1)
            structure(zz, names = nam)
        }
        print(rq, digits = digits, ...)
    }
    else if (rdf > 0L) {
        print(resid, digits = digits, ...)
    }
    else {
        cat("ALL", df[1L], "residuals are 0: no residual degrees of freedom!\n")
    }
#     if (length(x$aliased) == 0L) {
#         cat("\nNo Coefficients\n")
#     }
#     else {
#         if (nsingular <- df[3L] - df[1L]) 
#             cat("\nCoefficients: (", nsingular, " not defined because of singularities)\n", 
#                 sep = "")
#         else cat("\nCoefficients:\n")
#         coefs <- x$coefficients
#         if (!is.null(aliased <- x$aliased) && any(aliased)) {
#             cn <- names(aliased)
#             coefs <- matrix(NA, length(aliased), 4, dimnames = list(cn, 
#                 colnames(coefs)))
#             coefs[!aliased, ] <- x$coefficients
#         }
#         printCoefmat(coefs, digits = digits, signif.stars = signif.stars, 
#             na.print = "NA", ...)
#     }


cat("\\end{verbatim}")

print(xtable(x),   latex.environments = "left") # x is a summary of some lm object

cat("\\begin{verbatim}")
    cat("Residual standard error:", format(signif(x$sigma, 
        digits)), "on", rdf, "degrees of freedom\n")
    if (nzchar(mess <- naprint(x$na.action))) 
        cat("  (", mess, ")\n", sep = "")
    if (!is.null(x$fstatistic)) {
        cat("Multiple R-squared:", formatC(x$r.squared, digits = digits))
        cat(",\tAdjusted R-squared:", formatC(x$adj.r.squared, 
            digits = digits), "\nF-statistic:", formatC(x$fstatistic[1L], 
            digits = digits), "on", x$fstatistic[2L], "and", 
            x$fstatistic[3L], "DF,  p-value:", format.pval(pf(x$fstatistic[1L], 
                x$fstatistic[2L], x$fstatistic[3L], lower.tail = FALSE), 
                digits = digits), "\n")
    }
    correl <- x$correlation
    if (!is.null(correl)) {
        p <- NCOL(correl)
        if (p > 1L) {
            cat("\nCorrelation of Coefficients:\n")
            if (is.logical(symbolic.cor) && symbolic.cor) {
                print(symnum(correl, abbr.colnames = NULL))
            }
            else {
                correl <- format(round(correl, 2), nsmall = 2, 
                  digits = digits)
                correl[!lower.tri(correl)] <- ""
                print(correl[-1, -p, drop = FALSE], quote = FALSE)
            }
        }
    }
    cat("\n")
cat("\\end{verbatim}")
    invisible(x)
}

2

一种可能的解决方案是Sacha Epskamp 在Sweave软件包中打印统计结果

例子

library(swst)
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)
corTest <- cor.test(x, y, method = "kendall", alternative = "greater")
swst(corTest)

T=26p=0.06

# Chi-square test:
M <- as.table(rbind(c(762, 327, 468), c(484,239,477)))
dimnames(M) <- list(gender=c("M","F"),
party=c("Democrat","Independent", "Republican"))
chisqTest <- chisq.test(M)
swst(chisqTest)

chi2(2)=30.07p<0.001

# Linear model:
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
lm.D90 <- lm(weight ~ group - 1) # omitting intercept
swst(lm.D9)

F(1,18)=1.419p=0.249

swst(lm.D90)

F(2,18)=485.051p<0.001


0

我个人很喜欢texreg,它可以很好地与人玩,booktabs也可以高度自定义。

不完全是您要寻找的内容,但我认为对于此类工作也很不错。

*请注意,我与编写该软件包的菲利普(Philip)无关。大声笑。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.