Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. fit = function(x, y, wts, param, lev, last, classProbs, ...) {
  2. ## First fit the pls model, generate the training set scores,
  3. ## then attach what is needed to the random forest object to
  4. ## be used later
  5.  
  6. ## plsr only has a formula interface so create one data frame
  7. dat <- x
  8. dat$y <- y
  9. pre <- plsr(y~ ., data = dat, ncomp = param$ncomp)
  10. scores <- predict(pre, x, type = "scores")
  11. colnames(scores) <- paste("score", 1:param$ncomp, sep = "")
  12. mod <- randomForest(scores, y, mtry = param$mtry, ...)
  13. mod$projection <- pre$projection
  14. mod
  15. }
  16. predict = function(modelFit, newdata, submodels = NULL) {
  17. ## Now apply the same scaling to the new samples
  18. scores <- as.matrix(newdata) %*% modelFit$projection
  19. colnames(scores) <- paste("score", 1:ncol(scores), sep = "")
  20. scores <- as.data.frame(scores)
  21. ## Predict the random forest model
  22. predict(modelFit, scores)
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement