Alexandr M
2014-Nov-10 17:46 UTC
[R] Strange error while passing string as an argument to the function in bnlearn package
Hello everybody, I am working with the package bnlear, but, probably, error is caused not by the package itself. There is a function cpquery() to which I pass argument str2: str2="(lag1=='s')" prob.s = cpquery(fitted1, event=eval(parse(text="(M=='s')")), evidence=eval(parse(text=str2))) - Error in parse(text = str2) : object 'str2' not found fitted1 is a fitted model But if I put value of str2 directly into function call: prob.s = cpquery(fitted1, event=eval(parse(text="(M=='s')")), evidence="(lag1.M1=='s')") it works fine. Does anybody have any idea how to fix it? PS: - I used documentation to the package: http://cran.r-project.org/web/packages/bnlearn/bnlearn.pdf function cpquery() - I also trued to use backslashes \' \" - doesn't work too -- Best regards, Alexandr [[alternative HTML version deleted]]
Marco Scutari
2014-Nov-10 23:28 UTC
[R] Strange error while passing string as an argument to the function in bnlearn package
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