-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional Logic Nodes #30
Comments
Here's another example, one in which I'm thinking about putting into the vignette. It models the first roll in a craps game. If you roll a 2, 3, or 12, you lose. If you roll a 7 or 11, you win. Anything else and the "point" gets set and the "shooter" (the person rolling the dice) rolls again. craps <- hydeNetwork(~ d1 + d2 + outcome | d1*d2)
craps <- setNode(craps, d1, nodeType="dcat", pi = vectorProbs(p = rep(1/6,6), d1))
craps <- setNode(craps, d2, nodeType="dcat", pi = vectorProbs(p = rep(1/6,6), d2))
craps <- setNode(craps, outcome, ????????????)
craps |
Here's another wrinkle: g <- hydeNetwork(~ disease | comorbidityScore
+ test1 | disease
+ test2 | disease
+ surgery | test1*test2*insurance
+ surgicalComp | comorbidityScore*disease*surgery
+ death | disease*treatment*surgicalComp) Everything gets populated in the usual way except for node 'surgicalComp'. This is a sort of combined logical & random node. If treatment == 0 then surgicalComp = 0. If treatment == 1, then surgicalComp ~ some kind of logistic regression function of comorbidityScore. |
I think I may have thought of a solution to this by adding an extra "hypothetical" node. Not ideal, but at least it drives this feature down the priority list: g <- hydeNetwork(~ disease | comorbidityScore
+ test1 | disease
+ test2 | disease
+ surgery | test1*test2*insurance #stochastic Bernoulli
+ hypotheticalSurgicalComp | comorbidityScore #stochastic Bernoulli (based on logistic reg.)
+ surgicalComp | surgery*hypotheticalSurgicalComp #deterministic node
+ death | disease*treatment*surgicalComp) Node hypotheticalSurgicalComp models hypothetical outcome (0 or 1) If surgery was actually performed (surgery==1), then surgicalComp is equal to node hypotheticalSurgicalComp; otherwise, surgicalComp = 0. |
I used the craps example to make sure that the practical implementation could work. I'll work out an example with the PE data that might be more useful (and use fewer #* Define the probability distributions for rolling two dice
rolls <- expand.grid(r1 = 1:6,
r2 = 1:6)
rolls$d1 <- with(rolls, r1 + r2)
rolls$d2 <- with(rolls, r1 + r2)
#* Define the network
craps <- HydeNetwork(~ d1 +
roll_again | d1 +
d2 | roll_again +
eval_roll | d2 * roll_again +
result | eval_roll)
plot(craps)
craps <- setNode(craps, d1, "dcat",
pi=writeJagsFormula(xtabs(~ d1, data=rolls)))
craps <- setNode(craps, d2, "dcat",
pi=writeJagsFormula(xtabs(~ d2, data=rolls)))
craps <- setNode(craps, roll_again, "determ",
define = "ifelse(d1 == 2 || d1 == 3 || d1 == 7 || d1 == 11 || d1 == 12, 0, 1)",
validate=FALSE)
craps <- setNode(craps, eval_roll, "determ",
define = "ifelse(roll_again == 0, d1, d2)",
validate=FALSE)
craps <- setNode(craps, result, "determ",
define = "ifelse(eval_roll == 7 || eval_roll == 11, 1, 0)",
validate=FALSE)
compiledCraps <- compileJagsModel(craps)
codaCraps <- coda.samples(compiledCraps$jags,
craps$nodes,
n.iter=1000)
#* Probability of winning if we observe a 6 on the first roll
compiledCraps <- compileJagsModel(craps, data=list(d1=6))
codaCraps <- coda.samples(compiledCraps$jags,
"result",
n.iter=1000) |
This is fun. I like your model. This should definitely be in the docs! |
(This issue originated in #24 )
Just a thought - and maybe you're thinking along these lines already. Would it simplify things to do the following? Remember this is merely idea generation.
*define a series of "helper" functions that may be frequently used. Other packages have defined things like and(), or(), and xor(), etc., as an example. The way these might work could be like this: and() would verify logical nature of its arguments: f(parent1), f(parent2), f(parent3), ..., and then return 0 or 1 appropriately (or 1 or 2 if jags). So something like and(di_1 < 2, di_2 ==6, di_3 >= 4) for a dice network.
*Not sure what other functions come to mind but these set-theoretic functions might be a useful base set.
The text was updated successfully, but these errors were encountered: