﻿ Generating data based on condition using R code

# Generating data based on condition using R code

Suppose I want to generate X and Y condition of previous object of X and probability (F). My algorithm: In a particular day one individual have status 1/0. If status 0 then status of the next day will be `rbinom(1,1,0.5)`; if status 1 then staus of the next day will be `rbinom(1,1,F[this day])`. Here I am wanted to generate this data for N individuals and d days.

My code:

``````N <- 10 # number of individual
d <- 10 # days
F <- c(0.9,0.8, 0.7, 0.6, 0.5, 0.4) # Probabilities

for(j in 1:N){
x <- NULL
x[1] <- rbinom(1,1,0.5)
y <- NULL
y[1] <- ifelse(x[1]==1, 1, 0)
for(i in 2:d){
if(x[i-1]==0)
{
y[i] <- x[i-1]
x[i] <- rbinom(1,1,0.05)
}
if(x[i-1]==1)
{
y[i] <- y[i-1]+x[i-1]
x[i] <- rbinom(1,1,F[y[i]])
}
}

}
``````

This code produces data like,

``````   > x
[1] 1 1 0 0 0 0 0 0 0 1
> y
[1] 1 2 3 0 0 0 0 0 0 0
``````

But I want to generate X and Y like this:

`````` > x
[1] 1 1 0 0 0 0 0 0 0 1
> y
[1] 1 2 0 0 0 0 0 0 0 1
``````

I don't find out which part of the code is wrong. Could anybody help?

The outer loop can be removed and then you can store the output at each iteration using the `replicate` function. I'm also extending the process to 20 steps to see if you can see instances where you get two "events".

``````N <- 10
d <- 20
F <- c(0.9,0.8, 0.7, 0.6, 0.5, 0.4)
replicate(10,{     x <- NULL
x[1] <- rbinom(1,1,0.5)
y <- NULL
y[1] <- ifelse(x[1]==1, 1, 0)
for(i in 2:d){
if(x[i-1]==0)
{
y[i] <- x[i-1]
x[i] <- rbinom(1,1,0.05)
}
if(x[i-1]==1)
{
y[i] <- y[i-1]+x[i-1]
x[i] <- rbinom(1,1,F[y[i]])
}
}
return(rbind(x,y)) } )
``````

After a couple of runs I do see one where you get what (I think) you want, namely two series of positive values in x and y. It happened to be the seventh instance in that run of 10:

``````, , 7

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
x    0    0    0    0    0    0    0    0    1     1     1     0     0     0     1     1     1     1     0     0
y    0    0    0    0    0    0    0    0    0     1     2     3     0     0     0     1     2     3     4     0
``````

You never described in natural language what was not satisfying about your current output so I suppose this is still just guesswork. I also think you should assign the results of your simulations to an object so you can examine later programmatically when N becomes high enough that it's not feasible to review by eye.

``````res <- replicate( 1000, { code block here } )
``````