R/summary.R

Defines functions summary.linreg

Documented in summary.linreg

#' Return summary of a linreg object
#' @description \code{summary} for class "linreg".
#' @param object A linreg object.
#' @param ... Further arguments passed to or from other methods.
#' @return Returns a summary for each variable in the linreg object that includes \code{Estimate},\code{Std.Error}, \code{t.value} and \code{p.value}
#' together with significance codes. Also returns the \code{Residual standard error} and \code{Degrees of freedom} for the linreg object.
#' @examples
#' summary(linreg(Petal.Length ~ Species, iris))
#' @export
#'

# summary
summary.linreg <- function(object, ...){
  res1 <- data.frame("Estimate" = object$beta_hat, "Std.Error" = sqrt(diag(object$var_beta_hat)),
                     "t value" = object$t_beta, "p value" = object$p_value, row.names = rownames(object[[9]]))
  star_col <- apply(object$p_value, 1, function(x){
    if(x >= 0  & x < 0.001){
      return("***")
    } else if(x >= 0.001 & x < 0.01){
      return("**")
    } else if(x >= 0.01 & x < 0.05 ){
      return("*")
    } else if( x >= 0.05 & x < 0.1){
      return(".")
    } else if( x >= 0.1 & x <= 1){
      return(" ")
    } } )
  # Print output
  print(cbind(res1, " " = star_col))
  cat("\nSignif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1")
  cat("\n\nResidual standard error: ", round(sqrt(object[[8]]), 4)," on ", object[[7]],
      " degrees of freedom", sep = "")
  # Return list with values
  invisible(list("coefficients" = res1, "sigma" = c(object[[8]]), "df" = object[[7]]))
}
Sidryd/lab4sidjac documentation built on Oct. 17, 2020, 11:05 p.m.