我用了两种方法,载入Library(BRugs),然后运行如下代码:
- rm(list=ls(all=TRUE))
- library(BRugs)
- # Specify the model in BUGS language, but save it as a string in R:
- modelString = "
- model{
- for (y in 1:N.years.sites){
- for (d in 1:N.days){
- events[d,y] ~ dpois(p[d,y]) # raw data is {0/1} for each day
- p[d,y] <- max(p0[d,y],.001) # trick to avoid crashing from low values
- p0[d,y] <- a[d] * risk[d,y]
- risk[d,y] <- exp(beta[1]*temp.cum[d,y] + beta[2]*frost7[d,y] +
- beta[3]*precip7[d,y] ) # risk function that can include any covariates of interest, time-dependent or otherwise
- risk2[d,y] <- min(risk[d,y], 100)
- # events.pred[d,y] <- 1 - pow(exp(-sum(a[1 : d])), risk2[d,y] )
- }}
- for (d in 1:N.days){ #
- a[d] <- p1/ (1+exp((2.2/p3) * (p2-d))) # baseline function used in this study (could be modeled otherwise for different applications, or left non-parametric)
- #p1 <- 1 # max
- #p2 <- 200 # point of inflection
- #p3 <- 20 # time between .1 and .5 and .5 and .9
- }
- p1~dlnorm(p1.sp, p1.tau)
- p3~dlnorm(p3.sp, p3.tau)
- p2~dlnorm(p2.sp, p2.tau)
- ## Priors (BUGS normal distributions use precision instead of variance, where precision = 1/variance)
- p1.sp ~ dnorm(0,.001)
- p2.sp ~ dnorm(3,.001)
- p3.sp ~ dnorm(50,.001)
- p1.tau ~ dgamma(.01,.01)
- p2.tau ~ dgamma(.01,.01)
- p3.tau ~ dgamma(.01,.01)
- # Overall species baseline curve
- for (d in 1:N.days){ a.sp[d] <- p1.sp/ (1+exp((2.2/p3.sp) * (p2.sp-d))) }
- for(pp in 1:n.beta){
- beta[pp] ~ dnorm(beta0[pp], beta.tau[pp])
- }
- for(pp in 1:n.beta){
- beta0[pp]~ dnorm(0,.001)
- beta.tau[pp] <- pow(beta.sigma[pp], -2)
- beta.sigma[pp] ~ dunif(0,10)
- }
- } # end model
- "
- .temp = file("model.txt","w") ; writeLines(modelString,con=.temp) ; close(.temp)
- modelCheck( "model.txt" )
- # THE DATA.
- N.days = 250
- N.years.sites = 5
- n.beta = 3
- response.day = 250
- datas<-read.csv("E:/R/survival/data.csv")
- events<-matrix(nrow=250,ncol=5)
- frost7<-matrix(nrow=250,ncol=5)
- precip7<-matrix(nrow=250,ncol=5)
- temp.cum<-matrix(nrow=250,ncol=5)
- for (i in 1:5) {events[,i]<-datas[,i]}
- for (i in 1:5) {frost7[,i]<-datas[,i+5]}
- for (i in 1:5) {precip7[,i]<-datas[,i+10]}
- for (i in 1:5) {temp.cum[,i]<-datas[,i+15]}
- dataList = list(
- events = events,
- frost7 = frost7,
- precip7 = precip7,
- temp.cum = temp.cum,
- N.days = N.days,
- n.beta = n.beta,
- N.years.sites= N.years.sites
- # response.day = response.day
- )
- nChains = 3
- modelData( bugsData( dataList ) )
- modelCompile( numChains = nChains ) # BRugs tells BUGS to compile the model.
- modelGenInits()
- burninSteps = 1000
- modelUpdate( burninSteps )
Rstudio提示:
Error in handleRes(res) : NA
In addition: Warning message:
running command '"d:/Program Files/R/R-3.1.3patched/library/BRugs/exec/BugsHelper.exe" "C:/Users/TONGZ~1/AppData/Local/Temp/RtmpwPtl0g" "C:/Users/TONGZ~1/AppData/Local/Temp/RtmpwPtl0g/trash" "file18382a0663b.bug" "C:/Users/TONGZ~1/AppData/Local/Temp/RtmpwPtl0g/cmds.txt" "1"' had status 144
实在不明白怎么回事,找到了 Bugshelper.exe 双击打开提示
后来我在Openbugs 中找到libOpenBUGS.dll(重新安装了几次,该文件应当没有损坏。。),放到Bugshelper.exe旁边,又出现了第一张图的提示……
反复如此,用R和Rstudio都是一样的……
之后,我只好放弃Openbugs,改用R2WinBUGS
模型仍然是以上模型,读入的数据也是以上数据,最后运行bugs:
bugs.sim<-bugs(data,inits=NULL,parameters,noyear,n.chains=3,n.iter=40000,n.burnin=1000,bugs.directory="D:/winbugs14/WinBUGS14/",working.directory="c:/Bugs",debug=TRUE)
log信息如下:
display(log)
check(c:/Bugs/noyear.bug.txt)
model is syntactically correct
data(c:/Bugs/data.txt)
undefined variable
compile(3)
gen.inits()
command #Bugs:gen.inits cannot be executed (is greyed out)
thin.updater(117)
update(9)
command #Bugs:update cannot be executed (is greyed out)
set(beta)
command #Bugs:set cannot be executed (is greyed out)
set(p1)
command #Bugs:set cannot be executed (is greyed out)
set(p2)
command #Bugs:set cannot be executed (is greyed out)
set(p3)
command #Bugs:set cannot be executed (is greyed out)
set(deviance)
command #Bugs:set cannot be executed (is greyed out)
dic.set()
command #Bugs:dic.set cannot be executed (is greyed out)
update(334)
command #Bugs:update cannot be executed (is greyed out)
coda(*,c:/Bugs/coda)
command #Bugs:coda cannot be executed (is greyed out)
stats(*)
command #Bugs:stats cannot be executed (is greyed out)
dic.stats()
DIC
history(*,c:/Bugs/history.odc)
command #Bugs:history cannot be executed (is greyed out)
save(c:/Bugs/log.odc)
save(c:/Bugs/log.txt)
看这个意思好像是有未定义的变量。。?可是之前在BRugs中应该已经通过了啊。。
总是。。两种办法都卡住了。。。希望高手们看一下帮帮忙撒~~
十分感谢!!!!!


雷达卡





京公网安备 11010802022788号







