楼主: Lisrelchen
2810 1

[本週專題]WinBUGS: Undefined Real Result? [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6906
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2014-6-26 21:15:12 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
Dear lister
I have run a zero-inflated Poisson model in WinBUGS without problems, and now I am trying to run its equivalent negative binomial. However, I get an "undefined real result" trap message over and over. Here is the data set http://www.megafileupload.com/en/file/474809/data1.html. The trap arises from the Pr(Y > 0) part of the model since the Pr(Y = 0) runs without problems. I've tried different inits and priors to no avail.
Thanks for your help!

  1. #Data handling
  2. library(R2WinBUGS)
  3. setwd('') # working directory
  4. dget('data1')
  5. data1 <- data1[order(data1$C), ]
  6.     n = nrow(data1)
  7.     n0 = length(which(data1$C == 0))

  8. sink("ZINB.txt")
  9. cat("
  10. model {

  11. # Pr(Y=0|x,z) # probability of zeros
  12. K <- 10000
  13. for(i in 1:n0){
  14. zeros0 <- 0
  15. zeros0 ~ dpois(mu0)
  16. mu0 <- -log(p00 + (1-p00)*pow(overd, r)) + K

  17. overd <- r/(r + lambda0)
  18. p00 <- min( 0.999999, max(.000001,p0) ) # avoid stackoverflow
  19. logit(p0) <- a0 + b0.A*A + b0.M*M + b0.cat*cat + b0.d*d
  20. log(lambda0)<- a + logh + b.A*A + b.M*M + b.cebo*cebo + b.descart*descart + b.d*d
  21. }

  22. # Pr(Y>0|x,z) # probability of positive counts
  23. for(j in (n0+1):n){
  24. zeros[j] <- 0
  25. zeros[j] ~ dpois(mu[j])
  26. mu[j] <- -log((1-p1[j])*fnb[j]) + K

  27. lfnb[j] <-  r*log(overd[j]) + C[j]*log(1 - overd[j]) + loggam(C[j] + r) - loggam(r) - loggam(C[j] + 1) # - log(1 - pow(overd[j], r))
  28. fnb[j] <- exp( lfnb[j] )
  29. overd[j] <- r/(r + exp(lambda[j]))
  30. p1[j] <- min( 0.999999, max(.000001,p[j]) )
  31. logit(p[j]) <- a0 + b0.A*A[j] + b0.M*M[j] + b0.cat*cat[j] + b0.d*d[j] + b0.descart*descart[j]
  32. log(lambda[j])<- a + logh[j] + b.A*A[j] + b.M*M[j] + b.cebo*cebo[j] + b.descart*descart[j] + b.d*d[j] + b.dcos*dcos[j]
  33. pred[j] <- (1 - p1[j])*lambda[j] # predicted values (see Zuur et al 2009)
  34. var[j] <- (1 - p1[j])*(lambda[j] + lambda[j]*lambda[j]/r) + lambda[j]*lambda[j]*(p1[j]*p1[j] + p1[j]) # variance (see Zuur et al 2009)
  35. res[j] <- (C[j] - lambda[j]*(1 - p[j]))/sqrt( var[j] ) # Pearson residuals
  36. disp[j] <- pred[j]/var[j] # dispersion
  37. }

  38. # Vague priors for model coefficients
  39. r ~ dunif(0,10)
  40. a0 ~ dnorm(0,0.001)
  41. a ~ dnorm(0,0.001)
  42. b.A ~ dnorm(0,0.001)
  43. b0.A ~ dnorm(0,0.001)
  44. b.M ~ dnorm(0,0.001)
  45. b0.M ~ dnorm(0,0.001)
  46. b.d ~ dnorm(0,0.001)
  47. b0.d ~ dnorm(0,0.001)
  48. b.descart ~ dnorm(.0,0.001)
  49. b0.descart ~ dnorm(0,0.001)
  50. b0.cat ~ dnorm(0,0.001)
  51. b.dcos ~ dnorm(0,0.001)   
  52. b.cebo ~ dnorm(0,0.001)
  53. }
  54. ",fill=TRUE)
  55. sink()

  56. # Data passed to WinBUGS
  57. win.data <- list( C = data1$C, A = as.numeric(data1$A), M = as.numeric(data1$M), d = as.numeric(scale(data1$dcol)), dcos = as.numeric(scale(data1$dcos)), cebo = as.numeric(data1$cebo), descart = as.numeric(data1$descart), cat = as.numeric(data1$cat), logh = log(data1$h), n = n, n0 = n0 )

  58. # Initial values
  59. inits <- function(){ list( r = runif(1, 1, 5), a0 = rnorm(1), a = rnorm(1), b.A = rnorm(1), b0.A = rnorm(1), b.M = rnorm(1), b0.M = rnorm(1), b.d = rnorm(1), b0.d = rnorm(1), b.descart = rnorm(1), b0.descart = rnorm(1), b0.cat = rnorm(1), b.dcos = rnorm(1), b.cebo = rnorm(1) ) }

  60. # Parameters
  61. params <- c( "r", "mu", "lambda", "a", "a0", "b.A", "b0.A", "b.M", "b0.M", "b.d", "b0.d", "b.descart", "b0.descart", "b0.cat", "b.dcos", "b.cebo", "res", "pred", "var" )

  62. # MCMC settings
  63. nc <- 3
  64. ni <- 30000
  65. nb <- 3000
  66. nt <- 3

  67. # WinBUGS execution
  68. out <- bugs( data = win.data, inits = inits, parameters.to.save = params, model.file = "ZINB.txt", n.thin = nt, n.chains = nc, n.burnin = nb, n.iter = ni, debug = TRUE )

  69. print(out, dig = 2)
复制代码




二维码

扫码加我 拉你入群

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

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

关键词:Undefined winbugs Defined DEFINE WINBUG undefined equivalent different problems message

沙发
Lisrelchen 发表于 2014-6-26 21:17:26
Ok, I found a missing zero in one of the priors:

b.descart ~ dnorm(.0,0.001)  
Apparently:

BUGS doesn't take .0 as a 0.0, and
WinBUGS doesn't take .0 as a syntax error
Another point: the model neither runs with more than three covariates nor more than two categorical covariates. Any clue?

As an aside, the value I gave to the constant K makes the deviance and DIC to get wildly high values.

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-24 12:03