我正在学习R2WinBUGS。我毫不怀疑。谁能帮我解决这个问题?当我运行此示例时,出现错误消息“ ratsmodel1.txt”不存在。我应该在哪里保存模型文件代码?我该如何使用R2WinBUGS从R调用它?
setwd("C:/Program Files/R/R-3.5.1/")
ratsmodel1<-function(){
for( i in 1:N){
for(j in 1:T){
Y[i,j]~dnorm(mu[i,j],tau.c)
mu[i,j]<-alpha[i]+beta[i]+(x[j]-xbar)
}
alpha[i]~dnorm(alpha.c,alpha.tau)
beta[i]~dnorm(beta.c,beta,tau)
}
tau.c~dgamma(0.001,0.001)
sigma<-1/sqrt(tau.c)
alpha.c~dnorm(0.0,1.0E-6)
alpha.tau~dgamma(0.001,0.001)
beta.c~dnorm(0.0,1.0E-6)
beta.tau~dgamma(0.001,0.001)
alpha0<-alpha.c-xbar*beta.c
}
if(is.R()){
filename<-file.path(getwd(),"ratsmodel1.txt")
}
write.model(ratsmodel,filename)
file.show(filename)
library(R2WinBUGS)
rats.data<-list(
x=c(8.0,15.0,22.0,29.0,36.0),
xbar=22,
N=30,
T=5,
Y= structure(.Data=c(...),.Dim=c(30,5)))
Y<-rats.data$Y
T<-rats.data$T
x<-rats.data$x
xbar<-rats.data$xbar
N<-rats.data$N
rats.data<-list(Y=Y,x=x,T=T,N=N,xbar=xbar)
rats.params<-c("sigma","beta.c","alpha0")
rats.inits<-
list(alpha=c(...),beta=c(...),alpha.c=c(150),beta.c=c(10),
tau.c=c(1),beta.tau=c(100),alpha.tau=c(100))
ratsfit=bugs(rats.data,rats.inits,parameters.to.save=rats.params,
model.file="rat smodel1.txt",n.chain=7,n.iter=2000,n.burnin=1000,
n.thin=1,debug= FALSE,codaPkg=TRUE,
bugs.directory="C:/Program Files/WinBUGS14/",program=="WinBUGS")
rats.coda=read.bugs(ratsfit)
summary(rats.coda)