Re: [R] Simulating Conditional Distributions

From: Bernardo Rangel Tura <tura_at_centroin.com.br>
Date: Wed, 26 Mar 2008 00:47:13 -0300

On Fri, 2008-03-21 at 22:37 -0700, Sherri Rose wrote:
> Dear R-Help List,
> I'm trying to simulate data from a conditional distribution, and
> haven't been able to modify my existing code to do so. I searched
> the archives, but didn't find any previous post that matched my
> question.
>
> n=10000
> pop = data.frame(W1 = rbinom(n, 1, .2),
> W2 = runif(n, min = 3, max = 8), W3 = rnorm(n, mean=0, sd=2))
> pop = transform(pop,
> A = rbinom(n, 1, .5))
> pop = transform(pop,
> Y = rbinom(n, 1, 1/(1+exp(-(1.5*A-.05*W1-2*W2-2*W3+2*A*W1)))))
>
> In this population the probability of being "diseased" (Y=1) is
> approx 0.030. What I want to be able to do is specify a conditional
> distribution of (A, W1, W2, W3) given that Y=1 and one for (A, W1,
> W2, W3) given that Y=0. Then I can sample diseased and non-diseased
> individuals from these distributions without having to simulate a
> large base population. This will be particularly useful when the
> probability of being "diseased" is even smaller and I want a large
> number of diseased individuals.
>
> Any pointers to do this would be extremely helpful!
> Thank you,
> Sherri Rose
>
> UC Berkeley
> [[alternative HTML version deleted]]

If I understand your problem, this script solve your question:

n<-10000
Y<-rbinom(n,1,.3)
A<-ifelse(Y==0,1,rbinom(n, 1, .5))
W1<-ifelse(Y==0,1,rbinom(n, 1, .2))
W2<-ifelse(Y==0,1,runif(n, min = 3, max = 8))
W3<-ifelse(Y==0,1,rnorm(n, mean=0, sd=2)) pop<-data.frame(Y,A,W1,W2,W3)
pop
-- 
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 26 Mar 2008 - 03:51:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 26 Mar 2008 - 04:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive