楼主: 空sutra
5985 8

[问答] 有关OpenBUGS/R2WinBUGS的小问题求解答 [推广有奖]

  • 1关注
  • 3粉丝

已卖:240份资源

本科生

9%

还不是VIP/贵宾

-

威望
0
论坛币
884 个
通用积分
2.2407
学术水平
1 点
热心指数
2 点
信用等级
1 点
经验
4209 点
帖子
35
精华
0
在线时间
85 小时
注册时间
2014-8-3
最后登录
2020-6-15

楼主
空sutra 发表于 2015-3-17 10:17:33 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
大家好,这两天正在尝试用R语言做生存分析,其中需要贝叶斯统计;主要模型是来自文献资料,但是运行过程中反复出现问题,希望大神们帮忙解答,谢谢!
我用了两种方法,载入Library(BRugs),然后运行如下代码:
  1. rm(list=ls(all=TRUE))
  2. library(BRugs)
  3. # Specify the model in BUGS language, but save it as a string in R:
  4. modelString = "
  5. model{
  6.     for (y in 1:N.years.sites){  
  7.     for (d in 1:N.days){  
  8.       events[d,y] ~ dpois(p[d,y])  # raw data is {0/1} for each day
  9. p[d,y] <- max(p0[d,y],.001) # trick to avoid crashing from low values
  10. p0[d,y] <- a[d] * risk[d,y]
  11. risk[d,y] <- exp(beta[1]*temp.cum[d,y] + beta[2]*frost7[d,y] +
  12. beta[3]*precip7[d,y] )   # risk function that can include any covariates of interest, time-dependent or otherwise

  13. risk2[d,y] <- min(risk[d,y], 100)
  14. # events.pred[d,y] <- 1 - pow(exp(-sum(a[1 : d])), risk2[d,y] )
  15. }}

  16. for (d in 1:N.days){  #
  17. 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)

  18. #p1 <- 1 # max
  19. #p2 <- 200 # point of inflection
  20. #p3 <- 20 # time between .1 and .5 and .5 and .9

  21. }  
  22. p1~dlnorm(p1.sp, p1.tau)
  23. p3~dlnorm(p3.sp, p3.tau)

  24. p2~dlnorm(p2.sp, p2.tau)


  25. ## Priors (BUGS normal distributions use precision instead of variance, where precision = 1/variance)
  26. p1.sp ~ dnorm(0,.001)
  27. p2.sp ~ dnorm(3,.001)
  28. p3.sp ~ dnorm(50,.001)

  29. p1.tau ~ dgamma(.01,.01)
  30. p2.tau ~ dgamma(.01,.01)
  31. p3.tau ~ dgamma(.01,.01)

  32. # Overall species baseline curve
  33. for (d in 1:N.days){    a.sp[d] <- p1.sp/ (1+exp((2.2/p3.sp) * (p2.sp-d)))   }   


  34. for(pp in 1:n.beta){
  35. beta[pp] ~ dnorm(beta0[pp], beta.tau[pp])
  36. }

  37. for(pp in 1:n.beta){
  38. beta0[pp]~ dnorm(0,.001)  
  39. beta.tau[pp] <- pow(beta.sigma[pp], -2)
  40. beta.sigma[pp] ~ dunif(0,10)
  41. }

  42. } # end model
  43. "
  44. .temp = file("model.txt","w") ; writeLines(modelString,con=.temp) ; close(.temp)
  45. modelCheck( "model.txt" )
  46. # THE DATA.

  47. N.days = 250
  48. N.years.sites = 5
  49. n.beta = 3
  50. response.day = 250
  51. datas<-read.csv("E:/R/survival/data.csv")
  52. events<-matrix(nrow=250,ncol=5)
  53. frost7<-matrix(nrow=250,ncol=5)
  54. precip7<-matrix(nrow=250,ncol=5)
  55. temp.cum<-matrix(nrow=250,ncol=5)
  56. for (i in 1:5) {events[,i]<-datas[,i]}
  57. for (i in 1:5) {frost7[,i]<-datas[,i+5]}
  58. for (i in 1:5) {precip7[,i]<-datas[,i+10]}
  59. for (i in 1:5) {temp.cum[,i]<-datas[,i+15]}
  60. dataList = list(
  61.   events = events,
  62.   frost7 = frost7,
  63.   precip7 = precip7,
  64.   temp.cum = temp.cum,
  65.   N.days = N.days,
  66.   n.beta = n.beta,
  67.   N.years.sites= N.years.sites
  68. # response.day = response.day
  69.   )
  70. nChains = 3
  71. modelData( bugsData( dataList ) )
  72. modelCompile( numChains = nChains )  # BRugs tells BUGS to compile the model.
  73. modelGenInits()
  74. burninSteps = 1000
  75. modelUpdate( burninSteps )
复制代码
以上,前面model check通过,load data通过,model compile通过,但是问题在于,每次到了modelGenInits() 这一步,就会提示:
error.png
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 双击打开提示
error2.png
后来我在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中应该已经通过了啊。。
总是。。两种办法都卡住了。。。希望高手们看一下帮帮忙撒~~
十分感谢!!!!!

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:R2WinBUGS openbugs winbugs WINBUG BUGS library events 模型 统计 资料

沙发
nuomin 发表于 2015-3-17 19:40:15
找同学的机器跑一下程序

藤椅
空sutra 发表于 2015-3-19 23:09:06
nuomin 发表于 2015-3-17 19:40
找同学的机器跑一下程序
试过了。。。仍然这样。。。

板凳
leo4526 发表于 2016-3-18 10:01:30
请问您的问题解决了嘛?我也遇到了类似的问题:error:handleRes(res) : NA
在写论文,急求!看到请回复一下,谢谢!

报纸
空sutra 发表于 2016-4-7 10:48:22
leo4526 发表于 2016-3-18 10:01
请问您的问题解决了嘛?我也遇到了类似的问题:error:handleRes(res) : NA
在写论文,急求!看到请回复一 ...
用单独的OPENBUGS 软件跑,不要放到R里跑

地板
leo4526 发表于 2016-4-9 17:24:24
空sutra 发表于 2016-4-7 10:48
用单独的OPENBUGS 软件跑,不要放到R里跑
请问前辈是什么原因出现的这个问题?之前我做过用R跑OpenBUGS程序,最近还是一模一样的程序,就出现了这个问题。而且当时记得分别用R里做OpenBUGS和直接在OpenBUGS里做两种方法出来的结果有很大的差别,这是为什么呀?(伤心脸)

7
HZGCMO 发表于 2016-11-23 16:35:18
我是新手,请问 错误: 没有"Library"这个函数 怎么解决啊!!!!!!!

8
空sutra 发表于 2016-12-12 11:08:36
HZGCMO 发表于 2016-11-23 16:35
我是新手,请问 错误: 没有"Library"这个函数 怎么解决啊!!!!!!!
哈???没有library() 还是没有 RBugs 啊

9
zarelone 发表于 2017-2-14 16:18:28
leo4526 发表于 2016-4-9 17:24
请问前辈是什么原因出现的这个问题?之前我做过用R跑OpenBUGS程序,最近还是一模一样的程序,就出现了这个 ...
你好,请问你现在知道为什么用R调用Openbugs软件的结果跟直接跑差别很大了嘛?我最近写论文在考虑R调用这个问题

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 09:58