[R] Using "cpquery" function from bnlearn package inside loop

Marco Scutari marco.scutari at gmail.com
Thu Oct 10 11:16:54 CEST 2013

Dear Ryan,

On 9 October 2013 21:26, Ryan Morrison <ryan.r.morrison at me.com> wrote:
> I'm attempting to use the bnlearn package to calculate conditional probabilities, and
> I'm running into a problem when the "cpquery" function is used within a loop. I've
> created an example, shown below, using data included with the package. When
> using the cpquery function in a loop, a variable created in the loop ("evi" in the
> example) is not recognized by the function. I receive the error:
> Error in parse(text = evi) : object 'evi' not found

Based on the second example you emailed me off-list, it appears to be
a scoping problem; that's why the same code works if it's not inside a
function. I will try to debug this soon, but I am not an expert in R
parsing mechanisms so it will take some time. In the mean time, you
can use cpquery(..., method = "lw") instead of the default
cpquery(..., method = "ls") if your query looks like the one in the
example. The former does not rely on unevaluated expressions, but
takes the conditioning values as a list, and it should work
regardless. However, if you do so I suggest you should install the
latest bugfix snapshot from bnlearn.com to avoid a few other bugs in
cpquery(..., method = "lw").


Marco Scutari, Ph.D.
Research Associate, Genetics Institute (UGI)
University College London (UCL), United Kingdom

More information about the R-help mailing list