GG695 Posterior Assignment 6


title: "R Notebook"
output: html_notebook


Exercise 6.01

Beta distribution.

The shape parameter a increases fromleft to right across the columns,while the shape parameter b increases from top to bottom across the rows.

Exercise 6.02

(a) Map coordinates are interval-scale metric data.
(b) Distance are ratio-scale metric data.
(c) Temperatures are interval-scale metric data.
(d) Sorry, I don't understand the question. What is the meaning of start with eff and ell? Does it mean that like "effect" or something?
I think the categorical variables are factors in R, and their possible values are levels. But they are not start with eff and ell.
(e) exp(), log()

Exercise 6.1


source("C:/Users/User/Documents/DBDA2E-utilities.R")
source("C:/Users/User/Documents/BernBeta.R")
post=BernBeta(c(4,4), c(1)) #Exercise 6.1(a),the posterior is dbeta(theta|5,4)
post=BernBeta(post,c(1)) #Exercise 6.1(b),the prior is dbeta(theta|5,4), the posterior is dbeta(theta|6,4)
post=BernBeta(post,c(0)) #Exercise 6.1(c),the prior is dbeta(theta|6,4), the posterior is dbeta(theta|6,5)
post=BernBeta(c(4,4), c(0,1,1)) #Exercise 6.1(d), the prior is dbeta(theta|4,4), the posterior is dbeta(theta|6,5). Thus, they have the same posterior.
dev.off()

#Plot the priors and posteriors
x= seq(0.001,0.999,by=0.001) #Points for plotting
curve(dbeta(x,4,4),col='blue',ylim=c(0,3),xlab=bquote(theta),ylab="Distribution") #prior dbeta(theta|4,4)
curve(dbeta(x,5,4),col='red',add=T) #prior or posterior dbeta(theta|5,4)
curve(dbeta(x,6,4),col='green',add=T) #prior or posterior dbeat(theta|6,4)
curve(dbeta(x,6,5),col='black',add=T) #posterior dbeat(theta|6,5)
legend("topleft",legend=c("dbeta(theta|4,4)","dbeta(theta|5,4)","dbeta(theta|6,4)","dbeta(theta|6,5)"), lty=c(1,1),col=c("blue","red","green","black") )  

Exercise 6.2

source("C:/Users/User/Documents/DBDA2E-utilities.R")
source("C:/Users/User/Documents/BernBeta.R")
N=100 #the total sample size
sampleA=58 #the sample size of choosing A
#Assume perfer A=1, perfer B=0
CanA=rep(1,times=sampleA)#The choice set of candidate A
CanB=rep(0,times=(N-sampleA))#The choice set of candidate B
post=BernBeta(priorBetaAB = c(1,1),Data=c(CanA,CanB),showHDI = T,HDImass=0.95, showCentTend = "Mean") #Exercise 6.1(a),95% BCI (0.483,0.673)

N=100 #the total sample size
sampleA=57 #the sample size of choosing A
CanA=rep(1,times=sampleA)#The choice set of candidate A
CanB=rep(0,times=(N-sampleA))#The choice set of candidate B
post=BernBeta(post,Data=c(CanA,CanB),showHDI = T,HDImass=0.95, showCentTend = "Mode") #Exercise 6.1(b),95% BCI (0.506,0.642)

Exercise 6.4

source("C:/Users/User/Documents/DBDA2E-utilities.R")
source("C:/Users/User/Documents/BernBeta.R")

post=BernBeta(priorBetaAB = c(0.1,0.1), Data=c(rep(1,times=4),0),showHDI=T,showCentTend = "Mode")

Exercise 6.5

source("C:/Users/User/Documents/DBDA2E-utilities.R")
source("C:/Users/User/Documents/BernBeta.R")
post=BernBeta(priorBetaAB = c(100,100),Data = c(rep(1,9),0),showHDI = T,showCentTend = "Mean") #I have changed the prior for sevearl times, and found when the values of a and b get bigger, the mean of posterior gets closer to 0.5. Thus, bigger a and b means a "fair" prior, and will lead to a "fair" result.

post=BernBeta(priorBetaAB = c(0.01,0.01),Data=c(rep(1,9),0), showHDI = T,showCentTend = "Mean") # The smaller values of a and b lead to a "baisd" result. The mean equals to 0.899.

Exercise 6.6

Sorry I don't understand this exercise. Does it mean the posterior mean?
$$
Pr(Heads|y)=\frac{a+1}{10+a+b}
$$

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 那年二月,还没过春节,第一批知青到来了。公社给大队分了八个,除两兄妹二人外,大队给每个生产队分了一人。 初到三天没...
    谈马阅读 791评论 2 7
  • 感赏同事大姐给我桃子饼干吃,对我真好 感赏同事带文具给我儿子 感赏儿子老公带给我的两份好工作,有你们我才有这两份工...
    吴若阅读 196评论 0 2
  • 6月26日,北京又摇出了新一轮的买车摇号中签者。摇中者满心欢喜,未摇中者又是失望一场。买车摇号一直是僧多粥少,这种...
    小易车友会阅读 210评论 0 1