R2WinBUGS sending variables as factors

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

R2WinBUGS sending variables as factors

James.Dell
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
 
model
{
    #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
 
library(R2WinBUGS)
library(Rlab)
 
#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)
YFTCPUE <- d$CPUE
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

compile(3)

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)

gen.inits()

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

thin.updater(3)

update(334)

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

(error continues.....)


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R2WinBUGS sending variables as factors

Uwe Ligges
James,

the first non-regular message you got in the output was:

"made use of undefined node nSeaW",

so what about defining nSeaW (or did you mean to write mSeaW)?

If this is not the problem, please provide the data so that we can try
to reproduce your error, as the psoting guide (of R-help) ask you to do.
Please do not crosspost (at least not on R-help), the posting guide asks
you not to do so.

Best,
Uwe Ligges




[hidden email] wrote:

> 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
>  
> model
> {
>     #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
>  
> library(R2WinBUGS)
> library(Rlab)
>  
> #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)
> YFTCPUE <- d$CPUE
> 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
>
> compile(3)
>
> 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)
>
> gen.inits()
>
> command #Bugs:gen.inits cannot be executed (is greyed out)
>
> thin.updater(3)
>
> update(334)
>
> command #Bugs:update cannot be executed (is greyed out)
>
> (error continues.....)
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R2WinBUGS sending variables as factors

Gregor Gorjanc-2
In reply to this post by James.Dell
 <James.Dell <at> csiro.au> writes:
...
> 1) I can't seem to send variables classed as factors (Month), is there a
> way do this?

You can not use factors per se in BUGS. You have to convert them to numeric
(integer) variables before.

> 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.
...

> model
> {
>     #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)

I guess you want to use mSeaW here instead of nSeaW!
                        ^                     ^
>  # recalculate the original intercept term
>  Intercept <- delta0 - beta1 * nSeaW

I guess you want to use nSeaWiFS here instead of nSeaW!
...

>
> ##And the Error Message from WinBUGS
>
> model is syntactically correct
>
> data(C:/Program Files/R/R-2.6.0/data.txt)
>
> data loaded
>
> compile(3)
>
> made use of undefined node nSeaW

This warned you that the model is not OK!

Gregor

______________________________________________
[hidden email] 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.