Learn R Programming

popbio (version 2.4.4)

betaval: Generate beta-distributed random numbers

Description

This function calculates a random number from a beta distribution and uses the R function pbeta(x,vv,ww).

Usage

betaval(mn, sdev, fx=runif(1))

Arguments

mn

mean rate between 0 and 1

sdev

standard deviation

fx

cumulative distribution function, default is a random number between 0 and 1

Value

Returns a random beta value

Details

This function is used by vitalsim .

References

Morris, W. F., and D. F. Doak. 2002. Quantitative conservation biology: Theory and practice of population viability analysis. Sinauer, Sunderland, Massachusetts, USA.

See Also

Beta Distribution rbeta

Examples

Run this code
# NOT RUN {
betaval(.5, sd=.05)
betaval(.5, sd=.05)

## histogram with mean=0.5 and sd=0.05

x <- sapply(1:100, function(x) betaval(0.5, 0.05))
hist(x, seq(0,1,.025), col="green", ylim=c(0,25), xlab="Value",
main="Beta distribution with mean=0.5 and sd=0.05")

# generates a graph similar to Figure 8.2 A in Morris & Doak (2002:264)
# a much simpler version of BetaDemo in Box 8.3


x<-matrix(numeric(3*1000), nrow=3)
sd <-c(.05, .25, .45)
for (i in 1:3)
{
  for (j in 1:1000)
  {
    x[i,j]<-betaval(.5,sd[i])
  }
}
plot(0,0,xlim=c(0,1), ylim=c(0,0.4), type='n', ylab='Frequency',
xlab='Value', main="Examples of beta distributions")
for (i in 1:3)
{
   h<-hist(x[i,], plot=FALSE, breaks=seq(0,1,.02)  )
   lines(h$mids, h$counts/1000, type='l', col=1+i, lwd=2, lty=i)
}
legend(0.5,0.4, c("(0.50, 0.05)", "(0.50, 0.25)", "(0.50, 0.45)"),
lty=1:3, lwd=2, col=2:4, title="mean and sd")
# }

Run the code above in your browser using DataLab