R/reg.1fitBasic.R

Defines functions .reg1fitBasic

.reg1fitBasic <-
function(lm.out, TotSS, sy, digits_d=3, show_R=FALSE) {

  nm <- all.vars(lm.out$terms)  # names of vars in the model
  n.vars <- length(nm)
  n.pred <- n.vars - 1L
  n.obs <- nrow(lm.out$model)

  tx <- character(length = 0)

  # model fit
  sm <- summary(lm.out)

  if (is.null(getOption("knitr.in.progress"))) {
    tx[length(tx)+1] <- "-- Model Fit"
    tx[length(tx)+1] <- ""
  }

  if (!is.null(sy)) {  # assigned NULL in reg.zKfold.R)
    tx[length(tx)+1] <- paste("Standard deviation of ", nm[1], ": ",
    .fmt_cm(sy,digits_d), sep="")
    tx[length(tx)+1] <- ""
  }

  se <- sm$sigma
  tx[length(tx)+1] <- paste("Standard deviation of residuals:  ",
                            .fmt_cm(se,digits_d),
                            " for df=", sm$df[2], sep="")

  tcut <- -qt(0.025, df=sm$df[2])
  res_range <- 2*tcut*se
  tx[length(tx)+1] <- paste("95% range of residuals:  ",
          .fmt_cm(res_range,digits_d),
          " = 2 * (", .fmt(tcut,3), " * ", .fmt_cm(se,digits_d), ")", sep="")

  # predicted residual sum of squares
  hat <- lm.influence(lm.out)$hat
  prs.terms <- residuals(lm.out) / (1 - hat)
  # guard: hat≈1 → near-zero denominator
  prs.terms[hat >= 1 - .Machine$double.eps * 100] <- NA
  PRESS <- sum(prs.terms^2, na.rm=TRUE)
  RsqPRESS <- if (is.na(TotSS) || TotSS == 0) NA_real_ else 1 - (PRESS / TotSS)

  if (n.pred > 0) {
    pvl <- 1-pf(sm$fstatistic[1],sm$fstatistic[2],sm$fstatistic[3])
    tx[length(tx)+1] <- ""
    tx[length(tx)+1] <- paste("R-squared:", .fmt(sm$r.squared,3),
      "   Adjusted R-squared:", .fmt(sm$adj.r.squared,3),
      "   PRESS R-squared:", .fmt(RsqPRESS,3))
    tx[length(tx)+1] <- paste("\n",
        "Null hypothesis of all 0 population slope coefficients:\n",
        "  F-statistic: ", .fmt(sm$fstatistic[1],3),
        "     df: ", sm$fstatistic[2], " and ", sm$fstatistic[3],
        "     p-value:", .fmt(pvl, 3, 7), sep="")
  }

  return(list(out_fit=tx, se=sm$sigma, range=res_range, Rsq=sm$r.squared,
    Rsqadj=sm$adj.r.squared, PRESS=PRESS, RsqPRESS=RsqPRESS))

}

Try the lessR package in your browser

Any scripts or data that you put into this service are public.

lessR documentation built on Nov. 4, 2026, 1:07 a.m.