我正在尝试通过R
在R2Openbugs
中运行一些代码,遇到无法解决的问题。即使我尝试了其他建议的各种补救措施,它也会返回相同的错误代码。我将不胜感激任何帮助。预先感谢。
这是r代码:
Fullocc<-read.csv("FinalOccSheet.csv")
Fullocc<-as.matrix(Fullocc[,-1])
## build array where structure is [site,sample,species]
X<-array(data=c(Fullocc),dim=c(81,7,43))
# site covariates
###covs already standardized
SiteCovs<-read.csv("LAoldsitecovs.csv") # cont. values already standardized, PCA for veg chars
sitecovs<-SiteCovs [,-1]
summary(SiteCovs)
# survey covariates (plus standardization)
Temp<-read.csv('LAtempcov.csv')
Temp<-as.matrix(Temp[,-1])
### Function for anuran Multi-species occupancy
Fullocc<-function(DH=X,Soil=sitecovs$Soil.Type,Overst=sitecovs$Overstory.Composition,Veg1=sitecovs$PC1,Veg2=sitecovs$PC2,Veg3=sitecovs$PC3,Temp=Temp,nc=3,ni=40000,nb=5000,nt=3){
n<-dim(DH)[3] # number of species
J<-dim(DH)[1] # number of sites = rows
K<-dim(DH)[2] # number of occassions = columns
data<-list(Y=DH,n=n,J=J,K=K,Soil=Soil,Overst=Overst,Veg1=Veg1,Veg2=Veg2, Veg3=Veg3,Temp=Temp) #LU1=LU1,LU2=LU2,LU3=LU3,LU4=LU4
modelFilename = "MultiSpeciesOccModel.txt"
cat("
model{
psi.mean~dunif(0,1)
a<-log(psi.mean)-log(1-psi.mean)
p.mean~dunif(0,1)
b<-log(p.mean)-log(1-p.mean)
sigma.u~dunif(0,3)
sigma.v~dunif(0,3)
tau.u<-pow(sigma.u,-2)
tau.v<-pow(sigma.v,-2)
rho~dunif(-1,1)
var.v<-tau.v/(1-pow(rho,2))
mu.alpha1~dunif(-3,3)
mu.alpha2~dunif(-3,3)
mu.alpha3~dunif(-3,3)
mu.alpha4~dunif(-3,3)
mu.alpha5~dunif(-3,3)
mu.alpha6~dunif(-3,3)
mu.alpha7~dunif(-3,3)
mu.beta1~dunif(-3,3)
sigma.alpha1~dunif(0,3)
sigma.alpha2~dunif(0,3)
sigma.alpha3~dunif(0,3)
sigma.alpha4~dunif(0,3)
sigma.alpha5~dunif(0,3)
sigma.alpha6~dunif(0,3)
sigma.alpha7~dunif(0,3)
sigma.beta1~dunif(0,3)
tau.alpha1<-1/pow(sigma.alpha1,2)
tau.alpha2<-1/pow(sigma.alpha2,2)
tau.alpha3<-1/pow(sigma.alpha3,2)
tau.alpha4<-1/pow(sigma.alpha4,2)
tau.alpha5<-1/pow(sigma.alpha5,2)
tau.alpha6<-1/pow(sigma.alpha6,2)
tau.alpha7<-1/pow(sigma.alpha7,2)
tau.beta1<-1/pow(sigma.beta1,2)
for(i in 1:n){
u[i]~dnorm(a,tau.u)I(-3,3)
mu.v[i]<-b+(rho*sigma.v/sigma.u)*(u[i]-a)
v[i]~dnorm(mu.v[i],var.v)I(-3,3)
alpha1[i]~dnorm(mu.alpha1, tau.alpha1)I(-3,3)
alpha2[i]~dnorm(mu.alpha2, tau.alpha2)I(-3,3)
alpha3[i]~dnorm(mu.alpha3, tau.alpha3)I(-3,3)
alpha4[i]~dnorm(mu.alpha4, tau.alpha4)I(-3,3)
alpha5[i]~dnorm(mu.alpha5, tau.alpha5)I(-3,3)
alpha6[i]~dnorm(mu.alpha6, tau.alpha6)I(-3,3)
alpha7[i]~dnorm(mu.alpha7, tau.alpha7)I(-3,3)
beta1[i]~dnorm(mu.beta1, tau.beta1)I(-3,3)
#Estimate the occupancy probability (latent Z matrix) for each species at each site
for(j in 1:J){
logit(psi[j,i])<-u[i]+alpha1[i]*Soil[j]+alpha2[i]*Overst[j]+alpha3[i]*Veg1[j]+alpha4[i]*Veg2[j]+alpha5[i]*Veg3[j]
Z[j,i]~dbern(psi[j,i])
#Estimate the species specific detection probability for every rep at each point where the species occurs (Z=1)
for(k in 1:K){
logit(p[j,k,i])<-v[i]+beta1[i]*Temp[j,k]
mu.p[j,k,i]<-p[j,k,i]*Z[j,i]
Y[j,k,i]~dbern(mu.p[j,k,i])
#Create simulated dataset to calculate the Bayesian p-value
Ynew[j,k,i]~dbern(mu.p[j,k,i])
d[j,k,i]<-abs(Y[j,k,i]-mu.p[j,k,i])
dnew[j,k,i]<-abs(Ynew[j,k,i]-mu.p[j,k,i])
d2[j,k,i]<-pow(d[j,k,i],2)
dnew2[j,k,i]<-pow(dnew[j,k,i],2)
}
dsum[j,i]<-sum(d2[j,1:K,i])
dnewsum[j,i]<-sum(dnew2[j,1:K,i])
}
}
#Calculate the discrepancy measure, which is then defined as the mean(p.fit > p.fitnew)
P.fit<-sum(dsum[1:J,1:n])
P.fitnew<-sum(dnewsum[1:J,1:n])
BPvalue<-step(P.fitnew-P.fit)
# derived parameters
for(j in 1:J){
SpR[j]<-sum(Z[j,])
}
", fill=TRUE, file=modelFilename)
inits<-function(){
list(psi.mean=runif(1),p.mean=runif(1),sigma.u=runif(1,0,3),sigma.v=runif(1,0,3),rho=runif(1,-1,1),u=rnorm(n),v=rnorm(n),Z=matrix(1,nrow=J,ncol=n),
mu.alpha1=rnorm(1),mu.alpha2=rnorm(1),mu.alpha3=rnorm(1),mu.alpha4=rnorm(1),mu.alpha5=rnorm(1),mu.alpha6=rnorm(1),mu.alpha7=rnorm(1),
sigma.alpha1=runif(1,0,3),sigma.alpha2=runif(1,0,3),sigma.alpha3=runif(1,0,3),sigma.alpha4=runif(1,0,3),sigma.alpha5=runif(1,0,3),sigma.alpha6=runif(1,0,3),sigma.alpha7=runif(1,0,3),
mu.beta1=rnorm(1),sigma.beta1=runif(1,0,3))
}
params<-list('psi.mean','sigma.u','p.mean','sigma.v','SpR','mu.alpha1','mu.alpha2','mu.alpha3','mu.alpha4','mu.alpha5','mu.alpha6','mu.alpha7','sigma.alpha1','sigma.alpha2','sigma.alpha3','sigma.alpha4','sigma.alpha5','sigma.alpha6','sigma.alpha7',
'alpha1','alpha2','alpha3','alpha4','alpha5','alpha6','alpha7',
'mu.beta1','sigma.beta1','beta1','BPvalue','u','a','v','Sp.R.pred')
require(R2OpenBUGS)
fit<-bugs(data,inits,params,"MultiSpeciesOccModel.txt",n.chains=nc,n.iter=ni,n.burnin=nb,n.thin=nt,debug=TRUE,DIC=TRUE, codaPkg = TRUE)
print(fit,dig=3)
}
### Run function with augmented data
Fullocc<-Fullocc(DH=X,Soil=sitecovs$Soil.Type,Overst=sitecovs$Overstory.Composition,Veg1=sitecovs$PC1,Veg2=sitecovs$PC2,Veg3=sitecovs$PC3,Temp=Temp,nc=3,ni=40000,nb=5000,nt=3)
这是OpenBugs错误消息
无效或意外的令牌扫描错误pos 2892
必须在加载数据之前检查模型
必须在编译之前检查模型
模型必须已编译但尚未更新,才能更改RN生成器
BugsCmds:NoCompileInits
BugsCmds:NoCompileInits
BugsCmds:NoCompileInits
必须在生成初始值之前编译模型
模型必须在更新之前初始化
必须在使用监视器之前初始化模型
必须先初始化模型,然后才能监视DIC
模型必须在更新之前初始化
必须在使用监视器之前初始化模型
未设置DIC监视器
这是我关闭R
时弹出的openbugs
错误消息:
bugs.run(n.burnin,OpenBUGS.pgm,debug = debug,WINE = WINE,错误: 在C:/ Users / ethro / AppData / Local / Temp / RtmpolX83G中查看日志文件,然后 再次尝试使用'debug = TRUE'找出OpenBUGS中出了什么问题。