Vous êtes sur la page 1sur 7

We define the priors and we plot the prior distributions for each

parameter(alpha,beta,phi).

> win.graph(width=9, height=3)

> par(mfrow=c(1,3),oma=c(0,0,2,0))

> mu.alpha <- 0

> sigma.alpha <- 100

> alpha <- seq(mu.alpha-3*sigma.alpha,mu.alpha+4*sigma.alpha,0.1)

> sigmasq.alpha <- sigma.alpha^2

> phi.alpha <- 1/sigmasq.alpha

> palpha <- dnorm(alpha,mu.alpha,sigma.alpha)

> plot(alpha,palpha,type="l",main="alpha",col="red")

> mu.beta <- 0

> sigma.beta <- 100

> beta <- seq(mu.beta-3*sigma.beta,mu.beta+4*sigma.beta,0.1)

> sigmasq.beta <- sigma.beta^2

> phi.beta <- 1/sigmasq.beta

> pbeta <- dnorm(beta,mu.beta,sigma.beta)

> plot(beta,pbeta,type="l",main="beta",col="red")

> a.phi <- 1

> b.phi <- 1

> mean.phi <- a.phi/b.phi

> sd.phi <- sqrt(a.phi/(b.phi^2))

> phi <- seq(0.01,mean.phi+3*sd.phi,0.01)

> pphi <- dgamma(phi,a.phi,b.phi)

> plot(phi,pphi,type="l",main="phi",col="red")
> mtext("Priors",line=0.5,outer=T)

We obtained prior distributions.

We define the number of simulations and the burn-in period. A number of 200000 simulations from the
equilibrium distribution will be considered, but the first 100000 will be ignored (dropped). So only the
last 100000 simulated values will be used for our inference.

> Burn <- 100000

> Reps <- 200000

> alpha.Gibbs <- numeric(Reps)

> beta.Gibbs <- numeric(Reps)

> phi.Gibbs <- numeric(Reps)

> alpha.in <- 0


> beta.in <- 0

> phi.in <- 1

> alpha.Gibbs[1] <- alpha.in

> beta.Gibbs[1] <- beta.in

> phi.Gibbs[1] <- phi.in

#===Data===
Y<-c(1,3,3,3,5)
x<-c(1,2,3,4,5)

#===Components of least squares regression===


(n.obs<-length(Y))
(Y.bar<-mean(Y))
(x.bar<-mean(x))
(YmYbar<-Y-Y.bar) #Centred Y
(xmxbar<-x-x.bar) #Centred x
(SSY<-sum(YmYbar^2)) #Corrected sum of squares of Y
(SSx<-sum(xmxbar^2)) #Corrected sum of squares of x
(SSYx<-sum(YmYbar*xmxbar)) #Corrected sum of products of x and Y
#LS estimate of beta =
SSYx/SSx
#LS estimate of alpha with model parameterised with centred x = Y.bar
#Var(SSYx/SSx)
=(1/SSx)*sigma^2
#Var(Y.bar)
=(1/n.obs)*sigma^2

set.seed(1)
system.time(
for(h in 2:Reps){

# generate an alpha Gibbs sample


P <- phi.Gibbs[(h-1)]*n.obs
Q <- phi.alpha
pm.alpha <- (P/(P+Q))*Y.bar + (Q/(P+Q))*mu.alpha
pp.alpha <- P+Q
alpha.Gibbs[h] <- rnorm(1,pm.alpha,sqrt(1/pp.alpha))

# generate a beta Gibbs sample


P <- phi.Gibbs[(h-1)]*SSx
Q <- phi.beta
pm.beta <- (P/(P+Q))*(SSYx/SSx) + (Q/(P+Q))*mu.beta
pp.beta <- P+Q
beta.Gibbs[h] <- rnorm(1,pm.beta,sqrt(1/pp.beta))

# generate a phi Gibbs sample


pa.phi <- (n.obs/2) + a.phi
SSE <- sum((Y - (alpha.Gibbs[h] + beta.Gibbs[h]*xmxbar))^2)
pb.phi <- (SSE/2) + b.phi
phi.Gibbs[h] <- rgamma(1,pa.phi,pb.phi)

})

user system elapsed


7.30 0.00 7.38

#---Quick check of the chain---


alpha.Gibbs[1:10]
beta.Gibbs[1:10]
phi.Gibbs[1:10]

> alpha.Gibbs[1:10]
[1] 0.000000 2.719784 3.200123 3.471718 2.628536 3.535587 3.179302
3.343338
[9] 3.442093 2.670470
> beta.Gibbs[1:10]
[1] 0.0000000 0.8580648 0.2742523 1.0601760 0.4227764 0.7848617
0.4397609
[8] 0.8231414 1.4743099 1.0035676
> phi.Gibbs[1:10]
[1] 1.0000000 0.8579634 0.4896854 0.9256390 0.8820678 1.1801804
1.0375200
[8] 0.2592386 0.4214273 2.5518368

The Markov chains have forgotten their starting values!

> par(mfrow=c(4,1),oma=c(0,0,2,0))
>
> ts.plot(alpha.Gibbs,main="alpha")
> ts.plot(beta.Gibbs,main="beta")
> ts.plot(phi.Gibbs,main="phi")
>
> sigma.Gibbs <- 1/sqrt(phi.Gibbs)
> ts.plot(sigma.Gibbs,main="sigma")
>
> mtext("History plots", line = 0.5, outer=T)
Alpha si beta au convergenta, iar phi si sigma nu au convergenta.

Prior to Posterior Analysis

par(mfrow=c(2,2),oma=c(0,0,2,0))

# alpha
y.density <- density(alpha.Gibbs[(Burn+1):Reps], width = 1)
y.max <- max(y.density$y)

plot(alpha,palpha,type="l",main="alpha",ylim=c(0,y.max),xlim=c(-
1,7),col="red",lwd=2)
lines(y.density,type="l",col="darkgreen")
legend(locator(1),c("Prior","Posterior"),text.col=c("red","darkgreen")
,bty="n",cex=.6)

# beta
y.density <- density(beta.Gibbs[(Burn+1):Reps], width = 1)
y.max <- max(y.density$y)
plot(beta,pbeta,type="l",main="beta",ylim=c(0,y.max),xlim=c(-
2,4),col="red",lwd=2)
lines(y.density,type="l",col="darkgreen")
legend(locator(1),c("Prior","Posterior"),text.col=c("red","darkgreen")
,bty="n",cex=.6)

# phi
y.density <- density(phi.Gibbs[(Burn+1):Reps], width = 1)
y.max <- max(y.density$y)
plot(phi,pphi,type="l",main="phi",ylim=c(0,y.max),xlim=c(0,20),col="re
d")
lines(y.density,type="l",col="darkgreen")
legend(locator(1),c("Prior","Posterior"),text.col=c("red","darkgreen")
,bty="n",cex=.6)

# sigma
y.density <- density(sigma.Gibbs[(Burn+1):Reps], width = 1)
y.max <- max(y.density$y)
summary(sigma.Gibbs[(Burn+1):Reps])
plot(y.density,type="l",
main="sigma",ylim=c(0,y.max),xlim=c(0,7),col="darkgreen",xlab="sigma")
legend(locator(1),c("Posterior"),text.col=c("darkgreen"),bty="n",cex=.
6)

mtext("Prior to Posterior plots", line = 0.5, outer=T)

Vous aimerez peut-être aussi