I have the original R code as below.
Bernoulli <- rbinom(1000, 1, 0.5)
mix.sample <- rep(0, 1000) #reserve storage
for (i in 1:1000) { #for each Bernoulli realization
if (Bernoulli[i] == 1){ #sample corresponding normal component
mix.sample[i] <- rnorm(1, mean=10, sd=1)
}
else {
mix.sample[i] <- rnorm(1, mean=0, sd=1)
}
}
plot(density(mix.sample))
And I tried the following code instead of the for loop, but there appears a problem with the results been produced, could anyone help me out?
Bernorm <- ifelse(Bernoulli == 1, rnorm(1, mean=10, sd=1), rnorm(1, mean=0, sd=1))
Aucun commentaire:
Enregistrer un commentaire