[R] how to simulate from a conditional distribution
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Fri Jan 25 07:24:01 CET 2008
If "X ~ N(0, var1), and I know Y|X ~ N(0, var2)" as stated below, then X
and Y are in fact independent and you can simulate from that
distribution very simply indeed:
N <- 1000000 # or whatever...
Sim <- data.frame(X = rnorm(N, sd = sqrt(var1)),
Y = rnorm(N, sd = sqrt(var2)))
A more realistic example might be
"X ~ N(0, var1), and I know Y|X=x ~ N(x, x^2 + var2)"
which at least gives something that is not bivariate normal. This is
also easy, but requires two steps. I would do it as
N <- 1000000 # or whatever...
Sim <- data.frame(X = rnorm(N, sd = sqrt(var1)))
Sim <- transform(Sim, Y = rnorm(N, mean = X, sd = sqrt(X^2 + var2)))
with(Sim, plot(X, Y)) ## gives a big splodge with 1000000 points!
Bill Venables.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of qq ww
Sent: Friday, 25 January 2008 12:01 PM
To: r-help at r-project.org
Subject: [R] how to simulate from a conditional distribution
Dear all, I am new to R so please bear with me for the questions.
If I have X ~N(0, var1), and I know Y|X ~N(0, var2). X and Y are both
vectors of equal length.
How do I simulate samples for Y?
[[alternative HTML version deleted]]
______________________________________________
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.
More information about the R-help
mailing list