问题描述
我是RStudio的新手,我想我的问题很容易解决,但是很多搜索对我没有帮助.
I am new to RStudio and I guess my question is pretty easy to solve but a lot of searching did not help me.
我正在进行回归,并且summary(regression1)
向我显示了所有系数,依此类推.现在我正在使用coef(regression1)
,所以它只给了我要导出到文件中的系数.
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)
和"Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ""function"" to a data.frame"
出现.
如果您能帮助我,那会很好.我现在在网上搜索了几个小时,但没有成功.
Would be great If you could help me. I am searching the web for a few hours now and was not successful.
我是否必须以某种方式更改coef
,使其适合data.frame?
Do I have to change coef
somehow so it fits in a data.frame?
非常感谢!
推荐答案
有一个名为 broom
简化了此任务,它将模型输出转换为整洁的数据帧.这是一个自包含的可重现示例:
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:
下载并安装软件包:
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
在通过broom
软件包整理后,具有相同的模型输出,使用起来更方便,更方便:
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
这是将数据框写入CSV的方法:
And here's how you'd write that dataframe to CSV:
write.csv(tidy_lmfit, "tidy_lmfit.csv")
这篇关于如何将回归分析的系数导出到电子表格或csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!