nutterb / HydeNet

Hybrid Decision Networks in R
Other
23 stars 3 forks source link

Deterministic nodes #24

Closed nutterb closed 9 years ago

nutterb commented 9 years ago

There needs to be a mechanism to define deterministic nodes.

nutterb commented 9 years ago

A trivial test/illustration of the use of the deterministic node would be to define a network

net <- dag(x1 + x2 + diff|x1*x2)

Define diff with diff ~ x2 - x1 and we now have the makings of a Bayesian two sample t-test.

Like I said, trivial, but it should give me an idea on whether I've coded it correctly....I think.

jdutch27 commented 9 years ago

That would be paired, I think?

Diff ~ mean(X1) - mean(x2)?

On Mar 3, 2015, at 11:52 AM, Benjamin notifications@github.com<mailto:notifications@github.com> wrote:

A trivial test/illustration of the use of the deterministic node would be to define a network

net <- dag(x1 + x2 + diff|x1*x2)

Define diff with diff ~ x2 - x1 and we now have the makings of a Bayesian two sample t-test.

Like I said, trivial, but it should give me an idea on whether I've coded it correctly....I think.

— Reply to this email directly or view it on GitHubhttps://github.com/nutterb/HydeNet/issues/24#issuecomment-76984779.

Please consider the environment before printing this e-mail

Cleveland Clinic is ranked as one of the top hospitals in America by U.S.News & World Report (2014).
Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations.

Confidentiality Note: This message is intended for use only by the individual or entity to which it is addressed and may contain information that is privileged, confidential, and exempt from disclosure under applicable law. If the reader of this message is not the intended recipient or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please contact the sender immediately and destroy the material in its entirety, whether electronic or hard copy.

Thank you.

jdutch27 commented 9 years ago

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.

jdutch27 commented 9 years ago

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

nutterb commented 9 years ago

Deterministic nodes are now possible.

Use nodeType = "determ" with the node paramter define.

This is not a true JAGS distribution, but I added it to the jagsDists data set. The other option was to add an option for deterministic = TRUE and change the behavior of the object when a node had that condition. Writing it this way kept behavior consistent with other node types and I think is simple enough to grasp.

library(HydeNet)

#* Two Sample T-test
Net <- HydeNetwork(~ x1 + 
                     x2 + 
                     y | x1*x2)

Net <- setNode(Net, x1, "dnorm", mu=5, tau=1)
Net <- setNode(Net, x2, "dnorm", mu=7, tau=1)
Net <- setNode(Net, y, "determ", define=fromFormula(),
               nodeFormula=y ~ mean(x1) - mean(x2))

#* alternatively
Net <- setNode(Net, y, "determ", define="y <- mean(x1) - mean(x2)")

#****************************
#* Paired T-Test
Net <- HydeNetwork(~ x1 + 
                     x2 + 
                     y | x1*x2)

Net <- setNode(Net, x1, "dnorm", mu=5, tau=1)
Net <- setNode(Net, x2, "dnorm", mu=7, tau=1)
Net <- setNode(Net, y, "determ", define=fromFormula(),
               nodeFormula=y ~ x1 - x2)
#* Alternatively
Net <- setNode(Net, y, "determ", define="y <- x1 - x2")
jarrod-dalton commented 9 years ago

Maybe I'm doing something wrong here?

net <- HydeNetwork(~ card1.ace | card1
                   + card2.ace | card2
                   + initialPoints | card1*card2*card1.ace*card2.ace
                   + hit1 | initialPoints*dealerUpcard
                   + card3 | hit1
                   + card3.ace | card3
                   + pointsAfterCard3 | initialPoints*card3*card3.ace
                   + hit2 | pointsAfterCard3*dealerUpcard
                   + card4 | hit2
                   + card4.ace | card4
                   + pointsAfterCard4 | pointsAfterCard3*card4*card4.ace
                   + hit3 | pointsAfterCard4*dealerUpcard
                   + card5 | hit3
                   + card5.ace | card5
                   + pointsAfterCard5 | pointsAfterCard4*card5*card5.ace
                   + playerFinalPoints | initialPoints*hit1*pointsAfterCard3
                                         *hit2*pointsAfterCard4*hit3*pointsAfterCard5
                   + dealerFinalPoints | dealerUpcard
                   + payoff | playerFinalPoints*dealerFinalPoints)

setNode(net, card1.ace, "determ", define="card1.ace <- ifelse(card1==1,1,0)")

Error in setNode(net, card1.ace, "determ", define = "card1.ace <- mean(card1)") : 

1: Please define define such that plyr::is.formula(define) (or use validate=FALSE).
nutterb commented 9 years ago

This could be done in one of two ways:

setNode(net, card1.ace, "determ", define=fromFormula(),
       nodeFormula = card1.ace ~ ifelse(card1==1,1,0))

or

setNode(net, card1.ace, "determ", define="card1.ace <- ifelse(card1==1,1,0)", 
         validate = FALSE)
nutterb commented 9 years ago

Having said that now, let me ask, how could I tweak that error message to be more informative?

jarrod-dalton commented 9 years ago

I thought I would have been doing something bad if I set validate=FALSE. We all would like some validation from time to time! Something more positively affirming would do wonders for our egos.

Kidding aside, I couldn't figure out why it didn't pass this validation test and for that matter what the validation test is even doing. My equation didn't feel erroneous. Are you sure we need to mess with this parameter, or can we simply bury it as a default validate=FALSE (if necessary to get around the issue)? Trying to cut down on syntax where we can...

nutterb commented 9 years ago

The way the validation works is that if you pass numerical values to the parameters, HydeNet will make sure that they are in the domain of the parameters in jagsDists.

If you pass a character string, HydeNet assumes that you are passing something more complex (most likely, you're passing the raw JAGS code). But we have no way of validating raw jags code. This was why I used the option to validate or not validate.

But I see how this would be confusing to someone who didn't write the code themselves. Would you be satisfied if I passed a message (not even a warning) instead of an error that said something like "'HydeNet is unable to validate character strings. No validation was performed."