nutterb / HydeNet

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

Bug in vectorProbs() #4

Closed jdutch27 closed 9 years ago

jdutch27 commented 9 years ago

When you use setNode() in conjunction with vectorProbs, you can inherit the node parameter:

net <- setNode(net, wells, nodeType = "dcat", pi = vectorProbs(p = c(.3, .6, .1)) )

But when you do this, it doesn't construct the nodeParams string correctly:

net$nodeParams$wells

$pi

[1] "pi.[1] <- 0.3; pi.[2] <- 0.6; pi.[3] <- 0.1"

It should be:

$pi

[1] "pi.wells[1] <- 0.3; pi.wells[2] <- 0.6; pi.wells[3] <- 0.1"

nutterb commented 9 years ago

This is a quirk I'm not sure how to program my way out of. vectorProbs was meant to be a convenience that would allow the user to not have to remember the correct specification of the JAGS code. The downside is that, being an independent function, there's no good way to manipulate the scoping to get it to pull the node name from the network (because it isn't applied to a network, it is applied to a function).

How would you feel if I, instead, put in a parameter check that cast an error if node is missing. At least then the user will be reminded that they need to indicate which node these are applied to.

vectorProbs <- function(p, node, normalize=TRUE){
  if (missing(node)) stop("Please provide the node name (note: spelling is important)")
  node <- as.character(substitute(node))
  if (normalize) p <- p/sum(p)
  paste0("pi.", node, "[", 1:length(p), "] <- ", p, collapse="; ")
}