Skip to content
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

Closed
nutterb opened this issue Mar 9, 2015 · 5 comments
Closed

Conditional Logic Nodes #30

nutterb opened this issue Mar 9, 2015 · 5 comments

Comments

@nutterb
Copy link
Owner

nutterb commented Mar 9, 2015

(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.

  • Use rToJags() for the general case of specifying deterministic nodes with an equation
    *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.
  • we'd need to be careful about these names and whether or not they overlap with base pkg function names or other popular pkgs.
    *Not sure what other functions come to mind but these set-theoretic functions might be a useful base set.
@nutterb
Copy link
Owner Author

nutterb commented Mar 9, 2015

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

@jdutch27
Copy link
Collaborator

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.

@jdutch27
Copy link
Collaborator

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.

@nutterb
Copy link
Owner Author

nutterb commented Apr 10, 2015

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 determ nodes). In doing so, I may start another vignette that focuses on the conditional logic node.

#* 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)

@jdutch27
Copy link
Collaborator

This is fun. I like your model. This should definitely be in the docs!

nutterb added a commit that referenced this issue Nov 13, 2015
@nutterb nutterb closed this as completed Nov 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants