카테고리 보관물: stats

stats

R의 summary.lm 객체에 대한 LaTeX 출력-테이블 외부에 정보를 표시하는 동안 [닫힘]

이것은 기본적 인 것처럼 보였지만 온라인에서 솔루션을 찾을 수없는 것처럼 보였으므로 누락 된 것이 무엇인지 궁금했습니다.

작품 요약 객체의 출력을 Sweave (.Rnw) 문서에 포함하고 싶습니다. summary.lm을 그대로 출력하거나 xtable / Hmisc 패키지 (xtable 또는 latex 명령을 통해)를 사용할 수 있습니다. 테이블 외부에서 사용 가능한 요약 정보를 제공하는 xtable과 같은 것이 있습니까? ( , F 통계 등 …?)

R2



답변

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또한 고도의 사용자 정의 할 수 있습니다.

정확히 당신이 찾고있는 것은 아니지만, 나는 이것이 이런 종류의 작업에 좋은 독서 라고 생각 합니다 .

* 저는 그 패키지를 작성한 필립과는 아무 관계가 없습니다. 롤


답변