[R] R2WinBUGS sending variables as factors

From: <James.Dell_at_csiro.au>
Date: Fri, 4 Jan 2008 11:15:37 +1100

Hello R and BUGS users,  

I am writing a heirarchical model in R to send to BUGS via R2WinBUGS and I am finding it difficult to get the model to run. I seem to be having two problems.
1) I can't seem to send variables classed as factors (Month), is there a way do this?
2) Checking the Log in WinBUGS I can see that the model is Syntactically correct, but Bugs is not able to recognise the the initial values for each of the chains.  

I have been following the examples provided in the R vignette for R2WinBUGS and I am a little stumped on what do do next. Any suggestions and help will be greatly appreciated.
The Model and the R Code have been using are below. The Error from WinBUGS is at the end of this mail.  

Kind Regards  

Jim Dell
Phd Candidate
Quantitative Marine Science
University of Tasmania and CSIRO Marine and Atmospheric Research ph +62 3 6232 5182
mb +61 429 32 5182  


    #Centre variables
 mSeaW <- mean(SeaW[])
 s_dSeaW <- sd(SeaW[])  

#normalise Variables

   nSeaWiFS <- mSeaW/s_dSeaW

 for(i in 1:N) {
  log(lambda[i]) <- delta0 + alpha1 * Month[i] + alpha2 * Lat[i] + beta1 * (SeaW[i] - nSeaW)        

  logit(p[i]) <- gamma0
  mu[i, 1] <- 0
  mu[i, 2] <- lambda[i]
  mu.i[i] <- mu[i, index[i]]

  index[i] ~ dcat(theta[i, 1:2])
  theta[i, 1] <- p[i]
  theta[i, 2] <- 1 - p[i]

  # mixture
  YFTCPUE[i] ~ dpois(mu.i[i])   


# recalculate the original intercept term
 Intercept <- delta0 - beta1 * nSeaW  

#prior on regression coefficients

 beta1 ~ dnorm(0,1.0E-6)

 alpha2 ~ dnorm(0,1.0E-6)
 alpha1 ~ dnorm(0, 1.0E-6)
 delta0 ~ dnorm(0, 1.0E-6)
 gamma0 ~ dnorm(0, 1.0E-6)       


##R Code  


#sampling functions
#get a subset of the data to use in the modelling process LearnSamp <- function (dBUGS) # data frame of the variables of interest

                rnames<- row.names(dBUGS)
                sampRows <- sample(rnames,900)
                learnSamp <- subset(dBUGS,rnames%in%sampRows)

#maybe not needed index.inits <- function () # get data into correct form for R2WinBUGS  

d <-LearnSamp(dBUGS)
Month <- d$Month
Lat <- d$Lat
SeaW <- d$SeaW
N <- nrow(d)  

data <- list("N", "YFTCPUE","Lat","SeaW") #,"Month" #Month Taken out so that model runs  

#create initial values for the coefficients in the model (this is model specific)
#construct inits for the three chain  

inits1 <- list(gamma0 = 0, delta0 = 0,beta1 = 0, alpha1 = 0, alpha2 = 0, index = rbern(J,0.5)+1)
inits2 <- list(gamma0 = 0, delta0 = 0,beta1 = 0, alpha1 = 0, alpha2 = 0, index = rbern(J,0.5)+1)
inits3 <- list(gamma0 = 0, delta0 = 0,beta1 = 0, alpha1 = 0, alpha2 = 0, index = rbern(J,0.5)+1)  

inits <- list(inits1,inits2,inits3)    

## these data.frames need to be trimmed so that we can send varibles to WinBUGS  

parameters <- c("gamma0", "delta0", "beta1", "alpha1","alpha2", "index") model <- bugs(data, inits, parameters,
  model.file = "C:/Documents and Settings/del125/My Documents/PhD data/ETBF - BUGS/CodeR2BUGS/MinusMonthSeaWiFS.bug",   codaPkg = FALSE, bugs.directory = "C:/Program Files/WinBUGS14/", debug =TRUE)         ##And the Error Message from WinBUGS  

model is syntactically correct

data(C:/Program Files/R/R-2.6.0/data.txt)

data loaded


made use of undefined node nSeaW

inits(1,C:/Program Files/R/R-2.6.0/inits1.txt)

command #Bugs:inits cannot be executed (is greyed out)

inits(2,C:/Program Files/R/R-2.6.0/inits2.txt)

command #Bugs:inits cannot be executed (is greyed out)

inits(3,C:/Program Files/R/R-2.6.0/inits3.txt)

command #Bugs:inits cannot be executed (is greyed out)


command #Bugs:gen.inits cannot be executed (is greyed out)



command #Bugs:update cannot be executed (is greyed out)

(error continues.....)

        [[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 04 Jan 2008 - 00:19:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 05 Jan 2008 - 14:00:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive