how do you get a loop to repeat the process using different observations?
for example, if my data is x = c( "2" "-1" "2" "0" "1" "-2") and I want to simulate this 100 times, I know how to do a loop for one observation.
e.g. my initial state is "2"
i = "2"
simulationESO = rep(NA,100)
for(j in 1:100){
prob = ESO@transitionMatrix[i ,]
i = rmultinom(1,1,prob)
i = c(rownames(i)[i==1])
simulationESO[j] <- i
}
this works fine for me, but I want to simulate this 6 times so that I can simulate 6 different observations 100 times so that I can get 600 simulation results without having to change the initial states manually.
Thanks in advance
Perhaps something like this will work for you. outList will have 6 elements each of which will be 100 simulations using one value from the vector x. k will vary from 1 to 6 in the outer for loop
x = c( "2", "-1", "2", "0", "1", "-2")
outList <- vector(mode = "list",length = length(x))
for (k in seq.along(x)) {
i <- x[k]
simulationESO = rep(NA,100)
for(j in 1:100){
prob = ESO@transitionMatrix[i ,]
i = rmultinom(1,1,prob)
i = c(rownames(i)[i==1])
simulationESO[j] <- i
}
outList[[k]] <- simulationESO
}
I admit I do not understand your code. You seem to use i as a parameter and to store results and as an index. Since you say the code is working, I left it alone.
hi I've tried your code, but it would still give me 100 simulations. I tried changing simulationESO = rep(NA,600) but it would still give only 100 results, and the other 500 would be NA.
to clarify my code, I have data x = c( "2", "-1", "2", "0", "1", "-2") that has states -2, -1, 0, 1, 2.
ESO is a probability transition matrix that also has states -2,-1,0,1,2.
When I set my initial state as "2", and when I used the codes above, it provided me with 100 simulations. (which is what I wanted)
however, I want the code to repeat the simulation if I set the initial state as according to x so that I get 100 simulations for "2", "-1", "2", "0", "1", "-2" so 600 simulations in total. I hope this clarifies this bit more.