楼主: derekzhong9005
901 0

[问答] 【求助】R初学者,这个程序该怎么继续 [推广有奖]

  • 1关注
  • 0粉丝

高中生

30%

还不是VIP/贵宾

-

威望
0
论坛币
1364 个
通用积分
0.1500
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
586 点
帖子
4
精华
0
在线时间
47 小时
注册时间
2016-2-12
最后登录
2023-12-9

楼主
derekzhong9005 发表于 2016-2-12 10:05:05 |AI写论文
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)
到这一步该怎么做?

关键词:初学者 distribution Monte Carlo Likelihood Deviation overall percent course Create 初学者

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-4 08:33