Advertisement
Guest User

gbm_extract.log

a guest
Dec 12th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.50 KB | None | 0 0
  1.  
  2. R version 2.15.2 (2012-10-26) -- "Trick or Treat"
  3. Copyright (C) 2012 The R Foundation for Statistical Computing
  4. ISBN 3-900051-07-0
  5. Platform: x86_64-w64-mingw32/x64 (64-bit)
  6.  
  7. R is free software and comes with ABSOLUTELY NO WARRANTY.
  8. You are welcome to redistribute it under certain conditions.
  9. Type 'license()' or 'licence()' for distribution details.
  10.  
  11.   Natural language support but running in an English locale
  12.  
  13. R is a collaborative project with many contributors.
  14. Type 'contributors()' for more information and
  15. 'citation()' on how to cite R or R packages in publications.
  16.  
  17. Type 'demo()' for some demos, 'help()' for on-line help, or
  18. 'help.start()' for an HTML browser interface to help.
  19. Type 'q()' to quit R.
  20.  
  21.  
  22. This is a session spawned by NppToR.
  23.  
  24.  
  25. [Previously saved workspace restored]
  26.  
  27. >
  28. >
  29. > require(earth) # for etitantic data
  30. Loading required package: earth
  31. Loading required package: leaps
  32. Loading required package: plotmo
  33. Loading required package: plotrix
  34. > data(etitanic)
  35. > mydata <- etitanic
  36. >
  37. > require(gbm)
  38. Loading required package: gbm
  39. Loading required package: survival
  40. Loading required package: splines
  41. Loading required package: lattice
  42. Loaded gbm 1.6.3.2
  43.  
  44. > gbm1 <- gbm(survived ~ .,
  45. + data=mydata,
  46. + var.monotone=c(0,0,0,0,0),
  47. + distribution="bernoulli",
  48. + n.trees=2, # number of trees
  49. + shrinkage=0.005, interaction.depth=3, bag.fraction = 1, train.fraction = 1,
  50. + n.minobsinnode = 10, cv.folds = 5, keep.data=TRUE, verbose=FALSE)
  51. >
  52. >
  53. > # Two full trees
  54. > pretty.gbm.tree(gbm1, 1)
  55.   SplitVar SplitCodePred LeftNode RightNode MissingNode ErrorReduction Weight    Prediction
  56. 0        1  0.000000e+00        1         5           9      73.139438   1046 -2.010714e-17
  57. 1        2  9.500000e+00        2         3           4       6.512112    658 -4.202694e-03
  58. 2       -1  3.584234e-03       -1        -1          -1       0.000000     43  3.584234e-03
  59. 3       -1 -4.747146e-03       -1        -1          -1       0.000000    615 -4.747146e-03
  60. 4       -1 -4.202694e-03       -1        -1          -1       0.000000    658 -4.202694e-03
  61. 5        0  1.000000e+00        6         7           8      19.437432    388  7.127249e-03
  62. 6       -1  1.354899e-03       -1        -1          -1       0.000000    152  1.354899e-03
  63. 7       -1  1.084503e-02       -1        -1          -1       0.000000    236  1.084503e-02
  64. 8       -1  7.127249e-03       -1        -1          -1       0.000000    388  7.127249e-03
  65. 9       -1 -2.010714e-17       -1        -1          -1       0.000000   1046 -2.010714e-17
  66. > pretty.gbm.tree(gbm1, 2)
  67.   SplitVar SplitCodePred LeftNode RightNode MissingNode ErrorReduction Weight    Prediction
  68. 0        1  2.000000e+00        1         5           9      72.409580   1046 -7.371885e-06
  69. 1        2  9.500000e+00        2         3           4       6.447161    658 -4.185715e-03
  70. 2       -1  3.563973e-03       -1        -1          -1       0.000000     43  3.563973e-03
  71. 3       -1 -4.727564e-03       -1        -1          -1       0.000000    615 -4.727564e-03
  72. 4       -1 -4.185715e-03       -1        -1          -1       0.000000    658 -4.185715e-03
  73. 5        0  3.000000e+00        6         7           8      19.243329    388  7.078582e-03
  74. 6       -1  1.347789e-03       -1        -1          -1       0.000000    152  1.347789e-03
  75. 7       -1  1.076960e-02       -1        -1          -1       0.000000    236  1.076960e-02
  76. 8       -1  7.078582e-03       -1        -1          -1       0.000000    388  7.078582e-03
  77. 9       -1 -7.371885e-06       -1        -1          -1       0.000000   1046 -7.371885e-06
  78. >
  79. > # Which variable is used for the first splitting rule (row 0)?
  80. > pretty.gbm.tree(gbm1, 1)[0,]
  81. [1] SplitVar       SplitCodePred  LeftNode       RightNode      MissingNode    ErrorReduction Weight         Prediction    
  82. <0 rows> (or 0-length row.names)
  83. > pretty.gbm.tree(gbm1, 2)[0,]
  84. [1] SplitVar       SplitCodePred  LeftNode       RightNode      MissingNode    ErrorReduction Weight         Prediction    
  85. <0 rows> (or 0-length row.names)
  86. >
  87. > # SplitVar is 1 in both trees, but it is zero based, so add 1.
  88. > # So the splitting variable is 'sex'.
  89. > attr(gbm1$Terms,'term.labels')[1+1]
  90. [1] "sex"
  91. >
  92. > # Sex has two levels.
  93. > (var.levels <- gbm1$var.levels[[2]])
  94. [1] "female" "male"  
  95. >
  96. > # The first rule of the first tree splits on SplitCodePred=0.  
  97. > # This must be zero based, so it must be female.
  98. > var.levels[0+1]
  99. [1] "female"
  100. >
  101. > # The first rule of the second tree splits on SplidCodePred=2.
  102. > # Using the same "zero based" logic, now there is an out of bounds error.
  103. > var.levels[2+1]
  104. [1] NA
  105. >
  106. > # The splitting rules do not make sense, so try working backwards.
  107. > # The first node is female
  108. > mydata[1,'sex']
  109. [1] female
  110. Levels: female male
  111. >
  112. > # Predict the outcome of the first row.
  113. > predict(gbm1, newdata=mydata[1,], n.trees=1, single=T)
  114. [1] 0.01084503
  115. > predict(gbm1, newdata=mydata[1,], n.trees=2, single=T)
  116. [1] 0.0107696
  117. >
  118. > # These predictions correspond to these terminal nodes.
  119. > pretty.gbm.tree(gbm1, 1)[8,]
  120.   SplitVar SplitCodePred LeftNode RightNode MissingNode ErrorReduction Weight Prediction
  121. 7       -1    0.01084503       -1        -1          -1              0    236 0.01084503
  122. > pretty.gbm.tree(gbm1, 2)[8,]
  123.   SplitVar SplitCodePred LeftNode RightNode MissingNode ErrorReduction Weight Prediction
  124. 7       -1     0.0107696       -1        -1          -1              0    236  0.0107696
  125. >
  126. > # In both trees the terminal nodes trace back to row 0,
  127. > # which has different SplitVar.  I am confused.
  128. >
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement