全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
872 0
2016-02-12
悬赏 200 个论坛币 未解决
Assume the Bayesian model with likelihood Y j  Binomial(n; ) and prior   Beta(a; b).
(a) Write a function that uses Monte Carlo sampling to estimate the posterior mean and
standard deviation of  given we observe Y = y. The function should take inputs y, n,
a, and b. Given these inputs, the function should generate 1; 000; 000 samples of (; Y )
(by rst drawing  from a beta distribution and then Y j from a binomial distribution),
extract the samples with Y = y, and return the mean and standard deviation of  for
these samples. Include code for this function in your write-up.
(b) Use the code from (1) with n = 10 and a = b = 1 to compute the posterior mean and
standard deviation for  for all y = 0; 1; : : : ; n and plot the posterior mean and standard
deviation as a function of y.

MC <- function(y,n,a,b){
  theta <- rbeta(1000000,a,b)
  Y<- rbinom(1000000,n,theta)
  ind <- which(Y==y)
  ntheta<-theta(ind)
  theta_mn<-mean(ntheta)
  theta_sd<-sd(ntheta)
  output<-list(mean=theta_mn,
               stdev<-theta_sd)
  return(output)
}

out1 <- MC(y,n,a,b)
到这一步该怎么做?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群