R/summary.R

Defines functions summary.linreg

Documented in summary.linreg

#' Summary method for linreg objects
#' 
#' @description \code{summary} method for objects of the class "linreg".
#' @param object an object of class "linreg", generated by a call to \code{\link{linreg}}.
#' @param ... additional arguments.
#' @return A list of summary statistics from a linear regression model, \code{object}, of the class "linreg".
#' @examples 
#' linreg_obj <- linreg(Petal.Length~Species, datasets::iris)
#' summary(linreg_obj)
#' @export


summary.linreg <- function(object,...){
  m <- matrix(0, nrow = nrow(object$var_beta_hat), ncol = 4, dimnames = list(row.names(object$var_beta_hat), c("Estimate", "Std.Error", "t value", "Pr(>|t|)")))
  m[,1] <- object$beta_hat
  m[,2] <- sqrt(diag(object$var_beta_hat))
  m[,3] <- object$t_beta
  m[,4] <- object$p
  m <- round(m, digits = 5)
  dots <- apply(t(m[,4]), 2, FUN= function(x)if(x<0.001){
    paste("***")
    }else if(x<0.01){
      paste("**")
    }else if(x<0.05){
      paste("*")
    }else if(x<0.1){
      paste(".")
    }else if(x<1){
      paste(" ")
    })
  
  res <- paste("Residual standard error:", round(sqrt(object$sigma2_hat), digits = 4), "on", object$df, "degrees of freedom")
  
  t <- c()
  for (rows in 1:nrow(m)) {
    t[rows] <- paste(rownames(m)[rows], m[rows,1], m[rows,2], m[rows,3], m[rows,4], dots[rows])
  }
  cat(t, sep="\n")
  cat(" ", sep="\n")
  cat(res) 
}
hugkn566/Lab4HugoOtto documentation built on Oct. 20, 2020, 2:17 p.m.