RShiny中的朴素贝叶斯实现

时间:2018-06-28 13:25:07

标签: r naivebayes

这与使用数据集的朴素贝叶斯的RShiny实现有关。输入以某种方式未与输出连接-每次都会给出不同的错误。无法识别问题。 R代码,dput(数据集)的输出和sessionInfo()均按顺序给出。

library(shiny)
library(e1071)

ui <- fluidPage(

  radioButtons(inputId = "n",label = "no. of activities",list("2"='a', "3"='b')),  
  radioButtons(inputId = "p", label = "performance", list("A"='a',"B"='b',"C"='c',"D"='d',"E"='e')),
  sliderInput(inputId = "i", label = "income", min = 318, max = 1196, value = 320),  
  sliderInput(inputId = "t", label = "time taken in mins.", min = 45, max = 165, value = 105 ),  
  tableOutput(outputId = "table")     -----  *#defining output variable here* = table

             )

server <- function(input, output) {

  output$table <- renderTable({              --- #referring the output variable = table 

    dataset <- data.frame(read_excel("C:/Users/eajamat/Desktop/Data project 1 gen/school_dropout_dataset.xlsx")) # see dput() below
    dataset$y <- as.factor(dataset$y)
    dataset <- dataset[ ,-1]
    str(dataset)
    dt.frame  <- data.frame(input$n,input$p,input$i,input$t)   # referring multiple inputs here
    nbmodel1 <- naiveBayes(dataset$y ~ ., data = dt.frame)
    nb_prediction <-predict(nbmodel1, dt.frame)

    #confusion matrix to check accuracy
    table(nb_prediction) 


    })

}


shinyApp(ui = ui, server = server)

这是dput的数据,用于替换我在应用程序代码中读取的本地文件。加上我的seesionInfo()输出。

dataset <- structure(list(`Student ID` = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 
75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 
91, 92, 93, 94, 95, 96, 97, 98, 99, 100), `Time taken(in mins)` = c(45, 
45, 45, 45, 165, 45, 45, 105, 105, 45, 105, 165, 165, 45, 45, 
165, 45, 165, 45, 165, 45, 105, 105, 45, 105, 105, 45, 105, 105, 
165, 105, 165, 45, 105, 45, 165, 105, 165, 45, 45, 45, 105, 45, 
45, 165, 165, 165, 165, 105, 45, 105, 45, 45, 165, 165, 165, 
105, 105, 165, 165, 45, 45, 45, 105, 105, 45, 165, 165, 45, 165, 
45, 105, 105, 165, 105, 165, 105, 165, 105, 165, 45, 105, 165, 
105, 165, 165, 45, 105, 45, 105, 165, 105, 165, 165, 45, 45, 
45, 105, 165, 165), income = c(331, 1009, 655, 970, 967, 607, 
371, 589, 1024, 369, 399, 1104, 708, 692, 612, 969, 570, 959, 
1077, 452, 714, 318, 615, 502, 1176, 1158, 340, 564, 643, 749, 
1060, 369, 1116, 649, 484, 631, 443, 934, 1037, 1169, 474, 687, 
815, 1153, 984, 1169, 1083, 817, 883, 821, 1033, 821, 975, 848, 
531, 421, 484, 1151, 476, 525, 531, 751, 423, 719, 523, 790, 
895, 747, 923, 1004, 875, 569, 628, 447, 618, 367, 1167, 799, 
484, 815, 400, 372, 1145, 933, 866, 438, 741, 722, 345, 447, 
1112, 513, 739, 1069, 1117, 1152, 1081, 361, 1196, 649), performance = c("C", 
"B", "C", "C", "E", "D", "B", "B", "B", "C", "E", "E", "C", "A", 
"D", "C", "D", "C", "A", "C", "C", "E", "E", "B", "E", "E", "B", 
"C", "E", "E", "D", "A", "D", "B", "B", "A", "E", "B", "D", "B", 
"A", "D", "C", "E", "B", "A", "B", "B", "B", "B", "B", "E", "A", 
"C", "D", "A", "B", "E", "B", "A", "E", "C", "E", "E", "D", "A", 
"A", "B", "B", "D", "A", "E", "A", "B", "E", "B", "B", "E", "B", 
"B", "B", "B", "E", "E", "B", "B", "C", "C", "B", "A", "E", "E", 
"E", "C", "A", "E", "E", "A", "B", "C"), n = c(2, 2, 2, 2, 2, 
3, 2, 2, 2, 2, 3, 3, 2, 3, 3, 2, 3, 2, 3, 2, 2, 2, 3, 2, 3, 3, 
2, 2, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 2, 
2, 2, 2, 2, 3, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 2, 
2, 3, 3, 3, 3, 2, 3, 2, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 
3, 3, 2, 3, 2, 3, 3, 3, 3, 2, 2), y = c(1, 0, 0, 0, 1, 0, 0, 
0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 
0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 
1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 
0, 1, 0, 1, 1, 0, 0, 0, 0)), .Names = c("Student ID", "Time taken(in mins)", 
"income", "performance", "n", "y"), row.names = c(NA, -100L), class = c("tbl_df", 
"tbl", "data.frame"))


sessionInfo() 

R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] readxl_1.0.0 e1071_1.6-8  shiny_1.1.0 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.17      class_7.3-14      assertthat_0.2.0  utf8_1.1.3        digest_0.6.15     crayon_1.3.4      later_0.7.3      
 [8] mime_0.5          cellranger_1.1.0  R6_2.2.2          xtable_1.8-2      jsonlite_1.5      magrittr_1.5      pillar_1.1.0     
[15] cli_1.0.0         rlang_0.2.0       rstudioapi_0.7    promises_1.0.1    tools_3.4.3       httpuv_1.4.3      compiler_3.4.3   
[22] sourcetools_0.1.7 htmltools_0.3.6   tibble_1.4.2  

1 个答案:

答案 0 :(得分:0)

我不确定全局问题,但似乎有一个问题:

dt.frame  <- data.frame(input$n,input$p,input$i,input$t)   # referring multiple inputs here
    nbmodel1 <- naiveBayes(dataset$y ~ ., data = dt.frame)

您仅使用4个值构建naiveBayes模型的地方-input$n等只是奇异值,这里不是data = X在模型构建中寻找的数据集-或具有我误会了吗也许您是要在构建版本中使用data = dataset并在dt.frame调用中使用predict