r - 将函数输出分配给向量

时间:2015-11-14 09:29:14

标签: r

我想对1到10的多项式进行多项式回归:

library(ISLR)
attach(Auto)

myvec <- vector(length=10)
for (i in 1:length(myvec)){
    myvec[i]<-lm(mpg~poly(acceleration, i, raw=TRUE))
}

但是     summary(myvec[3]) 不同于: summary(var1 <- lm(mpg~poly(acceleration, 3, raw=TRUE)))

如何将函数输出放入具有原始输出类型的向量中?

1 个答案:

答案 0 :(得分:1)

这样做它应该有效:

mylist.names <- rep("A",10)
mylist <- vector("list", length(mylist.names))
names(mylist) <- mylist.names

for (i in 1:length(mylist)){
  mylist[[i]]<-lm(mpg~poly(acceleration, i, raw=TRUE))
}