如何修复R2jags :: jags中的“与父母不一致的节点" [英] How to fix 'Node inconsistent with parents' in R2jags::jags

查看:74
本文介绍了如何修复R2jags :: jags中的“与父母不一致的节点"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R-package R2jags.运行下面附加的代码后,R产生错误消息:节点与父代不一致".

I am working with the R-package R2jags. After running the code I attach below, R produced the error message: "Node inconsistent with parents".

我试图解决它.但是,错误消息仍然存在.我正在使用的变量是:

I tried to solve it. However, the error message persists. The variables I am using are:

i)"Adop":0-1的虚拟变量.

i) "Adop": a 0-1 dummy variable.

ii)"NumInfo":一个计数器变量,其范围为{0,1,2,...}.

ii) "NumInfo": a counter variable whose range is {0, 1, 2,...}.

iii)价格":5

iii) "Price": 5

iv)"NRows":326.

iv) "NRows": 326.

install.packages("R2jags")
library(R2jags)

# Data you need to run the model.
# Adop: a 0-1 dummy variable.
Adop <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0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0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1)

# NRows: length of both 'NumInfo' and 'Adop'.
NRows <- length(NumInfo)

# Price: 5
Price <- 5

Data <- list("NRows" = NRows, "Adop" = Adop, "NumInfo" = NumInfo, "Price" = Price)

# The Bayesian model. The parameters I would like to infer are: 'mu.m', 'tau2.m', 'r.s', 'lambda.s', 'k', 'c', and 'Sig2'. 
# I would like to obtain samples from the posterior distribution of the vector of parameters.

Bayesian_Model <- "model {
    mu.m ~ dnorm(0, 1)                      
    tau2.m ~ dgamma(1, 1)           
    r.s ~ dgamma(1, 1)
    lambda.s ~ dgamma(1, 1)
    k ~ dunif(1, 1/Price)
    c ~ dgamma(1, 1)
    Sig2 ~ dgamma(1, 1)

    precision.m <- 1/tau2.m
    m ~ dnorm(mu.m, precision.m)
    s2 ~ dgamma(r.s, lambda.s)

    for(i in 1:NRows){
        Media[i] <- NumInfo[i]/Sig2 * m
        Var[i] <- equals(NumInfo[i], 0) * 10 + (1 - equals(NumInfo[i], 0)) * NumInfo[i]/Sig2 * s2 * (NumInfo[i]/Sig2 + 1/s2)
        Prec[i] <- pow(Var[i], -1)
        W[i] ~ dnorm(Media[i], Prec[i])
        PrAd1[i] <- 1 - step(-m/s2 - 1/c * 1/s2 * log(1 - k * Price) + 1/2 * c)
        PrAd2[i] <- 1 - step(-W[i] - m/s2 - 1/c * 1/s2 * log(1 - k * Price) + 1/2 * c - 1/c * log(1 - k * Price))
        PrAd[i] <- equals(NumInfo[i], 0) * PrAd1[i] + (1 - equals(NumInfo[i], 0)) * PrAd2[i]
        Adop[i] ~ dbern(PrAd[i])
        }
    }"

# Save the Bayesian model in your computer with an extension '.bug'.
# Suppose that you saved the .bug file in: "C:/Users/Default/Bayesian_Model.bug".
writeLines(Bayesian_Model, "C:/Users/Default/Bayesian_Model.bug")

# Here I would like to use jags command from R-package called R2jags.
# I would like to generate 1000 iterations.
MCMC_Bayesian_Model <- R2jags::jags(
    model.file = "C:/Users/Default/Bayesian_Model.bug",
    data = Data, 
    n.chains = 1, 
    n.iter = 1000,
    parameters.to.save = c("mu.m", "tau2.m", "r.s", "lambda.s", "k", "c", "Sig2")
    )

运行代码时,R产生错误消息:节点与父代不一致".我不知道这是什么错误.我想知道您是否可以帮助我解决这个问题.如果您需要更多信息,请告诉我.非常感谢.

When running the code, R produced the error message: "Node inconsistent with parents". I do not know what the mistakes are. I was wondering if you could help me with this problem, please. If you need more information, please let me know. Thank you very much.

推荐答案

在不知道您要做什么的情况下很难找出模型,但是我建议进行两个修复:

It's a little hard to figure out the model without knowing what you're trying to do, but I suggest two fixes:

  1. 而不是 k〜dunif(1,1/Price),您的意思是 k〜dunif(0,1/Price)?对于 dunif(a,b),您必须具有 a<b (请参见第48页,此处: http://people.stat.sc.edu/hansont/stat740/jags_user_manual.pdf ).

  1. Instead of k ~ dunif(1, 1/Price), did you mean k ~ dunif(0, 1/Price)? For dunif(a, b), you must have a < b (see page 48 here: http://people.stat.sc.edu/hansont/stat740/jags_user_manual.pdf).

我在模型中插入了另一行,

I inserted an additional line in the model,

PrAd01[i] <- max(min(PrAd[i], 0.99), 0.01)

并将最后一行更改为

Adop[i] ~ dbern(PrAd01[i])

上述手册的第49页指出 0<p <1 表示 dbern(p).

Page 49 of the manual above states that 0 < p < 1 for dbern(p).

该模型具有以上两个更改.

The model runs with the above two changes.

这篇关于如何修复R2jags :: jags中的“与父母不一致的节点"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆