I'm running a linear model in R and I want the entire output of my model to be written to the same excel file.
Right now, I can do this only for the coefficients, which is the first example. The second example is when I try to get the entire output to write to excel, which throws an error on the second to last line of code, see below:
# creating data set for lm
df<- cbind.data.frame(var1= rnorm(10,3,2), var2= rnorm(10,4,1))
# running sample model
lmodel<- lm(var1~var2, data = df)
# assigning model results to a variable
mod_res<- summary(lmodel)
mod_res
# assigning model coefficients to a variable
modCoeff<- coef(summary(lmodel))
modCoeff
# getting model coefficients to open in an excel spreadsheet... THIS WORKS!
lmodCoeffs<- openxlsx::createWorkbook()
openxlsx::addWorksheet(lmodCoeffs, "coeffs")
openxlsx::writeData(lmodCoeffs, "coeffs", modCoeff, rowNames= TRUE)
openxlsx::openXL(coeffs)
# look at ALL model fit stats in excel... THIS DOES NOT WORK!
modResSheet<- openxlsx::createWorkbook()
openxlsx::addWorksheet(modResSheet, "res")
openxlsx::writeData(modResSheet, "res", mod_Res, rowNames= TRUE) # error thrown here
openxlsx::openXL(modResSheet)
Getting the coefficients only is useful, however, seeing all model fit stats within one single excel file will make model evaluation more comprehensive.