Thursday, February 12, 2015

R16. One Beta Distributed Sample in R

The function rbeta is used to create one Beta distributed sample of size N, which is 10000 in example.


The alpha is 2 and beta is 5, same as before.


The mean of sample is contrasted with the actual value.

# ex16.R
N <- 10000
alpha <- 2
beta <- 5
x <- rbeta(N, alpha,beta)
hist(x, breaks = 50, col = 'pink')
m1 <- mean(x)
m2 <- alpha/(alpha+beta)
error <- (m1-m2)/m2
cat("mean of sample = ", m1, " but should be ", m2)
cat("\nThe error = ", error)
# mean of sample =  0.2833446  but should be  0.2857143
# The error =  -0.008294062

No comments:

Post a Comment