1
votes

I am trying to simulate data where the data generating process is p(X, Y) = p(X)p(Z|X), where the distribution of binary Z depends on the value of X, which is generated from the standard normal distribution.

X is generated by the code below.

X <- rnorm(100, 0, 1)

How would I generate the vector Y conditional on X? My initial thought was to somehow generate a vector of probabilities P using X and then running the code below, but have not been successful in doing so.

sapply(P,function(Y){rbinom(1,1,Y)})
1

1 Answers

1
votes

X will vary from -Inf to +Inf so that will not work as the probability you need for the binomial distribution, but pnorm(X) would give you values from 0 to 1, then use ifelse() statements to convert values below .05 to .05 and over .95 to .95:

X <- rnorm(100)   # 0, 1 are the default values
Xp <- pnorm(X)    # transform to probabilities
Xp <- ifelse(Xp < .05, .05, ifelse(Xp > .95, .95, Xp)) # Bound the results
vals <- rbinom(length(Xp), 1, Xp)  # Your binary values