簡體   English   中英

通過plyr從嵌套的模型列表中提取系數

[英]Extracting coefficients from nested list of models via plyr

我有一個嵌套的模型列表,我想從中提取系數,然后創建一個數據框,其中每一行還包含存儲該模型的列表元素的名稱。 我想知道是否有一個plyr函數已經處理了嵌套列表,或者只是一種更干凈的方式來完成任務。

例如:

### Create nested list of models

iris.models <- list()
for (species in unique(iris$Species)) {

iris.models[[species]]<- list()

for (m in c("Sepal.Length","Sepal.Width","Petal.Length")) {

    iris.formula <- formula(paste("Petal.Width ~ ", m))
    iris.models[[species]][[m]] <- lm(iris.formula
                                      , data=iris
                                      , subset=Species==species)

    } # for m

} # for species 

### Create data frame of variable coefficients (excluding intercept)

irisCoefs <- ldply(names(iris.models)
             , function(sp) {
              ldply(iris.models[[sp]]
                    , function(meas) data.frame(Species=sp, Coef=coef(meas)[-1])
            )})
colnames(irisCoefs)[colnames(irisCoefs)==".id"] <- "Measure"
irisCoefs

此代碼產生如下數據幀:

  Measure      Species          Coef
1 Sepal.Length setosa     0.08314444
2 Sepal.Width  setosa     0.06470856
3 Petal.Length setosa     0.20124509
4 Sepal.Length versicolor 0.20935719
5 Sepal.Width  versicolor 0.41844560
6 Petal.Length versicolor 0.33105360
7 Sepal.Length virginica  0.12141646
8 Sepal.Width  virginica  0.45794906
9 Petal.Length virginica  0.16029696

雖然我的代碼有效,但最終的執行方式似乎不太優雅,我想知道是否可以進一步簡化(或在其他情況下將其推廣):

我的問題是:

使用嵌套列表似乎有些棘手。 在外部ldply調用中,我不得不使用列表項的名稱,但是在內部ldply調用中,我獲得了“免費”添加的.id列。 我想不出一種更簡單的方法來訪問所調用函數內的列表元素的名稱。

我也無法在第二個ldply函數調用本身中更改“ .id”中的列名。 因此,我最后添加了colnames語句。

有沒有辦法使我的代碼在plyr的處理方式中更直接?

我不知道這是否有助於澄清我的意圖,但是我想象代碼看起來像這樣:

ldply(iris.models, .id.col="Species", function(sp) ldply(sp, .id.col="Measure", function(x) data.frame(coef(x)[-1])))

謝謝。

plyr的方法:

#Melt the predictor variables
iris_m <- melt(iris[, -4], id.vars = "Species")
#Insert the dependant variable
iris_m$Petal.Width <- rep(iris$Petal.Width, 3)

#Make the models divide by species and variable
models <- dlply(iris_m, .(Species, variable), 
                function(x) lm(Petal.Width ~ value, data = x))
#Get the coefficients as a nice data.frame
ldply(models, function(x) coef(x)[-1])

     Species     variable      value
1     setosa Sepal.Length 0.08314444
2     setosa  Sepal.Width 0.06470856
3     setosa Petal.Length 0.20124509
4 versicolor Sepal.Length 0.20935719
5 versicolor  Sepal.Width 0.41844560
6 versicolor Petal.Length 0.33105360
7  virginica Sepal.Length 0.12141646
8  virginica  Sepal.Width 0.45794906
9  virginica Petal.Length 0.16029696

不完全是必需的格式,但這可以與基本函數一起使用。

m=c("Sepal.Length","Sepal.Width","Petal.Length")
do.call(rbind,
    by(iris,iris$Species,
      function(x) sapply(m, 
        function(y) coef(lm(paste('Petal.Width ~',y),data=x))) [2,]
   ) 
)

           Sepal.Length Sepal.Width Petal.Length
setosa       0.08314444  0.06470856    0.2012451
versicolor   0.20935719  0.41844560    0.3310536
virginica    0.12141646  0.45794906    0.1602970

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM