[R] if statement and truncated distribution
Nick Sabbe
nick.sabbe at ugent.be
Mon Oct 25 09:07:36 CEST 2010
What I guess you want is something like (this is for zero-truncation):
rZeroTruncNormal1d<-function(mu, sig, invalidSign) #sig holds standard
deviation!
{
val<-rnorm(1, mu, sig)
while(val * invalidSign > 0)
{
val<-rnorm(1, mu, sig)
}
return(val)
}
Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36
-- Do Not Disapprove
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Sally Luo
Sent: maandag 25 oktober 2010 2:01
To: r-help at r-project.org
Subject: [R] if statement and truncated distribution
Hi R helpers,
I am trying to use the if statement to generate a truncated random variable
as follows:
if (y[i]==0) { v[i] ~ rnorm(1,0,1) | (-inf ,0) }
if (y[i]==1) { v[i] ~ rnorm(1,0,1) | (0, inf) }
I guess I cannot use " | ( , ) " to restrict the range of a variable in R.
Could you let me know how to write the code correctly in R?
Many thanks for your help.
Maomao
[[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