R: Simulates from a truncated probability distribution
rtruncated
R Documentation
Simulates from a truncated probability distribution
Description
Simulates a sample from a truncated distribution where the functions for the cdf and inverse cdf are available.
Usage
rtruncated(n,lo,hi,pf,qf,...)
Arguments
n
size of simulated sample
lo
low truncation point
hi
high truncation point
pf
function containing cdf of untruncated distribution
qf
function containing inverse cdf of untruncated distribution
...
parameters used in the functions pf and qf
Value
vector of simulated draws from distribution
Author(s)
Jim Albert
Examples
# want a sample of 10 from normal(2, 1) distribution truncated below by 3
n=10
lo=3
hi=Inf
rtruncated(n,lo,hi,pnorm,qnorm,mean=2,sd=1)
# want a sample of 20 from beta(2, 5) distribution truncated to (.3, .8)
n=20
lo=0.3
hi=0.8
rtruncated(n,lo,hi,pbeta,qbeta,2,5)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(LearnBayes)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/LearnBayes/rtruncated.Rd_%03d_medium.png", width=480, height=480)
> ### Name: rtruncated
> ### Title: Simulates from a truncated probability distribution
> ### Aliases: rtruncated
> ### Keywords: models
>
> ### ** Examples
>
> # want a sample of 10 from normal(2, 1) distribution truncated below by 3
> n=10
> lo=3
> hi=Inf
> rtruncated(n,lo,hi,pnorm,qnorm,mean=2,sd=1)
[1] 3.459263 4.025342 3.219380 3.693929 3.871361 4.578368 3.036803 3.086510
[9] 3.461421 5.418028
> # want a sample of 20 from beta(2, 5) distribution truncated to (.3, .8)
> n=20
> lo=0.3
> hi=0.8
> rtruncated(n,lo,hi,pbeta,qbeta,2,5)
[1] 0.4696255 0.4520094 0.4030736 0.3348298 0.3222550 0.5367709 0.3160157
[8] 0.4397834 0.3636561 0.3776660 0.4425261 0.4913755 0.7160378 0.4878621
[15] 0.5119176 0.4383960 0.4152831 0.4033986 0.3661696 0.4117952
>
>
>
>
>
> dev.off()
null device
1
>