MainprogramOPEN<-function() ########################################################################################################## ########################################################################################################## # #Author: Ciprian Craniceanu #e-mail: ccrainic@jhsph.edu #Last revised: December 29, 2005 # # ########################################################################################################## ########################################################################################################## # #Description: # 1. Fits measurement error model for the OPEN data. WinBUGS model is stored in file # OPENdata_model.txt # 2. The mean and standard deviations of the outcome model parameters are stored and returned # ########################################################################################################## ########################################################################################################## # #Input: # # ########################################################################################################## ########################################################################################################## # #Output: # 1. output.Bayes: An Nsimulations*20 matrix containing the posterior mean and standard # deviations of the parameters of interest. # ########################################################################################################## ########################################################################################################## # #Call: # #MainprogramOPEN() # # ########################################################################################################## ########################################################################################################## {#begin function #Uses R2WinBUGS as an interface between R and WinBUGS library(R2WinBUGS) #Define the constants of the data and model Nobservations=223 nalphas=3 nbetas=2 #Load and assign data data.OPEN<-read.table(file="C:\\pctex_Raymond_Carroll_directory\\papers\\R2WinBUGS\\OPENdata\\simulatedOPENdata.txt",header=TRUE) attach(data.OPEN) output.Bayes<-rep(0,20) dim(output.Bayes)<-c(1,20) ################################################################################################## ################################################################################################## # # #Define data, initial values and parameters to be monitored in the Bayesian analysis # ################################################################################################## ################################################################################################## #Data data<-list("BMI","AGE","logprotein1","logprotein2","logFFQ1","logFFQ2","Nobservations","nalphas","nbetas") #Initial values inits<-function(){ list(lambda=0.5,taueps=0.1,taux=0.1,taur=0.1,alpha=c(0,0,0),beta=c(0,0)) } #Parameters parameters<-list("alpha","beta","attenuation","sigma2eps","sigma2x","sigma2u","sigma2r") #Run the Bayesian analysis of the Framingham data. Use 10000 burn-in and 300000 #simulations from the posterior distribution of the parameters given the data. #Retain only every 60th simulated sample date() fit<- bugs(data, inits, parameters, model.file = "OPEN.txt", n.chains = 1, n.iter = 100000, n.burnin = 10, n.thin = 100,debug = FALSE, DIC = FALSE, digits = 5, codaPkg = FALSE, bugs.directory = "c:/Program Files/WinBUGS14/") date() attach.all(fit) # Ray Code Here results<-fit$sims.matrix write.table(results,file="OPEN_Raystable.txt") output.Bayes[seq(1,20,by=2)]=c(fit$mean$alpha,fit$mean$beta,fit$mean$attenuation, fit$mean$sigma2eps,fit$mean$sigma2x,fit$mean$sigma2u,fit$mean$sigma2r) output.Bayes[seq(2,20,by=2)]<-c(fit$sd$alpha,fit$sd$beta,fit$sd$attenuation, fit$sd$sigma2eps,fit$sd$sigma2x,fit$sd$sigma2u,fit$sd$sigma2r) write.table(output.Bayes,file="OPEN_Bayesbetaparametersoutput.txt") return(list(output.Bayes)) }#end function