 # how do you get a loop to repeat the process using different observations?

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.

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.

2 Likes

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.

What is the result of the command

``````str(outList)
``````
1 Like

oh I think it returned 600 simulations! thank you

It returns as
a <- str(outList)
List of 6
: chr [1:100] "2" "2" "2" "2" ... : chr [1:100] "0" "1" "2" "2" ...
: chr [1:100] "2" "2" "2" "1" ... : chr [1:100] "-1" "0" "1" "0" ...
: chr [1:100] "2" "2" "2" "2" ... : chr [1:100] "-1" "0" "1" "2" ...

However, is there a way I can view the whole list? when I try summary(a) / table(a) / show(a) it just returns NULL for me

The command

``````outList[]
``````

will print the simulations that resulted from x, i.e. when i was equal to "2". You can store that in its own variable with

``````X1 <- outList[]
``````

You can make a single vector out of all of the results with

``````AllDat <- unlist(outList)
``````

thank you that helped out so much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.