0

I'm trying to get the likelihood of a data record to belong to a data set characterized by a Bayesian Network.

Here is my current simple code:

library(bnlearn)
library(gRain)
dag = hc(learning.test)
fitted.bnlearn = bn.fit(dag, learning.test, method = "bayes", iss=1)
fitted.grain = as.grain(fitted.bnlearn)
retractEvidence(fitted.grain)
evidence_probability <- setEvidence(fitted.grain, c("A","B", "C", "D", "E", "F"), c("Z", "Z", "Z", "Z", "Z", "Z"))
evidence_probability
pEvidence(evidence_probability)

this outputs:

> pEvidence(evidence_probability)
[1] 1

What definitely is not what I'm expecting. What I'm expecting is that by using the smoothing (iss=1) it should output a very small but not null probability.

There is definitely something that I don't understand (all 6 variables have on ly a, b or c values int the "learning.test" training data set).

Michal T
  • 601
  • 5
  • 14
  • why are you passing the states `Z` as evidence - these values are not present in the data - and fitting via Bayes wouldn't change this.. I'd of thought that `pEvidence` should through an error, or perhaps return zero, here but it doesn't. But, for example, `setEvidence(fitted.grain, c("A","B", "C", "D", "E", "F"), as.character(unlist(learning.test[1,])))` works as expected (I'm assuming but Ive not calculated it by hand) – user20650 May 05 '19 at 12:31
  • ... perhaps you are wanting something like `for(i in 1:ncol(learning.test)) levels(learning.test[[i]]) <- c(levels(learning.test[[i]]), "Z")` prior to running `hc` to introduce levels not known in the data?? – user20650 May 05 '19 at 12:50
  • Well, what I want to achieve is to get a very small but non 0 probability for unknown evidence. Following the explanation from this answer: https://stackoverflow.com/questions/51945372/bnlearnbn-fit-difference-and-calculation-of-methods-mle-and-bayes/52017677#52017677 – Michal T May 05 '19 at 22:21
  • you cannot get the likelihood of data (given the parameters) if there are states in your query without parameters attached. Fitting with Bayes, re the link, is one way to estimate so that probabilities are not zero but the states must be specified before fitting. You can trick it using [this](https://stackoverflow.com/questions/55984874/bnlearn-grain-pevidence-issue-with-a-bayesian-network?noredirect=1#comment98631990_55984874). – user20650 May 05 '19 at 22:30

0 Answers0