I'd like to create 100 simulations and create a linear model for each model using a matrix. I used the following code, but got an error. What is the best way to accomplish this?
n=15;nsims=100
factor=matrix(as.factor(rep(1:5,3)),n, nsims)
sim=matrix(rnorm(nfactor*nsims,0,1),n,nsims)
> dim(factor)
[1] 15 100
> lm1=lm(sim~factor)
Error in `[[<-.data.frame`(`*tmp*`, i, value = c(1L, 2L, 3L, 4L, 5L, 1L, :
replacement has 1500 rows, data has 15
I'm not clear what you want.
Create data:
n <- 15; nsims <- 100; nfactor <- 5
f <- matrix(as.factor(rep(1:nfactor,n/nfactor)), n, nsims)
set.seed(101)
sim <- matrix(rnorm(nfactor*nsims,0,1),n,nsims)
(In general it's bad practice to name variables with the names of existing functions ( factor
) ...)
res <- vector("list", nsims)
for (i in 1:nsims) {
res[[i]] <- lm(y~x,
data=data.frame(y=sim[,i],x=f[,i]))
}
A for
loop may seem clunky, but (as discussed in chapter 4 of the R Inferno ) it's often the clearest way to write your code ... If you really want to do it without a for
loop, inu the general case where the columns of f
might differ, you could do
res2 <- mapply(function(x,y) lm(y~x),
split(f,col(f)),split(sim,col(sim)),SIMPLIFY=FALSE)
but I consider that less clear than the for
loop ...
update : I don't know whether it was on purpose or not, but every column of your factor matrix is identical. Thus you can put the single input variable on the right-hand side of the formula and the matrix response on the left-hand side:
all(apply(f[,-1],2,identical,f[,1])) ## all TRUE
res2 <- lm(sim~f[,1])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.