Create and Call Linear Models from List

前端 未结 3 957
清酒与你
清酒与你 2020-12-10 00:05

So I\'m trying to compare different linear models in order to determine if one is better than another. However I have several models, so I want to create an list of models

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 00:36

    If you have two lists of models, and you want to compare each pair of models, then you want Map:

    models1 <- list(lm(y ~ a), lm(y ~ b), lm(y ~ c)
    models2 <- list(lm(y ~ a + b), lm(y ~ a + c), lm(y ~ b + c))
    
    Map(anova, models1, models2)
    

    This is basically equivalent to the following for loop:

    out <- vector("list", length(models1))
    for (i in seq_along(out) {
      out[[i]] <- anova(models1[[i]], models2[[i]])
    }
    

    Map is an example of a functional, and you can find out more about them at https://github.com/hadley/devtools/wiki/Functionals

提交回复
热议问题