SHARE
TWEET

Untitled

a guest Jul 18th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(caretEnsemble)
  2. library(readxl)
  3. library(caret)
  4. library(neuralnet)
  5.  
  6.  
  7.  
  8. Logs1 <- read_excel("C:/Users/Abdulazizs/Desktop/Projects/Indirect/Logisitc Idea.xlsx",
  9.     sheet = "Pivot Table", n_max = 5000)
  10.  
  11.  
  12. Logs1$Direct_or_Indirect <- NULL
  13. Logs1$parentaccount <- NULL
  14. Logs1$Rank <- as.factor((Logs1$Rank))
  15. head(Logs1)
  16.  
  17.  
  18. set.seed(32942899)
  19.  
  20. ##Seperating Data
  21. logpart <- Logs1[sample(nrow(Logs1)),]
  22. split <- floor(nrow(Logs1)/3)
  23. trainlog <- Logs1[(split+1):(split*2), ]
  24. testlog <- Logs1[(split*2+1):nrow(Logs1), ]
  25. validtest <- Logs1[0:split,]
  26. TrainingParameters1 <- trainControl(method = "repeatedcv", number = 10, repeats=10)
  27.  
  28. table(testlog$Rank)
  29.  
  30.  
  31. ##Models
  32. KNN1 <- train(Rank~., trainlog, method = "knn", trControl= TrainingParameters1, preProcess=c("scale","center"), na.action = na.omit)
  33. NModel <- train(Rank~., trainlog, method = "nnet", trControl= TrainingParameters,preProcess=c("scale","center"),na.action = na.omit)
  34.  
  35. ##Predicting off of test data
  36. KNNPrediction <-predict(KNN1, newdata = testlog)
  37. NMPred<-predict(NModel, testlog, type = "raw")
  38.  
  39.  
  40.  
  41. ##Placing Predicts vs actual in same table
  42. KNNTable<- table(KNNPrediction, testlog$Rank, useNA="ifany")
  43. NNTable<- table(NMPred, testlog$Rank, useNA="ifany")
  44.  
  45.  
  46. ## Confusion Matrix
  47. KNNCnn <- confusionMatrix(KNNTable)
  48. NNCnn <- confusionMatrix(NNTable)
  49. #####
  50.  
  51. Test_KNN_NNTable <- data.frame(NMPred,KNNPrediction, testlog,  stringsAsFactors = F )
  52. modelStack <- train(Rank~ ., data = Test_KNN_NNTable, method = "rf", na.action = na.omit)
  53.  
  54.  
  55. #################################################################START OF STACKED MODEL SECTION#################################################################################
  56.  
  57.  
  58. ## Creating new predicitions
  59. TestNN<- predict(NModel, newdata = validtest)
  60. TestKNN <- predict(KNN1, newdata = validtest)
  61. Test_KNN_NNTable2  <- data.frame(TestNN,TestKNN, validtest,  stringsAsFactors = F )
  62.  
  63. length(TestNN)
  64. length(TestKNN)
  65. modelStack <- modelStack[1:1666]
  66.  
  67. ***Test_KKN_NN_Pred <- predict(modelStack, Test_KNN_NNTable2  )***
  68.  
  69.  
  70. ## Checking lengths
  71. length(Test_KKN_NN_Pred)
  72. length(TestKNN)
  73. length(TestNN)
  74. length(validtest$Rank)
  75.  
  76.  
  77. ##Creating new tables
  78. Everything_Combined <- table(Test_KKN_NN_Pred[1:1666], validtest$Rank, useNA="ifany")
  79. KNN_New_Table <- table(TestKNN, validtest$Rank, useNA="ifany")
  80. NN_New_table <- table(TestNN, validtest$Rank, useNA="ifany")
  81.  
  82.  
  83. ##New COnfusion Matrix
  84. confusionMatrix(KNN_New_Table)
  85. confusionMatrix(NN_New_table)
  86. confusionMatrix(Everything_Combined)
  87. ```
  88.  
  89. Expected results are: Creating a more accurate model from stacking.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top