0
query_averager <- function(arg1,
                           arg2) {
n = tibble()
   i = 1
   while (i <= 100) {
   n[i] <- cpquery(fitted = fitted_bn_01,
        event = (ret == "Acima da Selic") ,
        evidence = (solidez == arg1) & (resultado == arg2))
   i = i+1

   }
return (mean(n))
}

Query_result <- query_averager(arg1 = "1", arg2 = "Lucro acima da mediana")

This gives me "object arg1 not found", but when I use the function cpquery() outside, it works perfectly.

Working code without the query_averager function:

cpquery(fitted = fitted_bn_01,
    event = (ret == "Acima da Selic") ,
    evidence = (solidez == "1") & (resultado == "Lucro acima da median"))

I believe it doesn't matter what the other variables are, the main problem is why:

 solidez == "1"

works and the code snippet below doesn't:

solidez == arg1
  • 3
    Where does the function `cpquery` come from? Does it use lazy evaluation? are you using variables when you use `cpquery` outside the function or are you putting in literal values? – MrFlick Jul 28 '20 at 21:12
  • 1
    Where are `ret`, `solidez`, `resultado`, `fitted_bn_01` defined? If you tag this with [tag:functional-programming], is it just irony that the scope breach suggested here defies some tenets of it, or is there something else going on here? – r2evans Jul 28 '20 at 21:18
  • The cpquery is a function from the bnlearn package; I will edit the main post with the working code that I described. I don't know if it uses lazy evaluation or not. // I don't know if the functional-programming tag is correct or not. May just be a newbie mistake, if so, please forgive me xD – Walber Moreira Jul 28 '20 at 21:59
  • @YBS Same problem. I've tried passing as argument "1" and 1, the output didn't change. – Walber Moreira Jul 29 '20 at 12:22
  • @MrFlick Now I know what Lazy Evaluation is, and yes it is lazy evaluated. – Walber Moreira Jul 29 '20 at 12:23
  • Answer by @Marco Sandri should help: https://stackoverflow.com/questions/44676501/r-bnlearn-eval-inside-function – YBS Jul 29 '20 at 15:57

1 Answers1

1

It seems you haven't have any variables in the evidence expression because they will not be evaluated in the function body. So it seems you have to manipulate the function call prior to evaluation. I don't have this package installed and working, but it seems like this would work:

query_averager <- function(arg1, arg2) {
   n = tibble()
   i = 1
   while (i <= 100) {
   n[i] <- eval(substitute(cpquery(fitted = fitted_bn_01,
        event = (ret == "Acima da Selic") ,
        evidence = (solidez == arg1) & (resultado == arg2))))
   i = i+1

   }
  mean(n)
}

The substitute() part injects the values of arg1 and arg2 into the expression. Then we evaluate that expression with eval().

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Perfect answer, do you know what any reference that I could study this kind of problem? The solution is perfect but I don't understand why. Also, I've found that the generic {predict()} function has the {method = "bayes-lw" , n = 100} that it's equivalent to the cpquery. – Walber Moreira Jul 30 '20 at 16:19
  • Most functions don't work this way so it's a bit unusual so sometimes you just have to guess if the function isn't well documented. If you haven't read the "metaprograming" section of "Advanced R" that might be a good place to get started. https://adv-r.hadley.nz/metaprogramming.html But there's nothing in there that would probably directly address this particular issue. – MrFlick Jul 31 '20 at 01:51