1

I'm trying to adapt some Dynamic Bayesian Network code to work with my own (made up) data. I'm working in R using the dbnR package. Attached screenshot of full code up to error. All working up to the inference step, where I get an error message. What I should be seeing is a cute little line graph predicting the behavior of the objective variable over time.

My advisor suggested the folded data and/or sigma matrix not being square might be the problem, but the dimensions in the fully functional example code are not square either. I've tried adjusting data values, and changing 0,1 binary to Y,N, and back again with no result. I really appreciate any advice. Error Message and Code

Exact message:

> obj_var<-c("AnxScl_t_0")
> res3<- (predict_dt(fit3,f_dt_val3,obj_var,verbose=T))  # STILL BROKEN HERE
Error in solve.default(sigma[ev_pos, ev_pos]) : 
  Lapack routine dgesv: system is exactly singular: U[1,1] = 0
In addition: Warning message:
In value[[3L]](cond) :
  The sigma matrix is computationally singular. Using the pseudo-inverse instead.

A reproducible example:

DF3 <- structure(list(DepScl = c(8, 6, 7, 8, 9, 5, 7, 5, 6, 8), 
                      AnxScl = c(9, 9, 10, 7, 8, 7, 8, 6, 4, 4), 
                      DMed = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
                      AMed = c(0, 0, 0, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15),
                      SubUse = c(1, 1, 1, 1, 0, 1, 0, 0, 0, 0), 
                      Hallucin = c(0, 1, 1, 1, 0, 0, 1, 0, 0, 0), 
                      SuicIdea = c(1, 0, 0, 0, 1, 0, 0, 0, 0, 1), 
                      Dissoc = c(1, 1, 0, 1, 1, 1, 1, 1, 0, 1), 
                      Work = c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1), 
                      Hospit = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), 
                 row.names = c(NA, 10L), class = "data.frame")

library(dbnR)
size <- 2
net3 <- learn_dbn_struc(DF3, size)
f_dt_val3 <- fold_dt(DF3, size)
fit3 <- fit_dbn_params(net3, f_dt_val3, method="mle")
obj_var <- "AnxScl_t_0"
res3 <- predict_dt(fit3, f_dt_val3, obj_var, verbose=TRUE)

Error in solve.default(sigma[ev_pos, ev_pos]) :
Lapack routine dgesv: system is exactly singular: U[2,2] = 0
In addition: Warning message:
In value[3L] :
The sigma matrix is computationally singular. Using the pseudo-inverse instead.

user20650
  • 24,654
  • 5
  • 56
  • 91
  • 1
    [`predict_dt`](https://github.com/dkesada/dbnR/blob/10fe63f23a7d40402751770befe9bf69c62f63af/R/dbn_inference.R#L64) seems to dispatch to [`predict_bn`](https://github.com/dkesada/dbnR/blob/10fe63f23a7d40402751770befe9bf69c62f63af/R/dbn_inference.R#L64) and then to [`mvn_inference`](https://github.com/dkesada/dbnR/blob/10fe63f23a7d40402751770befe9bf69c62f63af/R/dbn_inference.R#L64) which composes the full multivariate normal joint -- this can be unstable method of inference – user20650 Apr 29 '21 at 19:17
  • `predict_bn` help also seems to suggest that it takes predictions using samples (i,e. likely from bnlearn predict) but it doesn't look like that in the code. So I'd look at implementing my own predict method based on bnlearn's sampling -- the intro for dbnr seems to suggest this should be possible. – user20650 Apr 29 '21 at 19:18
  • ... in fact a further quick look is that there are additional inference methods e.g. `approximate_inference` , `approx_prediction_step` which can avoid trying to invert the covariance matrix – user20650 Apr 29 '21 at 21:08
  • Thank you so much for your response! I'm fearful of building the prediction code from scratch, but I was excited about your suggestion to use some of the approximate methods. I should have caught that, as I recall seeing in the documentation that exact methods are better for entirely continuous data. Frustratingly, when I try to use either of those commands I get this error message: – Katie Pennington Apr 30 '21 at 03:14
  • > res3<- (approximate_inference(f_dt_val3, fit3, size, obj_var, ini=50, rep=10, len=38, num_p=50)) Error in approximate_inference(f_dt_val3, fit3, size, obj_var, ini = 50, : could not find function "approximate_inference" > res3<- approx_prediction_step(fit3, obj_var, particles=f_dt_val3, n = 50) Error in approx_prediction_step(fit3, obj_var, particles = f_dt_val3, : could not find function "approx_prediction_step" I promise you I do have my packages turned on. – Katie Pennington Apr 30 '21 at 03:15
  • Can you add a **small** representative dataset to your question please (see [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for ways to do this), and I'll have a wee look with caveat I've not used / seen dbnr before – user20650 Apr 30 '21 at 11:17
  • 1
    Thank you, I hope this is helpful: > dput(DF3[1:10,]) structure(list(DepScl = c(8, 6, 7, 8, 9, 5, 7, 5, 6, 8), AnxScl = c(9, 9, 10, 7, 8, 7, 8, 6, 4, 4), DMed = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), AMed = c(0, 0, 0, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15), SubUse = c(1, 1, 1, 1, 0, 1, 0, 0, 0, 0), Hallucin = c(0, 1, 1, 1, 0, 0, 1, 0, 0, 0), SuicIdea = c(1, 0, 0, 0, 1, 0, 0, 0, 0, 1), Dissoc = c(1, 1, 0, 1, 1, 1, 1, 1, 0, 1), Work = c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1), Hospit = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame") – Katie Pennington Apr 30 '21 at 14:51
  • The approximate inference functions are not exported by the package (and I don't think that they are currently used in it either). But they are there. You can access them with the three colon notation e.g. `dbnR:::approximate_inference` and there are help files `?dbnR:::approximate_inference`, but no examples. They do not currently accept evidence. Be cautious using non-exported functions, especially in new packages. . – user20650 Apr 30 '21 at 20:56
  • ... That said, I can get some predictions with `dbnR:::approximate_inference(dt=f_dt_val3, fit=fit3, size=size, obj_vars=obj_var, ini=1, rep=5, len=3, num_p=1e4)` -- you will need to check how sensible this is. ps your data does not appear to multi-variate Gaussian, which i think the methods in this package assume – user20650 Apr 30 '21 at 20:56
  • pps `fit3` has class `bn.fit` I think you should be able to use standard `bnlearn` inference tools – user20650 Apr 30 '21 at 20:59

0 Answers0