Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. library(randomForest)
  2. library(caret)
  3. library(ggplot2)
  4.  
  5. data <- read.csv("http://pastebin.com/raw.php?i=mE5JL1dm")
  6.  
  7. data_pred <- data[, 1:(ncol(data) - 1)]
  8. data_resp <- as.factor(data$y)
  9.  
  10. data_trans <- preProcess(data_pred, method = c("center", "scale"))
  11. data_pred_scale <- predict(data_trans, data_pred)
  12.  
  13. trControl <- trainControl(method = "LGOCV", p = 0.9, savePredictions = T)
  14.  
  15. set.seed(123)
  16. model <- train(x = data_pred_scale, y = data_resp,
  17. method = "rf", scale = F,
  18. trControl = trControl)
  19.  
  20. > model
  21.  
  22. Random Forest
  23.  
  24. 516 samples
  25. 11 predictors
  26. 5 classes: '0', '0.5', '1', '1.5', '2'
  27.  
  28. No pre-processing
  29. Resampling: Repeated Train/Test Splits Estimated (25 reps, 0.9%)
  30.  
  31. Summary of sample sizes: 468, 468, 468, 468, 468, 468, ...
  32.  
  33. Resampling results across tuning parameters:
  34.  
  35. mtry Accuracy Kappa Accuracy SD Kappa SD
  36. 2 0.747 0.663 0.0643 0.0853
  37. 6 0.76 0.68 0.0507 0.068
  38. 11 0.758 0.678 0.0574 0.0763
  39.  
  40. Accuracy was used to select the optimal model using the largest value.
  41. The final value used for the model was mtry = 6.
  42.  
  43. # data set of model predictions on training data vs. actual observations
  44. results <- data.frame(pred = predict(model, data_pred_scale),
  45. obs = data_resp)
  46.  
  47. table(results)
  48.  
  49. obs
  50. pred 0 0.5 1 1.5 2
  51. 0 148 0 0 0 0
  52. 0.5 0 132 0 0 0
  53. 1 0 0 139 0 0
  54. 1.5 0 0 0 38 0
  55. 2 0 0 0 0 59
  56.  
  57. model_resamples <- model$pred[model$pred$mtry == 6, c("pred", "obs")
  58.  
  59. table(model_resamples)
  60.  
  61. 0 0.5 1 1.5 2
  62. 0 296 69 5 0 0
  63. 0.5 51 228 48 0 0
  64. 1 3 28 255 24 9
  65. 1.5 0 0 16 32 15
  66. 2 0 0 1 19 101
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement