使用大mapply
循环我生成了一个长度不均匀的列表,我必须将其组织成一些可读的输出。我的mapply
的参数来自一个矩阵,该矩阵的维度清楚地列出了我输出的模式。正如
## an example:
set.seed(123)
x <- matrix(rnorm(100), 10, 10)
w <- matrix(rnorm(100, x), 10, 10)
y <- matrix(rnorm(100, w), 10, 10)
Dat <- data.frame('y'=y,'x'=x,'w'=w)
forms <- outer(
colnames(Dat)[1:10],
c(
colnames(Dat)[11:20],
paste(colnames(Dat)[11:20], colnames(Dat)[21:30], sep='+')
), paste, sep=' ~ '
)
forms <- lapply(forms, as.formula) ## dimension attribute lost here
out <- mapply(lm, forms, data=list(Dat), SIMPLIFY=FALSE)
预期输出为:
> dim(out)
10 20
> out[5, 3] ## the 45th regression model: 45= (3-1)*20 + 5
Coefficients:
(Intercept) x.5
-0.5648 0.3318