[R] Strange error while passing string as an argument to the function in bnlearn package

Marco Scutari marco.scutari at gmail.com
Wed Nov 12 00:33:38 CET 2014


Hi Alexandr,

On 11 November 2014 00:10, Alexandr M <rus314 at gmail.com> wrote:
> Sorry that I formulated my question not very accurately.
> I form expressions/(logic conditions for parameters evidence and event)
> dynamically inside the loop and they are sometimes quite long.

By any chance, are using that for prediction? Because predict(...,
method = "bayes-lw") does posterior predictions from any set of
variables.

As an alternative, you can do cpdist(..., method = "lw") which also
generates from the posterior distribution:

> str(cpdist(fit, node = "A", evidence = list(B = "b"), method = "lw"))
Classes ‘bn.cpdist’ and 'data.frame':    10000 obs. of  1 variable:
 $ A: Factor w/ 3 levels "a","b","c": 1 2 2 3 1 1 1 2 1 1 ...
 - attr(*, "weights")= num  0.114 1 1 0.428 0.114 ...
 - attr(*, "method")= chr "lw"

To compute P(A = a | whatever you conditioned on), just sum the
corresponding weights over the total weight mass. Since no language
trickery is involved, this works reliably.

Cheers,
    Marco

-- 
Marco Scutari, Ph.D.
Lecturer in Statistics, Department of Statistics
University of Oxford, United Kingdom



More information about the R-help mailing list