car<-read.table("E:\\math664\\regtree\\carRegTree\\car_test_frame.txt", header=TRUE) summary(car) *load package tree auto.tree <- tree(Mileage~Weight, data=car) plot(auto.tree) text(auto.tree) title("A Tree-Based Model") prune.tree(auto.tree) deviance.tree(auto.tree) plot(prune.tree(auto.tree)) summary(prune.tree(auto.tree)) not imp plot(prune.tree(auto.tree, k = 25, method = c("deviance", "misclass"), loss, eps = 1e-3)) text(prune.tree(auto.tree, k = 25, method = c("deviance", "misclass"), loss, eps = 1e-3)) predict(auto.tree) (Note that alpha numeric variables are coded as a, b, c, d, .... in the alphabetic order, e.g. in the variable Country a = Germany, b = Japan, c = Japan/USA, d = Korea, e = Mexico, f = Sweden, g = USA.) kyop <-read.table("E:\\math664\\regtree\\carRegTree\\kyphosis1.txt", header=TRUE) summary(kyop) *load package tree try.tree <- tree(Kyphosis~Age + Number + Start, data=kyop) plot(try.tree) text(try.tree) prune.tree(try.tree) deviance.tree(try.tree) plot(prune.tree(try.tree)) plot(prune.tree(try.tree, k = 2.5, method = c("deviance", "misclass"), loss, eps = 1e-3)) text(prune.tree(try.tree, k = 2.5, method = c("deviance", "misclass"), loss, eps = 1e-3)) summary(prune.tree(try.tree)) predict(try.tree) R : Copyright 2005, The R Foundation for Statistical Computing Version 2.2.1 (2005-12-20 r36812) ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > kyop <-read.table("C:\\Dell_OptiPlex_GX1_7_14_2004\\Courses\\math664\\class3_reg_tree\\kyphosis1.txt", header=TRUE) > kyop Kyphosis Age Number Start 1 absent 71 3 5 2 absent 158 3 14 3 present 128 4 5 4 absent 2 5 1 5 absent 1 4 15 6 absent 1 2 16 7 absent 61 2 17 8 absent 37 3 16 9 absent 113 2 16 10 present 59 6 12 11 present 82 5 14 12 absent 148 3 16 13 absent 18 5 2 14 absent 1 4 12 15 absent 168 3 18 16 absent 1 3 16 17 absent 78 6 15 18 absent 175 5 13 19 absent 80 5 16 20 absent 27 4 9 21 absent 22 2 16 22 present 105 6 5 23 present 96 3 12 24 absent 131 2 3 25 present 15 7 2 26 absent 9 5 13 27 absent 8 3 6 28 absent 100 3 14 29 absent 4 3 16 30 absent 151 2 16 31 absent 31 3 16 32 absent 125 2 11 33 absent 130 5 13 34 absent 112 3 16 35 absent 140 5 11 36 absent 93 3 16 37 absent 1 3 9 38 present 52 5 6 39 absent 20 6 9 40 present 91 5 12 41 present 73 5 1 42 absent 35 3 13 43 absent 143 9 3 44 absent 61 4 1 45 absent 97 3 16 46 present 139 3 10 47 absent 136 4 15 48 absent 131 5 13 49 present 121 3 3 50 absent 177 2 14 51 absent 68 5 10 52 absent 9 2 17 53 present 139 10 6 54 absent 2 2 17 55 absent 140 4 15 56 absent 72 5 15 57 absent 2 3 13 58 present 120 5 8 59 absent 51 7 9 60 absent 102 3 13 61 present 130 4 1 62 present 114 7 8 63 absent 81 4 1 64 absent 118 3 16 65 absent 118 4 16 66 absent 17 4 10 67 absent 195 2 17 68 absent 159 4 13 69 absent 18 4 11 70 absent 15 5 16 71 absent 158 5 14 72 absent 127 4 12 73 absent 87 4 16 74 absent 206 4 10 75 absent 11 3 15 76 absent 178 4 15 77 present 157 3 13 78 absent 26 7 13 79 absent 120 2 13 80 present 42 7 6 81 absent 36 4 13 > local({pkg <- select.list(sort(.packages(all.available = TRUE))) + if(nchar(pkg)) library(pkg, character.only=TRUE)}) > try.tree <- tree(Kyphosis~Age, Number, Start, data=kyop) > plot(try.tree) > text(try.tree) > summary(kyop) Kyphosis Age Number Start absent :64 Min. : 1.00 Min. : 2.000 Min. : 1.00 present:17 1st Qu.: 26.00 1st Qu.: 3.000 1st Qu.: 9.00 Median : 87.00 Median : 4.000 Median :13.00 Mean : 83.65 Mean : 4.049 Mean :11.49 3rd Qu.:130.00 3rd Qu.: 5.000 3rd Qu.:16.00 Max. :206.00 Max. :10.000 Max. :18.00 > local({pkg <- select.list(sort(.packages(all.available = TRUE))) + if(nchar(pkg)) library(pkg, character.only=TRUE)}) > prune.tree(try.tree) $size [1] 4 1 $dev [1] 121.3587 274.2743 $k [1] -Inf 50.97188 $method [1] "deviance" attr(,"class") [1] "prune" "tree.sequence" > deviance.tree(try.tree) [1] 121.3587 > plot(prune.tree(try.tree)) > summary(prune.tree(try.tree)) Length Class Mode size 2 -none- numeric dev 2 -none- numeric k 2 -none- numeric method 1 -none- character > predict(try.tree) absent present 5 1.0000000 0.0000000 14 1.0000000 0.0000000 5.1 1.0000000 0.0000000 1 0.6190476 0.3809524 15 1.0000000 0.0000000 16 1.0000000 0.0000000 17 0.6190476 0.3809524 16.1 1.0000000 0.0000000 16.2 1.0000000 0.0000000 12 1.0000000 0.0000000 14.1 1.0000000 0.0000000 16.3 1.0000000 0.0000000 2 1.0000000 0.0000000 12.1 1.0000000 0.0000000 18 1.0000000 0.0000000 16.4 1.0000000 0.0000000 15.1 1.0000000 0.0000000 13 1.0000000 0.0000000 16.5 1.0000000 0.0000000 9 0.2285714 0.7714286 16.6 1.0000000 0.0000000 5.2 1.0000000 0.0000000 12.2 1.0000000 0.0000000 3 0.2285714 0.7714286 2.1 1.0000000 0.0000000 13.1 1.0000000 0.0000000 6 1.0000000 0.0000000 14.2 1.0000000 0.0000000 16.7 1.0000000 0.0000000 16.8 1.0000000 0.0000000 16.9 1.0000000 0.0000000 11 0.2285714 0.7714286 13.2 1.0000000 0.0000000 16.10 1.0000000 0.0000000 11.1 0.2285714 0.7714286 16.11 1.0000000 0.0000000 9.1 0.2285714 0.7714286 6.1 1.0000000 0.0000000 9.2 0.2285714 0.7714286 12.3 1.0000000 0.0000000 1.1 0.6190476 0.3809524 13.3 1.0000000 0.0000000 3.1 0.2285714 0.7714286 1.2 0.6190476 0.3809524 16.12 1.0000000 0.0000000 10 0.6190476 0.3809524 15.2 1.0000000 0.0000000 13.4 1.0000000 0.0000000 3.2 0.2285714 0.7714286 14.3 1.0000000 0.0000000 10.1 0.6190476 0.3809524 17.1 0.6190476 0.3809524 6.2 1.0000000 0.0000000 17.2 0.6190476 0.3809524 15.3 1.0000000 0.0000000 15.4 1.0000000 0.0000000 13.5 1.0000000 0.0000000 8 1.0000000 0.0000000 9.3 0.2285714 0.7714286 13.6 1.0000000 0.0000000 1.3 0.6190476 0.3809524 8.1 1.0000000 0.0000000 1.4 0.6190476 0.3809524 16.13 1.0000000 0.0000000 16.14 1.0000000 0.0000000 10.2 0.6190476 0.3809524 17.3 0.6190476 0.3809524 13.7 1.0000000 0.0000000 11.2 0.2285714 0.7714286 16.15 1.0000000 0.0000000 14.4 1.0000000 0.0000000 12.4 1.0000000 0.0000000 16.16 1.0000000 0.0000000 10.3 0.6190476 0.3809524 15.5 1.0000000 0.0000000 15.6 1.0000000 0.0000000 13.8 1.0000000 0.0000000 13.9 1.0000000 0.0000000 13.10 1.0000000 0.0000000 6.3 1.0000000 0.0000000 13.11 1.0000000 0.0000000