Hi,
The following might be of interest to some. I show my initial question to
Andrew Thomas and his reply.
Hi Andrew,
In the manual it states:
Repeated structures are specified using a "for-loop". The syntax for
this is:
for (i in a:b) {
list of statements to be repeated for
increasing values of loop-variable i
}
Note that neither a nor b may be stochastic
However, I have found that:
for (i in 1:N){
for (j in 1:i-1){
}
}
works as I expected. I know that 'i' is not stochastic in the WinBUGS
sense, but I just wanted to check that this is not a surprise and that I
am not mistaken in believing that this looping structure is safe to use.
Hello Phil,
WinBUGS must be able to evaluate the limits in loop index expressions at
compile, ie before the simulation starts. The loop construct makes the loop
index variable visible (in scope) from just after the "{" to just before the
closing "}". So in your example the inner for loop knows about the i
variable. This is not a feature I remember anyone else using so I can only
hope it will continue to work in future releases of WinBUGS
Best regards
Andrew
I was using it to include a 2 factor interaction term in a linear model:
#LIKELIHOOD
for (i in 1:N){
for (j in 1:P){
z.main[i,j] <- x[i,j]*b[j,j]
for (k in 1:j-1){
z.2fi[i,j,k] <- x[i,j]*x[i,k]*b[j,k]
}
}
sum.z2fi[i,1] <- 0
for (j in 2:P){
sum.z2fi[i,j] <- sum(z.2fi[i,j,1:j-1])
}
mu[i] <- beta0 + sum(z.main[i,1:P]) + sum(sum.z2fi[i,1:P])
Y[i] ~ dnorm(mu[i], tau)
}
If anyone knows of a more efficient (ie quicker running) way of specifying
these types of models I would be very interested to hear from them.
Phil Woodward
-------------------------------------------------------------------
This list is for discussion of modelling issues and the BUGS software.
For help with crashes and error messages, first mail [log in to unmask]
To mail the BUGS list, mail to [log in to unmask]
Before mailing, please check the archive at www.jiscmail.ac.uk/lists/bugs.html
Please do not mail attachments to the list.
To leave the BUGS list, send LEAVE BUGS to [log in to unmask]
If this fails, mail [log in to unmask], NOT the whole list
|