How can I do bayesian structure learning and inference for continuous variables with R?
I was using the 'bnlearn' package as follows:
For structure learning using the Hill Climbing algorithm , I do the following
mynetwork = hc(dataset,score='bic',restart = 0)
mynetwork.fitted = bn.fit(mynetwork , dataset, method='bayes')
mynetwork.grain <<- as.grain(mynetwork.fitted)
For inference, I was doing the following
predict(mynetwork.grain, response = c("myresponsevariable"), newdata = mytestdata, predictors = mypredictors, type = "distribution")$pred$myresponsevariable
This gives me output as follows for each possible state of the responsevariable
0 1
[1,] 0.8745255 0.1254745
However, the trouble is that this works only for categorical data(factors). When I use it on a dataset which has continuous variable like integer, numeric etc. it gives me the following error
By looking at the source of the package (Lines 666 and Lines 418-419 ), I could understand that the package expects all it's input columns to be of type factor.
Is there an alternative method (package) for computing bayesian networks in R, by which I can do network learning and inference on continuous (numeric) data for some of the columns?
EDIT : Thanks, I have tried the following as per user2957945's suggestion, and it works. However, is there a way to get the probability numbers of states 0 and 1 for the prediction. Like for example, say, I'm trying to predict for df[3,], and it gives me the prediction as 0. However, I would also like to see the probability of it being 0 and probability of it being 1, to set threshold above. See my original post, where I have posted some sample probabilities.
#making the data set
col1 = c(1,2,3,4,5)
col2 = c(10,20,30,40,50)
col3 = c(0,1,1,0,0)
df = data.frame(col1,col2,col3)
df$col3 = as.factor(df$col3)
#learning the network
hcbn = hc(df)
hcbn.fit = bn.fit(hcbn,df)
#doing prediction
> predict(hcbn.fit,"col3",method = "bayes-lw",data = df[3,])
[1] 0
Levels: 0 1