将lm输出列表转换为数据框

时间:2019-06-15 09:41:33

标签: r list dataframe output lm

我需要将列表转换为数据框。我的列表包含按天运行的lm函数的输出,并且是一个包含521个元素的大列表。

我尝试了以下代码:

som=MiniSom(x=10,y=10,input_len=15,sigma=1.0,learning_rate=0.5) som.random_weights_init(x) som.train_random(data=x,num_iteration=100) from pylab import bone, pcolor, colorbar, plot, show bone() pcolor(som.distance_map().T) colorbar() markers = ['o', 's'] colors = ['r', 'g'] for i, x1 in enumerate(x): w = som.winner(x) plot(w[0] + 0.5, w[1] + 0.5, markers[y[i]], markeredgecolor = colors[y[i]], markerfacecolor = 'None', markersize = 10, markeredgewidth = 2) show()

但我收到此错误:

df <- bind_rows(modelsT, .id = "column_label")

是否可以解决该错误?还是另一个选择,如何将列表转换为数据框?

2 个答案:

答案 0 :(得分:1)

这取决于您的需求:

model <- lm(mpg ~ disp, data = mtcars)
coef(model)
# (Intercept)        disp 
# 29.59985476 -0.04121512 

library(dplyr)
bind_rows(coef(model), coef(model))
# # A tibble: 2 x 2
#   `(Intercept)`    disp
#           <dbl>   <dbl>
# 1          29.6 -0.0412
# 2          29.6 -0.0412

正如@ jay.sf刚刚建议的那样

coef(summary(model))
#                Estimate  Std. Error   t value     Pr(>|t|)
# (Intercept) 29.59985476 1.229719515 24.070411 3.576586e-21
# disp        -0.04121512 0.004711833 -8.747152 9.380327e-10
bind_rows(as.data.frame(coef(summary(model))), as.data.frame(coef(summary(model))))
#      Estimate  Std. Error   t value     Pr(>|t|)
# 1 29.59985476 1.229719515 24.070411 3.576586e-21
# 2 -0.04121512 0.004711833 -8.747152 9.380327e-10
# 3 29.59985476 1.229719515 24.070411 3.576586e-21
# 4 -0.04121512 0.004711833 -8.747152 9.380327e-10

通常,如果要从结构中获取类似data.frame的内容,请查看其str的结构。可能会造成混淆,但是

str(model)
# List of 12
#  $ coefficients : Named num [1:2] 29.5999 -0.0412
#   ..- attr(*, "names")= chr [1:2] "(Intercept)" "disp"
#  $ residuals    : Named num [1:32] -2.01 -2.01 -2.35 2.43 3.94 ...
#   ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#  $ effects      : Named num [1:32] -113.65 -28.44 -1.79 2.65 3.92 ...
#   ..- attr(*, "names")= chr [1:32] "(Intercept)" "disp" "" "" ...
#  $ rank         : int 2
#  $ fitted.values: Named num [1:32] 23 23 25.1 19 14.8 ...
#   ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#  $ assign       : int [1:2] 0 1
#  $ qr           :List of 5
#   ..$ qr   : num [1:32, 1:2] -5.657 0.177 0.177 0.177 0.177 ...
#   .. ..- attr(*, "dimnames")=List of 2
#   .. .. ..$ : chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#   .. .. ..$ : chr [1:2] "(Intercept)" "disp"
#   .. ..- attr(*, "assign")= int [1:2] 0 1
#   ..$ qraux: num [1:2] 1.18 1.09
#   ..$ pivot: int [1:2] 1 2
#   ..$ tol  : num 1e-07
#   ..$ rank : int 2
#   ..- attr(*, "class")= chr "qr"
#  $ df.residual  : int 30
#  $ xlevels      : Named list()
#  $ call         : language lm(formula = mpg ~ disp, data = mtcars)
#  $ terms        :Classes 'terms', 'formula'  language mpg ~ disp
#   .. ..- attr(*, "variables")= language list(mpg, disp)
#   .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#   .. .. ..- attr(*, "dimnames")=List of 2
#   .. .. .. ..$ : chr [1:2] "mpg" "disp"
#   .. .. .. ..$ : chr "disp"
#   .. ..- attr(*, "term.labels")= chr "disp"
#   .. ..- attr(*, "order")= int 1
#   .. ..- attr(*, "intercept")= int 1
#   .. ..- attr(*, "response")= int 1
#   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#   .. ..- attr(*, "predvars")= language list(mpg, disp)
#   .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
#   .. .. ..- attr(*, "names")= chr [1:2] "mpg" "disp"
#  $ model        :'data.frame':    32 obs. of  2 variables:
#   ..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#   ..$ disp: num [1:32] 160 160 108 258 360 ...
#   ..- attr(*, "terms")=Classes 'terms', 'formula'  language mpg ~ disp
#   .. .. ..- attr(*, "variables")= language list(mpg, disp)
#   .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#   .. .. .. ..- attr(*, "dimnames")=List of 2
#   .. .. .. .. ..$ : chr [1:2] "mpg" "disp"
#   .. .. .. .. ..$ : chr "disp"
#   .. .. ..- attr(*, "term.labels")= chr "disp"
#   .. .. ..- attr(*, "order")= int 1
#   .. .. ..- attr(*, "intercept")= int 1
#   .. .. ..- attr(*, "response")= int 1
#   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#   .. .. ..- attr(*, "predvars")= language list(mpg, disp)
#   .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
#   .. .. .. ..- attr(*, "names")= chr [1:2] "mpg" "disp"
#  - attr(*, "class")= chr "lm"

str(summary(model))
# List of 11
#  $ call         : language lm(formula = mpg ~ disp, data = mtcars)
#  $ terms        :Classes 'terms', 'formula'  language mpg ~ disp
#   .. ..- attr(*, "variables")= language list(mpg, disp)
#   .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#   .. .. ..- attr(*, "dimnames")=List of 2
#   .. .. .. ..$ : chr [1:2] "mpg" "disp"
#   .. .. .. ..$ : chr "disp"
#   .. ..- attr(*, "term.labels")= chr "disp"
#   .. ..- attr(*, "order")= int 1
#   .. ..- attr(*, "intercept")= int 1
#   .. ..- attr(*, "response")= int 1
#   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#   .. ..- attr(*, "predvars")= language list(mpg, disp)
#   .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
#   .. .. ..- attr(*, "names")= chr [1:2] "mpg" "disp"
#  $ residuals    : Named num [1:32] -2.01 -2.01 -2.35 2.43 3.94 ...
#   ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#  $ coefficients : num [1:2, 1:4] 29.59985 -0.04122 1.22972 0.00471 24.07041 ...
#   ..- attr(*, "dimnames")=List of 2
#   .. ..$ : chr [1:2] "(Intercept)" "disp"
#   .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
#  $ aliased      : Named logi [1:2] FALSE FALSE
#   ..- attr(*, "names")= chr [1:2] "(Intercept)" "disp"
#  $ sigma        : num 3.25
#  $ df           : int [1:3] 2 30 2
#  $ r.squared    : num 0.718
#  $ adj.r.squared: num 0.709
#  $ fstatistic   : Named num [1:3] 76.5 1 30
#   ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
#  $ cov.unscaled : num [1:2, 1:2] 1.43e-01 -4.85e-04 -4.85e-04 2.10e-06
#   ..- attr(*, "dimnames")=List of 2
#   .. ..$ : chr [1:2] "(Intercept)" "disp"
#   .. ..$ : chr [1:2] "(Intercept)" "disp"
#  - attr(*, "class")= chr "summary.lm"

向您显示各个事物的外观,matrix形式的任何事物都可以使用as.data.frame()(可选地,stringsAsFactors=FALSE来轻松转换)。

答案 1 :(得分:0)

我通过使用以下代码解决了该问题:

coef <- ldply(modelsT, coef)