Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```{r knnModel}
- library(pacman)
- p_load(caret, ggplot)
- knnTrain = train_final
- knnTest = test_final
- # Categoricals to factors
- for (i in c(8:10)){
- knnTrain[,i] <- as.factor(knnTrain[,i])
- knnTest[,i] <- as.factor(knnTest[,i])
- }
- # Sample for faster performance
- set.seed(7)
- sample1 = sample(1:84616, 1000)
- # Adding names to factors due to error when running train() "one of the class levels is not a valid R variable name"
- levels(knnTrain$LOCATION_HOME) = c("A", "H")
- levels(knnTrain$FGM) = c("miss", "made")
- levels(knnTrain$PTS_TYPE_2) = c("three", "two")
- levels(knnTest$LOCATION_HOME) = c("A", "H")
- levels(knnTest$FGM) = c("miss", "made")
- levels(knnTest$PTS_TYPE_2) = c("three", "two")
- # Setting parameters for tuning repetitions and cross-validation in model
- ctrl <- trainControl(
- classProbs = TRUE,
- method = "cv", # cross-validation method
- number = 5, # number of folds
- )
- # Modeling with training set
- fgmknnModel <- train(FGM~., data = knnTrain, method = "knn", tuneGrid=expand.grid(k = c(200, 300)), trControl = ctrl)
- fgmknnModel
- summary(fgmknnModel)
- # Determining and plotting the variable importance
- fgmimpvar = varImp(fgmknnModel, scale = T)
- ggplot(fgmimpvar)
- # Predicting shot outcome and establishing final accuracy using test set
- TestPred <- predict(fgmknnModel, knnTest)
- # Creating the confusion matrix using the predicted and test values for FGM
- confusionMatrix(TestPred, knnTest$FGM, positive = "made")
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement