Thursday, February 12, 2015

R17. Central Limit Theorem and Mean of Beta Distribution in R

For any distribution (such as Beta, but not limited to Beta), the mean is a normal random variable. It does require that we use sample alpha and beta parameters, for the Beta, for example.


For the Beta distribution, we use rbeta. For the confidence level calculations, we use the qnorm for the cumulative density function.


The percentage of mean inside margin of error is calculated, for the 5000 Beta distributions.

# ex17.R
N <- 5000
n <- 1000
alpha <- 2
beta <- 5
mn <- numeric(N)
for (i in 1:N) {
    x <- rbeta(n, alpha, beta)
    mn[i] <- mean(x)
}
mn.samps <- mean(mn)
conf.range <- seq(5,95,5)
zin <- numeric(length(conf.range))
std.samps <- sd(mn)
for (i in 1:length(conf.range)) {
    p <- conf.range[i]
    z.crit <- qnorm((100+p)/200)
    moe <- z.crit*std.samps
    ins <- (mn.samps-moe < mn) & (mn < mn.samps+moe)
    zin[i] <- sum(ins)/N
}
plot(conf.range, zin*100, col = 'blue', type = 'l',
     xlab = 'Confidence Level',
     ylab = 'Percentage mean inside moe',
     main = 'Mean of Beta Samples',
     xlim = c(0,100), ylim = c(0,100))

Output:

No comments:

Post a Comment