【问题标题】:Exporting R lm regression with cbind to text使用 cbind 将 R lm 回归导出到文本
【发布时间】:2015-01-12 04:03:56
【问题描述】:

我有以下带有因变量向量的 lm:

> fit<-lm(cbind(X1m, X3m, X6m, X1y, X2y, X3y, X5y, X6y, X10y, 
                     X20y, X30y) ~ (ff + dc), data = yields)

当尝试将整个输出导出到 csv 时,我收到此错误:

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

如果我只导出系数,一切正常:

write.csv(as.data.frame(coef(fit)), file="regression1.csv")

不过,我希望将 t 统计量和标准误差与我的系数一起使用。

我知道这与因变量是一个列表(向量)这一事实有关。任何想法或建议将不胜感激。

【问题讨论】:

标签: r csv export lm


【解决方案1】:

我发现 tidy 在 broom 包中对此很有用:

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

【讨论】:

    【解决方案2】:
    x=1:100
    y=2*x+1
    fit<-lm(y~x)
    

    使用str(summary(fit))会看到summary的结构,里面有些东西不能转成dataframe

    > str(summary(fit))
    List of 11
     $ call         : language lm(formula = y ~ x)
     $ terms        :Classes 'terms', 'formula' length 3 y ~ x
      .. ..- attr(*, "variables")= language list(y, x)
      .. ..- attr(*, "factors")= int [1:2, 1] 0 1
      .. .. ..- attr(*, "dimnames")=List of 2
      .. .. .. ..$ : chr [1:2] "y" "x"
      .. .. .. ..$ : chr "x"
      .. ..- attr(*, "term.labels")= chr "x"
      .. ..- attr(*, "order")= int 1
      .. ..- attr(*, "intercept")= int 1
      .. ..- attr(*, "response")= int 1
      .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
      .. ..- attr(*, "predvars")= language list(y, x)
      .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
      .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
     $ residuals    : Named num [1:100] 7.15e-14 -5.36e-13 2.16e-14 9.05e-15 2.49e-14 ...
      ..- attr(*, "names")= chr [1:100] "1" "2" "3" "4" ...
     $ coefficients : num [1:2, 1:4] 1.00 2.00 1.12e-14 1.92e-16 8.93e+13 ...
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : chr [1:2] "(Intercept)" "x"
      .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
     $ aliased      : Named logi [1:2] FALSE FALSE
      ..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
     $ sigma        : num 5.56e-14
     $ df           : int [1:3] 2 98 2
     $ r.squared    : num 1
     $ adj.r.squared: num 1
     $ fstatistic   : Named num [1:3] 1.08e+32 1.00 9.80e+01
      ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
     $ cov.unscaled : num [1:2, 1:2] 0.040606 -0.000606 -0.000606 0.000012
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : chr [1:2] "(Intercept)" "x"
      .. ..$ : chr [1:2] "(Intercept)" "x"
     - attr(*, "class")= chr "summary.lm"
    

    所以用这个可以得到t统计量和标准误

    write.csv(summary(fit)$coefficients,'C:/Users/sony/Desktop/a.csv')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2015-11-20
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      相关资源
      最近更新 更多