File: dotplot.varImp.train.R

package info (click to toggle)
r-cran-caret 7.0-1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,036 kB
  • sloc: ansic: 210; sh: 10; makefile: 2
file content (65 lines) | stat: -rw-r--r-- 1,716 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#' Create a dotplot of variable importance values
#' 
#' A lattice \code{\link[lattice:xyplot]{dotplot}} is created from an object of
#' class \code{varImp.train}.
#' 
#' 
#' @param x an object of class \code{varImp.train}
#' @param top the number of predictors to plot
#' @param \dots options passed to \code{\link[lattice:xyplot]{dotplot}}
#' @return an object of class \code{trellis}.
#' @author Max Kuhn
#' @seealso \code{\link{varImp}}, \code{\link[lattice:xyplot]{dotplot}}
#' @keywords hplot
#' @examples
#' 
#' 
#' data(iris)
#' TrainData <- iris[,1:4]
#' TrainClasses <- iris[,5]
#' 
#' knnFit <- train(TrainData, TrainClasses, "knn")
#' 
#' knnImp <- varImp(knnFit)
#' 
#' dotPlot(knnImp)
#' 
#' 
#' @export dotPlot
dotPlot <- function (x, top = min(20, dim(x$importance)[1]), ...) 
{
   varSubset <- sortImp(x, top)
   plotObj <- stack(varSubset)

   if(dim(varSubset)[2] == 1)
   {
      plotObj <- varSubset
      names(plotObj) <- "values"
      plotObj$ind <- "Overall"
   } else plotObj <- stack(varSubset)
   
   plotObj$Var <- rep(rownames(varSubset), dim(varSubset)[2])
   plotObj$Var <- factor(plotObj$Var, levels = rev(rownames(varSubset)))
   if(dim(varSubset)[2] < 3)
   {
      if(dim(varSubset)[2] > 1) plotObj <- plotObj[plotObj$ind == levels(plotObj$ind)[1],]
      out <- dotplot(
         Var ~ values, 
         data = plotObj, 
         as.table = TRUE, 
         xlab = "Importance",
         ...)   
   
   } else {
      out <- dotplot(
         Var ~ values, 
         data = plotObj, 
         groups = plotObj$ind, 
         auto.key = list(columns = min(3, length(levels(plotObj$ind)))), 
         as.table = TRUE, 
         xlab = "Importance",
         ...)   
   }
   out
}