0

I am using the bnlearn package in R to predict certain outcomes. However, for all rows in my data set, I get the same predictions.

Training

buildModel <- function()
{
#building bn model

#for Hill Climbing, works fine
#hcbn = hc(bndf, blacklist = blacklist, score='bic',restart = 0)

#for Max Min Hill Climbing, also works fine, get different predictions for rows
#hcbn = mmhc(bndf, blacklist = blacklist, optimized=TRUE)

#for Grow Shrink, get the same predictions every row
hcbn = cextend(gs(bndf, blacklist = blacklist, optimized=TRUE))

hcbn.fitted = bn.fit(hcbn, bndf, method='bayes')
hcbn.grain <<- as.grain(hcbn.fitted)
}

Prediction

hcpredthirtyday1[[i]] <- foreach (j = start:min(end, nrow(predictdf)), .combine=rbind) %dopar%
    {
                predict(hcbn.grain, response = c("myresponse"), newdata = predictdf[j, ], predictors = myypredictors, type = "distribution")$pred$myresponse;
    }

Output for HC and MMHC (different predictions for different input)

              0          1
 [1,] 0.8617731 0.13822686
 [2,] 0.8617731 0.13822686
 [3,] 0.8617731 0.13822686
 [4,] 0.8617731 0.13822686
 [5,] 0.8617731 0.13822686
 [6,] 0.8617731 0.13822686
 [7,] 0.8617731 0.13822686
 [8,] 0.8617731 0.13822686
 [9,] 0.8617731 0.13822686
[10,] 0.9077158 0.09228421

Output for GS (same predictions for every row)

              0         1
 [1,] 0.8633219 0.1366781
 [2,] 0.8633219 0.1366781
 [3,] 0.8633219 0.1366781
 [4,] 0.8633219 0.1366781
 [5,] 0.8633219 0.1366781
 [6,] 0.8633219 0.1366781
 [7,] 0.8633219 0.1366781
 [8,] 0.8633219 0.1366781
 [9,] 0.8633219 0.1366781
[10,] 0.8633219 0.1366781
zx8754
  • 52,746
  • 12
  • 114
  • 209
saltmangotree
  • 171
  • 4
  • 11
  • I think you're missing some of the code for it to be reproducible. Can you do this with a builtin dataset so that it can be reproduced? – Hack-R Feb 07 '16 at 19:04
  • @Hack-R, sorry I didn't get you. I haven't posted the entire code here, however, parts of GS that are different from HC and MMHC. Both of these work, however, GS does not. – saltmangotree Feb 07 '16 at 19:06
  • 1
    On StackOverflow, you need to provide a reproducible example if at all possible. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example We should be able to take the code you post, run it, and see the problem that you're talking about so that we can fix it. I tried doing this with your code, but it's missing the data (use a built in dataset), the code related to parallel backend, etc so it throws this error `Error in nrow(predictdf) : object 'predictdf' not found In addition: Warning message: executing %dopar% sequentially: no parallel backend registered` – Hack-R Feb 07 '16 at 19:07
  • Thanks, I understand, I shall update the post with a reproducible example shortly. – saltmangotree Feb 07 '16 at 19:19

0 Answers0