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

Alexandr M rus314 at gmail.com
Tue Nov 11 01:10:01 CET 2014


Hi Marco,

Thanks for your reply!

Logic sampling in cpquery() relies on handling unevaluated
> expressions, so it is a tad fragile in any complex setting (inside
> loops and function calls, for example).


Actually I am doing it inside the loop.
Inside the loop I determine important features and form expressions
dynamically in the string format:

for( i in 1:N )
{
...

varnames = names(newrow) # new piece of data in a form of the row with
variables names

# like in the documentation: event parameter
str1 = paste("(", var[1] , "=='", as.character(newrow[1, 1]), "')", sep =
"")

# evidence parameter:
str2 = paste("(", var[-1], "=='", sapply(newrow[1,-1], as.character), "')",
sep = "", collapse = " & ")

# estimate conditional probability
cpquery(fitted.model,
    event       = eval(parse(text=str1)),
    evidence = eval(parse(text=str2)))
...
}

For the simple query you are trying to do, just use likelihood weighting


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.


--
Best regards,
Alexandr

On 10 November 2014 23:28, Marco Scutari <marco.scutari at gmail.com> wrote:

> Hi Alexandr,
>
> On 10 November 2014 17:46, Alexandr M <rus314 at gmail.com> wrote:
> > I am working with the package bnlear, but, probably, error is caused not
> by
> > the package itself.
>
> Logic sampling in cpquery() relies on handling unevaluated
> expressions, so it is a tad fragile in any complex setting (inside
> loops and function calls, for example). On its own, the
> eval(parse(...)) trick works if you do it in the global environment,
> or in relatively simple scripts.
>
> For the simple query you are trying to do, just use likelihood weighting:
>
> prob.s = cpquery(fitted1,
>                              event=eval(parse(text="(M=='s')")),
>                              evidence=list(lag1.M1='s'),
>                              method = "lw")
>
> passing str2 as a list.
>
> Cheers,
>     Marco
>
> --
> Marco Scutari, Ph.D.
> Lecturer in Statistics, Department of Statistics
> University of Oxford, United Kingdom
>



-- 
Best regards,
Alexander Maslov

	[[alternative HTML version deleted]]



More information about the R-help mailing list