Writing Regression summary as CSV, including Model Stats

324 views Asked by At

The question is as it sounds; at present I use the broom package to tidy up my regression summary and then use write_csv to turn that summary into a csv naturally. However, the problem is, is that this 'tidied' summary doesn't contain useful stats like R Squared, Residual distribution and p value from the F Statistic.

Does anyone know how to write up a regression summary into a csv which would contain this useful information?

Thanks.

3

There are 3 answers

0
RogiKo35 On

I assume, the core issue of your question is that you don't handle all summary tables as data.frames or list (by compiling all information), aren't you!?

So if you just want to write a particular statistics summary (aov, TukeyHSD, augmented, glance etc.) in csv, you should change it to a data.frame

Some example from "broom" vignette: https://cran.r-project.org/web/packages/broom/vignettes/broom.html

glmfit <- glm(am ~ wt, mtcars, family = "binomial")
tidy(glmfit)
fit1 <- as.data.frame(augment(glmfit))
write.csv(fit1, "test.csv")
0
AlexB On

An alternative would be to create a function that stores in a list all the information you need as follows:

lm(mpg ~ cyl, mtcars) -> model

model_stats <- function(model) {
  
  data.frame(model = toString(model$call),
             broom::augment(model)) -> info1
  
  data.frame(model = toString(model$call),
             broom::tidy(model)) -> info2
  
  data.frame(model = toString(model$call),
             broom::glance(model)) -> info3
  
  list(info1, info2, info3) -> info_all
  
  return(info_all)
  
}

out <- model_stats(model)

sapply(seq_along(out), function(i) write.csv(model_stats(model)[[i]], paste0('info', i, '.csv')))
0
Daniel Hoop On

The value returned by lm is an object which can be further processed using summary. From the value returned by summary you can access different information and process it manually.

# Data and model fit
df <- data.frame(a = rnorm(100), b = rnorm(100))
mod <- lm(a~b, data = df)
su <- summary(mod)

# Helper function to create the output
fill <- function(row, mat) {
  c(row, rep("", ncol(mat)-length(row)))
}

# Create the output. `su$coefficients` provides the matrix to which more information is added using `rbind`.
output <- rbind(
  su$coefficients,
  `Additional stats` = fill("", output),
  Rsq = fill(su$r.squared, output),
  Adj.Rsq = fill(su$adj.r.squared, output),
  F = fill(su$fstatistic, output))

# Write to disk with row and colnames (col.names = NA)
write.table(output, file="model-summary.csv", sep = ",", col.names=NA)