Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. ```{r}
  2. library(bnlearn)
  3. library(gRain)
  4.  
  5.  
  6. prediction <- function(junctree, data, observed_var, predicted_var) {
  7. predictions <- NULL
  8. X <- NULL
  9. #loop through rows in data
  10. for (i in 1:nrow(data)) {
  11. #loop through observed variables and set vector X to corresponding value
  12. for (j in observed_var) {
  13. X[j] <- if(data[i, j] == "yes") "yes" else "no" }
  14. #Get the list of potentials
  15. evidence <- setFinding(object = junctree, nodes = observed_var,states = X)
  16. #Get the probabilities for the values of the predicted value
  17. prob <- querygrain(object = evidence,nodes = predicted_var)$S
  18. predictions[i] <- if (prob["yes"] > 0.5) "yes" else "no"
  19.  
  20. }
  21. return (predictions)
  22. }
  23. ```
  24. ```{r}
  25. set.seed(567)
  26. data("asia")
  27. ind <- sample(1:5000, 4000)
  28. tr <- asia[ind,]
  29. te <- asia[-ind,]
  30.  
  31. #Create the native bayesian network
  32. BN <-model2network("[S][A|S][T|S][L|S][B|S][E|S][X|S][D|S]")
  33.  
  34. trainingPoints <- c(10,20,50,100,1000,2000)
  35.  
  36. accuracys <- matrix(NA, length(trainingPoints))
  37.  
  38. for (i in 1:length(trainingPoints)) {
  39.  
  40. trainData <- tr[1:trainingPoints[i],]
  41.  
  42. #Fit parameters of network to train data
  43. BN_fit <- bn.fit(x = BN, data = trainData, method="bayes")
  44.  
  45. #Convert to gRain-object
  46. BN_grain <- as.grain(BN_fit)
  47.  
  48. #Get junctiontree
  49. junctree <- compile(BN_grain)
  50.  
  51. # #Make predictions
  52. pred <- prediction(junctree = junctree, data = te, observed_var = c("A", "T", "L", "B", "E", "X", "D"),predicted_var = c("S"))
  53.  
  54. # Calculate confusion matricies
  55. confusion_matrix<- table(pred, te$S)
  56. accuracys[i] <- sum(diag(confusion_matrix))/ sum(confusion_matrix)
  57. }
  58. print(accuracys)
  59.  
  60. ```
  61. ```{r}
  62. #Create the native bayesian network
  63. BN <-model2network("[A][T][L][B][E][X][D][S|A:T:L:B:E:X:D]")
  64.  
  65. trainingPoints <- c(10,20,50,100,1000,2000)
  66.  
  67. accuracys <- matrix(NA, length(trainingPoints))
  68.  
  69. for (i in 1:length(trainingPoints)) {
  70.  
  71. trainData <- tr[1:trainingPoints[i],]
  72.  
  73. #Fit parameters of network to train data
  74. BN_fit <- bn.fit(x = BN, data = trainData, method="bayes")
  75.  
  76. #Convert to gRain-object
  77. BN_grain <- as.grain(BN_fit)
  78.  
  79. #Get junctiontree
  80. junctree_naive <- compile(BN_grain)
  81.  
  82. # #Make predictions
  83. pred <- prediction(junctree = junctree_naive, data = te, observed_var = c("A", "T", "L", "B", "E", "X", "D"),predicted_var = c("S"))
  84.  
  85. # Calculate confusion matricies
  86. confusion_matrix<- table(pred, te$S)
  87.  
  88. accuracys[i] <- sum(diag(confusion_matrix))/ sum(confusion_matrix)
  89. }
  90. print(accuracys)
  91. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement