r - RShiny 中的朴素贝叶斯实现
问题描述
这是关于使用数据集的朴素贝叶斯的 RShiny 实现。输入在某种程度上没有与输出连接 - 每次都会给出不同的错误。无法识别问题。R 代码、dput(dataset) 的输出和 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
解决方案
我不确定全球问题,但似乎有一个问题:
dt.frame <- data.frame(input$n,input$p,input$i,input$t) # referring multiple inputs here
nbmodel1 <- naiveBayes(dataset$y ~ ., data = dt.frame)
您在其中naiveBayes
仅使用 4 个值构建模型 -input$n
等等在这里只是奇异值,而不是data = X
在模型构建中寻找的数据集 - 还是我弄错了?也许您打算data = dataset
在构建和通话dt.frame
中使用predict
?
推荐阅读
- python - python - 使用 sympy 和 numpy 获取倒置浮点矩阵
- sql - SQL Server 的多子网故障转移
- java - Android - LayoutInflater - 无法使文本视图停留在另一个文本视图下方
- mysql - MYSQL LEFT JOIN 返回意外结果
- python - 如何在 Flask 的 url_for 方法中使用 kwargs
- node.js - httpS 和 http/2 有什么区别?
- python - Dask 与 HTCondor 调度程序
- visual-studio - Unity 无法使用 IL2CPP 后端为 UWP 构建 GRPC 项目
- backbone.js - 如何替换 Marionette 3 上的 RegionManager 实例
- php - 在类中使用“共享”私有属性来构建流畅/不断发展的数据库查询是否存在问题?