nutterb / HydeNet

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

Making setNode() and modelToNode() play together #20

Closed jdutch27 closed 9 years ago

jdutch27 commented 9 years ago

HydeNetwork.list() is awesome.

It would be equally beneficial to be able to apply modelToNode() for individual nodes. I think this is as simple as making a network object argument to the modelToNode methods (set to NULL). By default the function will work as-is, i.e., as called from HydeNetwork.list(). But if the user feeds a network object to the method, it will:

1) check that indeed the argument is of class 'HydeNetwork' 2) check that the name of the response variable in the 'model' argument is actually the name of a node in the network 3) check all.equal(sort(unique(names_of_variables_in_regression_equation))), sort(unique(names_of_parents_in_network_object))) 4) set all the list elements for that node within the HydeNetwork object and return an updated object.

Thoughts on its usefulness?

nutterb commented 9 years ago

Rather than putting this into setNode, I wrote setNodeModels. This will accept one or more model objects and apply the settings to the appropriate nodes in one pass.

As discussed in issue #7, setNode is pretty inflexibly written to deal with one node at a time, and to extend it to take a single model object and work properly is probably a fairly sizable headache. This is probably a smoother approach.

jdutch27 commented 9 years ago

There is an issue with (I think) modelToNode(). The output of writeNetworkModel in my "bag of models" example seems to be incorrect. All of the distributions in the JAGS code have " ~ " in the parameters. For example, the second "wells ~" should not be in the following expression: "wells ~ dnorm(wells ~ 3.79, 0.63)"

g1 <- lm(wells ~ 1, data=PE) g2 <- glm(pe ~ wells, data=PE, family="binomial") g3 <- lm(d.dimer ~ pe + pregnant, data=PE) g4 <- xtabs(~ pregnant, data=PE) g5 <- glm(angio ~ pe, data=PE, family="binomial") g6 <- glm(treat ~ d.dimer + angio, data=PE, family="binomial") g7 <- glm(death ~ pe + treat, data=PE, family="binomial")

bagOfModels <- list(g1,g2,g3,g4,g5,g6,g7)

bagNet <- HydeNetwork(bagOfModels) writeNetworkModel(bagNet, pretty=TRUE)

model{

wells ~ dnorm(wells ~ 3.7941999999999, 0.630504251834359)

pe ~ dbern(pe ~ ilogit(-3.90355 + 0.5757*wells))

d.dimer ~ dnorm(d.dimer ~ 210.24251 + 68.37938(pe==2) + 29.29496(pregnant==2), 0.0334725036145318)

pi.pregnant[1] <- 0.9014; pi.pregnant[2] <- 0.0986

pregnant ~ dcat(pi.pregnant)

angio ~ dbern(angio ~ ilogit(-2.22585 + 3.28411*(pe==2)))

treat ~ dbern(treat ~ ilogit(-5.89316 + 0.01994d.dimer + 1.73354(angio==2)))

death ~ dbern(death ~ ilogit(-4.18763 + 5.48082(pe==2) + -1.93576(treat==2)))

}

nutterb commented 9 years ago

This has been resolved.