独立抽样(一)
# simulations
wais<-read.csv("wais.csv",head=T)
y<-wais[,2]
x<-wais[,1]
m<-55000
mu.beta<-c(0,0)
sigma.beta<-c(100,100)
prop.s<-c(.1,.1)
beta<-matrix(nrow=m,ncol=2)
acc.prob<-0
current.beta<-c(0,0)
for(t in 1:m){
prop.beta<-rnorm(2,current.beta,prop.s)
cur.eta<-current.beta[1]+current.beta[2]*x
prop.eta<-prop.beta[1]+prop.beta[2]*x
loga<-(sum(y*prop.eta-log(1+exp(prop.eta))))-sum(y*cur.eta-log(1+exp(cur.eta)))+sum(dnorm(prop.beta,mu.beta,prop.s,log=T))-sum(dnorm(current.beta,mu.beta,prop.s,log=T))
u<-runif(1)
u<-log(u)
if(u<=loga){
current.beta<-prop.beta
acc.prob<-acc.prob+1
}
beta[t,]<-current.beta
}
#convergence diagnostic plot
erg.mean<-function(x){
n<-length(x)
result<-cumsum(x)/cumsum(rep(1,n))
}
burnin<-15000
idx<-seq(1,m,50