如何将回归分析的系数从RStudio导出到电子表格或csv文件? [英] How to export coefficients of the regression analysis from RStudio to a spreadsheet or csv file?

查看:1393
本文介绍了如何将回归分析的系数从RStudio导出到电子表格或csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢RStudio,我想我的问题很容易解决,但很多搜索没有帮助我。



我正在运行回归, summary(回归1)显示所有系数等等。
现在我使用 coef(regression1),所以它只给出了我要导出到文件的系数。



write.csv(coef,file =regression1.csv) as.data.frame.default中的错误(x [[i]],可选= TRUE):不能强制类函数到数据框架发生



如果你能帮助我,我现在正在网路搜索几个小时,并没有成功。



我必须更改 coef ,以便它适合data.frame?



非常感谢! p>

解决方案

有一个名为 扫帚 ,简化了此任务,将模型输出转换为整洁的数据框,这是一个独立的可重复的示例:



下载并安装软件包:

 库(devtools)
install_github(dgrtwo / broom )
库(扫帚)

这是正常的基本输出,不是很方便:

  lmfit < -  lm(mpg〜wt,mtcars)
lmfit

调用:
lm(formula = mpg〜wt,data = mtcars)

系数:
(截取)wt
37.285 -5.344

以下是与扫帚软件包整理后的相同模型输出更好,更容易使用:

  tidy_lmfit<  -  tidy(lmfit)
tidy_lmfit
术语估计std.error statistic p.value
1(截取)37.285126 1.877627 19.857575 8.241799e-19
2 wt -5.344472 0.559101 -9.559044 1.293959e-10
pre>

以下是将数据框写入CSV的方式:

  write.csv(tidy_lmfit,tidy_lmfit.csv)


I am new to RStudio and I guess my question is pretty easy to solve but a lot of searching did not help me.

I am running a regression and summary(regression1) shows me all the coefficients and so on. Now I am using coef(regression1) so it only gives me the coefficients which I want to export to a file.

write.csv(coef, file="regression1.csv) and the "Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ""function"" to a data.frame" occurs.

Would be great If you could help me. I am searching the web for a few hours now and was not successful.

Do I have to change coef somehow so it fits in a data.frame?

Thank you very much!

解决方案

There's a contributed package called broom that simplifies this task, it converts model output to tidy dataframes. Here's a self-contained reproducible example:

Download and install the package:

library(devtools)
install_github("dgrtwo/broom")
library(broom)

Here's the normal base output, not very convenient:

lmfit <- lm(mpg ~ wt, mtcars)
lmfit

Call:
lm(formula = mpg ~ wt, data = mtcars)

Coefficients:
(Intercept)           wt  
     37.285       -5.344 

Here's the same model output after it's been tidied up by the broom package, much nicer and easier to work with:

tidy_lmfit <- tidy(lmfit)
tidy_lmfit
         term  estimate std.error statistic      p.value
1 (Intercept) 37.285126  1.877627 19.857575 8.241799e-19
2          wt -5.344472  0.559101 -9.559044 1.293959e-10

And here's how you'd write that dataframe to CSV:

write.csv(tidy_lmfit, "tidy_lmfit.csv")

这篇关于如何将回归分析的系数从RStudio导出到电子表格或csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆