A simple function to create a regression output in R

There are several ways to create an output of regression analysis in R. This is a simple custom made function.

# Clear all
rm(list=ls())

# Libraries
library(QuantPsyc)

# Regression summary function
table_lm <- function(lm_object, digit_rounding){

  summary_lmobject = summary(lm_object)
  df_output = data.frame(
  predictors = names(lm_object$coefficients),
  beta = round(as.numeric(summary_lmobject$coefficients[,1]), digit_rounding),
  SE = round(as.numeric(summary_lmobject$coefficients[,2]), digit_rounding),
  SE95ci_low = round(as.numeric(confint(lm_object, level = 0.95)[,1]),digit_rounding),
  SE95ci_high = round(as.numeric(confint(lm_object, level = 0.95)[,2]),digit_rounding),
  stdbeta = c(NA,round(lm.beta(lm_object), digit_rounding)),
  ttest = round(summary_lmobject$coefficients[,3], digit_rounding),
  pvalues = sapply(as.numeric(summary_lmobject$coefficients[,4]),
                   round, digits= digit_rounding))

  return(df_output)
}

# Regression analysis
lm_mod <- lm(mpg ~ hp + wt, data = mtcars)

# Function: lm_mod is the lm object and 3 is the number of decimals in the table.
table_lm(lm_object = lm_mod, digit_rounding = 3)


Comments

Popular posts from this blog

JASP - Statistical software

Cold shower

Get some morning light!