【问题标题】:Print the summary of an lm or fastLm() model without printing the coefficients打印 lm 或 fastLm() 模型的摘要而不打印系数
【发布时间】:2016-01-04 12:24:01
【问题描述】:

使用我自己的数据集,我的系数太多了。 我只是想打印摘要而不(或部分)打印系数。

示例脚本:

lm.fit <- lm(iris$Sepal.Length ~ iris$Petal.Length)
summary(lm.fit)

输出:

> summary(lm.fit)

Call:
lm(formula = iris$Sepal.Length ~ iris$Petal.Length)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.24675 -0.29657 -0.01515  0.27676  1.00269 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
(Intercept)        4.30660    0.07839   54.94   <2e-16 ***
iris$Petal.Length  0.40892    0.01889   21.65   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.4071 on 148 degrees of freedom
Multiple R-squared:   0.76, Adjusted R-squared:  0.7583 
F-statistic: 468.6 on 1 and 148 DF,  p-value: < 2.2e-16

期望的输出:

Call:
lm(formula = iris$Sepal.Length ~ iris$Petal.Length)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.24675 -0.29657 -0.01515  0.27676  1.00269 

---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.4071 on 148 degrees of freedom
Multiple R-squared:   0.76, Adjusted R-squared:  0.7583 
F-statistic: 468.6 on 1 and 148 DF,  p-value: < 2.2e-16

我知道我可以使用从下面提到的names 中选择的summary(lm.fit)$names 进行呼叫。但我需要的是相反的。总结没有系数。

> names(summary(lm.fit))
 [1] "call"          "terms"         "residuals"     "coefficients" 
 [5] "aliased"       "sigma"         "df"            "r.squared"    
 [9] "adj.r.squared" "fstatistic"    "cov.unscaled" 

这可能吗?如果有,怎么做?


编辑:我实际上需要这个来自 RcppArmadillo 包的 fastLm。
我只是希望lm()fastLm() 的输出相同。 @LyzanderR 提供的代码非常适用于 lm()

使用@LyzanderR 的示例,我在谷歌上搜索并创建了以下sn-p,它替换了RcppArmadillo 包提供的打印摘要。

print.summary.fastLm <- function(x, ...) {
    ## Alternate print summary
    ## Prints without coefficients
    cat("\nCall:\n")
    print(x$call)
    cat("\nResiduals:\n")
    print(x$residSum)
    cat("\n")

    #printCoefmat(x$coefficients, P.values=TRUE, has.Pvalue=TRUE)
    digits <- max(3, getOption("digits") - 3)
    cat("\nResidual standard error: ", formatC(x$sigma, digits=digits), " on ",
    formatC(x$df), " degrees of freedom\n", sep="")
    cat("Multiple R-squared: ", formatC(x$r.squared, digits=digits),
    ",\tAdjusted R-squared: ",formatC(x$adj.r.squared, digits=digits),
    "\n", sep="")
    invisible(x)
}

【问题讨论】:

    标签: r lm summary


    【解决方案1】:

    我稍微改变了print.summary.lm函数,只是注释掉了打印系数的部分(注释的代码行可以在下面看到):

    print.sum2 <-
      function (x, digits = max(3L, getOption("digits") - 3L), symbolic.cor = x$symbolic.cor, 
                signif.stars = getOption("show.signif.stars"), ...) 
      {
        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$weights) && diff(range(x$weights))) 
          "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 + 1L)
            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!")
          cat("\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("\nResidual standard error:", format(signif(x$sigma, 
                                                        digits)), "on", rdf, "degrees of freedom")
        cat("\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))
          cat("\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")
        invisible(x)
      }
    

    现在如果你运行:

    > print.sum2(summary(lm.fit))
    
    Call:
    lm(formula = iris$Sepal.Length ~ iris$Petal.Length)
    
    Residuals:
         Min       1Q   Median       3Q      Max 
    -1.24675 -0.29657 -0.01515  0.27676  1.00269 
    
    Residual standard error: 0.4071 on 148 degrees of freedom
    Multiple R-squared:   0.76, Adjusted R-squared:  0.7583 
    F-statistic: 468.6 on 1 and 148 DF,  p-value: < 2.2e-16
    

    它完全按照您的意愿工作。

    【讨论】:

    • 谢谢,我实际上需要它来用于 fastLm,但我自己使用您的示例设法做到了。我还更改了标题并发布了我所做的,因此它适用于 lm 和 fastLm!
    • 为了澄清,所以你认为我的选择不适用于两者!你的为lm() 工作,我的现在为fastLm() 工作:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多