Hi Kerry,
Right now at each iteration, you just overwrite everything. If you
want a matrix, you need to instantiate Score as a matrix and then
assign the results into each element of it. That said, I do not think
you really need a loop. rpois() is vectorized, so you can in one pass
generate a large vector of data and use that for your model.
We do not have things like T, but something like:
Score <- matrix(NA, nrow = n.simul, ncol = 12)
for (your loop) {
Score[, i] <- your formula
}
each of your models then would be assigned into the ith column of the
Score matrix, giving you the final product you expect.
Cheers,
Josh
On Mon, Mar 5, 2012 at 11:15 AM, kerry1912 <kerry1912 at hotmail.com>
wrote:> I am trying to run this loop but it keeps coming up with the following
error:
>
> 'Error: no function to return from, jumping to top level'
>
> n.simul <- 1000
> A <- {for(i in 1:12){{
> nT <- rpois(n.simul, T[i])
> nP <- rpois(n.simul, P[i])
> nD <- rpois(n.simul, D[i])
> nC <- rbinom(n.simul, nT, C[i])
> Score <- (5*nT + 2*nC + 3*nP + 3*nD)
> }
> return(Score)}}
>
> My aim is to calculate nT, nP, nD and nC (which predicts the number of
> tries, penalties, drop goals and conversions scored in matches, which are
> simulated 1000 times which give 1000 scores) ?then to use these to
calculate
> the Score using these and multiplying by a constant for teams 1 to 12.
>
> So i am expecting this to give me a 1000 x 12 matrix of simulated scores
>
> Please ask in any other information is required.
> Many Thanks
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Trouble-with-a-loop-tp4447271p4447271.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/