[R] winbugs - R question
allan clark
allan at stats.uct.ac.za
Mon Oct 9 08:23:20 CEST 2006
hi all
i would like to use r and winbugs in order to undertake seemingly unrelated regression. i am using the R2WINBUGS library. i just simulated a simple example (sample size is 25) in order to get the correct code.
i suspect that the problem is in my definition of the prior. it wants a multivariate node.
the example could be extended by including more than 1 X variable in the system of equations. how can one specify the following prior: the sum of the estimated betas (including the constant) is normal "a" and variance "b" say?
my code is given below:
library(R2WinBUGS)
set.seed(1)
x1=rnorm(25)
x2=rnorm(25)
n=25
#i know that the systems are not related but this will be extended later.
y1=2+5*x1+rnorm(25)*2
y2=25-7*x2+rnorm(25)*2
X1=cbind(1,x1)
X2=cbind(1,x2)
Y=cbind(y1,y2)
I=diag(2)
J=diag(2)*0.001
m=matrix(0,nrow=2,ncol=0)
init<-list(b=matrix(0,nrow=2,ncol=2),tau=1)
inits<-list(init,init,init)
data<-c("n","Y","X1","X2","I","J","m")
parameters<-c("b","tau")
a<-bugs(data=data, inits=inits, parameters, model.file="c:/try/sur.txt", n.chains = 3, n.iter = 1000, bugs.directory = "c:/Program Files/WinBUGS14/", working.directory = "c:/try", clearWD = FALSE,codaPkg = FALSE,debug=T)
model
{
for (i in 1:n)
{
Y[i,1:2] ~ dmnorm(mu[i,],P[1:2,1:2])
# means in separate time series
mu[i,1] <- inprod(X1[i,],b[,1])
mu[i,2] <- inprod(X2[i,],b[,2])
P[1:2,1:2]<-tau*I[1:2,1:2]
}
# priors on regression coefficients
for (i in 1:2)
{
for (j in 1:2)
{
b[i,j]<-dnorm(0,0.001)
}
}
tau~dgamma(0.001,0.001)
}
thanking you in advance
/
allan
More information about the R-help
mailing list