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}
$$

你可能感兴趣的:(GG695 Posterior Assignment 6)