我在 R 中运行线性模型,我希望将模型的整个输出写入同一个 excel 文件。
现在,我只能对系数执行此操作,这是第一个示例。第二个示例是当我尝试将整个输出写入 excel 时,这会在倒数第二行代码中引发错误,如下所示:
# 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)
仅获取系数很有用,但是,在单个 excel 文件中查看所有模型拟合统计信息将使模型评估更加全面。