资源描述
Cliquez pour modifier le style du titre,Cliquez pour modifier les styles du texte du masque,Deuxime niveau,Troisime niveau,Quatrime niveau,Cinquime niveau,*,Introduction to,WinBUGS,Olivier,Gimenez,University of St Andrews,Scotland,A brief history,1989,:project began with a Unix version called BUGS,1998,:first Windows version,WinBUGS,was born,Initially developed by the,MRC Biostatistics Unit,in Cambridge and now joint work with,Imperial College School of Medicine,at St Marys,London.,Win,dows,B,ayesian inference,U,sing,G,ibbs,S,ampling,Software for the Bayesian analysis of complex statistical models using Markov chain Monte Carlo(MCMC)methods,Nicky Best,Imperial College Faculty of Medicine,London(UK),Thomas Andrew,University of Helsinki,Helsinki(Finland),David,Spiegelhalter,MRC Biostatistics Unit Institute of Public Health Cambridge(UK),Wally,Gilks,MRC Biostatistics Unit Institute of Public Health Cambridge(UK),Who?,Freely downloadable from:,www.mrc-bsu.cam.ac.uk/bugs/winbugs/contents.shtml,Key principle,You,specify the prior and build up the likelihood,WinBUGS,computes the posterior by running a Gibbs sampling algorithm,based on:,(,|D),/,L(D|,),(,),WinBUGS,computes some convergence diagnostics that,you,have to check,Demographic,components,(,fecundity,breeding,success,survival,etc,),A,biological,example,throughout,White,stork,(,Ciconia,ciconia,)in Alsace 1948-1970,Climate,(,Temperature,rainfall,etc,),15.1 67,13.3 52,15.3 88,13.3 61,14.6 32,15.6 36,13.1 72,13.1 43,15.0 92,11.7 32,15.3 86,14.4 28,14.4 57,12.7 55,11.7 66,11.9 26,15.9 28,13.4 96,14.0 48,13.9 90,12.9 86,15.1 78,13.0 87,2.55,1.85,2.05,2.88,3.13,2.21,2.43,2.69,2.55,2.84,2.47,2.69,2.52,2.31,2.07,2.35,2.98,1.98,2.53,2.21,2.62,1.78,2.30,WinBUGS,&,Linear,Regression,Y,Number,of,chicks,per,pairs,T,Temp.May(C),R,Rainf,.May(mm),1.Do,temperature,and,rainfall,affect,the,number,of,chicks,?,Y,i,=,+,r,R,i,+,t,T,i,+,i,i=1,.,23,i,i.i.d.,N(0,2,),2.,Regression,model:,3.Estimation,of,parameters,:,r,t,4.,Frequentist,inference,uses t-tests,Y,i,i.i.d.,N(,i,2,),i=1,.,23,i,=,+,r,R,i,+,t,T,i,WinBUGS,&,Linear,Regression,15.1 67,13.3 52,15.3 88,13.3 61,14.6 32,15.6 36,.,13.0 87,2.55,1.85,2.05,2.88,3.13,2.21,.,2.30,Y,Number,of,chicks,per,pairs,T,Temp.May(C),R,Rainf,.May(mm),Estimate,Std,.,Error,t value,Pr,(|t|),temperature,0.0310690.054690 0.568 0.57629,rainfall,-0.0073160.002897 -2.525 0.02011*,Linear,Regression,using,Frequentist,approach,Y=2.451 +0.031 T -0.007 R,15.1 67,13.3 52,15.3 88,13.3 61,14.6 32,15.6 36,.,13.0 87,2.55,1.85,2.05,2.88,3.13,2.21,.,2.30,Y,Number,of,chicks,per,pairs,T,Temp.May(C),R,Rainf,.May(mm),Linear,Regression,using,Frequentist,approach,Influence,of,Rainfall,only,Estimate,Std,.,Error,t value,Pr,(|t|),temperature,0.0310690.054690 0.568 0.57629,rainfall,-0.0073160.002897 -2.525 0.02011*,Y=2.451 +0.031 T -0.007 R,Running,WinBUGS,What,do,you,need,?,1-a model giving the likelihood and the priors,2-some data of course,3-initial values to start the MCMC algorithm,Running,WinBUGS,The,model,use the,WinBUGS,command model,dont forget to embrace the model with.,Define the likelihood.,Y,i,N(,+,r,R,i,+,t,T,i,2,),Note,:,2,=1/,Specify the priors,We use,noninformative,or,vague,or,flat,priors here,Monitor any other parameter youd like to.,e.g.,2,=1/,Running,WinBUGS,Data,and,initial values,Use list structures(R/,Splus,syntax).,.and vector structures(R/,Splus,syntax),Running,WinBUGS,Overall,1-a model giving the likelihood and the priors,2-data,3-initial values,1-check model,2-load data,3-compile model,4-load initial values,5-generate burn-in values,6-parameters to be monitored,7-perform the sampling to generate posteriors,8-check convergence and display results,Running,WinBUGS,At,last!,Running,WinBUGS,1.,Check,model,Running,WinBUGS,1.,Check,model,:,highlight,model,Running,WinBUGS,1.,Check,model,:,open the Model Specification Tool,Running,WinBUGS,1.,Check,model,:,Now,click,check,model,Running,WinBUGS,1.,Check,model,:,Watch out for the confirmation at the foot of the screen,Running,WinBUGS,2.,Load,data:,Now highlight the list in the data window,Running,WinBUGS,2.,Load,data:,then,click,load,data,Running,WinBUGS,2.,Load,data:,watch,out for,the,confirmation,at,the,foot,of,the,screen,Running,WinBUGS,3.Compile,model,:,Next,click compile,Running,WinBUGS,3.Compile,model,:,watch,out for,the,confirmation,at,the,foot,of,the,screen,Running,WinBUGS,4.,Load,initial values:,highlight the list in the data window,Running,WinBUGS,4.,Load,initial values:,click load,inits,Running,WinBUGS,4.,Load,initial values:,watch out for the confirmation at the foot of the screen,Running,WinBUGS,5.,Generate,Burn-in,values:,Open the Model Update Tool,Running,WinBUGS,5.,Generate,Burn-in,values:,Give the number of burn-in iterations(1000),Running,WinBUGS,5.,Generate,Burn-in,values:,click update,to do,the,sampling,Running,WinBUGS,6.Monitor,parameters,:,open the Inference Samples Tool,Running,WinBUGS,6.Monitor,parameters,:,Enter intercept in the node box and click set,Running,WinBUGS,6.Monitor,parameters,:Enter,slope_temperature,in the node box and click set,Running,WinBUGS,6.Monitor,parameters,:Enter,slope_rainfall,in the node box and click set,Running,WinBUGS,7.,Generate,posterior,values,:enter the number of samples you want to take(10000),Running,WinBUGS,7.,Generate,posterior,values,:click update to do the sampling,Running,WinBUGS,8.,Summarize,posteriors,:Enter*in the node box and click stats,Running,WinBUGS,8.,Summarize,posteriors,:mean,median and credible intervals,Running,WinBUGS,8.,Summarize,posteriors,:95%Credible intervals,tell us the same story,Estimate,Std,.,Error,t value,Pr,(|t|),temperature,0.0310690.054690 0.568,0.57629,rainfall,-0.0073160.002897 -2.525,0.02011*,Running,WinBUGS,8.,Summarize,posteriors,:95%Credible intervals,tell us the same story,Estimate,Std,.,Error,t value,Pr,(|t|),temperature,0.031069,0.054690 0.568 0.57629,rainfall,-0.007316,0.002897 -2.525 0.02011*,Running,WinBUGS,8.,Summarize,posteriors,:click history,Running,WinBUGS,8.,Summarize,posteriors,:click auto,cor,Problem,of,autocorrelation,Coping,with,autocorrelation,use,standardized,covariates,Coping,with,autocorrelation,use,standardized,covariates,Re-running,WinBUGS,1,2,.7,and,8.,Summarize,posteriors,:click auto,cor,autocorrelation,OK,Re-running,WinBUGS,1,2,.7,and,8.,Summarize,posteriors,:click density,Re-running,WinBUGS,1,2,.7,and,8.,Summarize,posteriors,:click,quantiles,Running,WinBUGS,8.,Checking,for convergence,using,the,Brooks-Gelman-Rubin,criterion,A way to identify non-convergence is to simulate multiple sequences for over-dispersed starting points,Intuitively,the,behaviour,of all of the chains should be basically the same.,In other words,the variance within the chains should be the same as the variance across the chains.,In,WinBUGS,stipulate the number of chains after load data and before compile(obviously,as many sets of initial values as chains have to be loaded,or generated),Running,WinBUGS,8.,Checking,for convergence,using,the,Brooks-Gelman-Rubin,criterion,The normalized width of the central 80%interval of the pooled runs is green,The normalized average width of the 80%intervals within the individual runs is blue,Re-running,WinBUGS,1,2,.7,and,8.,Summarize,posteriors,:others.,Click coda to produce lists of data suitable for external treatment via the Coda R package,Click trace to produce dynamic history changing in real time,15.1 67,13.3 52,15.3 88,13.3 61,14.6 32,15.6 36,13.1 72,13.1 43,15.0 92,11.7 32,15.3 86,14.4 28,14.4 57,12.7 55,11.7 66,11.9 26,15.9 28,13.4 96,14.0 48,13.9 90,12.9 86,15.1 78,13.0 87,151/173,105/164,73/103,107/113,113/122,87/112,77/98,108/121,118/132,122/136,112/133,120/137,122/145,89/117,69/90,71/80,53/67,41/54,53/58,31/39,35/42,14/23,18/23,Another,example,:,logistic,regression,Y,Proportion,of,nests,with,success,(0,youngs,),T,Temp.May(C),R,Rainf,.May(mm),Performing,a,logistic,regression,with,WinBUGS,model,#,succ,.in year,i,Bin,(,p,i,total#couples in year,i,),where,p,i,the probability of success in year,i,logit(p,i,)=,+,r,R,i,+,t,T,i,i=1,.,23,Performing,a,logistic,regression,with,WinBUGS,noninformative,priors,Performing,a,logistic,regression,with,WinBUGS,data&initial values,Performing,a,logistic,regression,with,WinBUGS,Performing,a,logistic,regression,with,WinBUGS,the,results,influence of rainfall,but not temperature(see,credible intervals,),lower,upper,additional parameters as a by-product of the MCMC samples:just add them in the model as parameters to be monitored,-,geometric mean,:,geom,-pow(prod(p),1/N),-,odds-ratio,:,odds.rainfall,-,exp(slope.rainfall,),odds.temperature,-,exp(slope.temperature,),Performing,a,logistic,regression,with,WinBUGS,the,results,additional parameters as a by-product of the MCMC samples,-,geom.mean,probability of success around 82%81%;84%,-,odds-ratio,:-16%for an increase of rainfall of 1 unit,Performing,a,logistic,regression,with,WinBUGS,the,results,Running,WinBUGS,from,R:R2WinBUGS package,The,logistic,regression,example,revisited,It may be uneasy to read complex sets of data and initial values,It is also quite boring to specify the parameters to be monitored in each run,It might be interesting to save the output and read it into R for further analyses,solution 1,:,WinBUGS,can be used in batch mode using scripts,solution 2,:R2WinBUGS allows,WinBUGS,to be run from R,We can work with the results after importing them back into R again,create graphical displays of data and posterior simulations or use,WinBUGS,in Monte Carlo simulation studies,Running,WinBUGS,from,R:R2WinBUGS package,The,logistic,regression,example,revisited,To call,WinBUGS,from R:,1.Write a,WinBUGS,model in a ASCII file.,2.Go into R.,3.Prepare the inputs to the bugs function and run it,4.A,WinBUGS,window will pop up,amd,R will freeze up.The,model will now run in,WinBUGS,.You will see things happening,in the Log window within,WinBUGS,.When,WinBugs,is done,its,window will close and R will work again.,If an error message appears,re-run with debug=TRUE.,Running,WinBUGS,from,R:R2WinBUGS package,1-,Write,the,WinBUGS,code in a ASCII file,#This covers logistic regression model using two explicative variables.,#The White storks in Baden-Wurttemberg(Germany)data set is provided,model,for(i in 1:N),#A binomial distribution as a likelihood,nbsuccessi,dbin(pi,nbpairsi,),#The probability of success is a function of both rainfall and temperature,logit(pi,)-intercept+,slope.temperature,*(,temperaturei,-,mean(temperature)/(sd(temperature,)+,slope.rainfall,*(,rainfalli,-,mean(rainfall)/(sd(rainfall,),#priors for regression parameters,intercept dnorm(0,0.001),slope.temperature,dnorm(0,0.001),slope.rainfall,dnorm(0,0.001),Running,WinBUGS,from,R:R2WinBUGS package,3-,Prepare the inputs to the bugs function,Nbsuccess,nbpairs,temperaturerainfall,15117315.167,105 164 13.3 52,73 103 15.3 88,107 113 13.3 61,113 122 14.6 32,87 112 15.6 36,53 58 14.0 48,31 39 13.9 90,35 42 12.9 86,14 23 15.1 78,18 23 13.0 87,Running,WinBUGS,from,R:R2WinBUGS package,The,logistic,regression,example,revisited,#Load R2WinBUGS package,library(R2WinBUGS),#Data(R list format),N=23,data=,read.table(datalogistic.dat,header,=T),attach(data,),datax,=,list(N,nbsuccess,nbpairs,temperature,rainfall,),#MCMC details,nb.iterations,=10000,nb.burnin,=1000,Running,WinBUGS,from,R:R2WinBUGS package,The,logistic,regression,example,revisited,#Initial values,init1=,list(intercept,=-1,slope.temperature=-1,slope.rainfall=-1),init2=,list(intercept,=0,slope.temperature=0,slope.rainfall=0),init3=,list(intercept,=1,slope.temperature=1,slope.rainfall=1),inits,=list(init1,init2,init3),nb.chains,=,length(inits,),#Parameters to be monitored,parameters-,c(intercept,slope.temperature,slope.rainfall,),#MCMC simulations,res.sim,-,bugs,(datax,inits,parameters,logisticregressionstandardized.bug,n.chains,=,nb.chains,n.iter,=,nb.iterations,n.burnin,=,nb.burnin,),#Save results,save(res.sim,file=,logistic.Rdata,),Running,WinBUGS,from,R:R2WinBUGS package,The,logistic,regression,example,revisited,#Summarize results,res.sim$summary,#use,print(res.sim,)alternatively,mean,sd,50%97.5%,Rhat,Intercept 1.55122555 0.05396574 1.55100 1.6589750 1.0020641,slope.temperature,0.03030854 0.06128879 0.03148 0.1510975 0.9997848,slope.rainfall,-0.15302837 0.06206554 -0.15295 -0.0243025 1.0014894,deviance 204.60259481 2.48898337 203.90000 211.2000000 1.0002280,Running,WinBUGS,from,R:R2WinBUGS package,The,logistic,regression,example,revisited,#Numerical summaries for,slope.rainfall,?,quantile(slope.rainfall,c(0.025,0.975),2.5%97.5%,-0.2693375-0.0243025,#Calculate the odds-ratio,odds.rainfall,-,exp(slope.rainfall,),quantile(odds.rainfall,c(0.025,0.975),2.5%97.5%,0.7638855 0.9759904,Running,WinBUGS,from,R:R2WinBUGS package,The,logistic,regression,example,revisited,#Graphical summaries for,slope.rainfall,?,plot(density(slope.rainfall),xlab,=,ylab,=,main=,slope.rainfall,a,posteriori,density),Recent,developments,in,WinBUGS,Model,selection,using,RJMCMC,We consider data relating to population of White storks breeding in Baden W,rttemberg,(Germany).,Interest lies in the impact of climate variation(,rainfall,)in their wintering area on their population dynamics(,adult survival rates,).,Mark-recapture data from 1956-71 are available.,The covariates relate to the amount of rainfall between June-September each year from 10 weather stations located around the,Sahel,region.,Interest lies in identifying the given rainfall locations that explain the adult survival rates.,Bayesian Model Selection,Discriminating between different models can often be of particular interest,since they represent competing biological hypotheses.,How do we decide which covariates to use?often there may be a large number of possible covariates.,Example(cont),We express the survival rate as a logistic function of the covariates:,logit,t,=,+,T,x,t,+,t,where,x,t,denotes the set of covariate values at time,t,and,t,are random effects,t,N(0,2,).,However,which rainfalls explain the survival rates for the adults?,Alternatively,what values of,are non-zero?,Model Selection,In the classical framework,likelihood ratio tests or information criterion(e.g.AIC)are often used.,There is a“similar Bayesian statistic the DIC.,This is programmed within,WinBUGS,however its implementation is not suitable for hierarchical models(e.g.random effect models).,In addition,the DIC is known to give fallacious results in even simple problems.,Within the general Bayesian framework,there is a more natural way of dealing with the issue of model discrimination.,Bayesian Approach,We treat the model itself as an unknown parameter to be estimated.,Then,applying,Bayes,Theorem we obtain the posterior distribution over both parameter and model space:,(,m,m,|data),L(data,|,m,m,),p,(,m,),p,(,m,).,Here,m,denotes the parameters in model,m,.,Likelihood,Prior on parameters in model,m,Prior on model,m,Posterior Model Probabilities,The Bayesian approach then allows us to quantitatively discriminate between competing models via posterior model probabilities:,(,m,|data)=,s,(,m,m,|data),d,m,p,(,m,),s,L(data,|,m
展开阅读全文