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

机器算法验证 r 回归
2022-03-30 16:40:00

这对我来说似乎是基本的,但我似乎无法在网上找到解决方案,所以我想知道我可能会错过什么。

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

4个回答

查看包apsrtable您可以按照您想要的方式调整输出,并总结几个模型而不是一个模型。

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

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)
}

一种可能的解决方案是swst:在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=26,p=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.07,p<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.419,p=0.249)

swst(lm.D90)

(F(2,18)=485.051,p<0.001)

就我个人而言,我喜欢texreg,它非常适合booktabs并且高度可定制。

不完全是你要找的东西,但我认为也是这类工作的好读物。

*注意,我与编写该包裹的菲利普无关。哈哈。