Guest User

Untitled

a guest
Nov 18th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. library(shiny)
  2. library(e1071)
  3. library(rminer)
  4. library(dplyr)
  5. library(tidyr)
  6. library(ggplot2)
  7. library(ggvis)
  8. library(corrplot)
  9. library(DT)
  10. library(caret)
  11. ui <- navbarPage(title = "HR Analytics ",
  12.  
  13. tabPanel("Data Import",
  14. sidebarLayout(sidebarPanel(
  15. fileInput('file1', 'Choose CSV File to upload',
  16. accept=c('text/csv',
  17. 'text/comma-separated-values,text/plain',
  18. '.csv')),
  19. helpText("Note: Please ensure that the the file is in .csv",
  20. "format and contains headers."),
  21. tags$hr(),
  22. actionButton("do", "Import")
  23. ),
  24. mainPanel(h2(helpText("Descriptive Statistics")),
  25. verbatimTextOutput('contents'))
  26. )
  27. ),#tabpanel
  28. tabPanel("Predictive Model",
  29. sidebarLayout(sidebarPanel(
  30. uiOutput("model_select"),
  31. actionButton("enter", "Enter")
  32. ),
  33. mainPanel(h2(helpText("Model Output")),
  34. verbatimTextOutput('modelOutput'))
  35. )
  36. ),#tabpanel
  37. tabPanel("Report",
  38. sidebarLayout(sidebarPanel(
  39. tags$style(type="text/css",
  40. ".shiny-output-error { visibility: hidden; }",
  41. ".shiny-output-error:before { visibility: hidden; }"
  42. ),
  43. helpText("Download final list of employess to be retained"),
  44. br(),
  45. uiOutput("modsel"),
  46. helpText("Select Model"),
  47. uiOutput("noselect"),
  48. helpText("Select number pf employess"),
  49. downloadButton('downloadData', 'Download'),
  50. helpText("Download final list of employees to be retained")
  51. ),
  52.  
  53. mainPanel(h2(helpText("Retained Employees")),
  54. dataTableOutput("reportOutput"))
  55. )
  56. )#tabpanel
  57.  
  58.  
  59. )
  60.  
  61.  
  62. library(shiny)
  63.  
  64. server <- function(input, output) {
  65.  
  66. hr = eventReactive(input$do,{
  67. inFile <- input$file1
  68.  
  69. if (is.null(inFile))
  70. return(NULL)
  71.  
  72. hr = read.csv(inFile$datapath, header=T, sep=",")
  73. })
  74.  
  75. output$contents <- renderPrint({
  76. return(summary(hr()))
  77. })
  78.  
  79. output$model_select<-renderUI({
  80. selectInput("modelselect","Select the model",choices = c("Tree Learning"="rpart","Logistic Regression"="LogitBoost", "Naive Bayes" = "nb"))
  81. })
  82.  
  83. output$modsel<-renderUI({
  84. selectInput("modelselect2","Select Algo",choices = c("Logistic Regression","Naives Bayes","Tree Learning"),selected = "Logistic_reg")
  85. })
  86.  
  87. output$noselect<- renderUI({
  88. sliderInput("noselect", "Number of observations:",
  89. min = 0, max = 300, value = 20)})
  90.  
  91.  
  92. algo = eventReactive(input$enter,{
  93. return(input$modelselect)
  94. })
  95.  
  96.  
  97. output$modelOutput <- renderPrint({
  98. hr_model <- hr() %>% filter(left==0 | last_evaluation >= 0.70 | time_spend_company >= 4 | number_project > 5)
  99. hr_model$left <- as.factor(hr_model$left)
  100. train_control<- trainControl(method="cv", number=5, repeats=3)
  101. rpartmodel<- train(left~., data=hr_model, trControl=train_control, method=algo())
  102. # make predictions
  103. predictions<- predict(rpartmodel,hr_model)
  104. hr_model_tree<- cbind(hr_model,predictions)
  105. # summarize results
  106. confusionMatrix<- confusionMatrix(hr_model_tree$predictions,hr_model_tree$left)
  107. confusionMatrix
  108. })
  109.  
  110. rt <- reactive(
  111. if(input$modelselect2== "Logistic Regression"){
  112. f1<-data()
  113. hr_model1 <- hr() %>% filter(left==0 |last_evaluation >= 0.70 | time_spend_company >= 4 | number_project > 5)
  114. hr_model1$left <- as.factor(hr_model1$left)
  115. train_control<- trainControl(method="cv", number=5, repeats=3)
  116. # Keep some data to test again the final model
  117. set.seed(100)
  118. inTraining <- createDataPartition(hr_model1$left, p = .75, list = FALSE)
  119. training <- hr_model1[ inTraining,]
  120. testing <- hr_model1[-inTraining,]
  121. # Estimate the drivers of attrition
  122. logreg = glm(left ~ ., family=binomial(logit), data=training)
  123. # Make predictions on the out-of-sample data
  124. probaToLeave=predict(logreg,newdata=testing,type="response")
  125. # Structure the prediction output in a table
  126. predattrition = data.frame(probaToLeave)
  127. # Add a column to the predattrition dataframe containing the performance
  128. predattrition$performance=testing$last_evaluation
  129.  
  130. predattrition$priority=predattrition$performance*predattrition$probaToLeave
  131. orderpredattrition=predattrition[order(predattrition$priority,decreasing = TRUE),]
  132. orderpredattrition <- head(orderpredattrition, n=input$noselect)
  133. or<- data.frame(orderpredattrition)
  134. or
  135. }
  136. else if(input$modelselect2== "Naives Bayes"){
  137. f1<-data()
  138. hr_model1 <- hr() %>% filter(left==0 |last_evaluation >= 0.70 | time_spend_company >= 4 | number_project > 5)
  139. hr_model1$left <- as.factor(hr_model1$left)
  140. train_control<- trainControl(method="cv", number=5, repeats=3)
  141. # Keep some data to test again the final model
  142. set.seed(100)
  143. inTraining <- createDataPartition(hr_model1$left, p = .75, list = FALSE)
  144. training <- hr_model1[ inTraining,]
  145. testing <- hr_model1[-inTraining,]
  146.  
  147. # Estimate the drivers of attrition
  148. e1071model2 = naiveBayes(left ~ ., data=training)
  149. # Make predictions on the out-of-sample data
  150. probaToLeave=predict( e1071model2,newdata=testing[,c(-7,-9,-10)],type="raw")
  151. # Structure the prediction output in a table
  152. predattrition = data.frame(probaToLeave)
  153. colnames(predattrition) <- c("c","probaToLeave")
  154. predattrition[1] <- NULL
  155. # Add a column to the predattrition dataframe containing the performance
  156. predattrition$performance=testing$last_evaluation
  157. predattrition$priority=predattrition$performance*predattrition$probaToLeave
  158. orderpredattrition=predattrition[order(predattrition$priority,decreasing = TRUE),]
  159. orderpredattrition <- head(orderpredattrition, n=input$noselect)
  160. or<- data.frame(orderpredattrition)
  161.  
  162. }
  163.  
  164. else if(input$modelselect2== "Tree Learning"){
  165. f1<-data()
  166. hr_model1 <- hr() %>% filter(left==0 |last_evaluation >= 0.70 | time_spend_company >= 4 | number_project > 5)
  167. hr_model1$left <- as.factor(hr_model1$left)
  168. train_control<- trainControl(method="cv", number=5, repeats=3)
  169. # Keep some data to test again the final model
  170. set.seed(100)
  171. inTraining <- createDataPartition(hr_model1$left, p = .75, list = FALSE)
  172. training <- hr_model1[ inTraining,]
  173. testing <- hr_model1[-inTraining,]
  174. # Estimate the drivers of attrition
  175. rpartmodel = rpart(left ~ satisfaction_level+last_evaluation+number_project+average_montly_hours+time_spend_company+Work_accident+promotion_last_5years,method = "anova",data=training)
  176. # Make predictions on the out-of-sample data
  177. probaToLeave=predict(rpartmodel,newdata=testing[,c(-7,-9,-10)],type="vector")
  178. # Structure the prediction output in a table
  179. predattrition = data.frame(probaToLeave)*0.5
  180. # Add a column to the predattrition dataframe containing the performance
  181. predattrition$performance=testing$last_evaluation
  182.  
  183. predattrition$priority=predattrition$performance*predattrition$probaToLeave
  184. orderpredattrition=predattrition[order(predattrition$priority,decreasing = TRUE),]
  185. orderpredattrition <- head(orderpredattrition, n=input$noselect)
  186.  
  187. or<- data.frame(orderpredattrition)
  188. or
  189. }
  190. )
  191.  
  192.  
  193. output$reportOutput = renderDataTable({
  194. rt()
  195. })
  196.  
  197. output$downloadData <- downloadHandler(
  198. filename = function() { paste(input$modelselect2, '.csv', sep='') },
  199. content = function(file){
  200. write.csv(rt(), file)
  201. }
  202. )
  203.  
  204. }
  205. shinyApp(ui=ui, server = server)
  206.  
  207. output$table_out <- DT::renderDataTable(
  208. datatable(
  209. data,
  210. rownames = TRUE,
  211. options = list(
  212. fixedColumns = TRUE,
  213. autoWidth = TRUE,
  214. ordering = FALSE,
  215. dom = 'tB',
  216. buttons = c('copy', 'csv', 'excel', 'pdf')
  217. ),
  218. class = "display" #if you want to modify via .css
  219. )
  220.  
  221. DT::dataTableOutput("table_out")
Add Comment
Please, Sign In to add comment