使用cbind导出R lm回归到文本 [英] Exporting R lm regression with cbind to text
本文介绍了使用cbind导出R lm回归到文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下lm与一个从属变量的向量:
>拟合<-lm(cbind(X1m,X3m,X6m,X1y,X2y,X3y,X5y,X6y,X10y,
X20y,X30y)〜(ff + dc),data = yield)
当尝试将整个输出导出到csv时,我收到此错误:
code code code $($)总结(合适)):
不能强制类listof到一个data.frame
如果我只输出系数,一切正常:
write.csv(as.data.frame(coef(fit) ),file =regression1.csv)
然而,我想要有t统计和标准错误以及我的系数。
我知道这与因变量是列表(向量)的事实有关。任何想法或建议将不胜感激。
解决方案
我在 tidy http://cran.r-project.org/web/packages/broom/rel =nofollow>扫帚包对此有用:
fit< - lm(y〜x,data.frame(x = 1:10,y = rnorm(10)))#dummy example
library扫帚)
tidy(fit)
#result是一个data.frame:
#term estimate std.error statistic p.value
#1(截取) - 0.3317979 1.2034887 -0.2756967 0.7897685
#2 x -0.0663678 0.1939598 -0.3421730 0.7410364
I have the following lm with a vector of dependent variables:
> fit<-lm(cbind(X1m, X3m, X6m, X1y, X2y, X3y, X5y, X6y, X10y,
X20y, X30y) ~ (ff + dc), data = yields)
When attempting to export the entire output to csv, I get this error:
write.csv(as.data.frame(summary(fit)), file="regression1.csv")
Error in as.data.frame.default(summary(fit)) :
cannot coerce class ""listof"" to a data.frame
If I export just coefficients, everything works fine:
write.csv(as.data.frame(coef(fit)), file="regression1.csv")
I would like, however, to have t-statistics and standard errors along with my coefficients.
I know this has to do with the fact that the dependent variables are a list (vector). Any thoughts or suggestions would be appreciated.
解决方案
I've found tidy
in the broom package useful for this:
fit <- lm(y ~ x, data.frame(x=1:10, y=rnorm(10))) # dummy example
library(broom)
tidy(fit)
# result is a data.frame:
# term estimate std.error statistic p.value
#1 (Intercept) -0.3317979 1.2034887 -0.2756967 0.7897685
#2 x -0.0663678 0.1939598 -0.3421730 0.7410364
这篇关于使用cbind导出R lm回归到文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文