paulgovan / BayesianNetwork

Bayesian Network Modeling and Analysis
http://paulgovan.github.io/BayesianNetwork/
Apache License 2.0
119 stars 38 forks source link

Request Help #3

Closed pmultani20 closed 7 years ago

pmultani20 commented 7 years ago

Hi Paul,

Good day to you! Your contribution on bayesian networks is very fascinating. I am a student of Data Science and I am new to R programming. I am trying to learn bayesian networks from data, but I am stuck at one point as I need my network to be built from selective variables of my dataset. For example, I am working on a test dataset which has 10 variables, but in this I need to use only 5 variables for my network. Could you please help me with this.

Kind Regards, Paras

paulgovan commented 7 years ago

Welcome to the awesome world of R programming! From the sound of it, you just need to subset variables, I highly recommend the dplyr package. So for example, if you wanted to build a network from a subset of the built in dataset iris:

head(iris)
install.packages('dplyr')
library('dplyr')
networkData <- dplyr::select(iris, Sepal.Width, Petal.Length, Species)
head(networkData)

where Sepal.Width, Petal.Length, Species are the variables you want to include in your model. Then just upload networkData to the app.

BTW as you're learning R, Google is your friend. Chances are if you have a question, someone out there has already posted a solution.

Cheers

pmultani20 commented 7 years ago

Paul,thanks a lot for your views. Unfortunately,I had little knowledge about dplyr,but this is static usage. What I meant in my question is that lets suppose Sepal.Width, Petal.Length and Species are selected by user randomly,then how should I use these dynamically chosen columns to build the new dataset, as for multiple runs I have to try with different selections , like first time I will choose Sepal.Width and Petal.Length, second time I will choose Petal.Length and Species etc. and I should get 2 different datasets in this case and hence 2 separate networks finally. Could you please advice on this.

Thanks Paras

paulgovan commented 7 years ago

You could do this from within the editor tab. Here is some sample code using the default data set learning.test. You should be able to simply copy the below markdown into the editor to see how it works.

Sample Code

Here is some sample markdown to help illustrate the editor.

Get the reactive values and show the first few

Use dat() to get the active data set

head(dat())

Subset data

dat <- dplyr::select(dat(), A, B, C)
head(dat)

Learn the structure of the network

dag <- bnlearn::cextend(bnlearn::gs(dat))

Plot the force directed network

networkData <- data.frame(bnlearn::arcs(dag))

networkD3::simpleNetwork(
  networkData,
  Source = "from",
  Target = "to"
)

Print the network score

bnlearn::score(dag, dat)

Fit the model parameters and show the CPT for node A

fit <- bnlearn::bn.fit(dag, dat)
fit$A

Plot the model parameters for node A

bnlearn::bn.fit.barchart(fit[["A"]])

Get the Markov blanket for node A

bnlearn::mb(dag, "A")

Plot a d3 heatmap of the adjacency matrix

d3heatmap::d3heatmap(
  bnlearn::amat(dag),
  symm = TRUE,
  colors = "Blues"
)

### Generate some random data from the network and show the first few values
```{r}
simData <- bnlearn::rbn(fit, n = 100, dat)
head(simData)

# Put your own code here...
pmultani20 commented 7 years ago

I tried below

ui.R

library(shiny)

ui = basicPage( selectInput("select", "Select columns to display", names(iris), multiple = TRUE), dataTableOutput('mytable') )

server.R

library(shiny)

server = function(input, output) { output$mytable = renderDataTable({ columns = names(iris) if (!is.null(input$select)) { columns = input$select } iris[,columns,drop=FALSE] })

}

With this I am able to display user selected column values,I need to store the selected columns in a csv file,that I am not able to do. Any help on this please.

Thanks Paras

paulgovan commented 7 years ago

You could create a download handler:

ui.R

library(shiny)

ui = basicPage(
  selectInput("select", "Select columns to display", names(iris), multiple = TRUE),
  # Create a download button
  shiny::downloadButton('downloadData', 'Download'),
  hr(),
  dataTableOutput('mytable')
)

server.R

library(shiny)

server = function(input, output) {
  # Create a reactive object for the selected data
  myIris <- reactive({
    columns = names(iris)
    if (!is.null(input$select)) {
      columns = input$select
    }
    iris[, columns]
  })

  output$mytable = renderDataTable({
    myIris()
  })

  # Create a handler for downloading the data
  output$downloadData <- shiny::downloadHandler(
    filename = function() {
      paste('myData', '.csv', sep = '')
    },
    content = function(file) {
      write.csv(myIris(), file)
    }
  )
}

Hope this helps

pmultani20 commented 7 years ago

Thanks Paul, that solved my problem.

pmultani20 commented 7 years ago

Hi Paul,

I have doubt in rendering simple network, I am trying to show arrowheads in the network, i tried using force network and arrow argument,but for force network I need the group column in my node list.How to get this or is there any way to use arrow argument in simple network?

paulgovan commented 7 years ago

Unfortunately, networkD3 doesn't let you show arrowheads. You may want to check out http://datastorm-open.github.io/visNetwork/ which has a lot more options, including the ability to show arrows.

rawiasammout commented 6 years ago

i want to use Iamb (incremental association markov blanket to do feature selectiion for one node ) no example in cran are available and i am stuck any help library(bnlearn) MarkovBlanket <- iamb(data, test = "mi-cg", alpha = 0.05, undirected = FALSE) plot(MarkovBlanket)