mnomial.r_function(m) { # Data from mnomial.b. Female cancers example. x_c(14080,12990,6440,4350,3420,3190,2600,2420,1820,1760,23610) # Simulation from the posterior density for (theta_1,...,theta_l) pi.post_rdiric.r(10000,x+1) # posterior Mean, Median and 95%CI for the category-1 probability mm_summary(pi.post[,1])[3:4] ci_quantile(pi.post[,1],c(.025,.975)) # Alternatively, analytical computation of the posterior mean #(x[1]+1)/sum(x+1) return(mm,ci) } # R code for generating n random samples from a Dirichlet(a) rdiric.r_function(n,a) { p <- length(a) m <- matrix(nrow=n,ncol=p) for (i in 1:p) { m[,i] <- rgamma(n,a[i]) } sumvec <- m %*% rep(1,p) m / as.vector(sumvec) }