R/summary_linreg.R

Defines functions summary.linreg

Documented in summary.linreg

#' @title Summarise Linear Regression output
#' @param object an object of class \code{linreg}
#' @param ... additional parameters
#' @export

# s3 method

summary.linreg <- function(object, ...) {
  summ_df <- as.data.frame(object$coefficients)
  summ_df[, 1] <- round(as.numeric(summ_df[, 1]), 5)
  summ_df[, 2] <- round(as.numeric(sqrt(diag(object$variance_of_coefficients))), 5)
  summ_df[, 3] <- round(as.numeric(object$t_values), 5)
  summ_df[, 4] <- sapply(object$p_values,
                         function(x) if (suppressWarnings(!is.na(as.numeric(x)))) {
                           if (round(as.numeric(x), 5) == 0) {
                             "<1e-5"
                             } else {
                               round(as.numeric(x), 5)
                               }
                           } else {x})
  summ_df[, 5] <- sapply(object$p_values, 
                         function(x) if(x < 0.001) {"***"} 
                         else if (x < 0.01) {"**"} 
                         else if (x < 0.05) {"*"} 
                         else if (x < 0.1) {"."} 
                         else {" "})
  
  colnames(summ_df) <- c("Estimate", "Std. Error", "t value", "Pr(>|t|)", "")
  cat("Coefficients:\n")
  print(summ_df)
  cat("---\n")
  cat("Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\n")
  cat("Residual standard error:", sqrt(object$residual_variance), "on", 
      object$df, "degrees of freedom")
}
dsn00b/linear_regression documentation built on Nov. 9, 2021, 11:39 p.m.