
[34mr$>[0m #######################################################################
[34mr$>[0m #	Read in files
[34mr$>[0m col.name <- c("Inc", "S ..." ... [TRUNCATED] 

[34mr$>[0m data <- read.table("income.data", sep = ",", col.names = col.name)

[34mr$>[0m data$Sex <- as.factor(data$Sex)

[34mr$>[0m data$Mar <- as.factor(data$Mar)

[34mr$>[0m data$Occ <- as.factor(data$Occ)

[34mr$>[0m data$Dua <- as.factor(data$Dua)

[34mr$>[0m data$Hou <- as.factor(data$Hou)

[34mr$>[0m data$Hom <- as.factor(data$Hom)

[34mr$>[0m data$Eth <- as.factor(data$Eth)

[34mr$>[0m data$Lan <- as.factor(data$Lan)

[34mr$>[0m #######################################################################
[34mr$>[0m #	Train the tree, plot the CV error
[34mr$>[0m lib .... [TRUNCATED] 

[34mr$>[0m set.seed(20) # This seed is for CV, not tree itself

[34mr$>[0m # This method="anova" means a regression tree
[34mr$>[0m tree.full <- rpart(Inc ~., data = data, method = "anova", control = rpart.cont .... [TRUNCATED] 

[34mr$>[0m printcp(tree.full)

Regression tree:
rpart(formula = Inc ~ ., data = data, method = "anova", control = rpart.control(cp = 5e-04))

Variables actually used in tree construction:
 [1] Age Chi Dua Edu Eth Hom Hou Lan Mar Occ Per Res Sex

Root node error: 69179/8993 = 7.6925

n= 8993 

           CP nsplit rel error  xerror      xstd
1  0.22392203      0   1.00000 1.00025 0.0082586
2  0.08076978      1   0.77608 0.77636 0.0088547
3  0.04472770      2   0.69531 0.69580 0.0091839
4  0.03069866      3   0.65058 0.65113 0.0093138
5  0.01377787      4   0.61988 0.62072 0.0089339
6  0.01301330      5   0.60610 0.61102 0.0089367
7  0.01221720      6   0.59309 0.60325 0.0088478
8  0.01185992      7   0.58087 0.59330 0.0088278
9  0.00755031      8   0.56901 0.57428 0.0082941
10 0.00602973      9   0.56146 0.57130 0.0083241
11 0.00589364     10   0.55543 0.56536 0.0082052
12 0.00581791     11   0.54954 0.56348 0.0082177
13 0.00561150     12   0.54372 0.55932 0.0081879
14 0.00524841     13   0.53811 0.55356 0.0081699
15 0.00298929     14   0.53286 0.53976 0.0080837
16 0.00219259     15   0.52987 0.53771 0.0080903
17 0.00201948     16   0.52768 0.53628 0.0080932
18 0.00201879     17   0.52566 0.53518 0.0080901
19 0.00198908     18   0.52364 0.53479 0.0080930
20 0.00186779     19   0.52165 0.53390 0.0080972
21 0.00180666     20   0.51979 0.53253 0.0080766
22 0.00162021     21   0.51798 0.53142 0.0080764
23 0.00154282     22   0.51636 0.53055 0.0080718
24 0.00138784     23   0.51482 0.52918 0.0080677
25 0.00133764     25   0.51204 0.52919 0.0080845
26 0.00130037     26   0.51070 0.52781 0.0080772
27 0.00127274     27   0.50940 0.52845 0.0080968
28 0.00122122     28   0.50813 0.52954 0.0081437
29 0.00110474     29   0.50691 0.52809 0.0081669
30 0.00107603     30   0.50580 0.52804 0.0082164
31 0.00106676     31   0.50473 0.52751 0.0081941
32 0.00094022     33   0.50259 0.52747 0.0082341
33 0.00092518     34   0.50165 0.52793 0.0082678
34 0.00092162     38   0.49777 0.52742 0.0082537
35 0.00091488     39   0.49685 0.52698 0.0082431
36 0.00091399     40   0.49594 0.52690 0.0082500
37 0.00088117     41   0.49502 0.52618 0.0082383
38 0.00087456     42   0.49414 0.52655 0.0082737
39 0.00087045     43   0.49327 0.52636 0.0082745
40 0.00085493     44   0.49240 0.52618 0.0082728
41 0.00081217     45   0.49154 0.52543 0.0082763
42 0.00079792     46   0.49073 0.52673 0.0083258
43 0.00079523     47   0.48993 0.52620 0.0083330
44 0.00077363     48   0.48914 0.52599 0.0083434
45 0.00074574     52   0.48593 0.52642 0.0083759
46 0.00068929     53   0.48519 0.52628 0.0084149
47 0.00068145     54   0.48450 0.52664 0.0084551
48 0.00064324     56   0.48313 0.52679 0.0084735
49 0.00063956     57   0.48249 0.52713 0.0085143
50 0.00063504     59   0.48121 0.52703 0.0085119
51 0.00063287     60   0.48058 0.52725 0.0085244
52 0.00062823     61   0.47994 0.52726 0.0085222
53 0.00061486     62   0.47932 0.52691 0.0085183
54 0.00061170     63   0.47870 0.52744 0.0085271
55 0.00059932     65   0.47748 0.52714 0.0085376
56 0.00058876     67   0.47628 0.52622 0.0085349
57 0.00058134     68   0.47569 0.52577 0.0085261
58 0.00057982     70   0.47453 0.52659 0.0085735
59 0.00057958     71   0.47395 0.52664 0.0085722
60 0.00057614     72   0.47337 0.52667 0.0085740
61 0.00056309     73   0.47279 0.52668 0.0085831
62 0.00056219     74   0.47223 0.52729 0.0085941
63 0.00056005     75   0.47167 0.52758 0.0086053
64 0.00054940     76   0.47111 0.52749 0.0086178
65 0.00054554     77   0.47056 0.52812 0.0086436
66 0.00054349     78   0.47001 0.52805 0.0086455
67 0.00053429     79   0.46947 0.52787 0.0086457
68 0.00052221     80   0.46893 0.52715 0.0086594
69 0.00052132     81   0.46841 0.52804 0.0086808
70 0.00051347     82   0.46789 0.52842 0.0087038
71 0.00050821     84   0.46686 0.52832 0.0087273
72 0.00050520     85   0.46636 0.52888 0.0087459
73 0.00050345     86   0.46585 0.53122 0.0087953
74 0.00050196     87   0.46535 0.53184 0.0088114
75 0.00050000     88   0.46484 0.53187 0.0088097

[34mr$>[0m pdf("regression_tree_cptable_R.pdf", width=7, height=14)

[34mr$>[0m plotcp(tree.full)

[34mr$>[0m dev.off()
null device 
          1 

[34mr$>[0m #######################################################################
[34mr$>[0m # find the cp value that minimizes the cv error
[34 .... [TRUNCATED] 

[34mr$>[0m tree.full$cptable[ind, ] # This one contains 45 splits
          CP       nsplit    rel error       xerror         xstd 
8.121679e-04 4.500000e+01 4.915410e-01 5.254319e-01 8.276271e-03 

[34mr$>[0m cp.best.cv <- tree.full$cptable[ind, "CP"]

[34mr$>[0m # if 1-se rule is used
[34mr$>[0m ind <- which.min(tree.full$cptable[, "xerror"])[1]

[34mr$>[0m ind.1se <- which(tree.full$cptable[, "xerror"] <= tree.full$cptable[ind, "xerror"] + tree.full$cptable[ind, "xstd"])[1]

[34mr$>[0m tree.full$cptable[ind.1se, ] # This one contains 20 splits
          CP       nsplit    rel error       xerror         xstd 
 0.001806660 20.000000000  0.519785006  0.532529357  0.008076648 

[34mr$>[0m cp.best.1se <- tree.full$cptable[ind.1se, "CP"]

[34mr$>[0m #######################################################################
[34mr$>[0m #	prune the tree using the 1se rule
[34mr$>[0m tre .... [TRUNCATED] 

[34mr$>[0m print(tree.pruned)
n= 8993 

node), split, n, deviance, yval
      * denotes terminal node

  1) root 8993 69178.9100 4.895029  
    2) Hou=2,3 5646 37580.7100 3.884520  
      4) Age< 1.5 853  2426.6570 1.526377  
        8) Occ=1,5,6 689   127.4194 1.047896 *
        9) Occ=2,3,4,7,8,9 164  1478.7800 3.536585 *
      5) Age>=1.5 4793 29566.4900 4.304194  
       10) Occ=2,3,4,5,6,7,8,9 3399 20470.6000 3.789644  
         20) Mar=2,3,4,5 2631 15683.4000 3.511593  
           40) Hom=3,4,5 1406  5963.8180 3.146515  
             80) Occ=5,6,7,8,9 631  2663.1510 2.564184 *
             81) Occ=2,3,4 775  2912.4700 3.620645  
              162) Age< 2.5 279   913.7061 2.931900 *
              163) Age>=2.5 496  1791.9680 4.008065 *
           41) Hom=1,2 1225  9317.1020 3.930612  
             82) Occ=5,9 133   854.0000 3.000000 *
             83) Occ=2,3,4,6,7,8 1092  8333.8900 4.043956 *
         21) Mar=1 768  3886.9530 4.742188  
           42) Edu< 3.5 384  1737.9900 4.005208  
             84) Edu< 2.5 91   360.4615 2.923077 *
             85) Edu>=2.5 293  1237.8700 4.341297 *
           43) Edu>=3.5 384  1731.8330 5.479167  
             86) Age< 2.5 64   275.7344 4.140625 *
             87) Age>=2.5 320  1318.4970 5.746875 *
       11) Occ=1 1394  6001.6760 5.558824  
         22) Dua=1,3 1062  4420.1880 5.216573  
           44) Age< 2.5 248  1112.8670 4.157258 *
           45) Age>=2.5 814  2944.2420 5.539312 *
         23) Dua=2 332  1059.1660 6.653614 *
    3) Hou=1 3347 16107.5200 6.599641  
      6) Mar=3,4,5 918  5604.2060 5.303922  
       12) Occ=2,3,4,5,6,7,8,9 555  3442.3170 4.527928 *
       13) Occ=1 363  1316.7160 6.490358 *
      7) Mar=1,2 2429  8379.6140 7.089337  
       14) Occ=2,3,4,5,6,7,8,9 1340  5336.1870 6.524627  
         28) Edu< 3.5 450  2062.6240 5.748889  
           56) Edu< 2.5 66   346.9848 4.348485 *
           57) Edu>=2.5 384  1563.9580 5.989583 *
         29) Edu>=3.5 890  2865.8470 6.916854  
           58) Hom=3,4,5 45   267.2000 5.200000 *
           59) Hom=1,2 845  2458.9420 7.008284 *
       15) Occ=1 1089  2090.2880 7.784206 *

[34mr$>[0m pdf("regression_tree_pruned_R.pdf", width=12, height=8)

[34mr$>[0m plot(tree.pruned, main = "Pruned Regression Tree")

[34mr$>[0m text(tree.pruned)

[34mr$>[0m dev.off()
null device 
          1 

[34mr$>[0m ####################################################################
[34mr$>[0m # a bootstrap sample: showing how sensitive the tree st .... [TRUNCATED] 

[34mr$>[0m data <- data[sample(1 : nrow(data), nrow(data), replace=TRUE), ]

[34mr$>[0m tree.full <- rpart(Inc ~., data = data, method = "anova", control = rpart.control(cp = .0005))

[34mr$>[0m tree.pruned <- prune(tree.full, cp = cp.best.1se)

[34mr$>[0m pdf("regression_tree_bootstraped_R.pdf", width=12, height=8)

[34mr$>[0m plot(tree.pruned, main = "Pruned Regression Tree")

[34mr$>[0m text(tree.pruned)

[34mr$>[0m dev.off()
null device 
          1 

[34mr$>[0m #######################################################################
[34mr$>[0m #	Stop redirecting console output
[34mr$>[0m sink( .... [TRUNCATED] 
