├── Advertising.csv ├── All Labs.txt ├── Auto.csv ├── Auto.data.txt ├── Ch10Ex11.csv ├── Chapter 10 Labs.txt ├── Chapter 2 Lab.txt ├── Chapter 3 Lab.txt ├── Chapter 4 Lab.txt ├── Chapter 5 Lab.txt ├── Chapter 6 Labs.txt ├── Chapter 7 Lab.txt ├── Chapter 8 Lab.txt ├── Chapter 9 Lab.txt ├── College.csv ├── Credit.csv ├── Heart.csv ├── ISLR02.R ├── ISLR03.R ├── ISLR04.R ├── ISLR05.R ├── ISLR06.R ├── ISLR07.R ├── ISLR08.R ├── ISLR09.R ├── Income1.csv ├── Income2.csv └── README.md /Advertising.csv: -------------------------------------------------------------------------------- 1 | "","TV","Radio","Newspaper","Sales" 2 | "1",230.1,37.8,69.2,22.1 3 | "2",44.5,39.3,45.1,10.4 4 | "3",17.2,45.9,69.3,9.3 5 | "4",151.5,41.3,58.5,18.5 6 | "5",180.8,10.8,58.4,12.9 7 | "6",8.7,48.9,75,7.2 8 | "7",57.5,32.8,23.5,11.8 9 | "8",120.2,19.6,11.6,13.2 10 | "9",8.6,2.1,1,4.8 11 | "10",199.8,2.6,21.2,10.6 12 | "11",66.1,5.8,24.2,8.6 13 | "12",214.7,24,4,17.4 14 | "13",23.8,35.1,65.9,9.2 15 | "14",97.5,7.6,7.2,9.7 16 | "15",204.1,32.9,46,19 17 | "16",195.4,47.7,52.9,22.4 18 | "17",67.8,36.6,114,12.5 19 | "18",281.4,39.6,55.8,24.4 20 | "19",69.2,20.5,18.3,11.3 21 | "20",147.3,23.9,19.1,14.6 22 | "21",218.4,27.7,53.4,18 23 | "22",237.4,5.1,23.5,12.5 24 | "23",13.2,15.9,49.6,5.6 25 | "24",228.3,16.9,26.2,15.5 26 | "25",62.3,12.6,18.3,9.7 27 | "26",262.9,3.5,19.5,12 28 | "27",142.9,29.3,12.6,15 29 | "28",240.1,16.7,22.9,15.9 30 | "29",248.8,27.1,22.9,18.9 31 | "30",70.6,16,40.8,10.5 32 | "31",292.9,28.3,43.2,21.4 33 | "32",112.9,17.4,38.6,11.9 34 | "33",97.2,1.5,30,9.6 35 | "34",265.6,20,0.3,17.4 36 | "35",95.7,1.4,7.4,9.5 37 | "36",290.7,4.1,8.5,12.8 38 | "37",266.9,43.8,5,25.4 39 | "38",74.7,49.4,45.7,14.7 40 | "39",43.1,26.7,35.1,10.1 41 | "40",228,37.7,32,21.5 42 | "41",202.5,22.3,31.6,16.6 43 | "42",177,33.4,38.7,17.1 44 | "43",293.6,27.7,1.8,20.7 45 | "44",206.9,8.4,26.4,12.9 46 | "45",25.1,25.7,43.3,8.5 47 | "46",175.1,22.5,31.5,14.9 48 | "47",89.7,9.9,35.7,10.6 49 | "48",239.9,41.5,18.5,23.2 50 | "49",227.2,15.8,49.9,14.8 51 | "50",66.9,11.7,36.8,9.7 52 | "51",199.8,3.1,34.6,11.4 53 | "52",100.4,9.6,3.6,10.7 54 | "53",216.4,41.7,39.6,22.6 55 | "54",182.6,46.2,58.7,21.2 56 | "55",262.7,28.8,15.9,20.2 57 | "56",198.9,49.4,60,23.7 58 | "57",7.3,28.1,41.4,5.5 59 | "58",136.2,19.2,16.6,13.2 60 | "59",210.8,49.6,37.7,23.8 61 | "60",210.7,29.5,9.3,18.4 62 | "61",53.5,2,21.4,8.1 63 | "62",261.3,42.7,54.7,24.2 64 | "63",239.3,15.5,27.3,15.7 65 | "64",102.7,29.6,8.4,14 66 | "65",131.1,42.8,28.9,18 67 | "66",69,9.3,0.9,9.3 68 | "67",31.5,24.6,2.2,9.5 69 | "68",139.3,14.5,10.2,13.4 70 | "69",237.4,27.5,11,18.9 71 | "70",216.8,43.9,27.2,22.3 72 | "71",199.1,30.6,38.7,18.3 73 | "72",109.8,14.3,31.7,12.4 74 | "73",26.8,33,19.3,8.8 75 | "74",129.4,5.7,31.3,11 76 | "75",213.4,24.6,13.1,17 77 | "76",16.9,43.7,89.4,8.7 78 | "77",27.5,1.6,20.7,6.9 79 | "78",120.5,28.5,14.2,14.2 80 | "79",5.4,29.9,9.4,5.3 81 | "80",116,7.7,23.1,11 82 | "81",76.4,26.7,22.3,11.8 83 | "82",239.8,4.1,36.9,12.3 84 | "83",75.3,20.3,32.5,11.3 85 | "84",68.4,44.5,35.6,13.6 86 | "85",213.5,43,33.8,21.7 87 | "86",193.2,18.4,65.7,15.2 88 | "87",76.3,27.5,16,12 89 | "88",110.7,40.6,63.2,16 90 | "89",88.3,25.5,73.4,12.9 91 | "90",109.8,47.8,51.4,16.7 92 | "91",134.3,4.9,9.3,11.2 93 | "92",28.6,1.5,33,7.3 94 | "93",217.7,33.5,59,19.4 95 | "94",250.9,36.5,72.3,22.2 96 | "95",107.4,14,10.9,11.5 97 | "96",163.3,31.6,52.9,16.9 98 | "97",197.6,3.5,5.9,11.7 99 | "98",184.9,21,22,15.5 100 | "99",289.7,42.3,51.2,25.4 101 | "100",135.2,41.7,45.9,17.2 102 | "101",222.4,4.3,49.8,11.7 103 | "102",296.4,36.3,100.9,23.8 104 | "103",280.2,10.1,21.4,14.8 105 | "104",187.9,17.2,17.9,14.7 106 | "105",238.2,34.3,5.3,20.7 107 | "106",137.9,46.4,59,19.2 108 | "107",25,11,29.7,7.2 109 | "108",90.4,0.3,23.2,8.7 110 | "109",13.1,0.4,25.6,5.3 111 | "110",255.4,26.9,5.5,19.8 112 | "111",225.8,8.2,56.5,13.4 113 | "112",241.7,38,23.2,21.8 114 | "113",175.7,15.4,2.4,14.1 115 | "114",209.6,20.6,10.7,15.9 116 | "115",78.2,46.8,34.5,14.6 117 | "116",75.1,35,52.7,12.6 118 | "117",139.2,14.3,25.6,12.2 119 | "118",76.4,0.8,14.8,9.4 120 | "119",125.7,36.9,79.2,15.9 121 | "120",19.4,16,22.3,6.6 122 | "121",141.3,26.8,46.2,15.5 123 | "122",18.8,21.7,50.4,7 124 | "123",224,2.4,15.6,11.6 125 | "124",123.1,34.6,12.4,15.2 126 | "125",229.5,32.3,74.2,19.7 127 | "126",87.2,11.8,25.9,10.6 128 | "127",7.8,38.9,50.6,6.6 129 | "128",80.2,0,9.2,8.8 130 | "129",220.3,49,3.2,24.7 131 | "130",59.6,12,43.1,9.7 132 | "131",0.7,39.6,8.7,1.6 133 | "132",265.2,2.9,43,12.7 134 | "133",8.4,27.2,2.1,5.7 135 | "134",219.8,33.5,45.1,19.6 136 | "135",36.9,38.6,65.6,10.8 137 | "136",48.3,47,8.5,11.6 138 | "137",25.6,39,9.3,9.5 139 | "138",273.7,28.9,59.7,20.8 140 | "139",43,25.9,20.5,9.6 141 | "140",184.9,43.9,1.7,20.7 142 | "141",73.4,17,12.9,10.9 143 | "142",193.7,35.4,75.6,19.2 144 | "143",220.5,33.2,37.9,20.1 145 | "144",104.6,5.7,34.4,10.4 146 | "145",96.2,14.8,38.9,11.4 147 | "146",140.3,1.9,9,10.3 148 | "147",240.1,7.3,8.7,13.2 149 | "148",243.2,49,44.3,25.4 150 | "149",38,40.3,11.9,10.9 151 | "150",44.7,25.8,20.6,10.1 152 | "151",280.7,13.9,37,16.1 153 | "152",121,8.4,48.7,11.6 154 | "153",197.6,23.3,14.2,16.6 155 | "154",171.3,39.7,37.7,19 156 | "155",187.8,21.1,9.5,15.6 157 | "156",4.1,11.6,5.7,3.2 158 | "157",93.9,43.5,50.5,15.3 159 | "158",149.8,1.3,24.3,10.1 160 | "159",11.7,36.9,45.2,7.3 161 | "160",131.7,18.4,34.6,12.9 162 | "161",172.5,18.1,30.7,14.4 163 | "162",85.7,35.8,49.3,13.3 164 | "163",188.4,18.1,25.6,14.9 165 | "164",163.5,36.8,7.4,18 166 | "165",117.2,14.7,5.4,11.9 167 | "166",234.5,3.4,84.8,11.9 168 | "167",17.9,37.6,21.6,8 169 | "168",206.8,5.2,19.4,12.2 170 | "169",215.4,23.6,57.6,17.1 171 | "170",284.3,10.6,6.4,15 172 | "171",50,11.6,18.4,8.4 173 | "172",164.5,20.9,47.4,14.5 174 | "173",19.6,20.1,17,7.6 175 | "174",168.4,7.1,12.8,11.7 176 | "175",222.4,3.4,13.1,11.5 177 | "176",276.9,48.9,41.8,27 178 | "177",248.4,30.2,20.3,20.2 179 | "178",170.2,7.8,35.2,11.7 180 | "179",276.7,2.3,23.7,11.8 181 | "180",165.6,10,17.6,12.6 182 | "181",156.6,2.6,8.3,10.5 183 | "182",218.5,5.4,27.4,12.2 184 | "183",56.2,5.7,29.7,8.7 185 | "184",287.6,43,71.8,26.2 186 | "185",253.8,21.3,30,17.6 187 | "186",205,45.1,19.6,22.6 188 | "187",139.5,2.1,26.6,10.3 189 | "188",191.1,28.7,18.2,17.3 190 | "189",286,13.9,3.7,15.9 191 | "190",18.7,12.1,23.4,6.7 192 | "191",39.5,41.1,5.8,10.8 193 | "192",75.5,10.8,6,9.9 194 | "193",17.2,4.1,31.6,5.9 195 | "194",166.8,42,3.6,19.6 196 | "195",149.7,35.6,6,17.3 197 | "196",38.2,3.7,13.8,7.6 198 | "197",94.2,4.9,8.1,9.7 199 | "198",177,9.3,6.4,12.8 200 | "199",283.6,42,66.2,25.5 201 | "200",232.1,8.6,8.7,13.4 202 | -------------------------------------------------------------------------------- /All Labs.txt: -------------------------------------------------------------------------------- 1 | # Chapter 2 Lab: Introduction to R 2 | 3 | # Basic Commands 4 | 5 | x <- c(1,3,2,5) 6 | x 7 | x = c(1,6,2) 8 | x 9 | y = c(1,4,3) 10 | length(x) 11 | length(y) 12 | x+y 13 | ls() 14 | rm(x,y) 15 | ls() 16 | rm(list=ls()) 17 | ?matrix 18 | x=matrix(data=c(1,2,3,4), nrow=2, ncol=2) 19 | x 20 | x=matrix(c(1,2,3,4),2,2) 21 | matrix(c(1,2,3,4),2,2,byrow=TRUE) 22 | sqrt(x) 23 | x^2 24 | x=rnorm(50) 25 | y=x+rnorm(50,mean=50,sd=.1) 26 | cor(x,y) 27 | set.seed(1303) 28 | rnorm(50) 29 | set.seed(3) 30 | y=rnorm(100) 31 | mean(y) 32 | var(y) 33 | sqrt(var(y)) 34 | sd(y) 35 | 36 | # Graphics 37 | 38 | x=rnorm(100) 39 | y=rnorm(100) 40 | plot(x,y) 41 | plot(x,y,xlab="this is the x-axis",ylab="this is the y-axis",main="Plot of X vs Y") 42 | pdf("Figure.pdf") 43 | plot(x,y,col="green") 44 | dev.off() 45 | x=seq(1,10) 46 | x 47 | x=1:10 48 | x 49 | x=seq(-pi,pi,length=50) 50 | y=x 51 | f=outer(x,y,function(x,y)cos(y)/(1+x^2)) 52 | contour(x,y,f) 53 | contour(x,y,f,nlevels=45,add=T) 54 | fa=(f-t(f))/2 55 | contour(x,y,fa,nlevels=15) 56 | image(x,y,fa) 57 | persp(x,y,fa) 58 | persp(x,y,fa,theta=30) 59 | persp(x,y,fa,theta=30,phi=20) 60 | persp(x,y,fa,theta=30,phi=70) 61 | persp(x,y,fa,theta=30,phi=40) 62 | 63 | # Indexing Data 64 | 65 | A=matrix(1:16,4,4) 66 | A 67 | A[2,3] 68 | A[c(1,3),c(2,4)] 69 | A[1:3,2:4] 70 | A[1:2,] 71 | A[,1:2] 72 | A[1,] 73 | A[-c(1,3),] 74 | A[-c(1,3),-c(1,3,4)] 75 | dim(A) 76 | 77 | # Loading Data 78 | 79 | Auto=read.table("Auto.data") 80 | fix(Auto) 81 | Auto=read.table("Auto.data",header=T,na.strings="?") 82 | fix(Auto) 83 | Auto=read.csv("Auto.csv",header=T,na.strings="?") 84 | fix(Auto) 85 | dim(Auto) 86 | Auto[1:4,] 87 | Auto=na.omit(Auto) 88 | dim(Auto) 89 | names(Auto) 90 | 91 | # Additional Graphical and Numerical Summaries 92 | 93 | plot(cylinders, mpg) 94 | plot(Auto$cylinders, Auto$mpg) 95 | attach(Auto) 96 | plot(cylinders, mpg) 97 | cylinders=as.factor(cylinders) 98 | plot(cylinders, mpg) 99 | plot(cylinders, mpg, col="red") 100 | plot(cylinders, mpg, col="red", varwidth=T) 101 | plot(cylinders, mpg, col="red", varwidth=T,horizontal=T) 102 | plot(cylinders, mpg, col="red", varwidth=T, xlab="cylinders", ylab="MPG") 103 | hist(mpg) 104 | hist(mpg,col=2) 105 | hist(mpg,col=2,breaks=15) 106 | pairs(Auto) 107 | pairs(~ mpg + displacement + horsepower + weight + acceleration, Auto) 108 | plot(horsepower,mpg) 109 | identify(horsepower,mpg,name) 110 | summary(Auto) 111 | summary(mpg) 112 | 113 | 114 | 115 | # Chapter 3 Lab: Linear Regression 116 | 117 | library(MASS) 118 | library(ISLR) 119 | 120 | # Simple Linear Regression 121 | 122 | fix(Boston) 123 | names(Boston) 124 | lm.fit=lm(medv~lstat) 125 | lm.fit=lm(medv~lstat,data=Boston) 126 | attach(Boston) 127 | lm.fit=lm(medv~lstat) 128 | lm.fit 129 | summary(lm.fit) 130 | names(lm.fit) 131 | coef(lm.fit) 132 | confint(lm.fit) 133 | predict(lm.fit,data.frame(lstat=(c(5,10,15))), interval="confidence") 134 | predict(lm.fit,data.frame(lstat=(c(5,10,15))), interval="prediction") 135 | plot(lstat,medv) 136 | abline(lm.fit) 137 | abline(lm.fit,lwd=3) 138 | abline(lm.fit,lwd=3,col="red") 139 | plot(lstat,medv,col="red") 140 | plot(lstat,medv,pch=20) 141 | plot(lstat,medv,pch="+") 142 | plot(1:20,1:20,pch=1:20) 143 | par(mfrow=c(2,2)) 144 | plot(lm.fit) 145 | plot(predict(lm.fit), residuals(lm.fit)) 146 | plot(predict(lm.fit), rstudent(lm.fit)) 147 | plot(hatvalues(lm.fit)) 148 | which.max(hatvalues(lm.fit)) 149 | 150 | # Multiple Linear Regression 151 | 152 | lm.fit=lm(medv~lstat+age,data=Boston) 153 | summary(lm.fit) 154 | lm.fit=lm(medv~.,data=Boston) 155 | summary(lm.fit) 156 | library(car) 157 | vif(lm.fit) 158 | lm.fit1=lm(medv~.-age,data=Boston) 159 | summary(lm.fit1) 160 | lm.fit1=update(lm.fit, ~.-age) 161 | 162 | # Interaction Terms 163 | 164 | summary(lm(medv~lstat*age,data=Boston)) 165 | 166 | # Non-linear Transformations of the Predictors 167 | 168 | lm.fit2=lm(medv~lstat+I(lstat^2)) 169 | summary(lm.fit2) 170 | lm.fit=lm(medv~lstat) 171 | anova(lm.fit,lm.fit2) 172 | par(mfrow=c(2,2)) 173 | plot(lm.fit2) 174 | lm.fit5=lm(medv~poly(lstat,5)) 175 | summary(lm.fit5) 176 | summary(lm(medv~log(rm),data=Boston)) 177 | 178 | # Qualitative Predictors 179 | 180 | fix(Carseats) 181 | names(Carseats) 182 | lm.fit=lm(Sales~.+Income:Advertising+Price:Age,data=Carseats) 183 | summary(lm.fit) 184 | attach(Carseats) 185 | contrasts(ShelveLoc) 186 | 187 | # Writing Functions 188 | 189 | LoadLibraries 190 | LoadLibraries() 191 | LoadLibraries=function(){ 192 | library(ISLR) 193 | library(MASS) 194 | print("The libraries have been loaded.") 195 | } 196 | LoadLibraries 197 | LoadLibraries() 198 | 199 | 200 | # Chapter 4 Lab: Logistic Regression, LDA, QDA, and KNN 201 | 202 | # The Stock Market Data 203 | 204 | library(ISLR) 205 | names(Smarket) 206 | dim(Smarket) 207 | summary(Smarket) 208 | pairs(Smarket) 209 | cor(Smarket) 210 | cor(Smarket[,-9]) 211 | attach(Smarket) 212 | plot(Volume) 213 | 214 | # Logistic Regression 215 | 216 | glm.fit=glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,data=Smarket,family=binomial) 217 | summary(glm.fit) 218 | coef(glm.fit) 219 | summary(glm.fit)$coef 220 | summary(glm.fit)$coef[,4] 221 | glm.probs=predict(glm.fit,type="response") 222 | glm.probs[1:10] 223 | contrasts(Direction) 224 | glm.pred=rep("Down",1250) 225 | glm.pred[glm.probs>.5]="Up" 226 | table(glm.pred,Direction) 227 | (507+145)/1250 228 | mean(glm.pred==Direction) 229 | train=(Year<2005) 230 | Smarket.2005=Smarket[!train,] 231 | dim(Smarket.2005) 232 | Direction.2005=Direction[!train] 233 | glm.fit=glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,data=Smarket,family=binomial,subset=train) 234 | glm.probs=predict(glm.fit,Smarket.2005,type="response") 235 | glm.pred=rep("Down",252) 236 | glm.pred[glm.probs>.5]="Up" 237 | table(glm.pred,Direction.2005) 238 | mean(glm.pred==Direction.2005) 239 | mean(glm.pred!=Direction.2005) 240 | glm.fit=glm(Direction~Lag1+Lag2,data=Smarket,family=binomial,subset=train) 241 | glm.probs=predict(glm.fit,Smarket.2005,type="response") 242 | glm.pred=rep("Down",252) 243 | glm.pred[glm.probs>.5]="Up" 244 | table(glm.pred,Direction.2005) 245 | mean(glm.pred==Direction.2005) 246 | 106/(106+76) 247 | predict(glm.fit,newdata=data.frame(Lag1=c(1.2,1.5),Lag2=c(1.1,-0.8)),type="response") 248 | 249 | # Linear Discriminant Analysis 250 | 251 | library(MASS) 252 | lda.fit=lda(Direction~Lag1+Lag2,data=Smarket,subset=train) 253 | lda.fit 254 | plot(lda.fit) 255 | lda.pred=predict(lda.fit, Smarket.2005) 256 | names(lda.pred) 257 | lda.class=lda.pred$class 258 | table(lda.class,Direction.2005) 259 | mean(lda.class==Direction.2005) 260 | sum(lda.pred$posterior[,1]>=.5) 261 | sum(lda.pred$posterior[,1]<.5) 262 | lda.pred$posterior[1:20,1] 263 | lda.class[1:20] 264 | sum(lda.pred$posterior[,1]>.9) 265 | 266 | # Quadratic Discriminant Analysis 267 | 268 | qda.fit=qda(Direction~Lag1+Lag2,data=Smarket,subset=train) 269 | qda.fit 270 | qda.class=predict(qda.fit,Smarket.2005)$class 271 | table(qda.class,Direction.2005) 272 | mean(qda.class==Direction.2005) 273 | 274 | # K-Nearest Neighbors 275 | 276 | library(class) 277 | train.X=cbind(Lag1,Lag2)[train,] 278 | test.X=cbind(Lag1,Lag2)[!train,] 279 | train.Direction=Direction[train] 280 | set.seed(1) 281 | knn.pred=knn(train.X,test.X,train.Direction,k=1) 282 | table(knn.pred,Direction.2005) 283 | (83+43)/252 284 | knn.pred=knn(train.X,test.X,train.Direction,k=3) 285 | table(knn.pred,Direction.2005) 286 | mean(knn.pred==Direction.2005) 287 | 288 | # An Application to Caravan Insurance Data 289 | 290 | dim(Caravan) 291 | attach(Caravan) 292 | summary(Purchase) 293 | 348/5822 294 | standardized.X=scale(Caravan[,-86]) 295 | var(Caravan[,1]) 296 | var(Caravan[,2]) 297 | var(standardized.X[,1]) 298 | var(standardized.X[,2]) 299 | test=1:1000 300 | train.X=standardized.X[-test,] 301 | test.X=standardized.X[test,] 302 | train.Y=Purchase[-test] 303 | test.Y=Purchase[test] 304 | set.seed(1) 305 | knn.pred=knn(train.X,test.X,train.Y,k=1) 306 | mean(test.Y!=knn.pred) 307 | mean(test.Y!="No") 308 | table(knn.pred,test.Y) 309 | 9/(68+9) 310 | knn.pred=knn(train.X,test.X,train.Y,k=3) 311 | table(knn.pred,test.Y) 312 | 5/26 313 | knn.pred=knn(train.X,test.X,train.Y,k=5) 314 | table(knn.pred,test.Y) 315 | 4/15 316 | glm.fit=glm(Purchase~.,data=Caravan,family=binomial,subset=-test) 317 | glm.probs=predict(glm.fit,Caravan[test,],type="response") 318 | glm.pred=rep("No",1000) 319 | glm.pred[glm.probs>.5]="Yes" 320 | table(glm.pred,test.Y) 321 | glm.pred=rep("No",1000) 322 | glm.pred[glm.probs>.25]="Yes" 323 | table(glm.pred,test.Y) 324 | 11/(22+11) 325 | 326 | 327 | 328 | # Chaper 5 Lab: Cross-Validation and the Bootstrap 329 | 330 | # The Validation Set Approach 331 | 332 | library(ISLR) 333 | set.seed(1) 334 | train=sample(392,196) 335 | lm.fit=lm(mpg~horsepower,data=Auto,subset=train) 336 | attach(Auto) 337 | mean((mpg-predict(lm.fit,Auto))[-train]^2) 338 | lm.fit2=lm(mpg~poly(horsepower,2),data=Auto,subset=train) 339 | mean((mpg-predict(lm.fit2,Auto))[-train]^2) 340 | lm.fit3=lm(mpg~poly(horsepower,3),data=Auto,subset=train) 341 | mean((mpg-predict(lm.fit3,Auto))[-train]^2) 342 | set.seed(2) 343 | train=sample(392,196) 344 | lm.fit=lm(mpg~horsepower,subset=train) 345 | mean((mpg-predict(lm.fit,Auto))[-train]^2) 346 | lm.fit2=lm(mpg~poly(horsepower,2),data=Auto,subset=train) 347 | mean((mpg-predict(lm.fit2,Auto))[-train]^2) 348 | lm.fit3=lm(mpg~poly(horsepower,3),data=Auto,subset=train) 349 | mean((mpg-predict(lm.fit3,Auto))[-train]^2) 350 | 351 | # Leave-One-Out Cross-Validation 352 | 353 | glm.fit=glm(mpg~horsepower,data=Auto) 354 | coef(glm.fit) 355 | lm.fit=lm(mpg~horsepower,data=Auto) 356 | coef(lm.fit) 357 | library(boot) 358 | glm.fit=glm(mpg~horsepower,data=Auto) 359 | cv.err=cv.glm(Auto,glm.fit) 360 | cv.err$delta 361 | cv.error=rep(0,5) 362 | for (i in 1:5){ 363 | glm.fit=glm(mpg~poly(horsepower,i),data=Auto) 364 | cv.error[i]=cv.glm(Auto,glm.fit)$delta[1] 365 | } 366 | cv.error 367 | 368 | # k-Fold Cross-Validation 369 | 370 | set.seed(17) 371 | cv.error.10=rep(0,10) 372 | for (i in 1:10){ 373 | glm.fit=glm(mpg~poly(horsepower,i),data=Auto) 374 | cv.error.10[i]=cv.glm(Auto,glm.fit,K=10)$delta[1] 375 | } 376 | cv.error.10 377 | 378 | # The Bootstrap 379 | 380 | alpha.fn=function(data,index){ 381 | X=data$X[index] 382 | Y=data$Y[index] 383 | return((var(Y)-cov(X,Y))/(var(X)+var(Y)-2*cov(X,Y))) 384 | } 385 | alpha.fn(Portfolio,1:100) 386 | set.seed(1) 387 | alpha.fn(Portfolio,sample(100,100,replace=T)) 388 | boot(Portfolio,alpha.fn,R=1000) 389 | 390 | # Estimating the Accuracy of a Linear Regression Model 391 | 392 | boot.fn=function(data,index) 393 | return(coef(lm(mpg~horsepower,data=data,subset=index))) 394 | boot.fn(Auto,1:392) 395 | set.seed(1) 396 | boot.fn(Auto,sample(392,392,replace=T)) 397 | boot.fn(Auto,sample(392,392,replace=T)) 398 | boot(Auto,boot.fn,1000) 399 | summary(lm(mpg~horsepower,data=Auto))$coef 400 | boot.fn=function(data,index) 401 | coefficients(lm(mpg~horsepower+I(horsepower^2),data=data,subset=index)) 402 | set.seed(1) 403 | boot(Auto,boot.fn,1000) 404 | summary(lm(mpg~horsepower+I(horsepower^2),data=Auto))$coef 405 | 406 | 407 | 408 | # Chapter 6 Lab 1: Subset Selection Methods 409 | 410 | # Best Subset Selection 411 | 412 | library(ISLR) 413 | fix(Hitters) 414 | names(Hitters) 415 | dim(Hitters) 416 | sum(is.na(Hitters$Salary)) 417 | Hitters=na.omit(Hitters) 418 | dim(Hitters) 419 | sum(is.na(Hitters)) 420 | library(leaps) 421 | regfit.full=regsubsets(Salary~.,Hitters) 422 | summary(regfit.full) 423 | regfit.full=regsubsets(Salary~.,data=Hitters,nvmax=19) 424 | reg.summary=summary(regfit.full) 425 | names(reg.summary) 426 | reg.summary$rsq 427 | par(mfrow=c(2,2)) 428 | plot(reg.summary$rss,xlab="Number of Variables",ylab="RSS",type="l") 429 | plot(reg.summary$adjr2,xlab="Number of Variables",ylab="Adjusted RSq",type="l") 430 | which.max(reg.summary$adjr2) 431 | points(11,reg.summary$adjr2[11], col="red",cex=2,pch=20) 432 | plot(reg.summary$cp,xlab="Number of Variables",ylab="Cp",type='l') 433 | which.min(reg.summary$cp) 434 | points(10,reg.summary$cp[10],col="red",cex=2,pch=20) 435 | which.min(reg.summary$bic) 436 | plot(reg.summary$bic,xlab="Number of Variables",ylab="BIC",type='l') 437 | points(6,reg.summary$bic[6],col="red",cex=2,pch=20) 438 | plot(regfit.full,scale="r2") 439 | plot(regfit.full,scale="adjr2") 440 | plot(regfit.full,scale="Cp") 441 | plot(regfit.full,scale="bic") 442 | coef(regfit.full,6) 443 | 444 | # Forward and Backward Stepwise Selection 445 | 446 | regfit.fwd=regsubsets(Salary~.,data=Hitters,nvmax=19,method="forward") 447 | summary(regfit.fwd) 448 | regfit.bwd=regsubsets(Salary~.,data=Hitters,nvmax=19,method="backward") 449 | summary(regfit.bwd) 450 | coef(regfit.full,7) 451 | coef(regfit.fwd,7) 452 | coef(regfit.bwd,7) 453 | 454 | # Choosing Among Models 455 | 456 | set.seed(1) 457 | train=sample(c(TRUE,FALSE), nrow(Hitters),rep=TRUE) 458 | test=(!train) 459 | regfit.best=regsubsets(Salary~.,data=Hitters[train,],nvmax=19) 460 | test.mat=model.matrix(Salary~.,data=Hitters[test,]) 461 | val.errors=rep(NA,19) 462 | for(i in 1:19){ 463 | coefi=coef(regfit.best,id=i) 464 | pred=test.mat[,names(coefi)]%*%coefi 465 | val.errors[i]=mean((Hitters$Salary[test]-pred)^2) 466 | } 467 | val.errors 468 | which.min(val.errors) 469 | coef(regfit.best,10) 470 | predict.regsubsets=function(object,newdata,id,...){ 471 | form=as.formula(object$call[[2]]) 472 | mat=model.matrix(form,newdata) 473 | coefi=coef(object,id=id) 474 | xvars=names(coefi) 475 | mat[,xvars]%*%coefi 476 | } 477 | regfit.best=regsubsets(Salary~.,data=Hitters,nvmax=19) 478 | coef(regfit.best,10) 479 | k=10 480 | set.seed(1) 481 | folds=sample(1:k,nrow(Hitters),replace=TRUE) 482 | cv.errors=matrix(NA,k,19, dimnames=list(NULL, paste(1:19))) 483 | for(j in 1:k){ 484 | best.fit=regsubsets(Salary~.,data=Hitters[folds!=j,],nvmax=19) 485 | for(i in 1:19){ 486 | pred=predict(best.fit,Hitters[folds==j,],id=i) 487 | cv.errors[j,i]=mean( (Hitters$Salary[folds==j]-pred)^2) 488 | } 489 | } 490 | mean.cv.errors=apply(cv.errors,2,mean) 491 | mean.cv.errors 492 | par(mfrow=c(1,1)) 493 | plot(mean.cv.errors,type='b') 494 | reg.best=regsubsets(Salary~.,data=Hitters, nvmax=19) 495 | coef(reg.best,11) 496 | 497 | 498 | # Chapter 6 Lab 2: Ridge Regression and the Lasso 499 | 500 | x=model.matrix(Salary~.,Hitters)[,-1] 501 | y=Hitters$Salary 502 | 503 | # Ridge Regression 504 | 505 | library(glmnet) 506 | grid=10^seq(10,-2,length=100) 507 | ridge.mod=glmnet(x,y,alpha=0,lambda=grid) 508 | dim(coef(ridge.mod)) 509 | ridge.mod$lambda[50] 510 | coef(ridge.mod)[,50] 511 | sqrt(sum(coef(ridge.mod)[-1,50]^2)) 512 | ridge.mod$lambda[60] 513 | coef(ridge.mod)[,60] 514 | sqrt(sum(coef(ridge.mod)[-1,60]^2)) 515 | predict(ridge.mod,s=50,type="coefficients")[1:20,] 516 | set.seed(1) 517 | train=sample(1:nrow(x), nrow(x)/2) 518 | test=(-train) 519 | y.test=y[test] 520 | ridge.mod=glmnet(x[train,],y[train],alpha=0,lambda=grid, thresh=1e-12) 521 | ridge.pred=predict(ridge.mod,s=4,newx=x[test,]) 522 | mean((ridge.pred-y.test)^2) 523 | mean((mean(y[train])-y.test)^2) 524 | ridge.pred=predict(ridge.mod,s=1e10,newx=x[test,]) 525 | mean((ridge.pred-y.test)^2) 526 | ridge.pred=predict(ridge.mod,s=0,newx=x[test,],exact=T) 527 | mean((ridge.pred-y.test)^2) 528 | lm(y~x, subset=train) 529 | predict(ridge.mod,s=0,exact=T,type="coefficients")[1:20,] 530 | set.seed(1) 531 | cv.out=cv.glmnet(x[train,],y[train],alpha=0) 532 | plot(cv.out) 533 | bestlam=cv.out$lambda.min 534 | bestlam 535 | ridge.pred=predict(ridge.mod,s=bestlam,newx=x[test,]) 536 | mean((ridge.pred-y.test)^2) 537 | out=glmnet(x,y,alpha=0) 538 | predict(out,type="coefficients",s=bestlam)[1:20,] 539 | 540 | # The Lasso 541 | 542 | lasso.mod=glmnet(x[train,],y[train],alpha=1,lambda=grid) 543 | plot(lasso.mod) 544 | set.seed(1) 545 | cv.out=cv.glmnet(x[train,],y[train],alpha=1) 546 | plot(cv.out) 547 | bestlam=cv.out$lambda.min 548 | lasso.pred=predict(lasso.mod,s=bestlam,newx=x[test,]) 549 | mean((lasso.pred-y.test)^2) 550 | out=glmnet(x,y,alpha=1,lambda=grid) 551 | lasso.coef=predict(out,type="coefficients",s=bestlam)[1:20,] 552 | lasso.coef 553 | lasso.coef[lasso.coef!=0] 554 | 555 | 556 | # Chapter 6 Lab 3: PCR and PLS Regression 557 | 558 | # Principal Components Regression 559 | 560 | library(pls) 561 | set.seed(2) 562 | pcr.fit=pcr(Salary~., data=Hitters,scale=TRUE,validation="CV") 563 | summary(pcr.fit) 564 | validationplot(pcr.fit,val.type="MSEP") 565 | set.seed(1) 566 | pcr.fit=pcr(Salary~., data=Hitters,subset=train,scale=TRUE, validation="CV") 567 | validationplot(pcr.fit,val.type="MSEP") 568 | pcr.pred=predict(pcr.fit,x[test,],ncomp=7) 569 | mean((pcr.pred-y.test)^2) 570 | pcr.fit=pcr(y~x,scale=TRUE,ncomp=7) 571 | summary(pcr.fit) 572 | 573 | # Partial Least Squares 574 | 575 | set.seed(1) 576 | pls.fit=plsr(Salary~., data=Hitters,subset=train,scale=TRUE, validation="CV") 577 | summary(pls.fit) 578 | validationplot(pls.fit,val.type="MSEP") 579 | pls.pred=predict(pls.fit,x[test,],ncomp=2) 580 | mean((pls.pred-y.test)^2) 581 | pls.fit=plsr(Salary~., data=Hitters,scale=TRUE,ncomp=2) 582 | summary(pls.fit) 583 | 584 | 585 | 586 | # Chapter 7 Lab: Non-linear Modeling 587 | 588 | library(ISLR) 589 | attach(Wage) 590 | 591 | # Polynomial Regression and Step Functions 592 | 593 | fit=lm(wage~poly(age,4),data=Wage) 594 | coef(summary(fit)) 595 | fit2=lm(wage~poly(age,4,raw=T),data=Wage) 596 | coef(summary(fit2)) 597 | fit2a=lm(wage~age+I(age^2)+I(age^3)+I(age^4),data=Wage) 598 | coef(fit2a) 599 | fit2b=lm(wage~cbind(age,age^2,age^3,age^4),data=Wage) 600 | agelims=range(age) 601 | age.grid=seq(from=agelims[1],to=agelims[2]) 602 | preds=predict(fit,newdata=list(age=age.grid),se=TRUE) 603 | se.bands=cbind(preds$fit+2*preds$se.fit,preds$fit-2*preds$se.fit) 604 | par(mfrow=c(1,2),mar=c(4.5,4.5,1,1),oma=c(0,0,4,0)) 605 | plot(age,wage,xlim=agelims,cex=.5,col="darkgrey") 606 | title("Degree-4 Polynomial",outer=T) 607 | lines(age.grid,preds$fit,lwd=2,col="blue") 608 | matlines(age.grid,se.bands,lwd=1,col="blue",lty=3) 609 | preds2=predict(fit2,newdata=list(age=age.grid),se=TRUE) 610 | max(abs(preds$fit-preds2$fit)) 611 | fit.1=lm(wage~age,data=Wage) 612 | fit.2=lm(wage~poly(age,2),data=Wage) 613 | fit.3=lm(wage~poly(age,3),data=Wage) 614 | fit.4=lm(wage~poly(age,4),data=Wage) 615 | fit.5=lm(wage~poly(age,5),data=Wage) 616 | anova(fit.1,fit.2,fit.3,fit.4,fit.5) 617 | coef(summary(fit.5)) 618 | (-11.983)^2 619 | fit.1=lm(wage~education+age,data=Wage) 620 | fit.2=lm(wage~education+poly(age,2),data=Wage) 621 | fit.3=lm(wage~education+poly(age,3),data=Wage) 622 | anova(fit.1,fit.2,fit.3) 623 | fit=glm(I(wage>250)~poly(age,4),data=Wage,family=binomial) 624 | preds=predict(fit,newdata=list(age=age.grid),se=T) 625 | pfit=exp(preds$fit)/(1+exp(preds$fit)) 626 | se.bands.logit = cbind(preds$fit+2*preds$se.fit, preds$fit-2*preds$se.fit) 627 | se.bands = exp(se.bands.logit)/(1+exp(se.bands.logit)) 628 | preds=predict(fit,newdata=list(age=age.grid),type="response",se=T) 629 | plot(age,I(wage>250),xlim=agelims,type="n",ylim=c(0,.2)) 630 | points(jitter(age), I((wage>250)/5),cex=.5,pch="|",col="darkgrey") 631 | lines(age.grid,pfit,lwd=2, col="blue") 632 | matlines(age.grid,se.bands,lwd=1,col="blue",lty=3) 633 | table(cut(age,4)) 634 | fit=lm(wage~cut(age,4),data=Wage) 635 | coef(summary(fit)) 636 | 637 | # Splines 638 | 639 | library(splines) 640 | fit=lm(wage~bs(age,knots=c(25,40,60)),data=Wage) 641 | pred=predict(fit,newdata=list(age=age.grid),se=T) 642 | plot(age,wage,col="gray") 643 | lines(age.grid,pred$fit,lwd=2) 644 | lines(age.grid,pred$fit+2*pred$se,lty="dashed") 645 | lines(age.grid,pred$fit-2*pred$se,lty="dashed") 646 | dim(bs(age,knots=c(25,40,60))) 647 | dim(bs(age,df=6)) 648 | attr(bs(age,df=6),"knots") 649 | fit2=lm(wage~ns(age,df=4),data=Wage) 650 | pred2=predict(fit2,newdata=list(age=age.grid),se=T) 651 | lines(age.grid, pred2$fit,col="red",lwd=2) 652 | plot(age,wage,xlim=agelims,cex=.5,col="darkgrey") 653 | title("Smoothing Spline") 654 | fit=smooth.spline(age,wage,df=16) 655 | fit2=smooth.spline(age,wage,cv=TRUE) 656 | fit2$df 657 | lines(fit,col="red",lwd=2) 658 | lines(fit2,col="blue",lwd=2) 659 | legend("topright",legend=c("16 DF","6.8 DF"),col=c("red","blue"),lty=1,lwd=2,cex=.8) 660 | plot(age,wage,xlim=agelims,cex=.5,col="darkgrey") 661 | title("Local Regression") 662 | fit=loess(wage~age,span=.2,data=Wage) 663 | fit2=loess(wage~age,span=.5,data=Wage) 664 | lines(age.grid,predict(fit,data.frame(age=age.grid)),col="red",lwd=2) 665 | lines(age.grid,predict(fit2,data.frame(age=age.grid)),col="blue",lwd=2) 666 | legend("topright",legend=c("Span=0.2","Span=0.5"),col=c("red","blue"),lty=1,lwd=2,cex=.8) 667 | 668 | # GAMs 669 | 670 | gam1=lm(wage~ns(year,4)+ns(age,5)+education,data=Wage) 671 | library(gam) 672 | gam.m3=gam(wage~s(year,4)+s(age,5)+education,data=Wage) 673 | par(mfrow=c(1,3)) 674 | plot(gam.m3, se=TRUE,col="blue") 675 | plot.gam(gam1, se=TRUE, col="red") 676 | gam.m1=gam(wage~s(age,5)+education,data=Wage) 677 | gam.m2=gam(wage~year+s(age,5)+education,data=Wage) 678 | anova(gam.m1,gam.m2,gam.m3,test="F") 679 | summary(gam.m3) 680 | preds=predict(gam.m2,newdata=Wage) 681 | gam.lo=gam(wage~s(year,df=4)+lo(age,span=0.7)+education,data=Wage) 682 | plot.gam(gam.lo, se=TRUE, col="green") 683 | gam.lo.i=gam(wage~lo(year,age,span=0.5)+education,data=Wage) 684 | library(akima) 685 | plot(gam.lo.i) 686 | gam.lr=gam(I(wage>250)~year+s(age,df=5)+education,family=binomial,data=Wage) 687 | par(mfrow=c(1,3)) 688 | plot(gam.lr,se=T,col="green") 689 | table(education,I(wage>250)) 690 | gam.lr.s=gam(I(wage>250)~year+s(age,df=5)+education,family=binomial,data=Wage,subset=(education!="1. < HS Grad")) 691 | plot(gam.lr.s,se=T,col="green") 692 | 693 | 694 | 695 | # Chapter 8 Lab: Decision Trees 696 | 697 | # Fitting Classification Trees 698 | 699 | library(tree) 700 | library(ISLR) 701 | attach(Carseats) 702 | High=ifelse(Sales<=8,"No","Yes") 703 | Carseats=data.frame(Carseats,High) 704 | tree.carseats=tree(High~.-Sales,Carseats) 705 | summary(tree.carseats) 706 | plot(tree.carseats) 707 | text(tree.carseats,pretty=0) 708 | tree.carseats 709 | set.seed(2) 710 | train=sample(1:nrow(Carseats), 200) 711 | Carseats.test=Carseats[-train,] 712 | High.test=High[-train] 713 | tree.carseats=tree(High~.-Sales,Carseats,subset=train) 714 | tree.pred=predict(tree.carseats,Carseats.test,type="class") 715 | table(tree.pred,High.test) 716 | (86+57)/200 717 | set.seed(3) 718 | cv.carseats=cv.tree(tree.carseats,FUN=prune.misclass) 719 | names(cv.carseats) 720 | cv.carseats 721 | par(mfrow=c(1,2)) 722 | plot(cv.carseats$size,cv.carseats$dev,type="b") 723 | plot(cv.carseats$k,cv.carseats$dev,type="b") 724 | prune.carseats=prune.misclass(tree.carseats,best=9) 725 | plot(prune.carseats) 726 | text(prune.carseats,pretty=0) 727 | tree.pred=predict(prune.carseats,Carseats.test,type="class") 728 | table(tree.pred,High.test) 729 | (94+60)/200 730 | prune.carseats=prune.misclass(tree.carseats,best=15) 731 | plot(prune.carseats) 732 | text(prune.carseats,pretty=0) 733 | tree.pred=predict(prune.carseats,Carseats.test,type="class") 734 | table(tree.pred,High.test) 735 | (86+62)/200 736 | 737 | # Fitting Regression Trees 738 | 739 | library(MASS) 740 | set.seed(1) 741 | train = sample(1:nrow(Boston), nrow(Boston)/2) 742 | tree.boston=tree(medv~.,Boston,subset=train) 743 | summary(tree.boston) 744 | plot(tree.boston) 745 | text(tree.boston,pretty=0) 746 | cv.boston=cv.tree(tree.boston) 747 | plot(cv.boston$size,cv.boston$dev,type='b') 748 | prune.boston=prune.tree(tree.boston,best=5) 749 | plot(prune.boston) 750 | text(prune.boston,pretty=0) 751 | yhat=predict(tree.boston,newdata=Boston[-train,]) 752 | boston.test=Boston[-train,"medv"] 753 | plot(yhat,boston.test) 754 | abline(0,1) 755 | mean((yhat-boston.test)^2) 756 | 757 | # Bagging and Random Forests 758 | 759 | library(randomForest) 760 | set.seed(1) 761 | bag.boston=randomForest(medv~.,data=Boston,subset=train,mtry=13,importance=TRUE) 762 | bag.boston 763 | yhat.bag = predict(bag.boston,newdata=Boston[-train,]) 764 | plot(yhat.bag, boston.test) 765 | abline(0,1) 766 | mean((yhat.bag-boston.test)^2) 767 | bag.boston=randomForest(medv~.,data=Boston,subset=train,mtry=13,ntree=25) 768 | yhat.bag = predict(bag.boston,newdata=Boston[-train,]) 769 | mean((yhat.bag-boston.test)^2) 770 | set.seed(1) 771 | rf.boston=randomForest(medv~.,data=Boston,subset=train,mtry=6,importance=TRUE) 772 | yhat.rf = predict(rf.boston,newdata=Boston[-train,]) 773 | mean((yhat.rf-boston.test)^2) 774 | importance(rf.boston) 775 | varImpPlot(rf.boston) 776 | 777 | # Boosting 778 | 779 | library(gbm) 780 | set.seed(1) 781 | boost.boston=gbm(medv~.,data=Boston[train,],distribution="gaussian",n.trees=5000,interaction.depth=4) 782 | summary(boost.boston) 783 | par(mfrow=c(1,2)) 784 | plot(boost.boston,i="rm") 785 | plot(boost.boston,i="lstat") 786 | yhat.boost=predict(boost.boston,newdata=Boston[-train,],n.trees=5000) 787 | mean((yhat.boost-boston.test)^2) 788 | boost.boston=gbm(medv~.,data=Boston[train,],distribution="gaussian",n.trees=5000,interaction.depth=4,shrinkage=0.2,verbose=F) 789 | yhat.boost=predict(boost.boston,newdata=Boston[-train,],n.trees=5000) 790 | mean((yhat.boost-boston.test)^2) 791 | 792 | 793 | 794 | # Chapter 9 Lab: Support Vector Machines 795 | 796 | # Support Vector Classifier 797 | 798 | set.seed(1) 799 | x=matrix(rnorm(20*2), ncol=2) 800 | y=c(rep(-1,10), rep(1,10)) 801 | x[y==1,]=x[y==1,] + 1 802 | plot(x, col=(3-y)) 803 | dat=data.frame(x=x, y=as.factor(y)) 804 | library(e1071) 805 | svmfit=svm(y~., data=dat, kernel="linear", cost=10,scale=FALSE) 806 | plot(svmfit, dat) 807 | svmfit$index 808 | summary(svmfit) 809 | svmfit=svm(y~., data=dat, kernel="linear", cost=0.1,scale=FALSE) 810 | plot(svmfit, dat) 811 | svmfit$index 812 | set.seed(1) 813 | tune.out=tune(svm,y~.,data=dat,kernel="linear",ranges=list(cost=c(0.001, 0.01, 0.1, 1,5,10,100))) 814 | summary(tune.out) 815 | bestmod=tune.out$best.model 816 | summary(bestmod) 817 | xtest=matrix(rnorm(20*2), ncol=2) 818 | ytest=sample(c(-1,1), 20, rep=TRUE) 819 | xtest[ytest==1,]=xtest[ytest==1,] + 1 820 | testdat=data.frame(x=xtest, y=as.factor(ytest)) 821 | ypred=predict(bestmod,testdat) 822 | table(predict=ypred, truth=testdat$y) 823 | svmfit=svm(y~., data=dat, kernel="linear", cost=.01,scale=FALSE) 824 | ypred=predict(svmfit,testdat) 825 | table(predict=ypred, truth=testdat$y) 826 | x[y==1,]=x[y==1,]+0.5 827 | plot(x, col=(y+5)/2, pch=19) 828 | dat=data.frame(x=x,y=as.factor(y)) 829 | svmfit=svm(y~., data=dat, kernel="linear", cost=1e5) 830 | summary(svmfit) 831 | plot(svmfit, dat) 832 | svmfit=svm(y~., data=dat, kernel="linear", cost=1) 833 | summary(svmfit) 834 | plot(svmfit,dat) 835 | 836 | # Support Vector Machine 837 | 838 | set.seed(1) 839 | x=matrix(rnorm(200*2), ncol=2) 840 | x[1:100,]=x[1:100,]+2 841 | x[101:150,]=x[101:150,]-2 842 | y=c(rep(1,150),rep(2,50)) 843 | dat=data.frame(x=x,y=as.factor(y)) 844 | plot(x, col=y) 845 | train=sample(200,100) 846 | svmfit=svm(y~., data=dat[train,], kernel="radial", gamma=1, cost=1) 847 | plot(svmfit, dat[train,]) 848 | summary(svmfit) 849 | svmfit=svm(y~., data=dat[train,], kernel="radial",gamma=1,cost=1e5) 850 | plot(svmfit,dat[train,]) 851 | set.seed(1) 852 | tune.out=tune(svm, y~., data=dat[train,], kernel="radial", ranges=list(cost=c(0.1,1,10,100,1000),gamma=c(0.5,1,2,3,4))) 853 | summary(tune.out) 854 | table(true=dat[-train,"y"], pred=predict(tune.out$best.model,newx=dat[-train,])) 855 | 856 | # ROC Curves 857 | 858 | library(ROCR) 859 | rocplot=function(pred, truth, ...){ 860 | predob = prediction(pred, truth) 861 | perf = performance(predob, "tpr", "fpr") 862 | plot(perf,...)} 863 | svmfit.opt=svm(y~., data=dat[train,], kernel="radial",gamma=2, cost=1,decision.values=T) 864 | fitted=attributes(predict(svmfit.opt,dat[train,],decision.values=TRUE))$decision.values 865 | par(mfrow=c(1,2)) 866 | rocplot(fitted,dat[train,"y"],main="Training Data") 867 | svmfit.flex=svm(y~., data=dat[train,], kernel="radial",gamma=50, cost=1, decision.values=T) 868 | fitted=attributes(predict(svmfit.flex,dat[train,],decision.values=T))$decision.values 869 | rocplot(fitted,dat[train,"y"],add=T,col="red") 870 | fitted=attributes(predict(svmfit.opt,dat[-train,],decision.values=T))$decision.values 871 | rocplot(fitted,dat[-train,"y"],main="Test Data") 872 | fitted=attributes(predict(svmfit.flex,dat[-train,],decision.values=T))$decision.values 873 | rocplot(fitted,dat[-train,"y"],add=T,col="red") 874 | 875 | # SVM with Multiple Classes 876 | 877 | set.seed(1) 878 | x=rbind(x, matrix(rnorm(50*2), ncol=2)) 879 | y=c(y, rep(0,50)) 880 | x[y==0,2]=x[y==0,2]+2 881 | dat=data.frame(x=x, y=as.factor(y)) 882 | par(mfrow=c(1,1)) 883 | plot(x,col=(y+1)) 884 | svmfit=svm(y~., data=dat, kernel="radial", cost=10, gamma=1) 885 | plot(svmfit, dat) 886 | 887 | # Application to Gene Expression Data 888 | 889 | library(ISLR) 890 | names(Khan) 891 | dim(Khan$xtrain) 892 | dim(Khan$xtest) 893 | length(Khan$ytrain) 894 | length(Khan$ytest) 895 | table(Khan$ytrain) 896 | table(Khan$ytest) 897 | dat=data.frame(x=Khan$xtrain, y=as.factor(Khan$ytrain)) 898 | out=svm(y~., data=dat, kernel="linear",cost=10) 899 | summary(out) 900 | table(out$fitted, dat$y) 901 | dat.te=data.frame(x=Khan$xtest, y=as.factor(Khan$ytest)) 902 | pred.te=predict(out, newdata=dat.te) 903 | table(pred.te, dat.te$y) 904 | 905 | 906 | 907 | # Chapter 10 Lab 1: Principal Components Analysis 908 | 909 | states=row.names(USArrests) 910 | states 911 | names(USArrests) 912 | apply(USArrests, 2, mean) 913 | apply(USArrests, 2, var) 914 | pr.out=prcomp(USArrests, scale=TRUE) 915 | names(pr.out) 916 | pr.out$center 917 | pr.out$scale 918 | pr.out$rotation 919 | dim(pr.out$x) 920 | biplot(pr.out, scale=0) 921 | pr.out$rotation=-pr.out$rotation 922 | pr.out$x=-pr.out$x 923 | biplot(pr.out, scale=0) 924 | pr.out$sdev 925 | pr.var=pr.out$sdev^2 926 | pr.var 927 | pve=pr.var/sum(pr.var) 928 | pve 929 | plot(pve, xlab="Principal Component", ylab="Proportion of Variance Explained", ylim=c(0,1),type='b') 930 | plot(cumsum(pve), xlab="Principal Component", ylab="Cumulative Proportion of Variance Explained", ylim=c(0,1),type='b') 931 | a=c(1,2,8,-3) 932 | cumsum(a) 933 | 934 | 935 | # Chapter 10 Lab 2: Clustering 936 | 937 | # K-Means Clustering 938 | 939 | set.seed(2) 940 | x=matrix(rnorm(50*2), ncol=2) 941 | x[1:25,1]=x[1:25,1]+3 942 | x[1:25,2]=x[1:25,2]-4 943 | km.out=kmeans(x,2,nstart=20) 944 | km.out$cluster 945 | plot(x, col=(km.out$cluster+1), main="K-Means Clustering Results with K=2", xlab="", ylab="", pch=20, cex=2) 946 | set.seed(4) 947 | km.out=kmeans(x,3,nstart=20) 948 | km.out 949 | plot(x, col=(km.out$cluster+1), main="K-Means Clustering Results with K=3", xlab="", ylab="", pch=20, cex=2) 950 | set.seed(3) 951 | km.out=kmeans(x,3,nstart=1) 952 | km.out$tot.withinss 953 | km.out=kmeans(x,3,nstart=20) 954 | km.out$tot.withinss 955 | 956 | # Hierarchical Clustering 957 | 958 | hc.complete=hclust(dist(x), method="complete") 959 | hc.average=hclust(dist(x), method="average") 960 | hc.single=hclust(dist(x), method="single") 961 | par(mfrow=c(1,3)) 962 | plot(hc.complete,main="Complete Linkage", xlab="", sub="", cex=.9) 963 | plot(hc.average, main="Average Linkage", xlab="", sub="", cex=.9) 964 | plot(hc.single, main="Single Linkage", xlab="", sub="", cex=.9) 965 | cutree(hc.complete, 2) 966 | cutree(hc.average, 2) 967 | cutree(hc.single, 2) 968 | cutree(hc.single, 4) 969 | xsc=scale(x) 970 | plot(hclust(dist(xsc), method="complete"), main="Hierarchical Clustering with Scaled Features") 971 | x=matrix(rnorm(30*3), ncol=3) 972 | dd=as.dist(1-cor(t(x))) 973 | plot(hclust(dd, method="complete"), main="Complete Linkage with Correlation-Based Distance", xlab="", sub="") 974 | 975 | 976 | # Chapter 10 Lab 3: NCI60 Data Example 977 | 978 | # The NCI60 data 979 | 980 | library(ISLR) 981 | nci.labs=NCI60$labs 982 | nci.data=NCI60$data 983 | dim(nci.data) 984 | nci.labs[1:4] 985 | table(nci.labs) 986 | 987 | # PCA on the NCI60 Data 988 | 989 | pr.out=prcomp(nci.data, scale=TRUE) 990 | Cols=function(vec){ 991 | cols=rainbow(length(unique(vec))) 992 | return(cols[as.numeric(as.factor(vec))]) 993 | } 994 | par(mfrow=c(1,2)) 995 | plot(pr.out$x[,1:2], col=Cols(nci.labs), pch=19,xlab="Z1",ylab="Z2") 996 | plot(pr.out$x[,c(1,3)], col=Cols(nci.labs), pch=19,xlab="Z1",ylab="Z3") 997 | summary(pr.out) 998 | plot(pr.out) 999 | pve=100*pr.out$sdev^2/sum(pr.out$sdev^2) 1000 | par(mfrow=c(1,2)) 1001 | plot(pve, type="o", ylab="PVE", xlab="Principal Component", col="blue") 1002 | plot(cumsum(pve), type="o", ylab="Cumulative PVE", xlab="Principal Component", col="brown3") 1003 | 1004 | # Clustering the Observations of the NCI60 Data 1005 | 1006 | sd.data=scale(nci.data) 1007 | par(mfrow=c(1,3)) 1008 | data.dist=dist(sd.data) 1009 | plot(hclust(data.dist), labels=nci.labs, main="Complete Linkage", xlab="", sub="",ylab="") 1010 | plot(hclust(data.dist, method="average"), labels=nci.labs, main="Average Linkage", xlab="", sub="",ylab="") 1011 | plot(hclust(data.dist, method="single"), labels=nci.labs, main="Single Linkage", xlab="", sub="",ylab="") 1012 | hc.out=hclust(dist(sd.data)) 1013 | hc.clusters=cutree(hc.out,4) 1014 | table(hc.clusters,nci.labs) 1015 | par(mfrow=c(1,1)) 1016 | plot(hc.out, labels=nci.labs) 1017 | abline(h=139, col="red") 1018 | hc.out 1019 | set.seed(2) 1020 | km.out=kmeans(sd.data, 4, nstart=20) 1021 | km.clusters=km.out$cluster 1022 | table(km.clusters,hc.clusters) 1023 | hc.out=hclust(dist(pr.out$x[,1:5])) 1024 | plot(hc.out, labels=nci.labs, main="Hier. Clust. on First Five Score Vectors") 1025 | table(cutree(hc.out,4), nci.labs) 1026 | 1027 | -------------------------------------------------------------------------------- /Auto.csv: -------------------------------------------------------------------------------- 1 | mpg,cylinders,displacement,horsepower,weight,acceleration,year,origin,name 2 | 18,8,307,130,3504,12,70,1,chevrolet chevelle malibu 3 | 15,8,350,165,3693,11.5,70,1,buick skylark 320 4 | 18,8,318,150,3436,11,70,1,plymouth satellite 5 | 16,8,304,150,3433,12,70,1,amc rebel sst 6 | 17,8,302,140,3449,10.5,70,1,ford torino 7 | 15,8,429,198,4341,10,70,1,ford galaxie 500 8 | 14,8,454,220,4354,9,70,1,chevrolet impala 9 | 14,8,440,215,4312,8.5,70,1,plymouth fury iii 10 | 14,8,455,225,4425,10,70,1,pontiac catalina 11 | 15,8,390,190,3850,8.5,70,1,amc ambassador dpl 12 | 15,8,383,170,3563,10,70,1,dodge challenger se 13 | 14,8,340,160,3609,8,70,1,plymouth 'cuda 340 14 | 15,8,400,150,3761,9.5,70,1,chevrolet monte carlo 15 | 14,8,455,225,3086,10,70,1,buick estate wagon (sw) 16 | 24,4,113,95,2372,15,70,3,toyota corona mark ii 17 | 22,6,198,95,2833,15.5,70,1,plymouth duster 18 | 18,6,199,97,2774,15.5,70,1,amc hornet 19 | 21,6,200,85,2587,16,70,1,ford maverick 20 | 27,4,97,88,2130,14.5,70,3,datsun pl510 21 | 26,4,97,46,1835,20.5,70,2,volkswagen 1131 deluxe sedan 22 | 25,4,110,87,2672,17.5,70,2,peugeot 504 23 | 24,4,107,90,2430,14.5,70,2,audi 100 ls 24 | 25,4,104,95,2375,17.5,70,2,saab 99e 25 | 26,4,121,113,2234,12.5,70,2,bmw 2002 26 | 21,6,199,90,2648,15,70,1,amc gremlin 27 | 10,8,360,215,4615,14,70,1,ford f250 28 | 10,8,307,200,4376,15,70,1,chevy c20 29 | 11,8,318,210,4382,13.5,70,1,dodge d200 30 | 9,8,304,193,4732,18.5,70,1,hi 1200d 31 | 27,4,97,88,2130,14.5,71,3,datsun pl510 32 | 28,4,140,90,2264,15.5,71,1,chevrolet vega 2300 33 | 25,4,113,95,2228,14,71,3,toyota corona 34 | 25,4,98,?,2046,19,71,1,ford pinto 35 | 19,6,232,100,2634,13,71,1,amc gremlin 36 | 16,6,225,105,3439,15.5,71,1,plymouth satellite custom 37 | 17,6,250,100,3329,15.5,71,1,chevrolet chevelle malibu 38 | 19,6,250,88,3302,15.5,71,1,ford torino 500 39 | 18,6,232,100,3288,15.5,71,1,amc matador 40 | 14,8,350,165,4209,12,71,1,chevrolet impala 41 | 14,8,400,175,4464,11.5,71,1,pontiac catalina brougham 42 | 14,8,351,153,4154,13.5,71,1,ford galaxie 500 43 | 14,8,318,150,4096,13,71,1,plymouth fury iii 44 | 12,8,383,180,4955,11.5,71,1,dodge monaco (sw) 45 | 13,8,400,170,4746,12,71,1,ford country squire (sw) 46 | 13,8,400,175,5140,12,71,1,pontiac safari (sw) 47 | 18,6,258,110,2962,13.5,71,1,amc hornet sportabout (sw) 48 | 22,4,140,72,2408,19,71,1,chevrolet vega (sw) 49 | 19,6,250,100,3282,15,71,1,pontiac firebird 50 | 18,6,250,88,3139,14.5,71,1,ford mustang 51 | 23,4,122,86,2220,14,71,1,mercury capri 2000 52 | 28,4,116,90,2123,14,71,2,opel 1900 53 | 30,4,79,70,2074,19.5,71,2,peugeot 304 54 | 30,4,88,76,2065,14.5,71,2,fiat 124b 55 | 31,4,71,65,1773,19,71,3,toyota corolla 1200 56 | 35,4,72,69,1613,18,71,3,datsun 1200 57 | 27,4,97,60,1834,19,71,2,volkswagen model 111 58 | 26,4,91,70,1955,20.5,71,1,plymouth cricket 59 | 24,4,113,95,2278,15.5,72,3,toyota corona hardtop 60 | 25,4,97.5,80,2126,17,72,1,dodge colt hardtop 61 | 23,4,97,54,2254,23.5,72,2,volkswagen type 3 62 | 20,4,140,90,2408,19.5,72,1,chevrolet vega 63 | 21,4,122,86,2226,16.5,72,1,ford pinto runabout 64 | 13,8,350,165,4274,12,72,1,chevrolet impala 65 | 14,8,400,175,4385,12,72,1,pontiac catalina 66 | 15,8,318,150,4135,13.5,72,1,plymouth fury iii 67 | 14,8,351,153,4129,13,72,1,ford galaxie 500 68 | 17,8,304,150,3672,11.5,72,1,amc ambassador sst 69 | 11,8,429,208,4633,11,72,1,mercury marquis 70 | 13,8,350,155,4502,13.5,72,1,buick lesabre custom 71 | 12,8,350,160,4456,13.5,72,1,oldsmobile delta 88 royale 72 | 13,8,400,190,4422,12.5,72,1,chrysler newport royal 73 | 19,3,70,97,2330,13.5,72,3,mazda rx2 coupe 74 | 15,8,304,150,3892,12.5,72,1,amc matador (sw) 75 | 13,8,307,130,4098,14,72,1,chevrolet chevelle concours (sw) 76 | 13,8,302,140,4294,16,72,1,ford gran torino (sw) 77 | 14,8,318,150,4077,14,72,1,plymouth satellite custom (sw) 78 | 18,4,121,112,2933,14.5,72,2,volvo 145e (sw) 79 | 22,4,121,76,2511,18,72,2,volkswagen 411 (sw) 80 | 21,4,120,87,2979,19.5,72,2,peugeot 504 (sw) 81 | 26,4,96,69,2189,18,72,2,renault 12 (sw) 82 | 22,4,122,86,2395,16,72,1,ford pinto (sw) 83 | 28,4,97,92,2288,17,72,3,datsun 510 (sw) 84 | 23,4,120,97,2506,14.5,72,3,toyouta corona mark ii (sw) 85 | 28,4,98,80,2164,15,72,1,dodge colt (sw) 86 | 27,4,97,88,2100,16.5,72,3,toyota corolla 1600 (sw) 87 | 13,8,350,175,4100,13,73,1,buick century 350 88 | 14,8,304,150,3672,11.5,73,1,amc matador 89 | 13,8,350,145,3988,13,73,1,chevrolet malibu 90 | 14,8,302,137,4042,14.5,73,1,ford gran torino 91 | 15,8,318,150,3777,12.5,73,1,dodge coronet custom 92 | 12,8,429,198,4952,11.5,73,1,mercury marquis brougham 93 | 13,8,400,150,4464,12,73,1,chevrolet caprice classic 94 | 13,8,351,158,4363,13,73,1,ford ltd 95 | 14,8,318,150,4237,14.5,73,1,plymouth fury gran sedan 96 | 13,8,440,215,4735,11,73,1,chrysler new yorker brougham 97 | 12,8,455,225,4951,11,73,1,buick electra 225 custom 98 | 13,8,360,175,3821,11,73,1,amc ambassador brougham 99 | 18,6,225,105,3121,16.5,73,1,plymouth valiant 100 | 16,6,250,100,3278,18,73,1,chevrolet nova custom 101 | 18,6,232,100,2945,16,73,1,amc hornet 102 | 18,6,250,88,3021,16.5,73,1,ford maverick 103 | 23,6,198,95,2904,16,73,1,plymouth duster 104 | 26,4,97,46,1950,21,73,2,volkswagen super beetle 105 | 11,8,400,150,4997,14,73,1,chevrolet impala 106 | 12,8,400,167,4906,12.5,73,1,ford country 107 | 13,8,360,170,4654,13,73,1,plymouth custom suburb 108 | 12,8,350,180,4499,12.5,73,1,oldsmobile vista cruiser 109 | 18,6,232,100,2789,15,73,1,amc gremlin 110 | 20,4,97,88,2279,19,73,3,toyota carina 111 | 21,4,140,72,2401,19.5,73,1,chevrolet vega 112 | 22,4,108,94,2379,16.5,73,3,datsun 610 113 | 18,3,70,90,2124,13.5,73,3,maxda rx3 114 | 19,4,122,85,2310,18.5,73,1,ford pinto 115 | 21,6,155,107,2472,14,73,1,mercury capri v6 116 | 26,4,98,90,2265,15.5,73,2,fiat 124 sport coupe 117 | 15,8,350,145,4082,13,73,1,chevrolet monte carlo s 118 | 16,8,400,230,4278,9.5,73,1,pontiac grand prix 119 | 29,4,68,49,1867,19.5,73,2,fiat 128 120 | 24,4,116,75,2158,15.5,73,2,opel manta 121 | 20,4,114,91,2582,14,73,2,audi 100ls 122 | 19,4,121,112,2868,15.5,73,2,volvo 144ea 123 | 15,8,318,150,3399,11,73,1,dodge dart custom 124 | 24,4,121,110,2660,14,73,2,saab 99le 125 | 20,6,156,122,2807,13.5,73,3,toyota mark ii 126 | 11,8,350,180,3664,11,73,1,oldsmobile omega 127 | 20,6,198,95,3102,16.5,74,1,plymouth duster 128 | 21,6,200,?,2875,17,74,1,ford maverick 129 | 19,6,232,100,2901,16,74,1,amc hornet 130 | 15,6,250,100,3336,17,74,1,chevrolet nova 131 | 31,4,79,67,1950,19,74,3,datsun b210 132 | 26,4,122,80,2451,16.5,74,1,ford pinto 133 | 32,4,71,65,1836,21,74,3,toyota corolla 1200 134 | 25,4,140,75,2542,17,74,1,chevrolet vega 135 | 16,6,250,100,3781,17,74,1,chevrolet chevelle malibu classic 136 | 16,6,258,110,3632,18,74,1,amc matador 137 | 18,6,225,105,3613,16.5,74,1,plymouth satellite sebring 138 | 16,8,302,140,4141,14,74,1,ford gran torino 139 | 13,8,350,150,4699,14.5,74,1,buick century luxus (sw) 140 | 14,8,318,150,4457,13.5,74,1,dodge coronet custom (sw) 141 | 14,8,302,140,4638,16,74,1,ford gran torino (sw) 142 | 14,8,304,150,4257,15.5,74,1,amc matador (sw) 143 | 29,4,98,83,2219,16.5,74,2,audi fox 144 | 26,4,79,67,1963,15.5,74,2,volkswagen dasher 145 | 26,4,97,78,2300,14.5,74,2,opel manta 146 | 31,4,76,52,1649,16.5,74,3,toyota corona 147 | 32,4,83,61,2003,19,74,3,datsun 710 148 | 28,4,90,75,2125,14.5,74,1,dodge colt 149 | 24,4,90,75,2108,15.5,74,2,fiat 128 150 | 26,4,116,75,2246,14,74,2,fiat 124 tc 151 | 24,4,120,97,2489,15,74,3,honda civic 152 | 26,4,108,93,2391,15.5,74,3,subaru 153 | 31,4,79,67,2000,16,74,2,fiat x1.9 154 | 19,6,225,95,3264,16,75,1,plymouth valiant custom 155 | 18,6,250,105,3459,16,75,1,chevrolet nova 156 | 15,6,250,72,3432,21,75,1,mercury monarch 157 | 15,6,250,72,3158,19.5,75,1,ford maverick 158 | 16,8,400,170,4668,11.5,75,1,pontiac catalina 159 | 15,8,350,145,4440,14,75,1,chevrolet bel air 160 | 16,8,318,150,4498,14.5,75,1,plymouth grand fury 161 | 14,8,351,148,4657,13.5,75,1,ford ltd 162 | 17,6,231,110,3907,21,75,1,buick century 163 | 16,6,250,105,3897,18.5,75,1,chevroelt chevelle malibu 164 | 15,6,258,110,3730,19,75,1,amc matador 165 | 18,6,225,95,3785,19,75,1,plymouth fury 166 | 21,6,231,110,3039,15,75,1,buick skyhawk 167 | 20,8,262,110,3221,13.5,75,1,chevrolet monza 2+2 168 | 13,8,302,129,3169,12,75,1,ford mustang ii 169 | 29,4,97,75,2171,16,75,3,toyota corolla 170 | 23,4,140,83,2639,17,75,1,ford pinto 171 | 20,6,232,100,2914,16,75,1,amc gremlin 172 | 23,4,140,78,2592,18.5,75,1,pontiac astro 173 | 24,4,134,96,2702,13.5,75,3,toyota corona 174 | 25,4,90,71,2223,16.5,75,2,volkswagen dasher 175 | 24,4,119,97,2545,17,75,3,datsun 710 176 | 18,6,171,97,2984,14.5,75,1,ford pinto 177 | 29,4,90,70,1937,14,75,2,volkswagen rabbit 178 | 19,6,232,90,3211,17,75,1,amc pacer 179 | 23,4,115,95,2694,15,75,2,audi 100ls 180 | 23,4,120,88,2957,17,75,2,peugeot 504 181 | 22,4,121,98,2945,14.5,75,2,volvo 244dl 182 | 25,4,121,115,2671,13.5,75,2,saab 99le 183 | 33,4,91,53,1795,17.5,75,3,honda civic cvcc 184 | 28,4,107,86,2464,15.5,76,2,fiat 131 185 | 25,4,116,81,2220,16.9,76,2,opel 1900 186 | 25,4,140,92,2572,14.9,76,1,capri ii 187 | 26,4,98,79,2255,17.7,76,1,dodge colt 188 | 27,4,101,83,2202,15.3,76,2,renault 12tl 189 | 17.5,8,305,140,4215,13,76,1,chevrolet chevelle malibu classic 190 | 16,8,318,150,4190,13,76,1,dodge coronet brougham 191 | 15.5,8,304,120,3962,13.9,76,1,amc matador 192 | 14.5,8,351,152,4215,12.8,76,1,ford gran torino 193 | 22,6,225,100,3233,15.4,76,1,plymouth valiant 194 | 22,6,250,105,3353,14.5,76,1,chevrolet nova 195 | 24,6,200,81,3012,17.6,76,1,ford maverick 196 | 22.5,6,232,90,3085,17.6,76,1,amc hornet 197 | 29,4,85,52,2035,22.2,76,1,chevrolet chevette 198 | 24.5,4,98,60,2164,22.1,76,1,chevrolet woody 199 | 29,4,90,70,1937,14.2,76,2,vw rabbit 200 | 33,4,91,53,1795,17.4,76,3,honda civic 201 | 20,6,225,100,3651,17.7,76,1,dodge aspen se 202 | 18,6,250,78,3574,21,76,1,ford granada ghia 203 | 18.5,6,250,110,3645,16.2,76,1,pontiac ventura sj 204 | 17.5,6,258,95,3193,17.8,76,1,amc pacer d/l 205 | 29.5,4,97,71,1825,12.2,76,2,volkswagen rabbit 206 | 32,4,85,70,1990,17,76,3,datsun b-210 207 | 28,4,97,75,2155,16.4,76,3,toyota corolla 208 | 26.5,4,140,72,2565,13.6,76,1,ford pinto 209 | 20,4,130,102,3150,15.7,76,2,volvo 245 210 | 13,8,318,150,3940,13.2,76,1,plymouth volare premier v8 211 | 19,4,120,88,3270,21.9,76,2,peugeot 504 212 | 19,6,156,108,2930,15.5,76,3,toyota mark ii 213 | 16.5,6,168,120,3820,16.7,76,2,mercedes-benz 280s 214 | 16.5,8,350,180,4380,12.1,76,1,cadillac seville 215 | 13,8,350,145,4055,12,76,1,chevy c10 216 | 13,8,302,130,3870,15,76,1,ford f108 217 | 13,8,318,150,3755,14,76,1,dodge d100 218 | 31.5,4,98,68,2045,18.5,77,3,honda accord cvcc 219 | 30,4,111,80,2155,14.8,77,1,buick opel isuzu deluxe 220 | 36,4,79,58,1825,18.6,77,2,renault 5 gtl 221 | 25.5,4,122,96,2300,15.5,77,1,plymouth arrow gs 222 | 33.5,4,85,70,1945,16.8,77,3,datsun f-10 hatchback 223 | 17.5,8,305,145,3880,12.5,77,1,chevrolet caprice classic 224 | 17,8,260,110,4060,19,77,1,oldsmobile cutlass supreme 225 | 15.5,8,318,145,4140,13.7,77,1,dodge monaco brougham 226 | 15,8,302,130,4295,14.9,77,1,mercury cougar brougham 227 | 17.5,6,250,110,3520,16.4,77,1,chevrolet concours 228 | 20.5,6,231,105,3425,16.9,77,1,buick skylark 229 | 19,6,225,100,3630,17.7,77,1,plymouth volare custom 230 | 18.5,6,250,98,3525,19,77,1,ford granada 231 | 16,8,400,180,4220,11.1,77,1,pontiac grand prix lj 232 | 15.5,8,350,170,4165,11.4,77,1,chevrolet monte carlo landau 233 | 15.5,8,400,190,4325,12.2,77,1,chrysler cordoba 234 | 16,8,351,149,4335,14.5,77,1,ford thunderbird 235 | 29,4,97,78,1940,14.5,77,2,volkswagen rabbit custom 236 | 24.5,4,151,88,2740,16,77,1,pontiac sunbird coupe 237 | 26,4,97,75,2265,18.2,77,3,toyota corolla liftback 238 | 25.5,4,140,89,2755,15.8,77,1,ford mustang ii 2+2 239 | 30.5,4,98,63,2051,17,77,1,chevrolet chevette 240 | 33.5,4,98,83,2075,15.9,77,1,dodge colt m/m 241 | 30,4,97,67,1985,16.4,77,3,subaru dl 242 | 30.5,4,97,78,2190,14.1,77,2,volkswagen dasher 243 | 22,6,146,97,2815,14.5,77,3,datsun 810 244 | 21.5,4,121,110,2600,12.8,77,2,bmw 320i 245 | 21.5,3,80,110,2720,13.5,77,3,mazda rx-4 246 | 43.1,4,90,48,1985,21.5,78,2,volkswagen rabbit custom diesel 247 | 36.1,4,98,66,1800,14.4,78,1,ford fiesta 248 | 32.8,4,78,52,1985,19.4,78,3,mazda glc deluxe 249 | 39.4,4,85,70,2070,18.6,78,3,datsun b210 gx 250 | 36.1,4,91,60,1800,16.4,78,3,honda civic cvcc 251 | 19.9,8,260,110,3365,15.5,78,1,oldsmobile cutlass salon brougham 252 | 19.4,8,318,140,3735,13.2,78,1,dodge diplomat 253 | 20.2,8,302,139,3570,12.8,78,1,mercury monarch ghia 254 | 19.2,6,231,105,3535,19.2,78,1,pontiac phoenix lj 255 | 20.5,6,200,95,3155,18.2,78,1,chevrolet malibu 256 | 20.2,6,200,85,2965,15.8,78,1,ford fairmont (auto) 257 | 25.1,4,140,88,2720,15.4,78,1,ford fairmont (man) 258 | 20.5,6,225,100,3430,17.2,78,1,plymouth volare 259 | 19.4,6,232,90,3210,17.2,78,1,amc concord 260 | 20.6,6,231,105,3380,15.8,78,1,buick century special 261 | 20.8,6,200,85,3070,16.7,78,1,mercury zephyr 262 | 18.6,6,225,110,3620,18.7,78,1,dodge aspen 263 | 18.1,6,258,120,3410,15.1,78,1,amc concord d/l 264 | 19.2,8,305,145,3425,13.2,78,1,chevrolet monte carlo landau 265 | 17.7,6,231,165,3445,13.4,78,1,buick regal sport coupe (turbo) 266 | 18.1,8,302,139,3205,11.2,78,1,ford futura 267 | 17.5,8,318,140,4080,13.7,78,1,dodge magnum xe 268 | 30,4,98,68,2155,16.5,78,1,chevrolet chevette 269 | 27.5,4,134,95,2560,14.2,78,3,toyota corona 270 | 27.2,4,119,97,2300,14.7,78,3,datsun 510 271 | 30.9,4,105,75,2230,14.5,78,1,dodge omni 272 | 21.1,4,134,95,2515,14.8,78,3,toyota celica gt liftback 273 | 23.2,4,156,105,2745,16.7,78,1,plymouth sapporo 274 | 23.8,4,151,85,2855,17.6,78,1,oldsmobile starfire sx 275 | 23.9,4,119,97,2405,14.9,78,3,datsun 200-sx 276 | 20.3,5,131,103,2830,15.9,78,2,audi 5000 277 | 17,6,163,125,3140,13.6,78,2,volvo 264gl 278 | 21.6,4,121,115,2795,15.7,78,2,saab 99gle 279 | 16.2,6,163,133,3410,15.8,78,2,peugeot 604sl 280 | 31.5,4,89,71,1990,14.9,78,2,volkswagen scirocco 281 | 29.5,4,98,68,2135,16.6,78,3,honda accord lx 282 | 21.5,6,231,115,3245,15.4,79,1,pontiac lemans v6 283 | 19.8,6,200,85,2990,18.2,79,1,mercury zephyr 6 284 | 22.3,4,140,88,2890,17.3,79,1,ford fairmont 4 285 | 20.2,6,232,90,3265,18.2,79,1,amc concord dl 6 286 | 20.6,6,225,110,3360,16.6,79,1,dodge aspen 6 287 | 17,8,305,130,3840,15.4,79,1,chevrolet caprice classic 288 | 17.6,8,302,129,3725,13.4,79,1,ford ltd landau 289 | 16.5,8,351,138,3955,13.2,79,1,mercury grand marquis 290 | 18.2,8,318,135,3830,15.2,79,1,dodge st. regis 291 | 16.9,8,350,155,4360,14.9,79,1,buick estate wagon (sw) 292 | 15.5,8,351,142,4054,14.3,79,1,ford country squire (sw) 293 | 19.2,8,267,125,3605,15,79,1,chevrolet malibu classic (sw) 294 | 18.5,8,360,150,3940,13,79,1,chrysler lebaron town @ country (sw) 295 | 31.9,4,89,71,1925,14,79,2,vw rabbit custom 296 | 34.1,4,86,65,1975,15.2,79,3,maxda glc deluxe 297 | 35.7,4,98,80,1915,14.4,79,1,dodge colt hatchback custom 298 | 27.4,4,121,80,2670,15,79,1,amc spirit dl 299 | 25.4,5,183,77,3530,20.1,79,2,mercedes benz 300d 300 | 23,8,350,125,3900,17.4,79,1,cadillac eldorado 301 | 27.2,4,141,71,3190,24.8,79,2,peugeot 504 302 | 23.9,8,260,90,3420,22.2,79,1,oldsmobile cutlass salon brougham 303 | 34.2,4,105,70,2200,13.2,79,1,plymouth horizon 304 | 34.5,4,105,70,2150,14.9,79,1,plymouth horizon tc3 305 | 31.8,4,85,65,2020,19.2,79,3,datsun 210 306 | 37.3,4,91,69,2130,14.7,79,2,fiat strada custom 307 | 28.4,4,151,90,2670,16,79,1,buick skylark limited 308 | 28.8,6,173,115,2595,11.3,79,1,chevrolet citation 309 | 26.8,6,173,115,2700,12.9,79,1,oldsmobile omega brougham 310 | 33.5,4,151,90,2556,13.2,79,1,pontiac phoenix 311 | 41.5,4,98,76,2144,14.7,80,2,vw rabbit 312 | 38.1,4,89,60,1968,18.8,80,3,toyota corolla tercel 313 | 32.1,4,98,70,2120,15.5,80,1,chevrolet chevette 314 | 37.2,4,86,65,2019,16.4,80,3,datsun 310 315 | 28,4,151,90,2678,16.5,80,1,chevrolet citation 316 | 26.4,4,140,88,2870,18.1,80,1,ford fairmont 317 | 24.3,4,151,90,3003,20.1,80,1,amc concord 318 | 19.1,6,225,90,3381,18.7,80,1,dodge aspen 319 | 34.3,4,97,78,2188,15.8,80,2,audi 4000 320 | 29.8,4,134,90,2711,15.5,80,3,toyota corona liftback 321 | 31.3,4,120,75,2542,17.5,80,3,mazda 626 322 | 37,4,119,92,2434,15,80,3,datsun 510 hatchback 323 | 32.2,4,108,75,2265,15.2,80,3,toyota corolla 324 | 46.6,4,86,65,2110,17.9,80,3,mazda glc 325 | 27.9,4,156,105,2800,14.4,80,1,dodge colt 326 | 40.8,4,85,65,2110,19.2,80,3,datsun 210 327 | 44.3,4,90,48,2085,21.7,80,2,vw rabbit c (diesel) 328 | 43.4,4,90,48,2335,23.7,80,2,vw dasher (diesel) 329 | 36.4,5,121,67,2950,19.9,80,2,audi 5000s (diesel) 330 | 30,4,146,67,3250,21.8,80,2,mercedes-benz 240d 331 | 44.6,4,91,67,1850,13.8,80,3,honda civic 1500 gl 332 | 40.9,4,85,?,1835,17.3,80,2,renault lecar deluxe 333 | 33.8,4,97,67,2145,18,80,3,subaru dl 334 | 29.8,4,89,62,1845,15.3,80,2,vokswagen rabbit 335 | 32.7,6,168,132,2910,11.4,80,3,datsun 280-zx 336 | 23.7,3,70,100,2420,12.5,80,3,mazda rx-7 gs 337 | 35,4,122,88,2500,15.1,80,2,triumph tr7 coupe 338 | 23.6,4,140,?,2905,14.3,80,1,ford mustang cobra 339 | 32.4,4,107,72,2290,17,80,3,honda accord 340 | 27.2,4,135,84,2490,15.7,81,1,plymouth reliant 341 | 26.6,4,151,84,2635,16.4,81,1,buick skylark 342 | 25.8,4,156,92,2620,14.4,81,1,dodge aries wagon (sw) 343 | 23.5,6,173,110,2725,12.6,81,1,chevrolet citation 344 | 30,4,135,84,2385,12.9,81,1,plymouth reliant 345 | 39.1,4,79,58,1755,16.9,81,3,toyota starlet 346 | 39,4,86,64,1875,16.4,81,1,plymouth champ 347 | 35.1,4,81,60,1760,16.1,81,3,honda civic 1300 348 | 32.3,4,97,67,2065,17.8,81,3,subaru 349 | 37,4,85,65,1975,19.4,81,3,datsun 210 mpg 350 | 37.7,4,89,62,2050,17.3,81,3,toyota tercel 351 | 34.1,4,91,68,1985,16,81,3,mazda glc 4 352 | 34.7,4,105,63,2215,14.9,81,1,plymouth horizon 4 353 | 34.4,4,98,65,2045,16.2,81,1,ford escort 4w 354 | 29.9,4,98,65,2380,20.7,81,1,ford escort 2h 355 | 33,4,105,74,2190,14.2,81,2,volkswagen jetta 356 | 34.5,4,100,?,2320,15.8,81,2,renault 18i 357 | 33.7,4,107,75,2210,14.4,81,3,honda prelude 358 | 32.4,4,108,75,2350,16.8,81,3,toyota corolla 359 | 32.9,4,119,100,2615,14.8,81,3,datsun 200sx 360 | 31.6,4,120,74,2635,18.3,81,3,mazda 626 361 | 28.1,4,141,80,3230,20.4,81,2,peugeot 505s turbo diesel 362 | 30.7,6,145,76,3160,19.6,81,2,volvo diesel 363 | 25.4,6,168,116,2900,12.6,81,3,toyota cressida 364 | 24.2,6,146,120,2930,13.8,81,3,datsun 810 maxima 365 | 22.4,6,231,110,3415,15.8,81,1,buick century 366 | 26.6,8,350,105,3725,19,81,1,oldsmobile cutlass ls 367 | 20.2,6,200,88,3060,17.1,81,1,ford granada gl 368 | 17.6,6,225,85,3465,16.6,81,1,chrysler lebaron salon 369 | 28,4,112,88,2605,19.6,82,1,chevrolet cavalier 370 | 27,4,112,88,2640,18.6,82,1,chevrolet cavalier wagon 371 | 34,4,112,88,2395,18,82,1,chevrolet cavalier 2-door 372 | 31,4,112,85,2575,16.2,82,1,pontiac j2000 se hatchback 373 | 29,4,135,84,2525,16,82,1,dodge aries se 374 | 27,4,151,90,2735,18,82,1,pontiac phoenix 375 | 24,4,140,92,2865,16.4,82,1,ford fairmont futura 376 | 36,4,105,74,1980,15.3,82,2,volkswagen rabbit l 377 | 37,4,91,68,2025,18.2,82,3,mazda glc custom l 378 | 31,4,91,68,1970,17.6,82,3,mazda glc custom 379 | 38,4,105,63,2125,14.7,82,1,plymouth horizon miser 380 | 36,4,98,70,2125,17.3,82,1,mercury lynx l 381 | 36,4,120,88,2160,14.5,82,3,nissan stanza xe 382 | 36,4,107,75,2205,14.5,82,3,honda accord 383 | 34,4,108,70,2245,16.9,82,3,toyota corolla 384 | 38,4,91,67,1965,15,82,3,honda civic 385 | 32,4,91,67,1965,15.7,82,3,honda civic (auto) 386 | 38,4,91,67,1995,16.2,82,3,datsun 310 gx 387 | 25,6,181,110,2945,16.4,82,1,buick century limited 388 | 38,6,262,85,3015,17,82,1,oldsmobile cutlass ciera (diesel) 389 | 26,4,156,92,2585,14.5,82,1,chrysler lebaron medallion 390 | 22,6,232,112,2835,14.7,82,1,ford granada l 391 | 32,4,144,96,2665,13.9,82,3,toyota celica gt 392 | 36,4,135,84,2370,13,82,1,dodge charger 2.2 393 | 27,4,151,90,2950,17.3,82,1,chevrolet camaro 394 | 27,4,140,86,2790,15.6,82,1,ford mustang gl 395 | 44,4,97,52,2130,24.6,82,2,vw pickup 396 | 32,4,135,84,2295,11.6,82,1,dodge rampage 397 | 28,4,120,79,2625,18.6,82,1,ford ranger 398 | 31,4,119,82,2720,19.4,82,1,chevy s-10 399 | -------------------------------------------------------------------------------- /Auto.data.txt: -------------------------------------------------------------------------------- 1 | mpg cylinders displacement horsepower weight acceleration year origin name 2 | 18.0 8 307.0 130.0 3504. 12.0 70 1 "chevrolet chevelle malibu" 3 | 15.0 8 350.0 165.0 3693. 11.5 70 1 "buick skylark 320" 4 | 18.0 8 318.0 150.0 3436. 11.0 70 1 "plymouth satellite" 5 | 16.0 8 304.0 150.0 3433. 12.0 70 1 "amc rebel sst" 6 | 17.0 8 302.0 140.0 3449. 10.5 70 1 "ford torino" 7 | 15.0 8 429.0 198.0 4341. 10.0 70 1 "ford galaxie 500" 8 | 14.0 8 454.0 220.0 4354. 9.0 70 1 "chevrolet impala" 9 | 14.0 8 440.0 215.0 4312. 8.5 70 1 "plymouth fury iii" 10 | 14.0 8 455.0 225.0 4425. 10.0 70 1 "pontiac catalina" 11 | 15.0 8 390.0 190.0 3850. 8.5 70 1 "amc ambassador dpl" 12 | 15.0 8 383.0 170.0 3563. 10.0 70 1 "dodge challenger se" 13 | 14.0 8 340.0 160.0 3609. 8.0 70 1 "plymouth 'cuda 340" 14 | 15.0 8 400.0 150.0 3761. 9.5 70 1 "chevrolet monte carlo" 15 | 14.0 8 455.0 225.0 3086. 10.0 70 1 "buick estate wagon (sw)" 16 | 24.0 4 113.0 95.00 2372. 15.0 70 3 "toyota corona mark ii" 17 | 22.0 6 198.0 95.00 2833. 15.5 70 1 "plymouth duster" 18 | 18.0 6 199.0 97.00 2774. 15.5 70 1 "amc hornet" 19 | 21.0 6 200.0 85.00 2587. 16.0 70 1 "ford maverick" 20 | 27.0 4 97.00 88.00 2130. 14.5 70 3 "datsun pl510" 21 | 26.0 4 97.00 46.00 1835. 20.5 70 2 "volkswagen 1131 deluxe sedan" 22 | 25.0 4 110.0 87.00 2672. 17.5 70 2 "peugeot 504" 23 | 24.0 4 107.0 90.00 2430. 14.5 70 2 "audi 100 ls" 24 | 25.0 4 104.0 95.00 2375. 17.5 70 2 "saab 99e" 25 | 26.0 4 121.0 113.0 2234. 12.5 70 2 "bmw 2002" 26 | 21.0 6 199.0 90.00 2648. 15.0 70 1 "amc gremlin" 27 | 10.0 8 360.0 215.0 4615. 14.0 70 1 "ford f250" 28 | 10.0 8 307.0 200.0 4376. 15.0 70 1 "chevy c20" 29 | 11.0 8 318.0 210.0 4382. 13.5 70 1 "dodge d200" 30 | 9.0 8 304.0 193.0 4732. 18.5 70 1 "hi 1200d" 31 | 27.0 4 97.00 88.00 2130. 14.5 71 3 "datsun pl510" 32 | 28.0 4 140.0 90.00 2264. 15.5 71 1 "chevrolet vega 2300" 33 | 25.0 4 113.0 95.00 2228. 14.0 71 3 "toyota corona" 34 | 25.0 4 98.00 ? 2046. 19.0 71 1 "ford pinto" 35 | 19.0 6 232.0 100.0 2634. 13.0 71 1 "amc gremlin" 36 | 16.0 6 225.0 105.0 3439. 15.5 71 1 "plymouth satellite custom" 37 | 17.0 6 250.0 100.0 3329. 15.5 71 1 "chevrolet chevelle malibu" 38 | 19.0 6 250.0 88.00 3302. 15.5 71 1 "ford torino 500" 39 | 18.0 6 232.0 100.0 3288. 15.5 71 1 "amc matador" 40 | 14.0 8 350.0 165.0 4209. 12.0 71 1 "chevrolet impala" 41 | 14.0 8 400.0 175.0 4464. 11.5 71 1 "pontiac catalina brougham" 42 | 14.0 8 351.0 153.0 4154. 13.5 71 1 "ford galaxie 500" 43 | 14.0 8 318.0 150.0 4096. 13.0 71 1 "plymouth fury iii" 44 | 12.0 8 383.0 180.0 4955. 11.5 71 1 "dodge monaco (sw)" 45 | 13.0 8 400.0 170.0 4746. 12.0 71 1 "ford country squire (sw)" 46 | 13.0 8 400.0 175.0 5140. 12.0 71 1 "pontiac safari (sw)" 47 | 18.0 6 258.0 110.0 2962. 13.5 71 1 "amc hornet sportabout (sw)" 48 | 22.0 4 140.0 72.00 2408. 19.0 71 1 "chevrolet vega (sw)" 49 | 19.0 6 250.0 100.0 3282. 15.0 71 1 "pontiac firebird" 50 | 18.0 6 250.0 88.00 3139. 14.5 71 1 "ford mustang" 51 | 23.0 4 122.0 86.00 2220. 14.0 71 1 "mercury capri 2000" 52 | 28.0 4 116.0 90.00 2123. 14.0 71 2 "opel 1900" 53 | 30.0 4 79.00 70.00 2074. 19.5 71 2 "peugeot 304" 54 | 30.0 4 88.00 76.00 2065. 14.5 71 2 "fiat 124b" 55 | 31.0 4 71.00 65.00 1773. 19.0 71 3 "toyota corolla 1200" 56 | 35.0 4 72.00 69.00 1613. 18.0 71 3 "datsun 1200" 57 | 27.0 4 97.00 60.00 1834. 19.0 71 2 "volkswagen model 111" 58 | 26.0 4 91.00 70.00 1955. 20.5 71 1 "plymouth cricket" 59 | 24.0 4 113.0 95.00 2278. 15.5 72 3 "toyota corona hardtop" 60 | 25.0 4 97.50 80.00 2126. 17.0 72 1 "dodge colt hardtop" 61 | 23.0 4 97.00 54.00 2254. 23.5 72 2 "volkswagen type 3" 62 | 20.0 4 140.0 90.00 2408. 19.5 72 1 "chevrolet vega" 63 | 21.0 4 122.0 86.00 2226. 16.5 72 1 "ford pinto runabout" 64 | 13.0 8 350.0 165.0 4274. 12.0 72 1 "chevrolet impala" 65 | 14.0 8 400.0 175.0 4385. 12.0 72 1 "pontiac catalina" 66 | 15.0 8 318.0 150.0 4135. 13.5 72 1 "plymouth fury iii" 67 | 14.0 8 351.0 153.0 4129. 13.0 72 1 "ford galaxie 500" 68 | 17.0 8 304.0 150.0 3672. 11.5 72 1 "amc ambassador sst" 69 | 11.0 8 429.0 208.0 4633. 11.0 72 1 "mercury marquis" 70 | 13.0 8 350.0 155.0 4502. 13.5 72 1 "buick lesabre custom" 71 | 12.0 8 350.0 160.0 4456. 13.5 72 1 "oldsmobile delta 88 royale" 72 | 13.0 8 400.0 190.0 4422. 12.5 72 1 "chrysler newport royal" 73 | 19.0 3 70.00 97.00 2330. 13.5 72 3 "mazda rx2 coupe" 74 | 15.0 8 304.0 150.0 3892. 12.5 72 1 "amc matador (sw)" 75 | 13.0 8 307.0 130.0 4098. 14.0 72 1 "chevrolet chevelle concours (sw)" 76 | 13.0 8 302.0 140.0 4294. 16.0 72 1 "ford gran torino (sw)" 77 | 14.0 8 318.0 150.0 4077. 14.0 72 1 "plymouth satellite custom (sw)" 78 | 18.0 4 121.0 112.0 2933. 14.5 72 2 "volvo 145e (sw)" 79 | 22.0 4 121.0 76.00 2511. 18.0 72 2 "volkswagen 411 (sw)" 80 | 21.0 4 120.0 87.00 2979. 19.5 72 2 "peugeot 504 (sw)" 81 | 26.0 4 96.00 69.00 2189. 18.0 72 2 "renault 12 (sw)" 82 | 22.0 4 122.0 86.00 2395. 16.0 72 1 "ford pinto (sw)" 83 | 28.0 4 97.00 92.00 2288. 17.0 72 3 "datsun 510 (sw)" 84 | 23.0 4 120.0 97.00 2506. 14.5 72 3 "toyouta corona mark ii (sw)" 85 | 28.0 4 98.00 80.00 2164. 15.0 72 1 "dodge colt (sw)" 86 | 27.0 4 97.00 88.00 2100. 16.5 72 3 "toyota corolla 1600 (sw)" 87 | 13.0 8 350.0 175.0 4100. 13.0 73 1 "buick century 350" 88 | 14.0 8 304.0 150.0 3672. 11.5 73 1 "amc matador" 89 | 13.0 8 350.0 145.0 3988. 13.0 73 1 "chevrolet malibu" 90 | 14.0 8 302.0 137.0 4042. 14.5 73 1 "ford gran torino" 91 | 15.0 8 318.0 150.0 3777. 12.5 73 1 "dodge coronet custom" 92 | 12.0 8 429.0 198.0 4952. 11.5 73 1 "mercury marquis brougham" 93 | 13.0 8 400.0 150.0 4464. 12.0 73 1 "chevrolet caprice classic" 94 | 13.0 8 351.0 158.0 4363. 13.0 73 1 "ford ltd" 95 | 14.0 8 318.0 150.0 4237. 14.5 73 1 "plymouth fury gran sedan" 96 | 13.0 8 440.0 215.0 4735. 11.0 73 1 "chrysler new yorker brougham" 97 | 12.0 8 455.0 225.0 4951. 11.0 73 1 "buick electra 225 custom" 98 | 13.0 8 360.0 175.0 3821. 11.0 73 1 "amc ambassador brougham" 99 | 18.0 6 225.0 105.0 3121. 16.5 73 1 "plymouth valiant" 100 | 16.0 6 250.0 100.0 3278. 18.0 73 1 "chevrolet nova custom" 101 | 18.0 6 232.0 100.0 2945. 16.0 73 1 "amc hornet" 102 | 18.0 6 250.0 88.00 3021. 16.5 73 1 "ford maverick" 103 | 23.0 6 198.0 95.00 2904. 16.0 73 1 "plymouth duster" 104 | 26.0 4 97.00 46.00 1950. 21.0 73 2 "volkswagen super beetle" 105 | 11.0 8 400.0 150.0 4997. 14.0 73 1 "chevrolet impala" 106 | 12.0 8 400.0 167.0 4906. 12.5 73 1 "ford country" 107 | 13.0 8 360.0 170.0 4654. 13.0 73 1 "plymouth custom suburb" 108 | 12.0 8 350.0 180.0 4499. 12.5 73 1 "oldsmobile vista cruiser" 109 | 18.0 6 232.0 100.0 2789. 15.0 73 1 "amc gremlin" 110 | 20.0 4 97.00 88.00 2279. 19.0 73 3 "toyota carina" 111 | 21.0 4 140.0 72.00 2401. 19.5 73 1 "chevrolet vega" 112 | 22.0 4 108.0 94.00 2379. 16.5 73 3 "datsun 610" 113 | 18.0 3 70.00 90.00 2124. 13.5 73 3 "maxda rx3" 114 | 19.0 4 122.0 85.00 2310. 18.5 73 1 "ford pinto" 115 | 21.0 6 155.0 107.0 2472. 14.0 73 1 "mercury capri v6" 116 | 26.0 4 98.00 90.00 2265. 15.5 73 2 "fiat 124 sport coupe" 117 | 15.0 8 350.0 145.0 4082. 13.0 73 1 "chevrolet monte carlo s" 118 | 16.0 8 400.0 230.0 4278. 9.50 73 1 "pontiac grand prix" 119 | 29.0 4 68.00 49.00 1867. 19.5 73 2 "fiat 128" 120 | 24.0 4 116.0 75.00 2158. 15.5 73 2 "opel manta" 121 | 20.0 4 114.0 91.00 2582. 14.0 73 2 "audi 100ls" 122 | 19.0 4 121.0 112.0 2868. 15.5 73 2 "volvo 144ea" 123 | 15.0 8 318.0 150.0 3399. 11.0 73 1 "dodge dart custom" 124 | 24.0 4 121.0 110.0 2660. 14.0 73 2 "saab 99le" 125 | 20.0 6 156.0 122.0 2807. 13.5 73 3 "toyota mark ii" 126 | 11.0 8 350.0 180.0 3664. 11.0 73 1 "oldsmobile omega" 127 | 20.0 6 198.0 95.00 3102. 16.5 74 1 "plymouth duster" 128 | 21.0 6 200.0 ? 2875. 17.0 74 1 "ford maverick" 129 | 19.0 6 232.0 100.0 2901. 16.0 74 1 "amc hornet" 130 | 15.0 6 250.0 100.0 3336. 17.0 74 1 "chevrolet nova" 131 | 31.0 4 79.00 67.00 1950. 19.0 74 3 "datsun b210" 132 | 26.0 4 122.0 80.00 2451. 16.5 74 1 "ford pinto" 133 | 32.0 4 71.00 65.00 1836. 21.0 74 3 "toyota corolla 1200" 134 | 25.0 4 140.0 75.00 2542. 17.0 74 1 "chevrolet vega" 135 | 16.0 6 250.0 100.0 3781. 17.0 74 1 "chevrolet chevelle malibu classic" 136 | 16.0 6 258.0 110.0 3632. 18.0 74 1 "amc matador" 137 | 18.0 6 225.0 105.0 3613. 16.5 74 1 "plymouth satellite sebring" 138 | 16.0 8 302.0 140.0 4141. 14.0 74 1 "ford gran torino" 139 | 13.0 8 350.0 150.0 4699. 14.5 74 1 "buick century luxus (sw)" 140 | 14.0 8 318.0 150.0 4457. 13.5 74 1 "dodge coronet custom (sw)" 141 | 14.0 8 302.0 140.0 4638. 16.0 74 1 "ford gran torino (sw)" 142 | 14.0 8 304.0 150.0 4257. 15.5 74 1 "amc matador (sw)" 143 | 29.0 4 98.00 83.00 2219. 16.5 74 2 "audi fox" 144 | 26.0 4 79.00 67.00 1963. 15.5 74 2 "volkswagen dasher" 145 | 26.0 4 97.00 78.00 2300. 14.5 74 2 "opel manta" 146 | 31.0 4 76.00 52.00 1649. 16.5 74 3 "toyota corona" 147 | 32.0 4 83.00 61.00 2003. 19.0 74 3 "datsun 710" 148 | 28.0 4 90.00 75.00 2125. 14.5 74 1 "dodge colt" 149 | 24.0 4 90.00 75.00 2108. 15.5 74 2 "fiat 128" 150 | 26.0 4 116.0 75.00 2246. 14.0 74 2 "fiat 124 tc" 151 | 24.0 4 120.0 97.00 2489. 15.0 74 3 "honda civic" 152 | 26.0 4 108.0 93.00 2391. 15.5 74 3 "subaru" 153 | 31.0 4 79.00 67.00 2000. 16.0 74 2 "fiat x1.9" 154 | 19.0 6 225.0 95.00 3264. 16.0 75 1 "plymouth valiant custom" 155 | 18.0 6 250.0 105.0 3459. 16.0 75 1 "chevrolet nova" 156 | 15.0 6 250.0 72.00 3432. 21.0 75 1 "mercury monarch" 157 | 15.0 6 250.0 72.00 3158. 19.5 75 1 "ford maverick" 158 | 16.0 8 400.0 170.0 4668. 11.5 75 1 "pontiac catalina" 159 | 15.0 8 350.0 145.0 4440. 14.0 75 1 "chevrolet bel air" 160 | 16.0 8 318.0 150.0 4498. 14.5 75 1 "plymouth grand fury" 161 | 14.0 8 351.0 148.0 4657. 13.5 75 1 "ford ltd" 162 | 17.0 6 231.0 110.0 3907. 21.0 75 1 "buick century" 163 | 16.0 6 250.0 105.0 3897. 18.5 75 1 "chevroelt chevelle malibu" 164 | 15.0 6 258.0 110.0 3730. 19.0 75 1 "amc matador" 165 | 18.0 6 225.0 95.00 3785. 19.0 75 1 "plymouth fury" 166 | 21.0 6 231.0 110.0 3039. 15.0 75 1 "buick skyhawk" 167 | 20.0 8 262.0 110.0 3221. 13.5 75 1 "chevrolet monza 2+2" 168 | 13.0 8 302.0 129.0 3169. 12.0 75 1 "ford mustang ii" 169 | 29.0 4 97.00 75.00 2171. 16.0 75 3 "toyota corolla" 170 | 23.0 4 140.0 83.00 2639. 17.0 75 1 "ford pinto" 171 | 20.0 6 232.0 100.0 2914. 16.0 75 1 "amc gremlin" 172 | 23.0 4 140.0 78.00 2592. 18.5 75 1 "pontiac astro" 173 | 24.0 4 134.0 96.00 2702. 13.5 75 3 "toyota corona" 174 | 25.0 4 90.00 71.00 2223. 16.5 75 2 "volkswagen dasher" 175 | 24.0 4 119.0 97.00 2545. 17.0 75 3 "datsun 710" 176 | 18.0 6 171.0 97.00 2984. 14.5 75 1 "ford pinto" 177 | 29.0 4 90.00 70.00 1937. 14.0 75 2 "volkswagen rabbit" 178 | 19.0 6 232.0 90.00 3211. 17.0 75 1 "amc pacer" 179 | 23.0 4 115.0 95.00 2694. 15.0 75 2 "audi 100ls" 180 | 23.0 4 120.0 88.00 2957. 17.0 75 2 "peugeot 504" 181 | 22.0 4 121.0 98.00 2945. 14.5 75 2 "volvo 244dl" 182 | 25.0 4 121.0 115.0 2671. 13.5 75 2 "saab 99le" 183 | 33.0 4 91.00 53.00 1795. 17.5 75 3 "honda civic cvcc" 184 | 28.0 4 107.0 86.00 2464. 15.5 76 2 "fiat 131" 185 | 25.0 4 116.0 81.00 2220. 16.9 76 2 "opel 1900" 186 | 25.0 4 140.0 92.00 2572. 14.9 76 1 "capri ii" 187 | 26.0 4 98.00 79.00 2255. 17.7 76 1 "dodge colt" 188 | 27.0 4 101.0 83.00 2202. 15.3 76 2 "renault 12tl" 189 | 17.5 8 305.0 140.0 4215. 13.0 76 1 "chevrolet chevelle malibu classic" 190 | 16.0 8 318.0 150.0 4190. 13.0 76 1 "dodge coronet brougham" 191 | 15.5 8 304.0 120.0 3962. 13.9 76 1 "amc matador" 192 | 14.5 8 351.0 152.0 4215. 12.8 76 1 "ford gran torino" 193 | 22.0 6 225.0 100.0 3233. 15.4 76 1 "plymouth valiant" 194 | 22.0 6 250.0 105.0 3353. 14.5 76 1 "chevrolet nova" 195 | 24.0 6 200.0 81.00 3012. 17.6 76 1 "ford maverick" 196 | 22.5 6 232.0 90.00 3085. 17.6 76 1 "amc hornet" 197 | 29.0 4 85.00 52.00 2035. 22.2 76 1 "chevrolet chevette" 198 | 24.5 4 98.00 60.00 2164. 22.1 76 1 "chevrolet woody" 199 | 29.0 4 90.00 70.00 1937. 14.2 76 2 "vw rabbit" 200 | 33.0 4 91.00 53.00 1795. 17.4 76 3 "honda civic" 201 | 20.0 6 225.0 100.0 3651. 17.7 76 1 "dodge aspen se" 202 | 18.0 6 250.0 78.00 3574. 21.0 76 1 "ford granada ghia" 203 | 18.5 6 250.0 110.0 3645. 16.2 76 1 "pontiac ventura sj" 204 | 17.5 6 258.0 95.00 3193. 17.8 76 1 "amc pacer d/l" 205 | 29.5 4 97.00 71.00 1825. 12.2 76 2 "volkswagen rabbit" 206 | 32.0 4 85.00 70.00 1990. 17.0 76 3 "datsun b-210" 207 | 28.0 4 97.00 75.00 2155. 16.4 76 3 "toyota corolla" 208 | 26.5 4 140.0 72.00 2565. 13.6 76 1 "ford pinto" 209 | 20.0 4 130.0 102.0 3150. 15.7 76 2 "volvo 245" 210 | 13.0 8 318.0 150.0 3940. 13.2 76 1 "plymouth volare premier v8" 211 | 19.0 4 120.0 88.00 3270. 21.9 76 2 "peugeot 504" 212 | 19.0 6 156.0 108.0 2930. 15.5 76 3 "toyota mark ii" 213 | 16.5 6 168.0 120.0 3820. 16.7 76 2 "mercedes-benz 280s" 214 | 16.5 8 350.0 180.0 4380. 12.1 76 1 "cadillac seville" 215 | 13.0 8 350.0 145.0 4055. 12.0 76 1 "chevy c10" 216 | 13.0 8 302.0 130.0 3870. 15.0 76 1 "ford f108" 217 | 13.0 8 318.0 150.0 3755. 14.0 76 1 "dodge d100" 218 | 31.5 4 98.00 68.00 2045. 18.5 77 3 "honda accord cvcc" 219 | 30.0 4 111.0 80.00 2155. 14.8 77 1 "buick opel isuzu deluxe" 220 | 36.0 4 79.00 58.00 1825. 18.6 77 2 "renault 5 gtl" 221 | 25.5 4 122.0 96.00 2300. 15.5 77 1 "plymouth arrow gs" 222 | 33.5 4 85.00 70.00 1945. 16.8 77 3 "datsun f-10 hatchback" 223 | 17.5 8 305.0 145.0 3880. 12.5 77 1 "chevrolet caprice classic" 224 | 17.0 8 260.0 110.0 4060. 19.0 77 1 "oldsmobile cutlass supreme" 225 | 15.5 8 318.0 145.0 4140. 13.7 77 1 "dodge monaco brougham" 226 | 15.0 8 302.0 130.0 4295. 14.9 77 1 "mercury cougar brougham" 227 | 17.5 6 250.0 110.0 3520. 16.4 77 1 "chevrolet concours" 228 | 20.5 6 231.0 105.0 3425. 16.9 77 1 "buick skylark" 229 | 19.0 6 225.0 100.0 3630. 17.7 77 1 "plymouth volare custom" 230 | 18.5 6 250.0 98.00 3525. 19.0 77 1 "ford granada" 231 | 16.0 8 400.0 180.0 4220. 11.1 77 1 "pontiac grand prix lj" 232 | 15.5 8 350.0 170.0 4165. 11.4 77 1 "chevrolet monte carlo landau" 233 | 15.5 8 400.0 190.0 4325. 12.2 77 1 "chrysler cordoba" 234 | 16.0 8 351.0 149.0 4335. 14.5 77 1 "ford thunderbird" 235 | 29.0 4 97.00 78.00 1940. 14.5 77 2 "volkswagen rabbit custom" 236 | 24.5 4 151.0 88.00 2740. 16.0 77 1 "pontiac sunbird coupe" 237 | 26.0 4 97.00 75.00 2265. 18.2 77 3 "toyota corolla liftback" 238 | 25.5 4 140.0 89.00 2755. 15.8 77 1 "ford mustang ii 2+2" 239 | 30.5 4 98.00 63.00 2051. 17.0 77 1 "chevrolet chevette" 240 | 33.5 4 98.00 83.00 2075. 15.9 77 1 "dodge colt m/m" 241 | 30.0 4 97.00 67.00 1985. 16.4 77 3 "subaru dl" 242 | 30.5 4 97.00 78.00 2190. 14.1 77 2 "volkswagen dasher" 243 | 22.0 6 146.0 97.00 2815. 14.5 77 3 "datsun 810" 244 | 21.5 4 121.0 110.0 2600. 12.8 77 2 "bmw 320i" 245 | 21.5 3 80.00 110.0 2720. 13.5 77 3 "mazda rx-4" 246 | 43.1 4 90.00 48.00 1985. 21.5 78 2 "volkswagen rabbit custom diesel" 247 | 36.1 4 98.00 66.00 1800. 14.4 78 1 "ford fiesta" 248 | 32.8 4 78.00 52.00 1985. 19.4 78 3 "mazda glc deluxe" 249 | 39.4 4 85.00 70.00 2070. 18.6 78 3 "datsun b210 gx" 250 | 36.1 4 91.00 60.00 1800. 16.4 78 3 "honda civic cvcc" 251 | 19.9 8 260.0 110.0 3365. 15.5 78 1 "oldsmobile cutlass salon brougham" 252 | 19.4 8 318.0 140.0 3735. 13.2 78 1 "dodge diplomat" 253 | 20.2 8 302.0 139.0 3570. 12.8 78 1 "mercury monarch ghia" 254 | 19.2 6 231.0 105.0 3535. 19.2 78 1 "pontiac phoenix lj" 255 | 20.5 6 200.0 95.00 3155. 18.2 78 1 "chevrolet malibu" 256 | 20.2 6 200.0 85.00 2965. 15.8 78 1 "ford fairmont (auto)" 257 | 25.1 4 140.0 88.00 2720. 15.4 78 1 "ford fairmont (man)" 258 | 20.5 6 225.0 100.0 3430. 17.2 78 1 "plymouth volare" 259 | 19.4 6 232.0 90.00 3210. 17.2 78 1 "amc concord" 260 | 20.6 6 231.0 105.0 3380. 15.8 78 1 "buick century special" 261 | 20.8 6 200.0 85.00 3070. 16.7 78 1 "mercury zephyr" 262 | 18.6 6 225.0 110.0 3620. 18.7 78 1 "dodge aspen" 263 | 18.1 6 258.0 120.0 3410. 15.1 78 1 "amc concord d/l" 264 | 19.2 8 305.0 145.0 3425. 13.2 78 1 "chevrolet monte carlo landau" 265 | 17.7 6 231.0 165.0 3445. 13.4 78 1 "buick regal sport coupe (turbo)" 266 | 18.1 8 302.0 139.0 3205. 11.2 78 1 "ford futura" 267 | 17.5 8 318.0 140.0 4080. 13.7 78 1 "dodge magnum xe" 268 | 30.0 4 98.00 68.00 2155. 16.5 78 1 "chevrolet chevette" 269 | 27.5 4 134.0 95.00 2560. 14.2 78 3 "toyota corona" 270 | 27.2 4 119.0 97.00 2300. 14.7 78 3 "datsun 510" 271 | 30.9 4 105.0 75.00 2230. 14.5 78 1 "dodge omni" 272 | 21.1 4 134.0 95.00 2515. 14.8 78 3 "toyota celica gt liftback" 273 | 23.2 4 156.0 105.0 2745. 16.7 78 1 "plymouth sapporo" 274 | 23.8 4 151.0 85.00 2855. 17.6 78 1 "oldsmobile starfire sx" 275 | 23.9 4 119.0 97.00 2405. 14.9 78 3 "datsun 200-sx" 276 | 20.3 5 131.0 103.0 2830. 15.9 78 2 "audi 5000" 277 | 17.0 6 163.0 125.0 3140. 13.6 78 2 "volvo 264gl" 278 | 21.6 4 121.0 115.0 2795. 15.7 78 2 "saab 99gle" 279 | 16.2 6 163.0 133.0 3410. 15.8 78 2 "peugeot 604sl" 280 | 31.5 4 89.00 71.00 1990. 14.9 78 2 "volkswagen scirocco" 281 | 29.5 4 98.00 68.00 2135. 16.6 78 3 "honda accord lx" 282 | 21.5 6 231.0 115.0 3245. 15.4 79 1 "pontiac lemans v6" 283 | 19.8 6 200.0 85.00 2990. 18.2 79 1 "mercury zephyr 6" 284 | 22.3 4 140.0 88.00 2890. 17.3 79 1 "ford fairmont 4" 285 | 20.2 6 232.0 90.00 3265. 18.2 79 1 "amc concord dl 6" 286 | 20.6 6 225.0 110.0 3360. 16.6 79 1 "dodge aspen 6" 287 | 17.0 8 305.0 130.0 3840. 15.4 79 1 "chevrolet caprice classic" 288 | 17.6 8 302.0 129.0 3725. 13.4 79 1 "ford ltd landau" 289 | 16.5 8 351.0 138.0 3955. 13.2 79 1 "mercury grand marquis" 290 | 18.2 8 318.0 135.0 3830. 15.2 79 1 "dodge st. regis" 291 | 16.9 8 350.0 155.0 4360. 14.9 79 1 "buick estate wagon (sw)" 292 | 15.5 8 351.0 142.0 4054. 14.3 79 1 "ford country squire (sw)" 293 | 19.2 8 267.0 125.0 3605. 15.0 79 1 "chevrolet malibu classic (sw)" 294 | 18.5 8 360.0 150.0 3940. 13.0 79 1 "chrysler lebaron town @ country (sw)" 295 | 31.9 4 89.00 71.00 1925. 14.0 79 2 "vw rabbit custom" 296 | 34.1 4 86.00 65.00 1975. 15.2 79 3 "maxda glc deluxe" 297 | 35.7 4 98.00 80.00 1915. 14.4 79 1 "dodge colt hatchback custom" 298 | 27.4 4 121.0 80.00 2670. 15.0 79 1 "amc spirit dl" 299 | 25.4 5 183.0 77.00 3530. 20.1 79 2 "mercedes benz 300d" 300 | 23.0 8 350.0 125.0 3900. 17.4 79 1 "cadillac eldorado" 301 | 27.2 4 141.0 71.00 3190. 24.8 79 2 "peugeot 504" 302 | 23.9 8 260.0 90.00 3420. 22.2 79 1 "oldsmobile cutlass salon brougham" 303 | 34.2 4 105.0 70.00 2200. 13.2 79 1 "plymouth horizon" 304 | 34.5 4 105.0 70.00 2150. 14.9 79 1 "plymouth horizon tc3" 305 | 31.8 4 85.00 65.00 2020. 19.2 79 3 "datsun 210" 306 | 37.3 4 91.00 69.00 2130. 14.7 79 2 "fiat strada custom" 307 | 28.4 4 151.0 90.00 2670. 16.0 79 1 "buick skylark limited" 308 | 28.8 6 173.0 115.0 2595. 11.3 79 1 "chevrolet citation" 309 | 26.8 6 173.0 115.0 2700. 12.9 79 1 "oldsmobile omega brougham" 310 | 33.5 4 151.0 90.00 2556. 13.2 79 1 "pontiac phoenix" 311 | 41.5 4 98.00 76.00 2144. 14.7 80 2 "vw rabbit" 312 | 38.1 4 89.00 60.00 1968. 18.8 80 3 "toyota corolla tercel" 313 | 32.1 4 98.00 70.00 2120. 15.5 80 1 "chevrolet chevette" 314 | 37.2 4 86.00 65.00 2019. 16.4 80 3 "datsun 310" 315 | 28.0 4 151.0 90.00 2678. 16.5 80 1 "chevrolet citation" 316 | 26.4 4 140.0 88.00 2870. 18.1 80 1 "ford fairmont" 317 | 24.3 4 151.0 90.00 3003. 20.1 80 1 "amc concord" 318 | 19.1 6 225.0 90.00 3381. 18.7 80 1 "dodge aspen" 319 | 34.3 4 97.00 78.00 2188. 15.8 80 2 "audi 4000" 320 | 29.8 4 134.0 90.00 2711. 15.5 80 3 "toyota corona liftback" 321 | 31.3 4 120.0 75.00 2542. 17.5 80 3 "mazda 626" 322 | 37.0 4 119.0 92.00 2434. 15.0 80 3 "datsun 510 hatchback" 323 | 32.2 4 108.0 75.00 2265. 15.2 80 3 "toyota corolla" 324 | 46.6 4 86.00 65.00 2110. 17.9 80 3 "mazda glc" 325 | 27.9 4 156.0 105.0 2800. 14.4 80 1 "dodge colt" 326 | 40.8 4 85.00 65.00 2110. 19.2 80 3 "datsun 210" 327 | 44.3 4 90.00 48.00 2085. 21.7 80 2 "vw rabbit c (diesel)" 328 | 43.4 4 90.00 48.00 2335. 23.7 80 2 "vw dasher (diesel)" 329 | 36.4 5 121.0 67.00 2950. 19.9 80 2 "audi 5000s (diesel)" 330 | 30.0 4 146.0 67.00 3250. 21.8 80 2 "mercedes-benz 240d" 331 | 44.6 4 91.00 67.00 1850. 13.8 80 3 "honda civic 1500 gl" 332 | 40.9 4 85.00 ? 1835. 17.3 80 2 "renault lecar deluxe" 333 | 33.8 4 97.00 67.00 2145. 18.0 80 3 "subaru dl" 334 | 29.8 4 89.00 62.00 1845. 15.3 80 2 "vokswagen rabbit" 335 | 32.7 6 168.0 132.0 2910. 11.4 80 3 "datsun 280-zx" 336 | 23.7 3 70.00 100.0 2420. 12.5 80 3 "mazda rx-7 gs" 337 | 35.0 4 122.0 88.00 2500. 15.1 80 2 "triumph tr7 coupe" 338 | 23.6 4 140.0 ? 2905. 14.3 80 1 "ford mustang cobra" 339 | 32.4 4 107.0 72.00 2290. 17.0 80 3 "honda accord" 340 | 27.2 4 135.0 84.00 2490. 15.7 81 1 "plymouth reliant" 341 | 26.6 4 151.0 84.00 2635. 16.4 81 1 "buick skylark" 342 | 25.8 4 156.0 92.00 2620. 14.4 81 1 "dodge aries wagon (sw)" 343 | 23.5 6 173.0 110.0 2725. 12.6 81 1 "chevrolet citation" 344 | 30.0 4 135.0 84.00 2385. 12.9 81 1 "plymouth reliant" 345 | 39.1 4 79.00 58.00 1755. 16.9 81 3 "toyota starlet" 346 | 39.0 4 86.00 64.00 1875. 16.4 81 1 "plymouth champ" 347 | 35.1 4 81.00 60.00 1760. 16.1 81 3 "honda civic 1300" 348 | 32.3 4 97.00 67.00 2065. 17.8 81 3 "subaru" 349 | 37.0 4 85.00 65.00 1975. 19.4 81 3 "datsun 210 mpg" 350 | 37.7 4 89.00 62.00 2050. 17.3 81 3 "toyota tercel" 351 | 34.1 4 91.00 68.00 1985. 16.0 81 3 "mazda glc 4" 352 | 34.7 4 105.0 63.00 2215. 14.9 81 1 "plymouth horizon 4" 353 | 34.4 4 98.00 65.00 2045. 16.2 81 1 "ford escort 4w" 354 | 29.9 4 98.00 65.00 2380. 20.7 81 1 "ford escort 2h" 355 | 33.0 4 105.0 74.00 2190. 14.2 81 2 "volkswagen jetta" 356 | 34.5 4 100.0 ? 2320. 15.8 81 2 "renault 18i" 357 | 33.7 4 107.0 75.00 2210. 14.4 81 3 "honda prelude" 358 | 32.4 4 108.0 75.00 2350. 16.8 81 3 "toyota corolla" 359 | 32.9 4 119.0 100.0 2615. 14.8 81 3 "datsun 200sx" 360 | 31.6 4 120.0 74.00 2635. 18.3 81 3 "mazda 626" 361 | 28.1 4 141.0 80.00 3230. 20.4 81 2 "peugeot 505s turbo diesel" 362 | 30.7 6 145.0 76.00 3160. 19.6 81 2 "volvo diesel" 363 | 25.4 6 168.0 116.0 2900. 12.6 81 3 "toyota cressida" 364 | 24.2 6 146.0 120.0 2930. 13.8 81 3 "datsun 810 maxima" 365 | 22.4 6 231.0 110.0 3415. 15.8 81 1 "buick century" 366 | 26.6 8 350.0 105.0 3725. 19.0 81 1 "oldsmobile cutlass ls" 367 | 20.2 6 200.0 88.00 3060. 17.1 81 1 "ford granada gl" 368 | 17.6 6 225.0 85.00 3465. 16.6 81 1 "chrysler lebaron salon" 369 | 28.0 4 112.0 88.00 2605. 19.6 82 1 "chevrolet cavalier" 370 | 27.0 4 112.0 88.00 2640. 18.6 82 1 "chevrolet cavalier wagon" 371 | 34.0 4 112.0 88.00 2395. 18.0 82 1 "chevrolet cavalier 2-door" 372 | 31.0 4 112.0 85.00 2575. 16.2 82 1 "pontiac j2000 se hatchback" 373 | 29.0 4 135.0 84.00 2525. 16.0 82 1 "dodge aries se" 374 | 27.0 4 151.0 90.00 2735. 18.0 82 1 "pontiac phoenix" 375 | 24.0 4 140.0 92.00 2865. 16.4 82 1 "ford fairmont futura" 376 | 36.0 4 105.0 74.00 1980. 15.3 82 2 "volkswagen rabbit l" 377 | 37.0 4 91.00 68.00 2025. 18.2 82 3 "mazda glc custom l" 378 | 31.0 4 91.00 68.00 1970. 17.6 82 3 "mazda glc custom" 379 | 38.0 4 105.0 63.00 2125. 14.7 82 1 "plymouth horizon miser" 380 | 36.0 4 98.00 70.00 2125. 17.3 82 1 "mercury lynx l" 381 | 36.0 4 120.0 88.00 2160. 14.5 82 3 "nissan stanza xe" 382 | 36.0 4 107.0 75.00 2205. 14.5 82 3 "honda accord" 383 | 34.0 4 108.0 70.00 2245 16.9 82 3 "toyota corolla" 384 | 38.0 4 91.00 67.00 1965. 15.0 82 3 "honda civic" 385 | 32.0 4 91.00 67.00 1965. 15.7 82 3 "honda civic (auto)" 386 | 38.0 4 91.00 67.00 1995. 16.2 82 3 "datsun 310 gx" 387 | 25.0 6 181.0 110.0 2945. 16.4 82 1 "buick century limited" 388 | 38.0 6 262.0 85.00 3015. 17.0 82 1 "oldsmobile cutlass ciera (diesel)" 389 | 26.0 4 156.0 92.00 2585. 14.5 82 1 "chrysler lebaron medallion" 390 | 22.0 6 232.0 112.0 2835 14.7 82 1 "ford granada l" 391 | 32.0 4 144.0 96.00 2665. 13.9 82 3 "toyota celica gt" 392 | 36.0 4 135.0 84.00 2370. 13.0 82 1 "dodge charger 2.2" 393 | 27.0 4 151.0 90.00 2950. 17.3 82 1 "chevrolet camaro" 394 | 27.0 4 140.0 86.00 2790. 15.6 82 1 "ford mustang gl" 395 | 44.0 4 97.00 52.00 2130. 24.6 82 2 "vw pickup" 396 | 32.0 4 135.0 84.00 2295. 11.6 82 1 "dodge rampage" 397 | 28.0 4 120.0 79.00 2625. 18.6 82 1 "ford ranger" 398 | 31.0 4 119.0 82.00 2720. 19.4 82 1 "chevy s-10" 399 | -------------------------------------------------------------------------------- /Chapter 10 Labs.txt: -------------------------------------------------------------------------------- 1 | # Chapter 10 Lab 1: Principal Components Analysis 2 | 3 | states=row.names(USArrests) 4 | states 5 | names(USArrests) 6 | apply(USArrests, 2, mean) 7 | apply(USArrests, 2, var) 8 | pr.out=prcomp(USArrests, scale=TRUE) 9 | names(pr.out) 10 | pr.out$center 11 | pr.out$scale 12 | pr.out$rotation 13 | dim(pr.out$x) 14 | biplot(pr.out, scale=0) 15 | pr.out$rotation=-pr.out$rotation 16 | pr.out$x=-pr.out$x 17 | biplot(pr.out, scale=0) 18 | pr.out$sdev 19 | pr.var=pr.out$sdev^2 20 | pr.var 21 | pve=pr.var/sum(pr.var) 22 | pve 23 | plot(pve, xlab="Principal Component", ylab="Proportion of Variance Explained", ylim=c(0,1),type='b') 24 | plot(cumsum(pve), xlab="Principal Component", ylab="Cumulative Proportion of Variance Explained", ylim=c(0,1),type='b') 25 | a=c(1,2,8,-3) 26 | cumsum(a) 27 | 28 | 29 | # Chapter 10 Lab 2: Clustering 30 | 31 | # K-Means Clustering 32 | 33 | set.seed(2) 34 | x=matrix(rnorm(50*2), ncol=2) 35 | x[1:25,1]=x[1:25,1]+3 36 | x[1:25,2]=x[1:25,2]-4 37 | km.out=kmeans(x,2,nstart=20) 38 | km.out$cluster 39 | plot(x, col=(km.out$cluster+1), main="K-Means Clustering Results with K=2", xlab="", ylab="", pch=20, cex=2) 40 | set.seed(4) 41 | km.out=kmeans(x,3,nstart=20) 42 | km.out 43 | plot(x, col=(km.out$cluster+1), main="K-Means Clustering Results with K=3", xlab="", ylab="", pch=20, cex=2) 44 | set.seed(3) 45 | km.out=kmeans(x,3,nstart=1) 46 | km.out$tot.withinss 47 | km.out=kmeans(x,3,nstart=20) 48 | km.out$tot.withinss 49 | 50 | # Hierarchical Clustering 51 | 52 | hc.complete=hclust(dist(x), method="complete") 53 | hc.average=hclust(dist(x), method="average") 54 | hc.single=hclust(dist(x), method="single") 55 | par(mfrow=c(1,3)) 56 | plot(hc.complete,main="Complete Linkage", xlab="", sub="", cex=.9) 57 | plot(hc.average, main="Average Linkage", xlab="", sub="", cex=.9) 58 | plot(hc.single, main="Single Linkage", xlab="", sub="", cex=.9) 59 | cutree(hc.complete, 2) 60 | cutree(hc.average, 2) 61 | cutree(hc.single, 2) 62 | cutree(hc.single, 4) 63 | xsc=scale(x) 64 | plot(hclust(dist(xsc), method="complete"), main="Hierarchical Clustering with Scaled Features") 65 | x=matrix(rnorm(30*3), ncol=3) 66 | dd=as.dist(1-cor(t(x))) 67 | plot(hclust(dd, method="complete"), main="Complete Linkage with Correlation-Based Distance", xlab="", sub="") 68 | 69 | 70 | # Chapter 10 Lab 3: NCI60 Data Example 71 | 72 | # The NCI60 data 73 | 74 | library(ISLR) 75 | nci.labs=NCI60$labs 76 | nci.data=NCI60$data 77 | dim(nci.data) 78 | nci.labs[1:4] 79 | table(nci.labs) 80 | 81 | # PCA on the NCI60 Data 82 | 83 | pr.out=prcomp(nci.data, scale=TRUE) 84 | Cols=function(vec){ 85 | cols=rainbow(length(unique(vec))) 86 | return(cols[as.numeric(as.factor(vec))]) 87 | } 88 | par(mfrow=c(1,2)) 89 | plot(pr.out$x[,1:2], col=Cols(nci.labs), pch=19,xlab="Z1",ylab="Z2") 90 | plot(pr.out$x[,c(1,3)], col=Cols(nci.labs), pch=19,xlab="Z1",ylab="Z3") 91 | summary(pr.out) 92 | plot(pr.out) 93 | pve=100*pr.out$sdev^2/sum(pr.out$sdev^2) 94 | par(mfrow=c(1,2)) 95 | plot(pve, type="o", ylab="PVE", xlab="Principal Component", col="blue") 96 | plot(cumsum(pve), type="o", ylab="Cumulative PVE", xlab="Principal Component", col="brown3") 97 | 98 | # Clustering the Observations of the NCI60 Data 99 | 100 | sd.data=scale(nci.data) 101 | par(mfrow=c(1,3)) 102 | data.dist=dist(sd.data) 103 | plot(hclust(data.dist), labels=nci.labs, main="Complete Linkage", xlab="", sub="",ylab="") 104 | plot(hclust(data.dist, method="average"), labels=nci.labs, main="Average Linkage", xlab="", sub="",ylab="") 105 | plot(hclust(data.dist, method="single"), labels=nci.labs, main="Single Linkage", xlab="", sub="",ylab="") 106 | hc.out=hclust(dist(sd.data)) 107 | hc.clusters=cutree(hc.out,4) 108 | table(hc.clusters,nci.labs) 109 | par(mfrow=c(1,1)) 110 | plot(hc.out, labels=nci.labs) 111 | abline(h=139, col="red") 112 | hc.out 113 | set.seed(2) 114 | km.out=kmeans(sd.data, 4, nstart=20) 115 | km.clusters=km.out$cluster 116 | table(km.clusters,hc.clusters) 117 | hc.out=hclust(dist(pr.out$x[,1:5])) 118 | plot(hc.out, labels=nci.labs, main="Hier. Clust. on First Five Score Vectors") 119 | table(cutree(hc.out,4), nci.labs) 120 | 121 | -------------------------------------------------------------------------------- /Chapter 2 Lab.txt: -------------------------------------------------------------------------------- 1 | # Chapter 2 Lab: Introduction to R 2 | 3 | # Basic Commands 4 | 5 | x <- c(1,3,2,5) 6 | x 7 | x = c(1,6,2) 8 | x 9 | y = c(1,4,3) 10 | length(x) 11 | length(y) 12 | x+y 13 | ls() 14 | rm(x,y) 15 | ls() 16 | rm(list=ls()) 17 | ?matrix 18 | x=matrix(data=c(1,2,3,4), nrow=2, ncol=2) 19 | x 20 | x=matrix(c(1,2,3,4),2,2) 21 | matrix(c(1,2,3,4),2,2,byrow=TRUE) 22 | sqrt(x) 23 | x^2 24 | x=rnorm(50) 25 | y=x+rnorm(50,mean=50,sd=.1) 26 | cor(x,y) 27 | set.seed(1303) 28 | rnorm(50) 29 | set.seed(3) 30 | y=rnorm(100) 31 | mean(y) 32 | var(y) 33 | sqrt(var(y)) 34 | sd(y) 35 | 36 | # Graphics 37 | 38 | x=rnorm(100) 39 | y=rnorm(100) 40 | plot(x,y) 41 | plot(x,y,xlab="this is the x-axis",ylab="this is the y-axis",main="Plot of X vs Y") 42 | pdf("Figure.pdf") 43 | plot(x,y,col="green") 44 | dev.off() 45 | x=seq(1,10) 46 | x 47 | x=1:10 48 | x 49 | x=seq(-pi,pi,length=50) 50 | y=x 51 | f=outer(x,y,function(x,y)cos(y)/(1+x^2)) 52 | contour(x,y,f) 53 | contour(x,y,f,nlevels=45,add=T) 54 | fa=(f-t(f))/2 55 | contour(x,y,fa,nlevels=15) 56 | image(x,y,fa) 57 | persp(x,y,fa) 58 | persp(x,y,fa,theta=30) 59 | persp(x,y,fa,theta=30,phi=20) 60 | persp(x,y,fa,theta=30,phi=70) 61 | persp(x,y,fa,theta=30,phi=40) 62 | 63 | # Indexing Data 64 | 65 | A=matrix(1:16,4,4) 66 | A 67 | A[2,3] 68 | A[c(1,3),c(2,4)] 69 | A[1:3,2:4] 70 | A[1:2,] 71 | A[,1:2] 72 | A[1,] 73 | A[-c(1,3),] 74 | A[-c(1,3),-c(1,3,4)] 75 | dim(A) 76 | 77 | # Loading Data 78 | 79 | Auto=read.table("Auto.data") 80 | fix(Auto) 81 | Auto=read.table("Auto.data",header=T,na.strings="?") 82 | fix(Auto) 83 | Auto=read.csv("Auto.csv",header=T,na.strings="?") 84 | fix(Auto) 85 | dim(Auto) 86 | Auto[1:4,] 87 | Auto=na.omit(Auto) 88 | dim(Auto) 89 | names(Auto) 90 | 91 | # Additional Graphical and Numerical Summaries 92 | 93 | plot(cylinders, mpg) 94 | plot(Auto$cylinders, Auto$mpg) 95 | attach(Auto) 96 | plot(cylinders, mpg) 97 | cylinders=as.factor(cylinders) 98 | plot(cylinders, mpg) 99 | plot(cylinders, mpg, col="red") 100 | plot(cylinders, mpg, col="red", varwidth=T) 101 | plot(cylinders, mpg, col="red", varwidth=T,horizontal=T) 102 | plot(cylinders, mpg, col="red", varwidth=T, xlab="cylinders", ylab="MPG") 103 | hist(mpg) 104 | hist(mpg,col=2) 105 | hist(mpg,col=2,breaks=15) 106 | pairs(Auto) 107 | pairs(~ mpg + displacement + horsepower + weight + acceleration, Auto) 108 | plot(horsepower,mpg) 109 | identify(horsepower,mpg,name) 110 | summary(Auto) 111 | summary(mpg) -------------------------------------------------------------------------------- /Chapter 3 Lab.txt: -------------------------------------------------------------------------------- 1 | # Chapter 3 Lab: Linear Regression 2 | 3 | library(MASS) 4 | library(ISLR) 5 | 6 | # Simple Linear Regression 7 | 8 | fix(Boston) 9 | names(Boston) 10 | lm.fit=lm(medv~lstat) 11 | lm.fit=lm(medv~lstat,data=Boston) 12 | attach(Boston) 13 | lm.fit=lm(medv~lstat) 14 | lm.fit 15 | summary(lm.fit) 16 | names(lm.fit) 17 | coef(lm.fit) 18 | confint(lm.fit) 19 | predict(lm.fit,data.frame(lstat=(c(5,10,15))), interval="confidence") 20 | predict(lm.fit,data.frame(lstat=(c(5,10,15))), interval="prediction") 21 | plot(lstat,medv) 22 | abline(lm.fit) 23 | abline(lm.fit,lwd=3) 24 | abline(lm.fit,lwd=3,col="red") 25 | plot(lstat,medv,col="red") 26 | plot(lstat,medv,pch=20) 27 | plot(lstat,medv,pch="+") 28 | plot(1:20,1:20,pch=1:20) 29 | par(mfrow=c(2,2)) 30 | plot(lm.fit) 31 | plot(predict(lm.fit), residuals(lm.fit)) 32 | plot(predict(lm.fit), rstudent(lm.fit)) 33 | plot(hatvalues(lm.fit)) 34 | which.max(hatvalues(lm.fit)) 35 | 36 | # Multiple Linear Regression 37 | 38 | lm.fit=lm(medv~lstat+age,data=Boston) 39 | summary(lm.fit) 40 | lm.fit=lm(medv~.,data=Boston) 41 | summary(lm.fit) 42 | library(car) 43 | vif(lm.fit) 44 | lm.fit1=lm(medv~.-age,data=Boston) 45 | summary(lm.fit1) 46 | lm.fit1=update(lm.fit, ~.-age) 47 | 48 | # Interaction Terms 49 | 50 | summary(lm(medv~lstat*age,data=Boston)) 51 | 52 | # Non-linear Transformations of the Predictors 53 | 54 | lm.fit2=lm(medv~lstat+I(lstat^2)) 55 | summary(lm.fit2) 56 | lm.fit=lm(medv~lstat) 57 | anova(lm.fit,lm.fit2) 58 | par(mfrow=c(2,2)) 59 | plot(lm.fit2) 60 | lm.fit5=lm(medv~poly(lstat,5)) 61 | summary(lm.fit5) 62 | summary(lm(medv~log(rm),data=Boston)) 63 | 64 | # Qualitative Predictors 65 | 66 | fix(Carseats) 67 | names(Carseats) 68 | lm.fit=lm(Sales~.+Income:Advertising+Price:Age,data=Carseats) 69 | summary(lm.fit) 70 | attach(Carseats) 71 | contrasts(ShelveLoc) 72 | 73 | # Writing Functions 74 | 75 | LoadLibraries 76 | LoadLibraries() 77 | LoadLibraries=function(){ 78 | library(ISLR) 79 | library(MASS) 80 | print("The libraries have been loaded.") 81 | } 82 | LoadLibraries 83 | LoadLibraries() 84 | -------------------------------------------------------------------------------- /Chapter 4 Lab.txt: -------------------------------------------------------------------------------- 1 | # Chapter 4 Lab: Logistic Regression, LDA, QDA, and KNN 2 | 3 | # The Stock Market Data 4 | 5 | library(ISLR) 6 | names(Smarket) 7 | dim(Smarket) 8 | summary(Smarket) 9 | pairs(Smarket) 10 | cor(Smarket) 11 | cor(Smarket[,-9]) 12 | attach(Smarket) 13 | plot(Volume) 14 | 15 | # Logistic Regression 16 | 17 | glm.fit=glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,data=Smarket,family=binomial) 18 | summary(glm.fit) 19 | coef(glm.fit) 20 | summary(glm.fit)$coef 21 | summary(glm.fit)$coef[,4] 22 | glm.probs=predict(glm.fit,type="response") 23 | glm.probs[1:10] 24 | contrasts(Direction) 25 | glm.pred=rep("Down",1250) 26 | glm.pred[glm.probs>.5]="Up" 27 | table(glm.pred,Direction) 28 | (507+145)/1250 29 | mean(glm.pred==Direction) 30 | train=(Year<2005) 31 | Smarket.2005=Smarket[!train,] 32 | dim(Smarket.2005) 33 | Direction.2005=Direction[!train] 34 | glm.fit=glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,data=Smarket,family=binomial,subset=train) 35 | glm.probs=predict(glm.fit,Smarket.2005,type="response") 36 | glm.pred=rep("Down",252) 37 | glm.pred[glm.probs>.5]="Up" 38 | table(glm.pred,Direction.2005) 39 | mean(glm.pred==Direction.2005) 40 | mean(glm.pred!=Direction.2005) 41 | glm.fit=glm(Direction~Lag1+Lag2,data=Smarket,family=binomial,subset=train) 42 | glm.probs=predict(glm.fit,Smarket.2005,type="response") 43 | glm.pred=rep("Down",252) 44 | glm.pred[glm.probs>.5]="Up" 45 | table(glm.pred,Direction.2005) 46 | mean(glm.pred==Direction.2005) 47 | 106/(106+76) 48 | predict(glm.fit,newdata=data.frame(Lag1=c(1.2,1.5),Lag2=c(1.1,-0.8)),type="response") 49 | 50 | # Linear Discriminant Analysis 51 | 52 | library(MASS) 53 | lda.fit=lda(Direction~Lag1+Lag2,data=Smarket,subset=train) 54 | lda.fit 55 | plot(lda.fit) 56 | lda.pred=predict(lda.fit, Smarket.2005) 57 | names(lda.pred) 58 | lda.class=lda.pred$class 59 | table(lda.class,Direction.2005) 60 | mean(lda.class==Direction.2005) 61 | sum(lda.pred$posterior[,1]>=.5) 62 | sum(lda.pred$posterior[,1]<.5) 63 | lda.pred$posterior[1:20,1] 64 | lda.class[1:20] 65 | sum(lda.pred$posterior[,1]>.9) 66 | 67 | # Quadratic Discriminant Analysis 68 | 69 | qda.fit=qda(Direction~Lag1+Lag2,data=Smarket,subset=train) 70 | qda.fit 71 | qda.class=predict(qda.fit,Smarket.2005)$class 72 | table(qda.class,Direction.2005) 73 | mean(qda.class==Direction.2005) 74 | 75 | # K-Nearest Neighbors 76 | 77 | library(class) 78 | train.X=cbind(Lag1,Lag2)[train,] 79 | test.X=cbind(Lag1,Lag2)[!train,] 80 | train.Direction=Direction[train] 81 | set.seed(1) 82 | knn.pred=knn(train.X,test.X,train.Direction,k=1) 83 | table(knn.pred,Direction.2005) 84 | (83+43)/252 85 | knn.pred=knn(train.X,test.X,train.Direction,k=3) 86 | table(knn.pred,Direction.2005) 87 | mean(knn.pred==Direction.2005) 88 | 89 | # An Application to Caravan Insurance Data 90 | 91 | dim(Caravan) 92 | attach(Caravan) 93 | summary(Purchase) 94 | 348/5822 95 | standardized.X=scale(Caravan[,-86]) 96 | var(Caravan[,1]) 97 | var(Caravan[,2]) 98 | var(standardized.X[,1]) 99 | var(standardized.X[,2]) 100 | test=1:1000 101 | train.X=standardized.X[-test,] 102 | test.X=standardized.X[test,] 103 | train.Y=Purchase[-test] 104 | test.Y=Purchase[test] 105 | set.seed(1) 106 | knn.pred=knn(train.X,test.X,train.Y,k=1) 107 | mean(test.Y!=knn.pred) 108 | mean(test.Y!="No") 109 | table(knn.pred,test.Y) 110 | 9/(68+9) 111 | knn.pred=knn(train.X,test.X,train.Y,k=3) 112 | table(knn.pred,test.Y) 113 | 5/26 114 | knn.pred=knn(train.X,test.X,train.Y,k=5) 115 | table(knn.pred,test.Y) 116 | 4/15 117 | glm.fit=glm(Purchase~.,data=Caravan,family=binomial,subset=-test) 118 | glm.probs=predict(glm.fit,Caravan[test,],type="response") 119 | glm.pred=rep("No",1000) 120 | glm.pred[glm.probs>.5]="Yes" 121 | table(glm.pred,test.Y) 122 | glm.pred=rep("No",1000) 123 | glm.pred[glm.probs>.25]="Yes" 124 | table(glm.pred,test.Y) 125 | 11/(22+11) -------------------------------------------------------------------------------- /Chapter 5 Lab.txt: -------------------------------------------------------------------------------- 1 | # Chaper 5 Lab: Cross-Validation and the Bootstrap 2 | 3 | # The Validation Set Approach 4 | 5 | library(ISLR) 6 | set.seed(1) 7 | train=sample(392,196) 8 | lm.fit=lm(mpg~horsepower,data=Auto,subset=train) 9 | attach(Auto) 10 | mean((mpg-predict(lm.fit,Auto))[-train]^2) 11 | lm.fit2=lm(mpg~poly(horsepower,2),data=Auto,subset=train) 12 | mean((mpg-predict(lm.fit2,Auto))[-train]^2) 13 | lm.fit3=lm(mpg~poly(horsepower,3),data=Auto,subset=train) 14 | mean((mpg-predict(lm.fit3,Auto))[-train]^2) 15 | set.seed(2) 16 | train=sample(392,196) 17 | lm.fit=lm(mpg~horsepower,subset=train) 18 | mean((mpg-predict(lm.fit,Auto))[-train]^2) 19 | lm.fit2=lm(mpg~poly(horsepower,2),data=Auto,subset=train) 20 | mean((mpg-predict(lm.fit2,Auto))[-train]^2) 21 | lm.fit3=lm(mpg~poly(horsepower,3),data=Auto,subset=train) 22 | mean((mpg-predict(lm.fit3,Auto))[-train]^2) 23 | 24 | # Leave-One-Out Cross-Validation 25 | 26 | glm.fit=glm(mpg~horsepower,data=Auto) 27 | coef(glm.fit) 28 | lm.fit=lm(mpg~horsepower,data=Auto) 29 | coef(lm.fit) 30 | library(boot) 31 | glm.fit=glm(mpg~horsepower,data=Auto) 32 | cv.err=cv.glm(Auto,glm.fit) 33 | cv.err$delta 34 | cv.error=rep(0,5) 35 | for (i in 1:5){ 36 | glm.fit=glm(mpg~poly(horsepower,i),data=Auto) 37 | cv.error[i]=cv.glm(Auto,glm.fit)$delta[1] 38 | } 39 | cv.error 40 | 41 | # k-Fold Cross-Validation 42 | 43 | set.seed(17) 44 | cv.error.10=rep(0,10) 45 | for (i in 1:10){ 46 | glm.fit=glm(mpg~poly(horsepower,i),data=Auto) 47 | cv.error.10[i]=cv.glm(Auto,glm.fit,K=10)$delta[1] 48 | } 49 | cv.error.10 50 | 51 | # The Bootstrap 52 | 53 | alpha.fn=function(data,index){ 54 | X=data$X[index] 55 | Y=data$Y[index] 56 | return((var(Y)-cov(X,Y))/(var(X)+var(Y)-2*cov(X,Y))) 57 | } 58 | alpha.fn(Portfolio,1:100) 59 | set.seed(1) 60 | alpha.fn(Portfolio,sample(100,100,replace=T)) 61 | boot(Portfolio,alpha.fn,R=1000) 62 | 63 | # Estimating the Accuracy of a Linear Regression Model 64 | 65 | boot.fn=function(data,index) 66 | return(coef(lm(mpg~horsepower,data=data,subset=index))) 67 | boot.fn(Auto,1:392) 68 | set.seed(1) 69 | boot.fn(Auto,sample(392,392,replace=T)) 70 | boot.fn(Auto,sample(392,392,replace=T)) 71 | boot(Auto,boot.fn,1000) 72 | summary(lm(mpg~horsepower,data=Auto))$coef 73 | boot.fn=function(data,index) 74 | coefficients(lm(mpg~horsepower+I(horsepower^2),data=data,subset=index)) 75 | set.seed(1) 76 | boot(Auto,boot.fn,1000) 77 | summary(lm(mpg~horsepower+I(horsepower^2),data=Auto))$coef 78 | 79 | -------------------------------------------------------------------------------- /Chapter 6 Labs.txt: -------------------------------------------------------------------------------- 1 | # Chapter 6 Lab 1: Subset Selection Methods 2 | 3 | # Best Subset Selection 4 | 5 | library(ISLR) 6 | fix(Hitters) 7 | names(Hitters) 8 | dim(Hitters) 9 | sum(is.na(Hitters$Salary)) 10 | Hitters=na.omit(Hitters) 11 | dim(Hitters) 12 | sum(is.na(Hitters)) 13 | library(leaps) 14 | regfit.full=regsubsets(Salary~.,Hitters) 15 | summary(regfit.full) 16 | regfit.full=regsubsets(Salary~.,data=Hitters,nvmax=19) 17 | reg.summary=summary(regfit.full) 18 | names(reg.summary) 19 | reg.summary$rsq 20 | par(mfrow=c(2,2)) 21 | plot(reg.summary$rss,xlab="Number of Variables",ylab="RSS",type="l") 22 | plot(reg.summary$adjr2,xlab="Number of Variables",ylab="Adjusted RSq",type="l") 23 | which.max(reg.summary$adjr2) 24 | points(11,reg.summary$adjr2[11], col="red",cex=2,pch=20) 25 | plot(reg.summary$cp,xlab="Number of Variables",ylab="Cp",type='l') 26 | which.min(reg.summary$cp) 27 | points(10,reg.summary$cp[10],col="red",cex=2,pch=20) 28 | which.min(reg.summary$bic) 29 | plot(reg.summary$bic,xlab="Number of Variables",ylab="BIC",type='l') 30 | points(6,reg.summary$bic[6],col="red",cex=2,pch=20) 31 | plot(regfit.full,scale="r2") 32 | plot(regfit.full,scale="adjr2") 33 | plot(regfit.full,scale="Cp") 34 | plot(regfit.full,scale="bic") 35 | coef(regfit.full,6) 36 | 37 | # Forward and Backward Stepwise Selection 38 | 39 | regfit.fwd=regsubsets(Salary~.,data=Hitters,nvmax=19,method="forward") 40 | summary(regfit.fwd) 41 | regfit.bwd=regsubsets(Salary~.,data=Hitters,nvmax=19,method="backward") 42 | summary(regfit.bwd) 43 | coef(regfit.full,7) 44 | coef(regfit.fwd,7) 45 | coef(regfit.bwd,7) 46 | 47 | # Choosing Among Models 48 | 49 | set.seed(1) 50 | train=sample(c(TRUE,FALSE), nrow(Hitters),rep=TRUE) 51 | test=(!train) 52 | regfit.best=regsubsets(Salary~.,data=Hitters[train,],nvmax=19) 53 | test.mat=model.matrix(Salary~.,data=Hitters[test,]) 54 | val.errors=rep(NA,19) 55 | for(i in 1:19){ 56 | coefi=coef(regfit.best,id=i) 57 | pred=test.mat[,names(coefi)]%*%coefi 58 | val.errors[i]=mean((Hitters$Salary[test]-pred)^2) 59 | } 60 | val.errors 61 | which.min(val.errors) 62 | coef(regfit.best,10) 63 | predict.regsubsets=function(object,newdata,id,...){ 64 | form=as.formula(object$call[[2]]) 65 | mat=model.matrix(form,newdata) 66 | coefi=coef(object,id=id) 67 | xvars=names(coefi) 68 | mat[,xvars]%*%coefi 69 | } 70 | regfit.best=regsubsets(Salary~.,data=Hitters,nvmax=19) 71 | coef(regfit.best,10) 72 | k=10 73 | set.seed(1) 74 | folds=sample(1:k,nrow(Hitters),replace=TRUE) 75 | cv.errors=matrix(NA,k,19, dimnames=list(NULL, paste(1:19))) 76 | for(j in 1:k){ 77 | best.fit=regsubsets(Salary~.,data=Hitters[folds!=j,],nvmax=19) 78 | for(i in 1:19){ 79 | pred=predict(best.fit,Hitters[folds==j,],id=i) 80 | cv.errors[j,i]=mean( (Hitters$Salary[folds==j]-pred)^2) 81 | } 82 | } 83 | mean.cv.errors=apply(cv.errors,2,mean) 84 | mean.cv.errors 85 | par(mfrow=c(1,1)) 86 | plot(mean.cv.errors,type='b') 87 | reg.best=regsubsets(Salary~.,data=Hitters, nvmax=19) 88 | coef(reg.best,11) 89 | 90 | 91 | # Chapter 6 Lab 2: Ridge Regression and the Lasso 92 | 93 | x=model.matrix(Salary~.,Hitters)[,-1] 94 | y=Hitters$Salary 95 | 96 | # Ridge Regression 97 | 98 | library(glmnet) 99 | grid=10^seq(10,-2,length=100) 100 | ridge.mod=glmnet(x,y,alpha=0,lambda=grid) 101 | dim(coef(ridge.mod)) 102 | ridge.mod$lambda[50] 103 | coef(ridge.mod)[,50] 104 | sqrt(sum(coef(ridge.mod)[-1,50]^2)) 105 | ridge.mod$lambda[60] 106 | coef(ridge.mod)[,60] 107 | sqrt(sum(coef(ridge.mod)[-1,60]^2)) 108 | predict(ridge.mod,s=50,type="coefficients")[1:20,] 109 | set.seed(1) 110 | train=sample(1:nrow(x), nrow(x)/2) 111 | test=(-train) 112 | y.test=y[test] 113 | ridge.mod=glmnet(x[train,],y[train],alpha=0,lambda=grid, thresh=1e-12) 114 | ridge.pred=predict(ridge.mod,s=4,newx=x[test,]) 115 | mean((ridge.pred-y.test)^2) 116 | mean((mean(y[train])-y.test)^2) 117 | ridge.pred=predict(ridge.mod,s=1e10,newx=x[test,]) 118 | mean((ridge.pred-y.test)^2) 119 | ridge.pred=predict(ridge.mod,s=0,newx=x[test,],exact=T) 120 | mean((ridge.pred-y.test)^2) 121 | lm(y~x, subset=train) 122 | predict(ridge.mod,s=0,exact=T,type="coefficients")[1:20,] 123 | set.seed(1) 124 | cv.out=cv.glmnet(x[train,],y[train],alpha=0) 125 | plot(cv.out) 126 | bestlam=cv.out$lambda.min 127 | bestlam 128 | ridge.pred=predict(ridge.mod,s=bestlam,newx=x[test,]) 129 | mean((ridge.pred-y.test)^2) 130 | out=glmnet(x,y,alpha=0) 131 | predict(out,type="coefficients",s=bestlam)[1:20,] 132 | 133 | # The Lasso 134 | 135 | lasso.mod=glmnet(x[train,],y[train],alpha=1,lambda=grid) 136 | plot(lasso.mod) 137 | set.seed(1) 138 | cv.out=cv.glmnet(x[train,],y[train],alpha=1) 139 | plot(cv.out) 140 | bestlam=cv.out$lambda.min 141 | lasso.pred=predict(lasso.mod,s=bestlam,newx=x[test,]) 142 | mean((lasso.pred-y.test)^2) 143 | out=glmnet(x,y,alpha=1,lambda=grid) 144 | lasso.coef=predict(out,type="coefficients",s=bestlam)[1:20,] 145 | lasso.coef 146 | lasso.coef[lasso.coef!=0] 147 | 148 | 149 | # Chapter 6 Lab 3: PCR and PLS Regression 150 | 151 | # Principal Components Regression 152 | 153 | library(pls) 154 | set.seed(2) 155 | pcr.fit=pcr(Salary~., data=Hitters,scale=TRUE,validation="CV") 156 | summary(pcr.fit) 157 | validationplot(pcr.fit,val.type="MSEP") 158 | set.seed(1) 159 | pcr.fit=pcr(Salary~., data=Hitters,subset=train,scale=TRUE, validation="CV") 160 | validationplot(pcr.fit,val.type="MSEP") 161 | pcr.pred=predict(pcr.fit,x[test,],ncomp=7) 162 | mean((pcr.pred-y.test)^2) 163 | pcr.fit=pcr(y~x,scale=TRUE,ncomp=7) 164 | summary(pcr.fit) 165 | 166 | # Partial Least Squares 167 | 168 | set.seed(1) 169 | pls.fit=plsr(Salary~., data=Hitters,subset=train,scale=TRUE, validation="CV") 170 | summary(pls.fit) 171 | validationplot(pls.fit,val.type="MSEP") 172 | pls.pred=predict(pls.fit,x[test,],ncomp=2) 173 | mean((pls.pred-y.test)^2) 174 | pls.fit=plsr(Salary~., data=Hitters,scale=TRUE,ncomp=2) 175 | summary(pls.fit) 176 | -------------------------------------------------------------------------------- /Chapter 7 Lab.txt: -------------------------------------------------------------------------------- 1 | # Chapter 7 Lab: Non-linear Modeling 2 | 3 | library(ISLR) 4 | attach(Wage) 5 | 6 | # Polynomial Regression and Step Functions 7 | 8 | fit=lm(wage~poly(age,4),data=Wage) 9 | coef(summary(fit)) 10 | fit2=lm(wage~poly(age,4,raw=T),data=Wage) 11 | coef(summary(fit2)) 12 | fit2a=lm(wage~age+I(age^2)+I(age^3)+I(age^4),data=Wage) 13 | coef(fit2a) 14 | fit2b=lm(wage~cbind(age,age^2,age^3,age^4),data=Wage) 15 | agelims=range(age) 16 | age.grid=seq(from=agelims[1],to=agelims[2]) 17 | preds=predict(fit,newdata=list(age=age.grid),se=TRUE) 18 | se.bands=cbind(preds$fit+2*preds$se.fit,preds$fit-2*preds$se.fit) 19 | par(mfrow=c(1,2),mar=c(4.5,4.5,1,1),oma=c(0,0,4,0)) 20 | plot(age,wage,xlim=agelims,cex=.5,col="darkgrey") 21 | title("Degree-4 Polynomial",outer=T) 22 | lines(age.grid,preds$fit,lwd=2,col="blue") 23 | matlines(age.grid,se.bands,lwd=1,col="blue",lty=3) 24 | preds2=predict(fit2,newdata=list(age=age.grid),se=TRUE) 25 | max(abs(preds$fit-preds2$fit)) 26 | fit.1=lm(wage~age,data=Wage) 27 | fit.2=lm(wage~poly(age,2),data=Wage) 28 | fit.3=lm(wage~poly(age,3),data=Wage) 29 | fit.4=lm(wage~poly(age,4),data=Wage) 30 | fit.5=lm(wage~poly(age,5),data=Wage) 31 | anova(fit.1,fit.2,fit.3,fit.4,fit.5) 32 | coef(summary(fit.5)) 33 | (-11.983)^2 34 | fit.1=lm(wage~education+age,data=Wage) 35 | fit.2=lm(wage~education+poly(age,2),data=Wage) 36 | fit.3=lm(wage~education+poly(age,3),data=Wage) 37 | anova(fit.1,fit.2,fit.3) 38 | fit=glm(I(wage>250)~poly(age,4),data=Wage,family=binomial) 39 | preds=predict(fit,newdata=list(age=age.grid),se=T) 40 | pfit=exp(preds$fit)/(1+exp(preds$fit)) 41 | se.bands.logit = cbind(preds$fit+2*preds$se.fit, preds$fit-2*preds$se.fit) 42 | se.bands = exp(se.bands.logit)/(1+exp(se.bands.logit)) 43 | preds=predict(fit,newdata=list(age=age.grid),type="response",se=T) 44 | plot(age,I(wage>250),xlim=agelims,type="n",ylim=c(0,.2)) 45 | points(jitter(age), I((wage>250)/5),cex=.5,pch="|",col="darkgrey") 46 | lines(age.grid,pfit,lwd=2, col="blue") 47 | matlines(age.grid,se.bands,lwd=1,col="blue",lty=3) 48 | table(cut(age,4)) 49 | fit=lm(wage~cut(age,4),data=Wage) 50 | coef(summary(fit)) 51 | 52 | # Splines 53 | 54 | library(splines) 55 | fit=lm(wage~bs(age,knots=c(25,40,60)),data=Wage) 56 | pred=predict(fit,newdata=list(age=age.grid),se=T) 57 | plot(age,wage,col="gray") 58 | lines(age.grid,pred$fit,lwd=2) 59 | lines(age.grid,pred$fit+2*pred$se,lty="dashed") 60 | lines(age.grid,pred$fit-2*pred$se,lty="dashed") 61 | dim(bs(age,knots=c(25,40,60))) 62 | dim(bs(age,df=6)) 63 | attr(bs(age,df=6),"knots") 64 | fit2=lm(wage~ns(age,df=4),data=Wage) 65 | pred2=predict(fit2,newdata=list(age=age.grid),se=T) 66 | lines(age.grid, pred2$fit,col="red",lwd=2) 67 | plot(age,wage,xlim=agelims,cex=.5,col="darkgrey") 68 | title("Smoothing Spline") 69 | fit=smooth.spline(age,wage,df=16) 70 | fit2=smooth.spline(age,wage,cv=TRUE) 71 | fit2$df 72 | lines(fit,col="red",lwd=2) 73 | lines(fit2,col="blue",lwd=2) 74 | legend("topright",legend=c("16 DF","6.8 DF"),col=c("red","blue"),lty=1,lwd=2,cex=.8) 75 | plot(age,wage,xlim=agelims,cex=.5,col="darkgrey") 76 | title("Local Regression") 77 | fit=loess(wage~age,span=.2,data=Wage) 78 | fit2=loess(wage~age,span=.5,data=Wage) 79 | lines(age.grid,predict(fit,data.frame(age=age.grid)),col="red",lwd=2) 80 | lines(age.grid,predict(fit2,data.frame(age=age.grid)),col="blue",lwd=2) 81 | legend("topright",legend=c("Span=0.2","Span=0.5"),col=c("red","blue"),lty=1,lwd=2,cex=.8) 82 | 83 | # GAMs 84 | 85 | gam1=lm(wage~ns(year,4)+ns(age,5)+education,data=Wage) 86 | library(gam) 87 | gam.m3=gam(wage~s(year,4)+s(age,5)+education,data=Wage) 88 | par(mfrow=c(1,3)) 89 | plot(gam.m3, se=TRUE,col="blue") 90 | plot.gam(gam1, se=TRUE, col="red") 91 | gam.m1=gam(wage~s(age,5)+education,data=Wage) 92 | gam.m2=gam(wage~year+s(age,5)+education,data=Wage) 93 | anova(gam.m1,gam.m2,gam.m3,test="F") 94 | summary(gam.m3) 95 | preds=predict(gam.m2,newdata=Wage) 96 | gam.lo=gam(wage~s(year,df=4)+lo(age,span=0.7)+education,data=Wage) 97 | plot.gam(gam.lo, se=TRUE, col="green") 98 | gam.lo.i=gam(wage~lo(year,age,span=0.5)+education,data=Wage) 99 | library(akima) 100 | plot(gam.lo.i) 101 | gam.lr=gam(I(wage>250)~year+s(age,df=5)+education,family=binomial,data=Wage) 102 | par(mfrow=c(1,3)) 103 | plot(gam.lr,se=T,col="green") 104 | table(education,I(wage>250)) 105 | gam.lr.s=gam(I(wage>250)~year+s(age,df=5)+education,family=binomial,data=Wage,subset=(education!="1. < HS Grad")) 106 | plot(gam.lr.s,se=T,col="green") 107 | 108 | -------------------------------------------------------------------------------- /Chapter 8 Lab.txt: -------------------------------------------------------------------------------- 1 | # Chapter 8 Lab: Decision Trees 2 | 3 | # Fitting Classification Trees 4 | 5 | library(tree) 6 | library(ISLR) 7 | attach(Carseats) 8 | High=ifelse(Sales<=8,"No","Yes") 9 | Carseats=data.frame(Carseats,High) 10 | tree.carseats=tree(High~.-Sales,Carseats) 11 | summary(tree.carseats) 12 | plot(tree.carseats) 13 | text(tree.carseats,pretty=0) 14 | tree.carseats 15 | set.seed(2) 16 | train=sample(1:nrow(Carseats), 200) 17 | Carseats.test=Carseats[-train,] 18 | High.test=High[-train] 19 | tree.carseats=tree(High~.-Sales,Carseats,subset=train) 20 | tree.pred=predict(tree.carseats,Carseats.test,type="class") 21 | table(tree.pred,High.test) 22 | (86+57)/200 23 | set.seed(3) 24 | cv.carseats=cv.tree(tree.carseats,FUN=prune.misclass) 25 | names(cv.carseats) 26 | cv.carseats 27 | par(mfrow=c(1,2)) 28 | plot(cv.carseats$size,cv.carseats$dev,type="b") 29 | plot(cv.carseats$k,cv.carseats$dev,type="b") 30 | prune.carseats=prune.misclass(tree.carseats,best=9) 31 | plot(prune.carseats) 32 | text(prune.carseats,pretty=0) 33 | tree.pred=predict(prune.carseats,Carseats.test,type="class") 34 | table(tree.pred,High.test) 35 | (94+60)/200 36 | prune.carseats=prune.misclass(tree.carseats,best=15) 37 | plot(prune.carseats) 38 | text(prune.carseats,pretty=0) 39 | tree.pred=predict(prune.carseats,Carseats.test,type="class") 40 | table(tree.pred,High.test) 41 | (86+62)/200 42 | 43 | # Fitting Regression Trees 44 | 45 | library(MASS) 46 | set.seed(1) 47 | train = sample(1:nrow(Boston), nrow(Boston)/2) 48 | tree.boston=tree(medv~.,Boston,subset=train) 49 | summary(tree.boston) 50 | plot(tree.boston) 51 | text(tree.boston,pretty=0) 52 | cv.boston=cv.tree(tree.boston) 53 | plot(cv.boston$size,cv.boston$dev,type='b') 54 | prune.boston=prune.tree(tree.boston,best=5) 55 | plot(prune.boston) 56 | text(prune.boston,pretty=0) 57 | yhat=predict(tree.boston,newdata=Boston[-train,]) 58 | boston.test=Boston[-train,"medv"] 59 | plot(yhat,boston.test) 60 | abline(0,1) 61 | mean((yhat-boston.test)^2) 62 | 63 | # Bagging and Random Forests 64 | 65 | library(randomForest) 66 | set.seed(1) 67 | bag.boston=randomForest(medv~.,data=Boston,subset=train,mtry=13,importance=TRUE) 68 | bag.boston 69 | yhat.bag = predict(bag.boston,newdata=Boston[-train,]) 70 | plot(yhat.bag, boston.test) 71 | abline(0,1) 72 | mean((yhat.bag-boston.test)^2) 73 | bag.boston=randomForest(medv~.,data=Boston,subset=train,mtry=13,ntree=25) 74 | yhat.bag = predict(bag.boston,newdata=Boston[-train,]) 75 | mean((yhat.bag-boston.test)^2) 76 | set.seed(1) 77 | rf.boston=randomForest(medv~.,data=Boston,subset=train,mtry=6,importance=TRUE) 78 | yhat.rf = predict(rf.boston,newdata=Boston[-train,]) 79 | mean((yhat.rf-boston.test)^2) 80 | importance(rf.boston) 81 | varImpPlot(rf.boston) 82 | 83 | # Boosting 84 | 85 | library(gbm) 86 | set.seed(1) 87 | boost.boston=gbm(medv~.,data=Boston[train,],distribution="gaussian",n.trees=5000,interaction.depth=4) 88 | summary(boost.boston) 89 | par(mfrow=c(1,2)) 90 | plot(boost.boston,i="rm") 91 | plot(boost.boston,i="lstat") 92 | yhat.boost=predict(boost.boston,newdata=Boston[-train,],n.trees=5000) 93 | mean((yhat.boost-boston.test)^2) 94 | boost.boston=gbm(medv~.,data=Boston[train,],distribution="gaussian",n.trees=5000,interaction.depth=4,shrinkage=0.2,verbose=F) 95 | yhat.boost=predict(boost.boston,newdata=Boston[-train,],n.trees=5000) 96 | mean((yhat.boost-boston.test)^2) 97 | 98 | -------------------------------------------------------------------------------- /Chapter 9 Lab.txt: -------------------------------------------------------------------------------- 1 | # Chapter 9 Lab: Support Vector Machines 2 | 3 | # Support Vector Classifier 4 | 5 | set.seed(1) 6 | x=matrix(rnorm(20*2), ncol=2) 7 | y=c(rep(-1,10), rep(1,10)) 8 | x[y==1,]=x[y==1,] + 1 9 | plot(x, col=(3-y)) 10 | dat=data.frame(x=x, y=as.factor(y)) 11 | library(e1071) 12 | svmfit=svm(y~., data=dat, kernel="linear", cost=10,scale=FALSE) 13 | plot(svmfit, dat) 14 | svmfit$index 15 | summary(svmfit) 16 | svmfit=svm(y~., data=dat, kernel="linear", cost=0.1,scale=FALSE) 17 | plot(svmfit, dat) 18 | svmfit$index 19 | set.seed(1) 20 | tune.out=tune(svm,y~.,data=dat,kernel="linear",ranges=list(cost=c(0.001, 0.01, 0.1, 1,5,10,100))) 21 | summary(tune.out) 22 | bestmod=tune.out$best.model 23 | summary(bestmod) 24 | xtest=matrix(rnorm(20*2), ncol=2) 25 | ytest=sample(c(-1,1), 20, rep=TRUE) 26 | xtest[ytest==1,]=xtest[ytest==1,] + 1 27 | testdat=data.frame(x=xtest, y=as.factor(ytest)) 28 | ypred=predict(bestmod,testdat) 29 | table(predict=ypred, truth=testdat$y) 30 | svmfit=svm(y~., data=dat, kernel="linear", cost=.01,scale=FALSE) 31 | ypred=predict(svmfit,testdat) 32 | table(predict=ypred, truth=testdat$y) 33 | x[y==1,]=x[y==1,]+0.5 34 | plot(x, col=(y+5)/2, pch=19) 35 | dat=data.frame(x=x,y=as.factor(y)) 36 | svmfit=svm(y~., data=dat, kernel="linear", cost=1e5) 37 | summary(svmfit) 38 | plot(svmfit, dat) 39 | svmfit=svm(y~., data=dat, kernel="linear", cost=1) 40 | summary(svmfit) 41 | plot(svmfit,dat) 42 | 43 | # Support Vector Machine 44 | 45 | set.seed(1) 46 | x=matrix(rnorm(200*2), ncol=2) 47 | x[1:100,]=x[1:100,]+2 48 | x[101:150,]=x[101:150,]-2 49 | y=c(rep(1,150),rep(2,50)) 50 | dat=data.frame(x=x,y=as.factor(y)) 51 | plot(x, col=y) 52 | train=sample(200,100) 53 | svmfit=svm(y~., data=dat[train,], kernel="radial", gamma=1, cost=1) 54 | plot(svmfit, dat[train,]) 55 | summary(svmfit) 56 | svmfit=svm(y~., data=dat[train,], kernel="radial",gamma=1,cost=1e5) 57 | plot(svmfit,dat[train,]) 58 | set.seed(1) 59 | tune.out=tune(svm, y~., data=dat[train,], kernel="radial", ranges=list(cost=c(0.1,1,10,100,1000),gamma=c(0.5,1,2,3,4))) 60 | summary(tune.out) 61 | table(true=dat[-train,"y"], pred=predict(tune.out$best.model,newx=dat[-train,])) 62 | 63 | # ROC Curves 64 | 65 | library(ROCR) 66 | rocplot=function(pred, truth, ...){ 67 | predob = prediction(pred, truth) 68 | perf = performance(predob, "tpr", "fpr") 69 | plot(perf,...)} 70 | svmfit.opt=svm(y~., data=dat[train,], kernel="radial",gamma=2, cost=1,decision.values=T) 71 | fitted=attributes(predict(svmfit.opt,dat[train,],decision.values=TRUE))$decision.values 72 | par(mfrow=c(1,2)) 73 | rocplot(fitted,dat[train,"y"],main="Training Data") 74 | svmfit.flex=svm(y~., data=dat[train,], kernel="radial",gamma=50, cost=1, decision.values=T) 75 | fitted=attributes(predict(svmfit.flex,dat[train,],decision.values=T))$decision.values 76 | rocplot(fitted,dat[train,"y"],add=T,col="red") 77 | fitted=attributes(predict(svmfit.opt,dat[-train,],decision.values=T))$decision.values 78 | rocplot(fitted,dat[-train,"y"],main="Test Data") 79 | fitted=attributes(predict(svmfit.flex,dat[-train,],decision.values=T))$decision.values 80 | rocplot(fitted,dat[-train,"y"],add=T,col="red") 81 | 82 | # SVM with Multiple Classes 83 | 84 | set.seed(1) 85 | x=rbind(x, matrix(rnorm(50*2), ncol=2)) 86 | y=c(y, rep(0,50)) 87 | x[y==0,2]=x[y==0,2]+2 88 | dat=data.frame(x=x, y=as.factor(y)) 89 | par(mfrow=c(1,1)) 90 | plot(x,col=(y+1)) 91 | svmfit=svm(y~., data=dat, kernel="radial", cost=10, gamma=1) 92 | plot(svmfit, dat) 93 | 94 | # Application to Gene Expression Data 95 | 96 | library(ISLR) 97 | names(Khan) 98 | dim(Khan$xtrain) 99 | dim(Khan$xtest) 100 | length(Khan$ytrain) 101 | length(Khan$ytest) 102 | table(Khan$ytrain) 103 | table(Khan$ytest) 104 | dat=data.frame(x=Khan$xtrain, y=as.factor(Khan$ytrain)) 105 | out=svm(y~., data=dat, kernel="linear",cost=10) 106 | summary(out) 107 | table(out$fitted, dat$y) 108 | dat.te=data.frame(x=Khan$xtest, y=as.factor(Khan$ytest)) 109 | pred.te=predict(out, newdata=dat.te) 110 | table(pred.te, dat.te$y) 111 | 112 | -------------------------------------------------------------------------------- /Credit.csv: -------------------------------------------------------------------------------- 1 | "","Income","Limit","Rating","Cards","Age","Education","Gender","Student","Married","Ethnicity","Balance" 2 | "1",14.891,3606,283,2,34,11," Male","No","Yes","Caucasian",333 3 | "2",106.025,6645,483,3,82,15,"Female","Yes","Yes","Asian",903 4 | "3",104.593,7075,514,4,71,11," Male","No","No","Asian",580 5 | "4",148.924,9504,681,3,36,11,"Female","No","No","Asian",964 6 | "5",55.882,4897,357,2,68,16," Male","No","Yes","Caucasian",331 7 | "6",80.18,8047,569,4,77,10," Male","No","No","Caucasian",1151 8 | "7",20.996,3388,259,2,37,12,"Female","No","No","African American",203 9 | "8",71.408,7114,512,2,87,9," Male","No","No","Asian",872 10 | "9",15.125,3300,266,5,66,13,"Female","No","No","Caucasian",279 11 | "10",71.061,6819,491,3,41,19,"Female","Yes","Yes","African American",1350 12 | "11",63.095,8117,589,4,30,14," Male","No","Yes","Caucasian",1407 13 | "12",15.045,1311,138,3,64,16," Male","No","No","Caucasian",0 14 | "13",80.616,5308,394,1,57,7,"Female","No","Yes","Asian",204 15 | "14",43.682,6922,511,1,49,9," Male","No","Yes","Caucasian",1081 16 | "15",19.144,3291,269,2,75,13,"Female","No","No","African American",148 17 | "16",20.089,2525,200,3,57,15,"Female","No","Yes","African American",0 18 | "17",53.598,3714,286,3,73,17,"Female","No","Yes","African American",0 19 | "18",36.496,4378,339,3,69,15,"Female","No","Yes","Asian",368 20 | "19",49.57,6384,448,1,28,9,"Female","No","Yes","Asian",891 21 | "20",42.079,6626,479,2,44,9," Male","No","No","Asian",1048 22 | "21",17.7,2860,235,4,63,16,"Female","No","No","Asian",89 23 | "22",37.348,6378,458,1,72,17,"Female","No","No","Caucasian",968 24 | "23",20.103,2631,213,3,61,10," Male","No","Yes","African American",0 25 | "24",64.027,5179,398,5,48,8," Male","No","Yes","African American",411 26 | "25",10.742,1757,156,3,57,15,"Female","No","No","Caucasian",0 27 | "26",14.09,4323,326,5,25,16,"Female","No","Yes","African American",671 28 | "27",42.471,3625,289,6,44,12,"Female","Yes","No","Caucasian",654 29 | "28",32.793,4534,333,2,44,16," Male","No","No","African American",467 30 | "29",186.634,13414,949,2,41,14,"Female","No","Yes","African American",1809 31 | "30",26.813,5611,411,4,55,16,"Female","No","No","Caucasian",915 32 | "31",34.142,5666,413,4,47,5,"Female","No","Yes","Caucasian",863 33 | "32",28.941,2733,210,5,43,16," Male","No","Yes","Asian",0 34 | "33",134.181,7838,563,2,48,13,"Female","No","No","Caucasian",526 35 | "34",31.367,1829,162,4,30,10," Male","No","Yes","Caucasian",0 36 | "35",20.15,2646,199,2,25,14,"Female","No","Yes","Asian",0 37 | "36",23.35,2558,220,3,49,12,"Female","Yes","No","Caucasian",419 38 | "37",62.413,6457,455,2,71,11,"Female","No","Yes","Caucasian",762 39 | "38",30.007,6481,462,2,69,9,"Female","No","Yes","Caucasian",1093 40 | "39",11.795,3899,300,4,25,10,"Female","No","No","Caucasian",531 41 | "40",13.647,3461,264,4,47,14," Male","No","Yes","Caucasian",344 42 | "41",34.95,3327,253,3,54,14,"Female","No","No","African American",50 43 | "42",113.659,7659,538,2,66,15," Male","Yes","Yes","African American",1155 44 | "43",44.158,4763,351,2,66,13,"Female","No","Yes","Asian",385 45 | "44",36.929,6257,445,1,24,14,"Female","No","Yes","Asian",976 46 | "45",31.861,6375,469,3,25,16,"Female","No","Yes","Caucasian",1120 47 | "46",77.38,7569,564,3,50,12,"Female","No","Yes","Caucasian",997 48 | "47",19.531,5043,376,2,64,16,"Female","Yes","Yes","Asian",1241 49 | "48",44.646,4431,320,2,49,15," Male","Yes","Yes","Caucasian",797 50 | "49",44.522,2252,205,6,72,15," Male","No","Yes","Asian",0 51 | "50",43.479,4569,354,4,49,13," Male","Yes","Yes","African American",902 52 | "51",36.362,5183,376,3,49,15," Male","No","Yes","African American",654 53 | "52",39.705,3969,301,2,27,20," Male","No","Yes","African American",211 54 | "53",44.205,5441,394,1,32,12," Male","No","Yes","Caucasian",607 55 | "54",16.304,5466,413,4,66,10," Male","No","Yes","Asian",957 56 | "55",15.333,1499,138,2,47,9,"Female","No","Yes","Asian",0 57 | "56",32.916,1786,154,2,60,8,"Female","No","Yes","Asian",0 58 | "57",57.1,4742,372,7,79,18,"Female","No","Yes","Asian",379 59 | "58",76.273,4779,367,4,65,14,"Female","No","Yes","Caucasian",133 60 | "59",10.354,3480,281,2,70,17," Male","No","Yes","Caucasian",333 61 | "60",51.872,5294,390,4,81,17,"Female","No","No","Caucasian",531 62 | "61",35.51,5198,364,2,35,20,"Female","No","No","Asian",631 63 | "62",21.238,3089,254,3,59,10,"Female","No","No","Caucasian",108 64 | "63",30.682,1671,160,2,77,7,"Female","No","No","Caucasian",0 65 | "64",14.132,2998,251,4,75,17," Male","No","No","Caucasian",133 66 | "65",32.164,2937,223,2,79,15,"Female","No","Yes","African American",0 67 | "66",12,4160,320,4,28,14,"Female","No","Yes","Caucasian",602 68 | "67",113.829,9704,694,4,38,13,"Female","No","Yes","Asian",1388 69 | "68",11.187,5099,380,4,69,16,"Female","No","No","African American",889 70 | "69",27.847,5619,418,2,78,15,"Female","No","Yes","Caucasian",822 71 | "70",49.502,6819,505,4,55,14," Male","No","Yes","Caucasian",1084 72 | "71",24.889,3954,318,4,75,12," Male","No","Yes","Caucasian",357 73 | "72",58.781,7402,538,2,81,12,"Female","No","Yes","Asian",1103 74 | "73",22.939,4923,355,1,47,18,"Female","No","Yes","Asian",663 75 | "74",23.989,4523,338,4,31,15," Male","No","No","Caucasian",601 76 | "75",16.103,5390,418,4,45,10,"Female","No","Yes","Caucasian",945 77 | "76",33.017,3180,224,2,28,16," Male","No","Yes","African American",29 78 | "77",30.622,3293,251,1,68,16," Male","Yes","No","Caucasian",532 79 | "78",20.936,3254,253,1,30,15,"Female","No","No","Asian",145 80 | "79",110.968,6662,468,3,45,11,"Female","No","Yes","Caucasian",391 81 | "80",15.354,2101,171,2,65,14," Male","No","No","Asian",0 82 | "81",27.369,3449,288,3,40,9,"Female","No","Yes","Caucasian",162 83 | "82",53.48,4263,317,1,83,15," Male","No","No","Caucasian",99 84 | "83",23.672,4433,344,3,63,11," Male","No","No","Caucasian",503 85 | "84",19.225,1433,122,3,38,14,"Female","No","No","Caucasian",0 86 | "85",43.54,2906,232,4,69,11," Male","No","No","Caucasian",0 87 | "86",152.298,12066,828,4,41,12,"Female","No","Yes","Asian",1779 88 | "87",55.367,6340,448,1,33,15," Male","No","Yes","Caucasian",815 89 | "88",11.741,2271,182,4,59,12,"Female","No","No","Asian",0 90 | "89",15.56,4307,352,4,57,8," Male","No","Yes","African American",579 91 | "90",59.53,7518,543,3,52,9,"Female","No","No","African American",1176 92 | "91",20.191,5767,431,4,42,16," Male","No","Yes","African American",1023 93 | "92",48.498,6040,456,3,47,16," Male","No","Yes","Caucasian",812 94 | "93",30.733,2832,249,4,51,13," Male","No","No","Caucasian",0 95 | "94",16.479,5435,388,2,26,16," Male","No","No","African American",937 96 | "95",38.009,3075,245,3,45,15,"Female","No","No","African American",0 97 | "96",14.084,855,120,5,46,17,"Female","No","Yes","African American",0 98 | "97",14.312,5382,367,1,59,17," Male","Yes","No","Asian",1380 99 | "98",26.067,3388,266,4,74,17,"Female","No","Yes","African American",155 100 | "99",36.295,2963,241,2,68,14,"Female","Yes","No","African American",375 101 | "100",83.851,8494,607,5,47,18," Male","No","No","Caucasian",1311 102 | "101",21.153,3736,256,1,41,11," Male","No","No","Caucasian",298 103 | "102",17.976,2433,190,3,70,16,"Female","Yes","No","Caucasian",431 104 | "103",68.713,7582,531,2,56,16," Male","Yes","No","Caucasian",1587 105 | "104",146.183,9540,682,6,66,15," Male","No","No","Caucasian",1050 106 | "105",15.846,4768,365,4,53,12,"Female","No","No","Caucasian",745 107 | "106",12.031,3182,259,2,58,18,"Female","No","Yes","Caucasian",210 108 | "107",16.819,1337,115,2,74,15," Male","No","Yes","Asian",0 109 | "108",39.11,3189,263,3,72,12," Male","No","No","Asian",0 110 | "109",107.986,6033,449,4,64,14," Male","No","Yes","Caucasian",227 111 | "110",13.561,3261,279,5,37,19," Male","No","Yes","Asian",297 112 | "111",34.537,3271,250,3,57,17,"Female","No","Yes","Asian",47 113 | "112",28.575,2959,231,2,60,11,"Female","No","No","African American",0 114 | "113",46.007,6637,491,4,42,14," Male","No","Yes","Caucasian",1046 115 | "114",69.251,6386,474,4,30,12,"Female","No","Yes","Asian",768 116 | "115",16.482,3326,268,4,41,15," Male","No","No","Caucasian",271 117 | "116",40.442,4828,369,5,81,8,"Female","No","No","African American",510 118 | "117",35.177,2117,186,3,62,16,"Female","No","No","Caucasian",0 119 | "118",91.362,9113,626,1,47,17," Male","No","Yes","Asian",1341 120 | "119",27.039,2161,173,3,40,17,"Female","No","No","Caucasian",0 121 | "120",23.012,1410,137,3,81,16," Male","No","No","Caucasian",0 122 | "121",27.241,1402,128,2,67,15,"Female","No","Yes","Asian",0 123 | "122",148.08,8157,599,2,83,13," Male","No","Yes","Caucasian",454 124 | "123",62.602,7056,481,1,84,11,"Female","No","No","Caucasian",904 125 | "124",11.808,1300,117,3,77,14,"Female","No","No","African American",0 126 | "125",29.564,2529,192,1,30,12,"Female","No","Yes","Caucasian",0 127 | "126",27.578,2531,195,1,34,15,"Female","No","Yes","Caucasian",0 128 | "127",26.427,5533,433,5,50,15,"Female","Yes","Yes","Asian",1404 129 | "128",57.202,3411,259,3,72,11,"Female","No","No","Caucasian",0 130 | "129",123.299,8376,610,2,89,17," Male","Yes","No","African American",1259 131 | "130",18.145,3461,279,3,56,15," Male","No","Yes","African American",255 132 | "131",23.793,3821,281,4,56,12,"Female","Yes","Yes","African American",868 133 | "132",10.726,1568,162,5,46,19," Male","No","Yes","Asian",0 134 | "133",23.283,5443,407,4,49,13," Male","No","Yes","African American",912 135 | "134",21.455,5829,427,4,80,12,"Female","No","Yes","African American",1018 136 | "135",34.664,5835,452,3,77,15,"Female","No","Yes","African American",835 137 | "136",44.473,3500,257,3,81,16,"Female","No","No","African American",8 138 | "137",54.663,4116,314,2,70,8,"Female","No","No","African American",75 139 | "138",36.355,3613,278,4,35,9," Male","No","Yes","Asian",187 140 | "139",21.374,2073,175,2,74,11,"Female","No","Yes","Caucasian",0 141 | "140",107.841,10384,728,3,87,7," Male","No","No","African American",1597 142 | "141",39.831,6045,459,3,32,12,"Female","Yes","Yes","African American",1425 143 | "142",91.876,6754,483,2,33,10," Male","No","Yes","Caucasian",605 144 | "143",103.893,7416,549,3,84,17," Male","No","No","Asian",669 145 | "144",19.636,4896,387,3,64,10,"Female","No","No","African American",710 146 | "145",17.392,2748,228,3,32,14," Male","No","Yes","Caucasian",68 147 | "146",19.529,4673,341,2,51,14," Male","No","No","Asian",642 148 | "147",17.055,5110,371,3,55,15,"Female","No","Yes","Caucasian",805 149 | "148",23.857,1501,150,3,56,16," Male","No","Yes","Caucasian",0 150 | "149",15.184,2420,192,2,69,11,"Female","No","Yes","Caucasian",0 151 | "150",13.444,886,121,5,44,10," Male","No","Yes","Asian",0 152 | "151",63.931,5728,435,3,28,14,"Female","No","Yes","African American",581 153 | "152",35.864,4831,353,3,66,13,"Female","No","Yes","Caucasian",534 154 | "153",41.419,2120,184,4,24,11,"Female","Yes","No","Caucasian",156 155 | "154",92.112,4612,344,3,32,17," Male","No","No","Caucasian",0 156 | "155",55.056,3155,235,2,31,16," Male","No","Yes","African American",0 157 | "156",19.537,1362,143,4,34,9,"Female","No","Yes","Asian",0 158 | "157",31.811,4284,338,5,75,13,"Female","No","Yes","Caucasian",429 159 | "158",56.256,5521,406,2,72,16,"Female","Yes","Yes","Caucasian",1020 160 | "159",42.357,5550,406,2,83,12,"Female","No","Yes","Asian",653 161 | "160",53.319,3000,235,3,53,13," Male","No","No","Asian",0 162 | "161",12.238,4865,381,5,67,11,"Female","No","No","Caucasian",836 163 | "162",31.353,1705,160,3,81,14," Male","No","Yes","Caucasian",0 164 | "163",63.809,7530,515,1,56,12," Male","No","Yes","Caucasian",1086 165 | "164",13.676,2330,203,5,80,16,"Female","No","No","African American",0 166 | "165",76.782,5977,429,4,44,12," Male","No","Yes","Asian",548 167 | "166",25.383,4527,367,4,46,11," Male","No","Yes","Caucasian",570 168 | "167",35.691,2880,214,2,35,15," Male","No","No","African American",0 169 | "168",29.403,2327,178,1,37,14,"Female","No","Yes","Caucasian",0 170 | "169",27.47,2820,219,1,32,11,"Female","No","Yes","Asian",0 171 | "170",27.33,6179,459,4,36,12,"Female","No","Yes","Caucasian",1099 172 | "171",34.772,2021,167,3,57,9," Male","No","No","Asian",0 173 | "172",36.934,4270,299,1,63,9,"Female","No","Yes","Caucasian",283 174 | "173",76.348,4697,344,4,60,18," Male","No","No","Asian",108 175 | "174",14.887,4745,339,3,58,12," Male","No","Yes","African American",724 176 | "175",121.834,10673,750,3,54,16," Male","No","No","African American",1573 177 | "176",30.132,2168,206,3,52,17," Male","No","No","Caucasian",0 178 | "177",24.05,2607,221,4,32,18," Male","No","Yes","Caucasian",0 179 | "178",22.379,3965,292,2,34,14,"Female","No","Yes","Asian",384 180 | "179",28.316,4391,316,2,29,10,"Female","No","No","Caucasian",453 181 | "180",58.026,7499,560,5,67,11,"Female","No","No","Caucasian",1237 182 | "181",10.635,3584,294,5,69,16," Male","No","Yes","Asian",423 183 | "182",46.102,5180,382,3,81,12," Male","No","Yes","African American",516 184 | "183",58.929,6420,459,2,66,9,"Female","No","Yes","African American",789 185 | "184",80.861,4090,335,3,29,15,"Female","No","Yes","Asian",0 186 | "185",158.889,11589,805,1,62,17,"Female","No","Yes","Caucasian",1448 187 | "186",30.42,4442,316,1,30,14,"Female","No","No","African American",450 188 | "187",36.472,3806,309,2,52,13," Male","No","No","African American",188 189 | "188",23.365,2179,167,2,75,15," Male","No","No","Asian",0 190 | "189",83.869,7667,554,2,83,11," Male","No","No","African American",930 191 | "190",58.351,4411,326,2,85,16,"Female","No","Yes","Caucasian",126 192 | "191",55.187,5352,385,4,50,17,"Female","No","Yes","Caucasian",538 193 | "192",124.29,9560,701,3,52,17,"Female","Yes","No","Asian",1687 194 | "193",28.508,3933,287,4,56,14," Male","No","Yes","Asian",336 195 | "194",130.209,10088,730,7,39,19,"Female","No","Yes","Caucasian",1426 196 | "195",30.406,2120,181,2,79,14," Male","No","Yes","African American",0 197 | "196",23.883,5384,398,2,73,16,"Female","No","Yes","African American",802 198 | "197",93.039,7398,517,1,67,12," Male","No","Yes","African American",749 199 | "198",50.699,3977,304,2,84,17,"Female","No","No","African American",69 200 | "199",27.349,2000,169,4,51,16,"Female","No","Yes","African American",0 201 | "200",10.403,4159,310,3,43,7," Male","No","Yes","Asian",571 202 | "201",23.949,5343,383,2,40,18," Male","No","Yes","African American",829 203 | "202",73.914,7333,529,6,67,15,"Female","No","Yes","Caucasian",1048 204 | "203",21.038,1448,145,2,58,13,"Female","No","Yes","Caucasian",0 205 | "204",68.206,6784,499,5,40,16,"Female","Yes","No","African American",1411 206 | "205",57.337,5310,392,2,45,7,"Female","No","No","Caucasian",456 207 | "206",10.793,3878,321,8,29,13," Male","No","No","Caucasian",638 208 | "207",23.45,2450,180,2,78,13," Male","No","No","Caucasian",0 209 | "208",10.842,4391,358,5,37,10,"Female","Yes","Yes","Caucasian",1216 210 | "209",51.345,4327,320,3,46,15," Male","No","No","African American",230 211 | "210",151.947,9156,642,2,91,11,"Female","No","Yes","African American",732 212 | "211",24.543,3206,243,2,62,12,"Female","No","Yes","Caucasian",95 213 | "212",29.567,5309,397,3,25,15," Male","No","No","Caucasian",799 214 | "213",39.145,4351,323,2,66,13," Male","No","Yes","Caucasian",308 215 | "214",39.422,5245,383,2,44,19," Male","No","No","African American",637 216 | "215",34.909,5289,410,2,62,16,"Female","No","Yes","Caucasian",681 217 | "216",41.025,4229,337,3,79,19,"Female","No","Yes","Caucasian",246 218 | "217",15.476,2762,215,3,60,18," Male","No","No","Asian",52 219 | "218",12.456,5395,392,3,65,14," Male","No","Yes","Caucasian",955 220 | "219",10.627,1647,149,2,71,10,"Female","Yes","Yes","Asian",195 221 | "220",38.954,5222,370,4,76,13,"Female","No","No","Caucasian",653 222 | "221",44.847,5765,437,3,53,13,"Female","Yes","No","Asian",1246 223 | "222",98.515,8760,633,5,78,11,"Female","No","No","African American",1230 224 | "223",33.437,6207,451,4,44,9," Male","Yes","No","Caucasian",1549 225 | "224",27.512,4613,344,5,72,17," Male","No","Yes","Asian",573 226 | "225",121.709,7818,584,4,50,6," Male","No","Yes","Caucasian",701 227 | "226",15.079,5673,411,4,28,15,"Female","No","Yes","Asian",1075 228 | "227",59.879,6906,527,6,78,15,"Female","No","No","Caucasian",1032 229 | "228",66.989,5614,430,3,47,14,"Female","No","Yes","Caucasian",482 230 | "229",69.165,4668,341,2,34,11,"Female","No","No","African American",156 231 | "230",69.943,7555,547,3,76,9," Male","No","Yes","Asian",1058 232 | "231",33.214,5137,387,3,59,9," Male","No","No","African American",661 233 | "232",25.124,4776,378,4,29,12," Male","No","Yes","Caucasian",657 234 | "233",15.741,4788,360,1,39,14," Male","No","Yes","Asian",689 235 | "234",11.603,2278,187,3,71,11," Male","No","Yes","Caucasian",0 236 | "235",69.656,8244,579,3,41,14," Male","No","Yes","African American",1329 237 | "236",10.503,2923,232,3,25,18,"Female","No","Yes","African American",191 238 | "237",42.529,4986,369,2,37,11," Male","No","Yes","Asian",489 239 | "238",60.579,5149,388,5,38,15," Male","No","Yes","Asian",443 240 | "239",26.532,2910,236,6,58,19,"Female","No","Yes","Caucasian",52 241 | "240",27.952,3557,263,1,35,13,"Female","No","Yes","Asian",163 242 | "241",29.705,3351,262,5,71,14,"Female","No","Yes","Asian",148 243 | "242",15.602,906,103,2,36,11," Male","No","Yes","African American",0 244 | "243",20.918,1233,128,3,47,18,"Female","Yes","Yes","Asian",16 245 | "244",58.165,6617,460,1,56,12,"Female","No","Yes","Caucasian",856 246 | "245",22.561,1787,147,4,66,15,"Female","No","No","Caucasian",0 247 | "246",34.509,2001,189,5,80,18,"Female","No","Yes","African American",0 248 | "247",19.588,3211,265,4,59,14,"Female","No","No","Asian",199 249 | "248",36.364,2220,188,3,50,19," Male","No","No","Caucasian",0 250 | "249",15.717,905,93,1,38,16," Male","Yes","Yes","Caucasian",0 251 | "250",22.574,1551,134,3,43,13,"Female","Yes","Yes","Caucasian",98 252 | "251",10.363,2430,191,2,47,18,"Female","No","Yes","Asian",0 253 | "252",28.474,3202,267,5,66,12," Male","No","Yes","Caucasian",132 254 | "253",72.945,8603,621,3,64,8,"Female","No","No","Caucasian",1355 255 | "254",85.425,5182,402,6,60,12," Male","No","Yes","African American",218 256 | "255",36.508,6386,469,4,79,6,"Female","No","Yes","Caucasian",1048 257 | "256",58.063,4221,304,3,50,8," Male","No","No","African American",118 258 | "257",25.936,1774,135,2,71,14,"Female","No","No","Asian",0 259 | "258",15.629,2493,186,1,60,14," Male","No","Yes","Asian",0 260 | "259",41.4,2561,215,2,36,14," Male","No","Yes","Caucasian",0 261 | "260",33.657,6196,450,6,55,9,"Female","No","No","Caucasian",1092 262 | "261",67.937,5184,383,4,63,12," Male","No","Yes","Asian",345 263 | "262",180.379,9310,665,3,67,8,"Female","Yes","Yes","Asian",1050 264 | "263",10.588,4049,296,1,66,13,"Female","No","Yes","Caucasian",465 265 | "264",29.725,3536,270,2,52,15,"Female","No","No","African American",133 266 | "265",27.999,5107,380,1,55,10," Male","No","Yes","Caucasian",651 267 | "266",40.885,5013,379,3,46,13,"Female","No","Yes","African American",549 268 | "267",88.83,4952,360,4,86,16,"Female","No","Yes","Caucasian",15 269 | "268",29.638,5833,433,3,29,15,"Female","No","Yes","Asian",942 270 | "269",25.988,1349,142,4,82,12," Male","No","No","Caucasian",0 271 | "270",39.055,5565,410,4,48,18,"Female","No","Yes","Caucasian",772 272 | "271",15.866,3085,217,1,39,13," Male","No","No","Caucasian",136 273 | "272",44.978,4866,347,1,30,10,"Female","No","No","Caucasian",436 274 | "273",30.413,3690,299,2,25,15,"Female","Yes","No","Asian",728 275 | "274",16.751,4706,353,6,48,14," Male","Yes","No","Asian",1255 276 | "275",30.55,5869,439,5,81,9,"Female","No","No","African American",967 277 | "276",163.329,8732,636,3,50,14," Male","No","Yes","Caucasian",529 278 | "277",23.106,3476,257,2,50,15,"Female","No","No","Caucasian",209 279 | "278",41.532,5000,353,2,50,12," Male","No","Yes","Caucasian",531 280 | "279",128.04,6982,518,2,78,11,"Female","No","Yes","Caucasian",250 281 | "280",54.319,3063,248,3,59,8,"Female","Yes","No","Caucasian",269 282 | "281",53.401,5319,377,3,35,12,"Female","No","No","African American",541 283 | "282",36.142,1852,183,3,33,13,"Female","No","No","African American",0 284 | "283",63.534,8100,581,2,50,17,"Female","No","Yes","Caucasian",1298 285 | "284",49.927,6396,485,3,75,17,"Female","No","Yes","Caucasian",890 286 | "285",14.711,2047,167,2,67,6," Male","No","Yes","Caucasian",0 287 | "286",18.967,1626,156,2,41,11,"Female","No","Yes","Asian",0 288 | "287",18.036,1552,142,2,48,15,"Female","No","No","Caucasian",0 289 | "288",60.449,3098,272,4,69,8," Male","No","Yes","Caucasian",0 290 | "289",16.711,5274,387,3,42,16,"Female","No","Yes","Asian",863 291 | "290",10.852,3907,296,2,30,9," Male","No","No","Caucasian",485 292 | "291",26.37,3235,268,5,78,11," Male","No","Yes","Asian",159 293 | "292",24.088,3665,287,4,56,13,"Female","No","Yes","Caucasian",309 294 | "293",51.532,5096,380,2,31,15," Male","No","Yes","Caucasian",481 295 | "294",140.672,11200,817,7,46,9," Male","No","Yes","African American",1677 296 | "295",42.915,2532,205,4,42,13," Male","No","Yes","Asian",0 297 | "296",27.272,1389,149,5,67,10,"Female","No","Yes","Caucasian",0 298 | "297",65.896,5140,370,1,49,17,"Female","No","Yes","Caucasian",293 299 | "298",55.054,4381,321,3,74,17," Male","No","Yes","Asian",188 300 | "299",20.791,2672,204,1,70,18,"Female","No","No","African American",0 301 | "300",24.919,5051,372,3,76,11,"Female","No","Yes","African American",711 302 | "301",21.786,4632,355,1,50,17," Male","No","Yes","Caucasian",580 303 | "302",31.335,3526,289,3,38,7,"Female","No","No","Caucasian",172 304 | "303",59.855,4964,365,1,46,13,"Female","No","Yes","Caucasian",295 305 | "304",44.061,4970,352,1,79,11," Male","No","Yes","African American",414 306 | "305",82.706,7506,536,2,64,13,"Female","No","Yes","Asian",905 307 | "306",24.46,1924,165,2,50,14,"Female","No","Yes","Asian",0 308 | "307",45.12,3762,287,3,80,8," Male","No","Yes","Caucasian",70 309 | "308",75.406,3874,298,3,41,14,"Female","No","Yes","Asian",0 310 | "309",14.956,4640,332,2,33,6," Male","No","No","Asian",681 311 | "310",75.257,7010,494,3,34,18,"Female","No","Yes","Caucasian",885 312 | "311",33.694,4891,369,1,52,16," Male","Yes","No","African American",1036 313 | "312",23.375,5429,396,3,57,15,"Female","No","Yes","Caucasian",844 314 | "313",27.825,5227,386,6,63,11," Male","No","Yes","Caucasian",823 315 | "314",92.386,7685,534,2,75,18,"Female","No","Yes","Asian",843 316 | "315",115.52,9272,656,2,69,14," Male","No","No","African American",1140 317 | "316",14.479,3907,296,3,43,16," Male","No","Yes","Caucasian",463 318 | "317",52.179,7306,522,2,57,14," Male","No","No","Asian",1142 319 | "318",68.462,4712,340,2,71,16," Male","No","Yes","Caucasian",136 320 | "319",18.951,1485,129,3,82,13,"Female","No","No","Caucasian",0 321 | "320",27.59,2586,229,5,54,16," Male","No","Yes","African American",0 322 | "321",16.279,1160,126,3,78,13," Male","Yes","Yes","African American",5 323 | "322",25.078,3096,236,2,27,15,"Female","No","Yes","Caucasian",81 324 | "323",27.229,3484,282,6,51,11," Male","No","No","Caucasian",265 325 | "324",182.728,13913,982,4,98,17," Male","No","Yes","Caucasian",1999 326 | "325",31.029,2863,223,2,66,17," Male","Yes","Yes","Asian",415 327 | "326",17.765,5072,364,1,66,12,"Female","No","Yes","Caucasian",732 328 | "327",125.48,10230,721,3,82,16," Male","No","Yes","Caucasian",1361 329 | "328",49.166,6662,508,3,68,14,"Female","No","No","Asian",984 330 | "329",41.192,3673,297,3,54,16,"Female","No","Yes","Caucasian",121 331 | "330",94.193,7576,527,2,44,16,"Female","No","Yes","Caucasian",846 332 | "331",20.405,4543,329,2,72,17," Male","Yes","No","Asian",1054 333 | "332",12.581,3976,291,2,48,16," Male","No","Yes","Caucasian",474 334 | "333",62.328,5228,377,3,83,15," Male","No","No","Caucasian",380 335 | "334",21.011,3402,261,2,68,17," Male","No","Yes","African American",182 336 | "335",24.23,4756,351,2,64,15,"Female","No","Yes","Caucasian",594 337 | "336",24.314,3409,270,2,23,7,"Female","No","Yes","Caucasian",194 338 | "337",32.856,5884,438,4,68,13," Male","No","No","Caucasian",926 339 | "338",12.414,855,119,3,32,12," Male","No","Yes","African American",0 340 | "339",41.365,5303,377,1,45,14," Male","No","No","Caucasian",606 341 | "340",149.316,10278,707,1,80,16," Male","No","No","African American",1107 342 | "341",27.794,3807,301,4,35,8,"Female","No","Yes","African American",320 343 | "342",13.234,3922,299,2,77,17,"Female","No","Yes","Caucasian",426 344 | "343",14.595,2955,260,5,37,9," Male","No","Yes","African American",204 345 | "344",10.735,3746,280,2,44,17,"Female","No","Yes","Caucasian",410 346 | "345",48.218,5199,401,7,39,10," Male","No","Yes","Asian",633 347 | "346",30.012,1511,137,2,33,17," Male","No","Yes","Caucasian",0 348 | "347",21.551,5380,420,5,51,18," Male","No","Yes","Asian",907 349 | "348",160.231,10748,754,2,69,17," Male","No","No","Caucasian",1192 350 | "349",13.433,1134,112,3,70,14," Male","No","Yes","Caucasian",0 351 | "350",48.577,5145,389,3,71,13,"Female","No","Yes","Asian",503 352 | "351",30.002,1561,155,4,70,13,"Female","No","Yes","Caucasian",0 353 | "352",61.62,5140,374,1,71,9," Male","No","Yes","Caucasian",302 354 | "353",104.483,7140,507,2,41,14," Male","No","Yes","African American",583 355 | "354",41.868,4716,342,2,47,18," Male","No","No","Caucasian",425 356 | "355",12.068,3873,292,1,44,18,"Female","No","Yes","Asian",413 357 | "356",180.682,11966,832,2,58,8,"Female","No","Yes","African American",1405 358 | "357",34.48,6090,442,3,36,14," Male","No","No","Caucasian",962 359 | "358",39.609,2539,188,1,40,14," Male","No","Yes","Asian",0 360 | "359",30.111,4336,339,1,81,18," Male","No","Yes","Caucasian",347 361 | "360",12.335,4471,344,3,79,12," Male","No","Yes","African American",611 362 | "361",53.566,5891,434,4,82,10,"Female","No","No","Caucasian",712 363 | "362",53.217,4943,362,2,46,16,"Female","No","Yes","Asian",382 364 | "363",26.162,5101,382,3,62,19,"Female","No","No","African American",710 365 | "364",64.173,6127,433,1,80,10," Male","No","Yes","Caucasian",578 366 | "365",128.669,9824,685,3,67,16," Male","No","Yes","Asian",1243 367 | "366",113.772,6442,489,4,69,15," Male","Yes","Yes","Caucasian",790 368 | "367",61.069,7871,564,3,56,14," Male","No","Yes","Caucasian",1264 369 | "368",23.793,3615,263,2,70,14," Male","No","No","African American",216 370 | "369",89,5759,440,3,37,6,"Female","No","No","Caucasian",345 371 | "370",71.682,8028,599,3,57,16," Male","No","Yes","Caucasian",1208 372 | "371",35.61,6135,466,4,40,12," Male","No","No","Caucasian",992 373 | "372",39.116,2150,173,4,75,15," Male","No","No","Caucasian",0 374 | "373",19.782,3782,293,2,46,16,"Female","Yes","No","Caucasian",840 375 | "374",55.412,5354,383,2,37,16,"Female","Yes","Yes","Caucasian",1003 376 | "375",29.4,4840,368,3,76,18,"Female","No","Yes","Caucasian",588 377 | "376",20.974,5673,413,5,44,16,"Female","No","Yes","Caucasian",1000 378 | "377",87.625,7167,515,2,46,10,"Female","No","No","African American",767 379 | "378",28.144,1567,142,3,51,10," Male","No","Yes","Caucasian",0 380 | "379",19.349,4941,366,1,33,19," Male","No","Yes","Caucasian",717 381 | "380",53.308,2860,214,1,84,10," Male","No","Yes","Caucasian",0 382 | "381",115.123,7760,538,3,83,14,"Female","No","No","African American",661 383 | "382",101.788,8029,574,2,84,11," Male","No","Yes","Caucasian",849 384 | "383",24.824,5495,409,1,33,9," Male","Yes","No","Caucasian",1352 385 | "384",14.292,3274,282,9,64,9," Male","No","Yes","Caucasian",382 386 | "385",20.088,1870,180,3,76,16," Male","No","No","African American",0 387 | "386",26.4,5640,398,3,58,15,"Female","No","No","Asian",905 388 | "387",19.253,3683,287,4,57,10," Male","No","No","African American",371 389 | "388",16.529,1357,126,3,62,9," Male","No","No","Asian",0 390 | "389",37.878,6827,482,2,80,13,"Female","No","No","Caucasian",1129 391 | "390",83.948,7100,503,2,44,18," Male","No","No","Caucasian",806 392 | "391",135.118,10578,747,3,81,15,"Female","No","Yes","Asian",1393 393 | "392",73.327,6555,472,2,43,15,"Female","No","No","Caucasian",721 394 | "393",25.974,2308,196,2,24,10," Male","No","No","Asian",0 395 | "394",17.316,1335,138,2,65,13," Male","No","No","African American",0 396 | "395",49.794,5758,410,4,40,8," Male","No","No","Caucasian",734 397 | "396",12.096,4100,307,3,32,13," Male","No","Yes","Caucasian",560 398 | "397",13.364,3838,296,5,65,17," Male","No","No","African American",480 399 | "398",57.872,4171,321,5,67,12,"Female","No","Yes","Caucasian",138 400 | "399",37.728,2525,192,1,44,13," Male","No","Yes","Caucasian",0 401 | "400",18.701,5524,415,5,64,7,"Female","No","No","Asian",966 402 | -------------------------------------------------------------------------------- /Heart.csv: -------------------------------------------------------------------------------- 1 | "","Age","Sex","ChestPain","RestBP","Chol","Fbs","RestECG","MaxHR","ExAng","Oldpeak","Slope","Ca","Thal","AHD" 2 | "1",63,1,"typical",145,233,1,2,150,0,2.3,3,0,"fixed","No" 3 | "2",67,1,"asymptomatic",160,286,0,2,108,1,1.5,2,3,"normal","Yes" 4 | "3",67,1,"asymptomatic",120,229,0,2,129,1,2.6,2,2,"reversable","Yes" 5 | "4",37,1,"nonanginal",130,250,0,0,187,0,3.5,3,0,"normal","No" 6 | "5",41,0,"nontypical",130,204,0,2,172,0,1.4,1,0,"normal","No" 7 | "6",56,1,"nontypical",120,236,0,0,178,0,0.8,1,0,"normal","No" 8 | "7",62,0,"asymptomatic",140,268,0,2,160,0,3.6,3,2,"normal","Yes" 9 | "8",57,0,"asymptomatic",120,354,0,0,163,1,0.6,1,0,"normal","No" 10 | "9",63,1,"asymptomatic",130,254,0,2,147,0,1.4,2,1,"reversable","Yes" 11 | "10",53,1,"asymptomatic",140,203,1,2,155,1,3.1,3,0,"reversable","Yes" 12 | "11",57,1,"asymptomatic",140,192,0,0,148,0,0.4,2,0,"fixed","No" 13 | "12",56,0,"nontypical",140,294,0,2,153,0,1.3,2,0,"normal","No" 14 | "13",56,1,"nonanginal",130,256,1,2,142,1,0.6,2,1,"fixed","Yes" 15 | "14",44,1,"nontypical",120,263,0,0,173,0,0,1,0,"reversable","No" 16 | "15",52,1,"nonanginal",172,199,1,0,162,0,0.5,1,0,"reversable","No" 17 | "16",57,1,"nonanginal",150,168,0,0,174,0,1.6,1,0,"normal","No" 18 | "17",48,1,"nontypical",110,229,0,0,168,0,1,3,0,"reversable","Yes" 19 | "18",54,1,"asymptomatic",140,239,0,0,160,0,1.2,1,0,"normal","No" 20 | "19",48,0,"nonanginal",130,275,0,0,139,0,0.2,1,0,"normal","No" 21 | "20",49,1,"nontypical",130,266,0,0,171,0,0.6,1,0,"normal","No" 22 | "21",64,1,"typical",110,211,0,2,144,1,1.8,2,0,"normal","No" 23 | "22",58,0,"typical",150,283,1,2,162,0,1,1,0,"normal","No" 24 | "23",58,1,"nontypical",120,284,0,2,160,0,1.8,2,0,"normal","Yes" 25 | "24",58,1,"nonanginal",132,224,0,2,173,0,3.2,1,2,"reversable","Yes" 26 | "25",60,1,"asymptomatic",130,206,0,2,132,1,2.4,2,2,"reversable","Yes" 27 | "26",50,0,"nonanginal",120,219,0,0,158,0,1.6,2,0,"normal","No" 28 | "27",58,0,"nonanginal",120,340,0,0,172,0,0,1,0,"normal","No" 29 | "28",66,0,"typical",150,226,0,0,114,0,2.6,3,0,"normal","No" 30 | "29",43,1,"asymptomatic",150,247,0,0,171,0,1.5,1,0,"normal","No" 31 | "30",40,1,"asymptomatic",110,167,0,2,114,1,2,2,0,"reversable","Yes" 32 | "31",69,0,"typical",140,239,0,0,151,0,1.8,1,2,"normal","No" 33 | "32",60,1,"asymptomatic",117,230,1,0,160,1,1.4,1,2,"reversable","Yes" 34 | "33",64,1,"nonanginal",140,335,0,0,158,0,0,1,0,"normal","Yes" 35 | "34",59,1,"asymptomatic",135,234,0,0,161,0,0.5,2,0,"reversable","No" 36 | "35",44,1,"nonanginal",130,233,0,0,179,1,0.4,1,0,"normal","No" 37 | "36",42,1,"asymptomatic",140,226,0,0,178,0,0,1,0,"normal","No" 38 | "37",43,1,"asymptomatic",120,177,0,2,120,1,2.5,2,0,"reversable","Yes" 39 | "38",57,1,"asymptomatic",150,276,0,2,112,1,0.6,2,1,"fixed","Yes" 40 | "39",55,1,"asymptomatic",132,353,0,0,132,1,1.2,2,1,"reversable","Yes" 41 | "40",61,1,"nonanginal",150,243,1,0,137,1,1,2,0,"normal","No" 42 | "41",65,0,"asymptomatic",150,225,0,2,114,0,1,2,3,"reversable","Yes" 43 | "42",40,1,"typical",140,199,0,0,178,1,1.4,1,0,"reversable","No" 44 | "43",71,0,"nontypical",160,302,0,0,162,0,0.4,1,2,"normal","No" 45 | "44",59,1,"nonanginal",150,212,1,0,157,0,1.6,1,0,"normal","No" 46 | "45",61,0,"asymptomatic",130,330,0,2,169,0,0,1,0,"normal","Yes" 47 | "46",58,1,"nonanginal",112,230,0,2,165,0,2.5,2,1,"reversable","Yes" 48 | "47",51,1,"nonanginal",110,175,0,0,123,0,0.6,1,0,"normal","No" 49 | "48",50,1,"asymptomatic",150,243,0,2,128,0,2.6,2,0,"reversable","Yes" 50 | "49",65,0,"nonanginal",140,417,1,2,157,0,0.8,1,1,"normal","No" 51 | "50",53,1,"nonanginal",130,197,1,2,152,0,1.2,3,0,"normal","No" 52 | "51",41,0,"nontypical",105,198,0,0,168,0,0,1,1,"normal","No" 53 | "52",65,1,"asymptomatic",120,177,0,0,140,0,0.4,1,0,"reversable","No" 54 | "53",44,1,"asymptomatic",112,290,0,2,153,0,0,1,1,"normal","Yes" 55 | "54",44,1,"nontypical",130,219,0,2,188,0,0,1,0,"normal","No" 56 | "55",60,1,"asymptomatic",130,253,0,0,144,1,1.4,1,1,"reversable","Yes" 57 | "56",54,1,"asymptomatic",124,266,0,2,109,1,2.2,2,1,"reversable","Yes" 58 | "57",50,1,"nonanginal",140,233,0,0,163,0,0.6,2,1,"reversable","Yes" 59 | "58",41,1,"asymptomatic",110,172,0,2,158,0,0,1,0,"reversable","Yes" 60 | "59",54,1,"nonanginal",125,273,0,2,152,0,0.5,3,1,"normal","No" 61 | "60",51,1,"typical",125,213,0,2,125,1,1.4,1,1,"normal","No" 62 | "61",51,0,"asymptomatic",130,305,0,0,142,1,1.2,2,0,"reversable","Yes" 63 | "62",46,0,"nonanginal",142,177,0,2,160,1,1.4,3,0,"normal","No" 64 | "63",58,1,"asymptomatic",128,216,0,2,131,1,2.2,2,3,"reversable","Yes" 65 | "64",54,0,"nonanginal",135,304,1,0,170,0,0,1,0,"normal","No" 66 | "65",54,1,"asymptomatic",120,188,0,0,113,0,1.4,2,1,"reversable","Yes" 67 | "66",60,1,"asymptomatic",145,282,0,2,142,1,2.8,2,2,"reversable","Yes" 68 | "67",60,1,"nonanginal",140,185,0,2,155,0,3,2,0,"normal","Yes" 69 | "68",54,1,"nonanginal",150,232,0,2,165,0,1.6,1,0,"reversable","No" 70 | "69",59,1,"asymptomatic",170,326,0,2,140,1,3.4,3,0,"reversable","Yes" 71 | "70",46,1,"nonanginal",150,231,0,0,147,0,3.6,2,0,"normal","Yes" 72 | "71",65,0,"nonanginal",155,269,0,0,148,0,0.8,1,0,"normal","No" 73 | "72",67,1,"asymptomatic",125,254,1,0,163,0,0.2,2,2,"reversable","Yes" 74 | "73",62,1,"asymptomatic",120,267,0,0,99,1,1.8,2,2,"reversable","Yes" 75 | "74",65,1,"asymptomatic",110,248,0,2,158,0,0.6,1,2,"fixed","Yes" 76 | "75",44,1,"asymptomatic",110,197,0,2,177,0,0,1,1,"normal","Yes" 77 | "76",65,0,"nonanginal",160,360,0,2,151,0,0.8,1,0,"normal","No" 78 | "77",60,1,"asymptomatic",125,258,0,2,141,1,2.8,2,1,"reversable","Yes" 79 | "78",51,0,"nonanginal",140,308,0,2,142,0,1.5,1,1,"normal","No" 80 | "79",48,1,"nontypical",130,245,0,2,180,0,0.2,2,0,"normal","No" 81 | "80",58,1,"asymptomatic",150,270,0,2,111,1,0.8,1,0,"reversable","Yes" 82 | "81",45,1,"asymptomatic",104,208,0,2,148,1,3,2,0,"normal","No" 83 | "82",53,0,"asymptomatic",130,264,0,2,143,0,0.4,2,0,"normal","No" 84 | "83",39,1,"nonanginal",140,321,0,2,182,0,0,1,0,"normal","No" 85 | "84",68,1,"nonanginal",180,274,1,2,150,1,1.6,2,0,"reversable","Yes" 86 | "85",52,1,"nontypical",120,325,0,0,172,0,0.2,1,0,"normal","No" 87 | "86",44,1,"nonanginal",140,235,0,2,180,0,0,1,0,"normal","No" 88 | "87",47,1,"nonanginal",138,257,0,2,156,0,0,1,0,"normal","No" 89 | "88",53,0,"nonanginal",128,216,0,2,115,0,0,1,0,NA,"No" 90 | "89",53,0,"asymptomatic",138,234,0,2,160,0,0,1,0,"normal","No" 91 | "90",51,0,"nonanginal",130,256,0,2,149,0,0.5,1,0,"normal","No" 92 | "91",66,1,"asymptomatic",120,302,0,2,151,0,0.4,2,0,"normal","No" 93 | "92",62,0,"asymptomatic",160,164,0,2,145,0,6.2,3,3,"reversable","Yes" 94 | "93",62,1,"nonanginal",130,231,0,0,146,0,1.8,2,3,"reversable","No" 95 | "94",44,0,"nonanginal",108,141,0,0,175,0,0.6,2,0,"normal","No" 96 | "95",63,0,"nonanginal",135,252,0,2,172,0,0,1,0,"normal","No" 97 | "96",52,1,"asymptomatic",128,255,0,0,161,1,0,1,1,"reversable","Yes" 98 | "97",59,1,"asymptomatic",110,239,0,2,142,1,1.2,2,1,"reversable","Yes" 99 | "98",60,0,"asymptomatic",150,258,0,2,157,0,2.6,2,2,"reversable","Yes" 100 | "99",52,1,"nontypical",134,201,0,0,158,0,0.8,1,1,"normal","No" 101 | "100",48,1,"asymptomatic",122,222,0,2,186,0,0,1,0,"normal","No" 102 | "101",45,1,"asymptomatic",115,260,0,2,185,0,0,1,0,"normal","No" 103 | "102",34,1,"typical",118,182,0,2,174,0,0,1,0,"normal","No" 104 | "103",57,0,"asymptomatic",128,303,0,2,159,0,0,1,1,"normal","No" 105 | "104",71,0,"nonanginal",110,265,1,2,130,0,0,1,1,"normal","No" 106 | "105",49,1,"nonanginal",120,188,0,0,139,0,2,2,3,"reversable","Yes" 107 | "106",54,1,"nontypical",108,309,0,0,156,0,0,1,0,"reversable","No" 108 | "107",59,1,"asymptomatic",140,177,0,0,162,1,0,1,1,"reversable","Yes" 109 | "108",57,1,"nonanginal",128,229,0,2,150,0,0.4,2,1,"reversable","Yes" 110 | "109",61,1,"asymptomatic",120,260,0,0,140,1,3.6,2,1,"reversable","Yes" 111 | "110",39,1,"asymptomatic",118,219,0,0,140,0,1.2,2,0,"reversable","Yes" 112 | "111",61,0,"asymptomatic",145,307,0,2,146,1,1,2,0,"reversable","Yes" 113 | "112",56,1,"asymptomatic",125,249,1,2,144,1,1.2,2,1,"normal","Yes" 114 | "113",52,1,"typical",118,186,0,2,190,0,0,2,0,"fixed","No" 115 | "114",43,0,"asymptomatic",132,341,1,2,136,1,3,2,0,"reversable","Yes" 116 | "115",62,0,"nonanginal",130,263,0,0,97,0,1.2,2,1,"reversable","Yes" 117 | "116",41,1,"nontypical",135,203,0,0,132,0,0,2,0,"fixed","No" 118 | "117",58,1,"nonanginal",140,211,1,2,165,0,0,1,0,"normal","No" 119 | "118",35,0,"asymptomatic",138,183,0,0,182,0,1.4,1,0,"normal","No" 120 | "119",63,1,"asymptomatic",130,330,1,2,132,1,1.8,1,3,"reversable","Yes" 121 | "120",65,1,"asymptomatic",135,254,0,2,127,0,2.8,2,1,"reversable","Yes" 122 | "121",48,1,"asymptomatic",130,256,1,2,150,1,0,1,2,"reversable","Yes" 123 | "122",63,0,"asymptomatic",150,407,0,2,154,0,4,2,3,"reversable","Yes" 124 | "123",51,1,"nonanginal",100,222,0,0,143,1,1.2,2,0,"normal","No" 125 | "124",55,1,"asymptomatic",140,217,0,0,111,1,5.6,3,0,"reversable","Yes" 126 | "125",65,1,"typical",138,282,1,2,174,0,1.4,2,1,"normal","Yes" 127 | "126",45,0,"nontypical",130,234,0,2,175,0,0.6,2,0,"normal","No" 128 | "127",56,0,"asymptomatic",200,288,1,2,133,1,4,3,2,"reversable","Yes" 129 | "128",54,1,"asymptomatic",110,239,0,0,126,1,2.8,2,1,"reversable","Yes" 130 | "129",44,1,"nontypical",120,220,0,0,170,0,0,1,0,"normal","No" 131 | "130",62,0,"asymptomatic",124,209,0,0,163,0,0,1,0,"normal","No" 132 | "131",54,1,"nonanginal",120,258,0,2,147,0,0.4,2,0,"reversable","No" 133 | "132",51,1,"nonanginal",94,227,0,0,154,1,0,1,1,"reversable","No" 134 | "133",29,1,"nontypical",130,204,0,2,202,0,0,1,0,"normal","No" 135 | "134",51,1,"asymptomatic",140,261,0,2,186,1,0,1,0,"normal","No" 136 | "135",43,0,"nonanginal",122,213,0,0,165,0,0.2,2,0,"normal","No" 137 | "136",55,0,"nontypical",135,250,0,2,161,0,1.4,2,0,"normal","No" 138 | "137",70,1,"asymptomatic",145,174,0,0,125,1,2.6,3,0,"reversable","Yes" 139 | "138",62,1,"nontypical",120,281,0,2,103,0,1.4,2,1,"reversable","Yes" 140 | "139",35,1,"asymptomatic",120,198,0,0,130,1,1.6,2,0,"reversable","Yes" 141 | "140",51,1,"nonanginal",125,245,1,2,166,0,2.4,2,0,"normal","No" 142 | "141",59,1,"nontypical",140,221,0,0,164,1,0,1,0,"normal","No" 143 | "142",59,1,"typical",170,288,0,2,159,0,0.2,2,0,"reversable","Yes" 144 | "143",52,1,"nontypical",128,205,1,0,184,0,0,1,0,"normal","No" 145 | "144",64,1,"nonanginal",125,309,0,0,131,1,1.8,2,0,"reversable","Yes" 146 | "145",58,1,"nonanginal",105,240,0,2,154,1,0.6,2,0,"reversable","No" 147 | "146",47,1,"nonanginal",108,243,0,0,152,0,0,1,0,"normal","Yes" 148 | "147",57,1,"asymptomatic",165,289,1,2,124,0,1,2,3,"reversable","Yes" 149 | "148",41,1,"nonanginal",112,250,0,0,179,0,0,1,0,"normal","No" 150 | "149",45,1,"nontypical",128,308,0,2,170,0,0,1,0,"normal","No" 151 | "150",60,0,"nonanginal",102,318,0,0,160,0,0,1,1,"normal","No" 152 | "151",52,1,"typical",152,298,1,0,178,0,1.2,2,0,"reversable","No" 153 | "152",42,0,"asymptomatic",102,265,0,2,122,0,0.6,2,0,"normal","No" 154 | "153",67,0,"nonanginal",115,564,0,2,160,0,1.6,2,0,"reversable","No" 155 | "154",55,1,"asymptomatic",160,289,0,2,145,1,0.8,2,1,"reversable","Yes" 156 | "155",64,1,"asymptomatic",120,246,0,2,96,1,2.2,3,1,"normal","Yes" 157 | "156",70,1,"asymptomatic",130,322,0,2,109,0,2.4,2,3,"normal","Yes" 158 | "157",51,1,"asymptomatic",140,299,0,0,173,1,1.6,1,0,"reversable","Yes" 159 | "158",58,1,"asymptomatic",125,300,0,2,171,0,0,1,2,"reversable","Yes" 160 | "159",60,1,"asymptomatic",140,293,0,2,170,0,1.2,2,2,"reversable","Yes" 161 | "160",68,1,"nonanginal",118,277,0,0,151,0,1,1,1,"reversable","No" 162 | "161",46,1,"nontypical",101,197,1,0,156,0,0,1,0,"reversable","No" 163 | "162",77,1,"asymptomatic",125,304,0,2,162,1,0,1,3,"normal","Yes" 164 | "163",54,0,"nonanginal",110,214,0,0,158,0,1.6,2,0,"normal","No" 165 | "164",58,0,"asymptomatic",100,248,0,2,122,0,1,2,0,"normal","No" 166 | "165",48,1,"nonanginal",124,255,1,0,175,0,0,1,2,"normal","No" 167 | "166",57,1,"asymptomatic",132,207,0,0,168,1,0,1,0,"reversable","No" 168 | "167",52,1,"nonanginal",138,223,0,0,169,0,0,1,NA,"normal","No" 169 | "168",54,0,"nontypical",132,288,1,2,159,1,0,1,1,"normal","No" 170 | "169",35,1,"asymptomatic",126,282,0,2,156,1,0,1,0,"reversable","Yes" 171 | "170",45,0,"nontypical",112,160,0,0,138,0,0,2,0,"normal","No" 172 | "171",70,1,"nonanginal",160,269,0,0,112,1,2.9,2,1,"reversable","Yes" 173 | "172",53,1,"asymptomatic",142,226,0,2,111,1,0,1,0,"reversable","No" 174 | "173",59,0,"asymptomatic",174,249,0,0,143,1,0,2,0,"normal","Yes" 175 | "174",62,0,"asymptomatic",140,394,0,2,157,0,1.2,2,0,"normal","No" 176 | "175",64,1,"asymptomatic",145,212,0,2,132,0,2,2,2,"fixed","Yes" 177 | "176",57,1,"asymptomatic",152,274,0,0,88,1,1.2,2,1,"reversable","Yes" 178 | "177",52,1,"asymptomatic",108,233,1,0,147,0,0.1,1,3,"reversable","No" 179 | "178",56,1,"asymptomatic",132,184,0,2,105,1,2.1,2,1,"fixed","Yes" 180 | "179",43,1,"nonanginal",130,315,0,0,162,0,1.9,1,1,"normal","No" 181 | "180",53,1,"nonanginal",130,246,1,2,173,0,0,1,3,"normal","No" 182 | "181",48,1,"asymptomatic",124,274,0,2,166,0,0.5,2,0,"reversable","Yes" 183 | "182",56,0,"asymptomatic",134,409,0,2,150,1,1.9,2,2,"reversable","Yes" 184 | "183",42,1,"typical",148,244,0,2,178,0,0.8,1,2,"normal","No" 185 | "184",59,1,"typical",178,270,0,2,145,0,4.2,3,0,"reversable","No" 186 | "185",60,0,"asymptomatic",158,305,0,2,161,0,0,1,0,"normal","Yes" 187 | "186",63,0,"nontypical",140,195,0,0,179,0,0,1,2,"normal","No" 188 | "187",42,1,"nonanginal",120,240,1,0,194,0,0.8,3,0,"reversable","No" 189 | "188",66,1,"nontypical",160,246,0,0,120,1,0,2,3,"fixed","Yes" 190 | "189",54,1,"nontypical",192,283,0,2,195,0,0,1,1,"reversable","Yes" 191 | "190",69,1,"nonanginal",140,254,0,2,146,0,2,2,3,"reversable","Yes" 192 | "191",50,1,"nonanginal",129,196,0,0,163,0,0,1,0,"normal","No" 193 | "192",51,1,"asymptomatic",140,298,0,0,122,1,4.2,2,3,"reversable","Yes" 194 | "193",43,1,"asymptomatic",132,247,1,2,143,1,0.1,2,NA,"reversable","Yes" 195 | "194",62,0,"asymptomatic",138,294,1,0,106,0,1.9,2,3,"normal","Yes" 196 | "195",68,0,"nonanginal",120,211,0,2,115,0,1.5,2,0,"normal","No" 197 | "196",67,1,"asymptomatic",100,299,0,2,125,1,0.9,2,2,"normal","Yes" 198 | "197",69,1,"typical",160,234,1,2,131,0,0.1,2,1,"normal","No" 199 | "198",45,0,"asymptomatic",138,236,0,2,152,1,0.2,2,0,"normal","No" 200 | "199",50,0,"nontypical",120,244,0,0,162,0,1.1,1,0,"normal","No" 201 | "200",59,1,"typical",160,273,0,2,125,0,0,1,0,"normal","Yes" 202 | "201",50,0,"asymptomatic",110,254,0,2,159,0,0,1,0,"normal","No" 203 | "202",64,0,"asymptomatic",180,325,0,0,154,1,0,1,0,"normal","No" 204 | "203",57,1,"nonanginal",150,126,1,0,173,0,0.2,1,1,"reversable","No" 205 | "204",64,0,"nonanginal",140,313,0,0,133,0,0.2,1,0,"reversable","No" 206 | "205",43,1,"asymptomatic",110,211,0,0,161,0,0,1,0,"reversable","No" 207 | "206",45,1,"asymptomatic",142,309,0,2,147,1,0,2,3,"reversable","Yes" 208 | "207",58,1,"asymptomatic",128,259,0,2,130,1,3,2,2,"reversable","Yes" 209 | "208",50,1,"asymptomatic",144,200,0,2,126,1,0.9,2,0,"reversable","Yes" 210 | "209",55,1,"nontypical",130,262,0,0,155,0,0,1,0,"normal","No" 211 | "210",62,0,"asymptomatic",150,244,0,0,154,1,1.4,2,0,"normal","Yes" 212 | "211",37,0,"nonanginal",120,215,0,0,170,0,0,1,0,"normal","No" 213 | "212",38,1,"typical",120,231,0,0,182,1,3.8,2,0,"reversable","Yes" 214 | "213",41,1,"nonanginal",130,214,0,2,168,0,2,2,0,"normal","No" 215 | "214",66,0,"asymptomatic",178,228,1,0,165,1,1,2,2,"reversable","Yes" 216 | "215",52,1,"asymptomatic",112,230,0,0,160,0,0,1,1,"normal","Yes" 217 | "216",56,1,"typical",120,193,0,2,162,0,1.9,2,0,"reversable","No" 218 | "217",46,0,"nontypical",105,204,0,0,172,0,0,1,0,"normal","No" 219 | "218",46,0,"asymptomatic",138,243,0,2,152,1,0,2,0,"normal","No" 220 | "219",64,0,"asymptomatic",130,303,0,0,122,0,2,2,2,"normal","No" 221 | "220",59,1,"asymptomatic",138,271,0,2,182,0,0,1,0,"normal","No" 222 | "221",41,0,"nonanginal",112,268,0,2,172,1,0,1,0,"normal","No" 223 | "222",54,0,"nonanginal",108,267,0,2,167,0,0,1,0,"normal","No" 224 | "223",39,0,"nonanginal",94,199,0,0,179,0,0,1,0,"normal","No" 225 | "224",53,1,"asymptomatic",123,282,0,0,95,1,2,2,2,"reversable","Yes" 226 | "225",63,0,"asymptomatic",108,269,0,0,169,1,1.8,2,2,"normal","Yes" 227 | "226",34,0,"nontypical",118,210,0,0,192,0,0.7,1,0,"normal","No" 228 | "227",47,1,"asymptomatic",112,204,0,0,143,0,0.1,1,0,"normal","No" 229 | "228",67,0,"nonanginal",152,277,0,0,172,0,0,1,1,"normal","No" 230 | "229",54,1,"asymptomatic",110,206,0,2,108,1,0,2,1,"normal","Yes" 231 | "230",66,1,"asymptomatic",112,212,0,2,132,1,0.1,1,1,"normal","Yes" 232 | "231",52,0,"nonanginal",136,196,0,2,169,0,0.1,2,0,"normal","No" 233 | "232",55,0,"asymptomatic",180,327,0,1,117,1,3.4,2,0,"normal","Yes" 234 | "233",49,1,"nonanginal",118,149,0,2,126,0,0.8,1,3,"normal","Yes" 235 | "234",74,0,"nontypical",120,269,0,2,121,1,0.2,1,1,"normal","No" 236 | "235",54,0,"nonanginal",160,201,0,0,163,0,0,1,1,"normal","No" 237 | "236",54,1,"asymptomatic",122,286,0,2,116,1,3.2,2,2,"normal","Yes" 238 | "237",56,1,"asymptomatic",130,283,1,2,103,1,1.6,3,0,"reversable","Yes" 239 | "238",46,1,"asymptomatic",120,249,0,2,144,0,0.8,1,0,"reversable","Yes" 240 | "239",49,0,"nontypical",134,271,0,0,162,0,0,2,0,"normal","No" 241 | "240",42,1,"nontypical",120,295,0,0,162,0,0,1,0,"normal","No" 242 | "241",41,1,"nontypical",110,235,0,0,153,0,0,1,0,"normal","No" 243 | "242",41,0,"nontypical",126,306,0,0,163,0,0,1,0,"normal","No" 244 | "243",49,0,"asymptomatic",130,269,0,0,163,0,0,1,0,"normal","No" 245 | "244",61,1,"typical",134,234,0,0,145,0,2.6,2,2,"normal","Yes" 246 | "245",60,0,"nonanginal",120,178,1,0,96,0,0,1,0,"normal","No" 247 | "246",67,1,"asymptomatic",120,237,0,0,71,0,1,2,0,"normal","Yes" 248 | "247",58,1,"asymptomatic",100,234,0,0,156,0,0.1,1,1,"reversable","Yes" 249 | "248",47,1,"asymptomatic",110,275,0,2,118,1,1,2,1,"normal","Yes" 250 | "249",52,1,"asymptomatic",125,212,0,0,168,0,1,1,2,"reversable","Yes" 251 | "250",62,1,"nontypical",128,208,1,2,140,0,0,1,0,"normal","No" 252 | "251",57,1,"asymptomatic",110,201,0,0,126,1,1.5,2,0,"fixed","No" 253 | "252",58,1,"asymptomatic",146,218,0,0,105,0,2,2,1,"reversable","Yes" 254 | "253",64,1,"asymptomatic",128,263,0,0,105,1,0.2,2,1,"reversable","No" 255 | "254",51,0,"nonanginal",120,295,0,2,157,0,0.6,1,0,"normal","No" 256 | "255",43,1,"asymptomatic",115,303,0,0,181,0,1.2,2,0,"normal","No" 257 | "256",42,0,"nonanginal",120,209,0,0,173,0,0,2,0,"normal","No" 258 | "257",67,0,"asymptomatic",106,223,0,0,142,0,0.3,1,2,"normal","No" 259 | "258",76,0,"nonanginal",140,197,0,1,116,0,1.1,2,0,"normal","No" 260 | "259",70,1,"nontypical",156,245,0,2,143,0,0,1,0,"normal","No" 261 | "260",57,1,"nontypical",124,261,0,0,141,0,0.3,1,0,"reversable","Yes" 262 | "261",44,0,"nonanginal",118,242,0,0,149,0,0.3,2,1,"normal","No" 263 | "262",58,0,"nontypical",136,319,1,2,152,0,0,1,2,"normal","Yes" 264 | "263",60,0,"typical",150,240,0,0,171,0,0.9,1,0,"normal","No" 265 | "264",44,1,"nonanginal",120,226,0,0,169,0,0,1,0,"normal","No" 266 | "265",61,1,"asymptomatic",138,166,0,2,125,1,3.6,2,1,"normal","Yes" 267 | "266",42,1,"asymptomatic",136,315,0,0,125,1,1.8,2,0,"fixed","Yes" 268 | "267",52,1,"asymptomatic",128,204,1,0,156,1,1,2,0,NA,"Yes" 269 | "268",59,1,"nonanginal",126,218,1,0,134,0,2.2,2,1,"fixed","Yes" 270 | "269",40,1,"asymptomatic",152,223,0,0,181,0,0,1,0,"reversable","Yes" 271 | "270",42,1,"nonanginal",130,180,0,0,150,0,0,1,0,"normal","No" 272 | "271",61,1,"asymptomatic",140,207,0,2,138,1,1.9,1,1,"reversable","Yes" 273 | "272",66,1,"asymptomatic",160,228,0,2,138,0,2.3,1,0,"fixed","No" 274 | "273",46,1,"asymptomatic",140,311,0,0,120,1,1.8,2,2,"reversable","Yes" 275 | "274",71,0,"asymptomatic",112,149,0,0,125,0,1.6,2,0,"normal","No" 276 | "275",59,1,"typical",134,204,0,0,162,0,0.8,1,2,"normal","Yes" 277 | "276",64,1,"typical",170,227,0,2,155,0,0.6,2,0,"reversable","No" 278 | "277",66,0,"nonanginal",146,278,0,2,152,0,0,2,1,"normal","No" 279 | "278",39,0,"nonanginal",138,220,0,0,152,0,0,2,0,"normal","No" 280 | "279",57,1,"nontypical",154,232,0,2,164,0,0,1,1,"normal","Yes" 281 | "280",58,0,"asymptomatic",130,197,0,0,131,0,0.6,2,0,"normal","No" 282 | "281",57,1,"asymptomatic",110,335,0,0,143,1,3,2,1,"reversable","Yes" 283 | "282",47,1,"nonanginal",130,253,0,0,179,0,0,1,0,"normal","No" 284 | "283",55,0,"asymptomatic",128,205,0,1,130,1,2,2,1,"reversable","Yes" 285 | "284",35,1,"nontypical",122,192,0,0,174,0,0,1,0,"normal","No" 286 | "285",61,1,"asymptomatic",148,203,0,0,161,0,0,1,1,"reversable","Yes" 287 | "286",58,1,"asymptomatic",114,318,0,1,140,0,4.4,3,3,"fixed","Yes" 288 | "287",58,0,"asymptomatic",170,225,1,2,146,1,2.8,2,2,"fixed","Yes" 289 | "288",58,1,"nontypical",125,220,0,0,144,0,0.4,2,NA,"reversable","No" 290 | "289",56,1,"nontypical",130,221,0,2,163,0,0,1,0,"reversable","No" 291 | "290",56,1,"nontypical",120,240,0,0,169,0,0,3,0,"normal","No" 292 | "291",67,1,"nonanginal",152,212,0,2,150,0,0.8,2,0,"reversable","Yes" 293 | "292",55,0,"nontypical",132,342,0,0,166,0,1.2,1,0,"normal","No" 294 | "293",44,1,"asymptomatic",120,169,0,0,144,1,2.8,3,0,"fixed","Yes" 295 | "294",63,1,"asymptomatic",140,187,0,2,144,1,4,1,2,"reversable","Yes" 296 | "295",63,0,"asymptomatic",124,197,0,0,136,1,0,2,0,"normal","Yes" 297 | "296",41,1,"nontypical",120,157,0,0,182,0,0,1,0,"normal","No" 298 | "297",59,1,"asymptomatic",164,176,1,2,90,0,1,2,2,"fixed","Yes" 299 | "298",57,0,"asymptomatic",140,241,0,0,123,1,0.2,2,0,"reversable","Yes" 300 | "299",45,1,"typical",110,264,0,0,132,0,1.2,2,0,"reversable","Yes" 301 | "300",68,1,"asymptomatic",144,193,1,0,141,0,3.4,2,2,"reversable","Yes" 302 | "301",57,1,"asymptomatic",130,131,0,0,115,1,1.2,2,1,"reversable","Yes" 303 | "302",57,0,"nontypical",130,236,0,2,174,0,0,2,1,"normal","Yes" 304 | "303",38,1,"nonanginal",138,175,0,0,173,0,0,1,NA,"normal","No" 305 | -------------------------------------------------------------------------------- /ISLR02.R: -------------------------------------------------------------------------------- 1 | # An Introduction to Statistical Learning with Applications in R 2 | # by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 3 | 4 | # Chapter 2: Statistical Learning 5 | 6 | # 2.3 Lab: Introduction to R 7 | 8 | # 2.3.1 Basic Commands 9 | 10 | x <- c(1, 3, 2, 5) 11 | x 12 | 13 | x = c(1, 6, 2) 14 | x 15 | y = c(1, 4, 3) 16 | 17 | length(x) 18 | length(y) 19 | x + y 20 | 21 | ls() 22 | rm(x, y) 23 | ls() 24 | 25 | rm(list = ls()) 26 | 27 | ?matrix 28 | 29 | x <- matrix(data = c(1,2,3,4), nrow = 2, ncol = 2) 30 | x 31 | 32 | x <- matrix(c(1, 2, 3, 4), 2, 2) 33 | 34 | matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE) 35 | 36 | sqrt(x) 37 | x^2 38 | 39 | x <- rnorm(50) 40 | y <- x + rnorm(50, mean = 50, sd = 0.1) 41 | cor(x, y) 42 | 43 | set.seed(1303) 44 | rnorm(50) 45 | 46 | set.seed(3) 47 | y <- rnorm(100) 48 | mean(y) 49 | var(y) 50 | sqrt(var(y)) 51 | sd(y) 52 | 53 | # 2.3.2 Graphics 54 | 55 | x <- rnorm(100) 56 | y <- rnorm(100) 57 | plot(x, y) 58 | plot(x, y, 59 | xlab = "this is the x-axis", ylab = "this is the y-axis", 60 | main = "Plot of X vs Y") 61 | 62 | pdf("Figure.pdf") 63 | plot(x, y, col = "green") 64 | dev.off() 65 | 66 | x <- seq(1, 10) 67 | x 68 | x <- 1:10 69 | x 70 | x <- seq(-pi, pi, length = 50) 71 | 72 | y <- x 73 | f <- outer(x, y, function(x, y) cos(y)/(1 + x^2)) 74 | contour(x, y, f) 75 | contour(x, y, f, nlevels = 45, add = T) 76 | fa <- (f - t(f))/2 77 | contour(x, y, fa, nlevels = 15) 78 | 79 | image(x, y, fa) 80 | persp(x, y, fa) 81 | persp(x, y, fa, theta = 30) 82 | persp(x, y, fa, theta = 30, phi = 20) 83 | persp(x, y, fa, theta = 30, phi = 70) 84 | persp(x, y, fa, theta = 30, phi = 40) 85 | 86 | # 2.3.3 Indexing Data 87 | 88 | A <- matrix(1:16,4,4) 89 | A 90 | 91 | A[2,3] 92 | 93 | A[c(1, 3), c(2, 4)] 94 | A[1:3, 2:4] 95 | A[1:2, ] 96 | A[ , 1:2] 97 | 98 | A[1, ] 99 | 100 | A[-c(1,3 ), ] 101 | A[-c(1, 3), -c(1, 3, 4)] 102 | 103 | dim(A) 104 | 105 | # 2.3.4 Loading Data 106 | 107 | Auto <- read.table("Auto.data") 108 | fix(Auto) 109 | 110 | Auto <- read.table("Auto.data", header = T, na.strings = "?") 111 | fix(Auto) 112 | 113 | Auto <- read.csv("Auto.csv", header = T, na.strings = "?") 114 | fix(Auto) 115 | dim(Auto) 116 | Auto[1:4, ] 117 | 118 | Auto <- na.omit(Auto) 119 | dim(Auto) 120 | 121 | names(Auto) 122 | 123 | # 2.3.5 Additional Graphical and Numerical Summaries 124 | 125 | plot(cylinders, mpg) 126 | 127 | plot(Auto$cylinders, Auto$mpg) 128 | attach(Auto) 129 | plot(cylinders, mpg) 130 | 131 | cylinders <- as.factor(cylinders) 132 | 133 | plot(cylinders, mpg) 134 | plot(cylinders, mpg, col="red") 135 | plot(cylinders, mpg, col="red", varwidth = T) 136 | plot(cylinders, mpg, col="red", varwidth = T, horizontal = T) 137 | plot(cylinders, mpg, col="red", varwidth = T, xlab = "cylinders", ylab = "MPG") 138 | 139 | hist(mpg) 140 | hist(mpg, col = 2) 141 | hist(mpg, col = 2, breaks = 15) 142 | 143 | pairs(Auto) 144 | pairs(~ mpg + displacement + horsepower + weight + acceleration, Auto) 145 | 146 | plot(horsepower, mpg) 147 | identify(horsepower, mpg, name) 148 | 149 | summary(Auto) 150 | 151 | summary(mpg) -------------------------------------------------------------------------------- /ISLR03.R: -------------------------------------------------------------------------------- 1 | # An Introduction to Statistical Learning with Applications in R 2 | # by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 3 | 4 | # Chapter 3: Linear Regression 5 | 6 | # 3.6 Lab: Linear Regression 7 | 8 | # 3.6.1 Libraries 9 | 10 | library(MASS) 11 | library(ISLR) 12 | 13 | # 3.6.2 Simple Linear Regression 14 | 15 | fix(Boston) 16 | names(Boston) 17 | 18 | lm.fit <- lm(medv ~ lstat) 19 | 20 | lm.fit <- lm(medv ~ lstat, data = Boston) 21 | attach(Boston) 22 | lm.fit <- lm(medv ~ lstat) 23 | 24 | lm.fit 25 | summary(lm.fit) 26 | 27 | names(lm.fit) 28 | coef(lm.fit) 29 | 30 | confint(lm.fit) 31 | 32 | predict(lm.fit, data.frame(lstat = (c(5, 10, 15))), interval = "confidence") 33 | predict(lm.fit, data.frame(lstat = (c(5, 10, 15))), interval = "prediction") 34 | 35 | plot(lstat,medv) 36 | abline(lm.fit) 37 | 38 | abline(lm.fit, lwd = 3) 39 | abline(lm.fit, lwd = 3, col = "red") 40 | plot(lstat, medv, col = "red") 41 | plot(lstat, medv, pch = 20) 42 | plot(lstat, medv, pch = "+") 43 | plot(1:20, 1:20, pch = 1:20) 44 | 45 | par(mfrow = c(2, 2)) 46 | plot(lm.fit) 47 | 48 | plot(predict(lm.fit), residuals(lm.fit)) 49 | plot(predict(lm.fit), rstudent(lm.fit)) 50 | 51 | plot(hatvalues(lm.fit)) 52 | which.max(hatvalues(lm.fit)) 53 | 54 | # 3.6.3 Multiple Linear Regression 55 | 56 | lm.fit <- lm(medv ~ lstat + age, data = Boston) 57 | summary(lm.fit) 58 | 59 | lm.fit <- lm(medv ~ ., data = Boston) 60 | summary(lm.fit) 61 | 62 | library(car) 63 | vif(lm.fit) 64 | 65 | lm.fit1 <- lm(medv ~ . -age, data = Boston) 66 | summary(lm.fit1) 67 | lm.fit1 <- update(lm.fit, ~ . -age) 68 | 69 | # 3.6.4 Interaction Terms 70 | 71 | summary(lm(medv ~ lstat*age, data = Boston)) 72 | 73 | # 3.6.5 Non-linear Transformations of the Predictors 74 | 75 | lm.fit2 <- lm(medv ~ lstat + I(lstat^2)) 76 | summary(lm.fit2) 77 | 78 | lm.fit <- lm(medv ~ lstat) 79 | anova(lm.fit, lm.fit2) 80 | 81 | par(mfrow = c(2, 2)) 82 | plot(lm.fit2) 83 | 84 | lm.fit5 <- lm(medv ~ poly(lstat, 5)) 85 | summary(lm.fit5) 86 | 87 | summary(lm(medv ~ log(rm), data = Boston)) 88 | 89 | # 3.6.6 Qualitative Predictors 90 | 91 | fix(Carseats) 92 | names(Carseats) 93 | 94 | lm.fit <- lm(Sales ~ . + Income:Advertising + Price:Age, data = Carseats) 95 | summary(lm.fit) 96 | 97 | attach(Carseats) 98 | contrasts(ShelveLoc) 99 | 100 | # 3.6.7 Writing Functions 101 | 102 | LoadLibraries 103 | LoadLibraries() 104 | 105 | LoadLibraries <- function() { 106 | library(ISLR) 107 | library(MASS) 108 | print("The libraries have been loaded.") 109 | } 110 | 111 | LoadLibraries 112 | 113 | LoadLibraries() -------------------------------------------------------------------------------- /ISLR04.R: -------------------------------------------------------------------------------- 1 | # An Introduction to Statistical Learning with Applications in R 2 | # by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 3 | 4 | # Chapter 4: Classification 5 | 6 | # 4.6 Lab: Logistic Regression, LDA, QDA, and KNN 7 | 8 | # 4.6.1 The Stock Market Data 9 | 10 | library(ISLR) 11 | names(Smarket) 12 | dim(Smarket) 13 | summary(Smarket) 14 | pairs(Smarket) 15 | 16 | cor(Smarket) 17 | cor(Smarket[, -9]) 18 | 19 | attach(Smarket) 20 | plot(Volume) 21 | 22 | # 4.6.2 Logistic Regression 23 | 24 | glm.fit <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, 25 | data = Smarket, family = binomial) 26 | summary(glm.fit) 27 | 28 | coef(glm.fit) 29 | summary(glm.fit)$coef 30 | summary(glm.fit)$coef[, 4] 31 | 32 | glm.probs <- predict(glm.fit, type = "response") 33 | glm.probs[1:10] 34 | contrasts(Direction) 35 | 36 | glm.pred <- rep("Down", 1250) 37 | glm.pred[glm.probs > 0.5] = "Up" 38 | 39 | table(glm.pred, Direction) 40 | (507 + 145)/1250 41 | mean(glm.pred == Direction) 42 | 43 | train <- (Year < 2005) 44 | Smarket.2005 <- Smarket[!train, ] 45 | dim(Smarket.2005) 46 | Direction.2005 <- Direction[!train] 47 | 48 | glm.fit <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, 49 | data = Smarket, family = binomial, subset = train) 50 | glm.probs <- predict(glm.fit, Smarket.2005, type = "response") 51 | 52 | glm.pred <- rep("Down", 252) 53 | glm.pred[glm.probs > 0.5] <- "Up" 54 | table(glm.pred, Direction.2005) 55 | mean(glm.pred == Direction.2005) 56 | mean(glm.pred != Direction.2005) 57 | 58 | glm.fit <- glm(Direction ~ Lag1 + Lag2, data = Smarket, 59 | family = binomial, subset = train) 60 | glm.probs <- predict(glm.fit, Smarket.2005, type = "response") 61 | glm.pred <- rep("Down", 252) 62 | glm.pred[glm.probs > 0.5] <- "Up" 63 | table(glm.pred, Direction.2005) 64 | mean(glm.pred == Direction.2005) 65 | 106/(106 + 76) 66 | 67 | predict(glm.fit, 68 | newdata = data.frame(Lag1 = c(1.2, 1.5), Lag2 = c(1.1, -0.8)), 69 | type = "response") 70 | 71 | # 4.6.3 Linear Discriminant Analysis 72 | 73 | library(MASS) 74 | lda.fit <- lda(Direction ~ Lag1 + Lag2, data = Smarket, subset = train) 75 | lda.fit 76 | plot(lda.fit) 77 | 78 | lda.pred <- predict(lda.fit, Smarket.2005) 79 | names(lda.pred) 80 | 81 | lda.class <- lda.pred$class 82 | table(lda.class, Direction.2005) 83 | mean(lda.class == Direction.2005) 84 | 85 | sum(lda.pred$posterior[ , 1] >= 0.5) 86 | sum(lda.pred$posterior[ , 1] < 0.5) 87 | 88 | lda.pred$posterior[1:20, 1] 89 | lda.class[1:20] 90 | 91 | sum(lda.pred$posterior[ , 1] > 0.9) 92 | 93 | # 4.6.4 Quadratic Discriminant Analysis 94 | 95 | qda.fit <- qda(Direction ~ Lag1 + Lag2, data = Smarket, subset = train) 96 | qda.fit 97 | 98 | qda.class <- predict(qda.fit, Smarket.2005)$class 99 | table(qda.class, Direction.2005) 100 | mean(qda.class == Direction.2005) 101 | 102 | # 4.6.5 K-Nearest Neighbors 103 | 104 | library(class) 105 | train.X <- cbind(Lag1,Lag2)[train, ] 106 | test.X <- cbind(Lag1, Lag2)[!train, ] 107 | train.Direction <- Direction[train] 108 | 109 | set.seed(1) 110 | knn.pred <- knn(train.X, test.X, train.Direction, k = 1) 111 | table(knn.pred, Direction.2005) 112 | (83 + 43)/252 113 | 114 | knn.pred <- knn(train.X, test.X, train.Direction, k = 3) 115 | table(knn.pred, Direction.2005) 116 | mean(knn.pred == Direction.2005) 117 | 118 | # 4.6.6 An Application to Caravan Insurance Data 119 | 120 | dim(Caravan) 121 | attach(Caravan) 122 | summary(Purchase) 123 | 348/5822 124 | 125 | standardized.X <- scale(Caravan[ , -86]) 126 | var(Caravan[,1]) 127 | var(Caravan[,2]) 128 | var(standardized.X[ ,1]) 129 | var(standardized.X[ ,2]) 130 | 131 | test <- 1:1000 132 | train.X <- standardized.X[-test, ] 133 | test.X <- standardized.X[ test, ] 134 | train.Y <- Purchase[-test] 135 | test.Y <- Purchase[ test] 136 | set.seed(1) 137 | knn.pred <- knn(train.X, test.X, train.Y, k = 1) 138 | mean(test.Y != knn.pred) 139 | mean(test.Y != "No") 140 | 141 | table(knn.pred, test.Y) 142 | 9/(68 + 9) 143 | 144 | knn.pred <- knn(train.X, test.X, train.Y, k = 3) 145 | table(knn.pred,test.Y) 146 | 5/26 147 | knn.pred <- knn(train.X, test.X, train.Y, k = 5) 148 | table(knn.pred, test.Y) 149 | 4/15 150 | 151 | glm.fit <- glm(Purchase ~ ., data = Caravan, family = binomial, subset = -test) 152 | glm.probs <- predict(glm.fit, Caravan[test, ], type = "response") 153 | glm.pred <- rep("No", 1000) 154 | glm.pred[glm.probs > 0.5] <- "Yes" 155 | table(glm.pred, test.Y) 156 | glm.pred <- rep("No", 1000) 157 | glm.pred[glm.probs > 0.25] <- "Yes" 158 | table(glm.pred, test.Y) 159 | 11/(22 + 11) -------------------------------------------------------------------------------- /ISLR05.R: -------------------------------------------------------------------------------- 1 | # An Introduction to Statistical Learning with Applications in R 2 | # by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 3 | 4 | # Chaper 5: Resampling Methods 5 | 6 | # 5.3 Lab: Cross-Validation and the Bootstrap 7 | 8 | # 5.3.1 The Validation Set Approach 9 | 10 | library(ISLR) 11 | set.seed(1) 12 | train <- sample(392, 196) 13 | 14 | lm.fit <- lm(mpg ~ horsepower, data = Auto, subset = train) 15 | 16 | attach(Auto) 17 | mean((mpg - predict(lm.fit,Auto))[-train]^2) 18 | 19 | lm.fit2 <- lm(mpg ~ poly(horsepower, 2), data = Auto, subset = train) 20 | mean((mpg - predict(lm.fit2,Auto))[-train]^2) 21 | lm.fit3 <- lm(mpg ~ poly(horsepower, 3), data = Auto, subset = train) 22 | mean((mpg - predict(lm.fit3, Auto))[-train]^2) 23 | 24 | set.seed(2) 25 | train <- sample(392, 196) 26 | lm.fit<- lm(mpg ~ horsepower, subset = train) 27 | mean((mpg - predict(lm.fit, Auto))[-train]^2) 28 | lm.fit2 <- lm(mpg ~ poly(horsepower, 2), data = Auto, subset = train) 29 | mean((mpg - predict(lm.fit2, Auto))[-train]^2) 30 | lm.fit3 <- lm(mpg ~ poly(horsepower, 3), data = Auto, subset = train) 31 | mean((mpg - predict(lm.fit3, Auto))[-train]^2) 32 | 33 | # 5.3.2 Leave-One-Out Cross-Validation 34 | 35 | glm.fit <- glm(mpg ~ horsepower, data = Auto) 36 | coef(glm.fit) 37 | 38 | lm.fit <- lm(mpg ~ horsepower, data = Auto) 39 | coef(lm.fit) 40 | 41 | library(boot) 42 | glm.fit <- glm(mpg ~ horsepower, data = Auto) 43 | cv.err <- cv.glm(Auto, glm.fit) 44 | cv.err$delta 45 | 46 | cv.error <- rep(0, 5) 47 | for (i in 1:5) { 48 | glm.fit <- glm(mpg ~ poly(horsepower, i), data = Auto) 49 | cv.error[i] <- cv.glm(Auto, glm.fit)$delta[1] 50 | } 51 | cv.error 52 | 53 | # 5.3.3 k-Fold Cross-Validation 54 | 55 | set.seed(17) 56 | cv.error.10 <- rep(0, 10) 57 | for (i in 1:10) { 58 | glm.fit <- glm(mpg ~ poly(horsepower, i), data = Auto) 59 | cv.error.10[i] <- cv.glm(Auto, glm.fit, K = 10)$delta[1] 60 | } 61 | cv.error.10 62 | 63 | # 5.3.4 The Bootstrap 64 | 65 | # Estimating the Accuracy of a Statistic of Interest 66 | 67 | alpha.fn <- function(data, index) { 68 | X <- data$X[index] 69 | Y <- data$Y[index] 70 | return((var(Y) - cov(X,Y))/(var(X) + var(Y) - 2*cov(X,Y))) 71 | } 72 | 73 | alpha.fn(Portfolio, 1:100) 74 | 75 | set.seed(1) 76 | alpha.fn(Portfolio, sample(100, 100, replace = TRUE)) 77 | 78 | boot(Portfolio, alpha.fn, R = 1000) 79 | 80 | # Estimating the Accuracy of a Linear Regression Model 81 | 82 | boot.fn <- function(data, index) 83 | return(coef(lm(mpg ~ horsepower, data = data, subset = index))) 84 | boot.fn(Auto, 1:392) 85 | 86 | set.seed(1) 87 | boot.fn(Auto, sample(392, 392, replace = TRUE)) 88 | boot.fn(Auto, sample(392, 392, replace = TRUE)) 89 | boot(Auto, boot.fn, 1000) 90 | 91 | summary(lm(mpg ~ horsepower, data = Auto))$coef 92 | 93 | boot.fn <- function(data, index) 94 | coefficients(lm(mpg ~ horsepower + I(horsepower^2), data = data, 95 | subset = index)) 96 | set.seed(1) 97 | boot(Auto, boot.fn, 1000) 98 | summary(lm(mpg ~ horsepower + I(horsepower^2), data = Auto))$coef -------------------------------------------------------------------------------- /ISLR06.R: -------------------------------------------------------------------------------- 1 | # An Introduction to Statistical Learning with Applications in R 2 | # by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 3 | 4 | # Chapter 6: Linear Model Selection and Regularization 5 | 6 | # 6.5 Lab 1: Subset Selection Methods 7 | 8 | # 6.5.1 Best Subset Selection 9 | 10 | library(ISLR) 11 | fix(Hitters) 12 | names(Hitters) 13 | dim(Hitters) 14 | sum(is.na(Hitters$Salary)) 15 | 16 | Hitters <- na.omit(Hitters) 17 | dim(Hitters) 18 | sum(is.na(Hitters)) 19 | 20 | library(leaps) 21 | regfit.full <- regsubsets(Salary ~ ., Hitters) 22 | summary(regfit.full) 23 | 24 | regfit.full <- regsubsets(Salary ~ ., data = Hitters, nvmax = 19) 25 | reg.summary <- summary(regfit.full) 26 | 27 | names(reg.summary) 28 | 29 | reg.summary$rsq 30 | 31 | par(mfrow = c(2, 2)) 32 | plot(reg.summary$rss, 33 | xlab = "Number of Variables", ylab = "RSS", type = "l") 34 | plot(reg.summary$adjr2, 35 | xlab = "Number of Variables", ylab = "Adjusted RSq", type = "l") 36 | 37 | which.max(reg.summary$adjr2) 38 | points(11, reg.summary$adjr2[11], col = "red", cex = 2, pch = 20) 39 | 40 | plot(reg.summary$cp, xlab = "Number of Variables", ylab = "Cp", type = "l") 41 | which.min(reg.summary$cp) 42 | points(10, reg.summary$cp[10], col = "red", cex = 2, pch = 20) 43 | which.min(reg.summary$bic) 44 | plot(reg.summary$bic, xlab = "Number of Variables", ylab = "BIC", type = "l") 45 | points(6, reg.summary$bic[6], col = "red", cex = 2, pch = 20) 46 | 47 | plot(regfit.full, scale = "r2") 48 | plot(regfit.full, scale = "adjr2") 49 | plot(regfit.full, scale = "Cp") 50 | plot(regfit.full, scale = "bic") 51 | 52 | coef(regfit.full, 6) 53 | 54 | # 6.5.2 Forward and Backward Stepwise Selection 55 | 56 | regfit.fwd <- regsubsets(Salary ~ ., data = Hitters, nvmax = 19, 57 | method = "forward") 58 | summary(regfit.fwd) 59 | regfit.bwd <- regsubsets(Salary ~ ., data = Hitters, nvmax = 19, 60 | method = "backward") 61 | summary(regfit.bwd) 62 | 63 | coef(regfit.full, 7) 64 | coef(regfit.fwd, 7) 65 | coef(regfit.bwd, 7) 66 | 67 | # 6.5.3 Choosing Among Models 68 | 69 | set.seed(1) 70 | train <- sample(c(TRUE, FALSE), nrow(Hitters), rep = TRUE) 71 | test <- !train 72 | 73 | regfit.best <- regsubsets(Salary ~ ., data = Hitters[train, ], nvmax = 19) 74 | 75 | test.mat <- model.matrix(Salary ~ ., data = Hitters[test, ]) 76 | 77 | val.errors <- rep(NA, 19) 78 | for(i in 1:19) { 79 | coefi <- coef(regfit.best, id = i) 80 | pred <- test.mat[ , names(coefi)] %*% coefi 81 | val.errors[i] <- mean((Hitters$Salary[test] - pred)^2) 82 | } 83 | 84 | val.errors 85 | which.min(val.errors) 86 | coef(regfit.best, 10) 87 | 88 | predict.regsubsets <- function(object, newdata, id, ...) { 89 | form <- as.formula(object$call[[2]]) 90 | mat <- model.matrix(form, newdata) 91 | coefi <- coef(object, id = id) 92 | xvars <- names(coefi) 93 | mat[ , xvars] %*% coefi 94 | } 95 | 96 | regfit.best <- regsubsets(Salary ~ ., data = Hitters, nvmax = 19) 97 | coef(regfit.best, 10) 98 | 99 | k <- 10 100 | set.seed(1) 101 | folds <- sample(1:k, nrow(Hitters), replace = TRUE) 102 | cv.errors <- matrix(NA, k, 19, dimnames = list(NULL, paste(1:19))) 103 | 104 | for(j in 1:k) { 105 | best.fit <- regsubsets(Salary ~ ., data = Hitters[folds != j, ], nvmax = 19) 106 | for(i in 1:19) { 107 | pred <- predict(best.fit, Hitters[folds == j, ], id = i) 108 | cv.errors[j,i] <- mean((Hitters$Salary[folds == j] - pred)^2) 109 | } 110 | } 111 | 112 | mean.cv.errors <- apply(cv.errors, 2, mean) 113 | mean.cv.errors 114 | par(mfrow = c(1, 1)) 115 | plot(mean.cv.errors, type = "b") 116 | 117 | reg.best <- regsubsets(Salary ~ ., data = Hitters, nvmax = 19) 118 | coef(reg.best, 11) 119 | 120 | # 6.6 Lab 2: Ridge Regression and the Lasso 121 | 122 | x <- model.matrix(Salary ~ ., Hitters)[ , -1] 123 | y <- Hitters$Salary 124 | 125 | # 6.6.1 Ridge Regression 126 | 127 | library(glmnet) 128 | grid <- 10^seq(10, -2, length = 100) 129 | ridge.mod <- glmnet(x, y, alpha = 0, lambda = grid) 130 | 131 | dim(coef(ridge.mod)) 132 | 133 | ridge.mod$lambda[50] 134 | coef(ridge.mod)[ , 50] 135 | sqrt(sum(coef(ridge.mod)[-1, 50]^2)) 136 | 137 | ridge.mod$lambda[60] 138 | coef(ridge.mod)[ , 60] 139 | sqrt(sum(coef(ridge.mod)[-1, 60]^2)) 140 | 141 | predict(ridge.mod, s = 50, type = "coefficients")[1:20, ] 142 | 143 | set.seed(1) 144 | train <- sample(1:nrow(x), nrow(x)/2) 145 | test <- -train 146 | y.test <- y[test] 147 | 148 | ridge.mod <- glmnet(x[train, ], y[train], alpha = 0, lambda = grid, 149 | thresh = 1e-12) 150 | ridge.pred <- predict(ridge.mod, s = 4, newx = x[test, ]) 151 | mean((ridge.pred - y.test)^2) 152 | 153 | mean((mean(y[train]) - y.test)^2) 154 | 155 | ridge.pred <- predict(ridge.mod, s = 1e10, newx = x[test, ]) 156 | mean((ridge.pred - y.test)^2) 157 | 158 | ridge.pred <- predict(ridge.mod, s = 0, newx = x[test, ], exact = TRUE) 159 | mean((ridge.pred - y.test)^2) 160 | lm(y ~ x, subset = train) 161 | predict(ridge.mod, s = 0, exact = TRUE, type = "coefficients")[1:20, ] 162 | 163 | set.seed(1) 164 | cv.out <- cv.glmnet(x[train, ], y[train], alpha = 0) 165 | plot(cv.out) 166 | bestlam <- cv.out$lambda.min 167 | bestlam 168 | 169 | ridge.pred <- predict(ridge.mod, s = bestlam, newx = x[test, ]) 170 | mean((ridge.pred - y.test)^2) 171 | 172 | out <- glmnet(x, y, alpha = 0) 173 | predict(out, type = "coefficients", s = bestlam)[1:20, ] 174 | 175 | # 6.6.2 The Lasso 176 | 177 | lasso.mod <- glmnet(x[train, ], y[train], alpha = 1, lambda = grid) 178 | plot(lasso.mod) 179 | 180 | set.seed(1) 181 | cv.out <- cv.glmnet(x[train, ], y[train], alpha = 1) 182 | plot(cv.out) 183 | bestlam <- cv.out$lambda.min 184 | lasso.pred <- predict(lasso.mod, s = bestlam, newx = x[test, ]) 185 | mean((lasso.pred - y.test)^2) 186 | 187 | out <- glmnet(x, y, alpha = 1, lambda = grid) 188 | lasso.coef <- predict(out, type = "coefficients", s = bestlam)[1:20, ] 189 | lasso.coef 190 | lasso.coef[lasso.coef != 0] 191 | 192 | # 6.7 Lab 3: PCR and PLS Regression 193 | 194 | # 6.7.1 Principal Components Regression 195 | 196 | library(pls) 197 | set.seed(2) 198 | pcr.fit <- pcr(Salary ~ ., data = Hitters, scale = TRUE, validation = "CV") 199 | 200 | summary(pcr.fit) 201 | 202 | validationplot(pcr.fit, val.type = "MSEP") 203 | 204 | set.seed(1) 205 | pcr.fit <- pcr(Salary ~ ., data = Hitters, subset = train, scale = TRUE, 206 | validation = "CV") 207 | validationplot(pcr.fit, val.type = "MSEP") 208 | 209 | pcr.pred <- predict(pcr.fit, x[test, ], ncomp = 7) 210 | mean((pcr.pred - y.test)^2) 211 | 212 | pcr.fit <- pcr(y ~ x, scale = TRUE, ncomp = 7) 213 | summary(pcr.fit) 214 | 215 | # 6.7.2 Partial Least Squares 216 | 217 | set.seed(1) 218 | pls.fit <- plsr(Salary ~ ., data = Hitters, subset = train, scale = TRUE, 219 | validation = "CV") 220 | summary(pls.fit) 221 | validationplot(pls.fit, val.type = "MSEP") 222 | 223 | pls.pred <- predict(pls.fit, x[test, ], ncomp = 2) 224 | mean((pls.pred - y.test)^2) 225 | 226 | pls.fit <- plsr(Salary ~ ., data = Hitters, scale = TRUE, ncomp = 2) 227 | summary(pls.fit) -------------------------------------------------------------------------------- /ISLR07.R: -------------------------------------------------------------------------------- 1 | # An Introduction to Statistical Learning with Applications in R 2 | # by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 3 | 4 | # Chapter 7: Moving Beyond Linearity 5 | 6 | # 7.8 Lab: Non-linear Modeling 7 | 8 | library(ISLR) 9 | attach(Wage) 10 | 11 | # 7.8.1 Polynomial Regression and Step Functions 12 | 13 | fit <- lm(wage ~ poly(age, 4), data = Wage) 14 | coef(summary(fit)) 15 | 16 | fit2 <- lm(wage ~ poly(age, 4, raw =TRUE), data = Wage) 17 | coef(summary(fit2)) 18 | 19 | fit2a <- lm(wage ~ age + I(age^2) + I(age^3) + I(age^4), data = Wage) 20 | coef(fit2a) 21 | 22 | fit2b <- lm(wage ~ cbind(age, age^2, age^3, age^4), data = Wage) 23 | 24 | agelims <- range(age) 25 | age.grid <- seq(from = agelims[1], to = agelims[2]) 26 | preds <- predict(fit, newdata = list(age = age.grid), se = TRUE) 27 | se.bands <- cbind(preds$fit + 2*preds$se.fit, preds$fit - 2*preds$se.fit) 28 | 29 | par(mfrow = c(1, 2), mar = c(4.5, 4.5, 1, 1), oma = c(0, 0, 4, 0)) 30 | plot(age, wage, xlim = agelims, cex = 0.5, col = "darkgrey") 31 | title("Degree-4 Polynomial", outer = TRUE) 32 | lines(age.grid, preds$fit, lwd = 2, col = "blue") 33 | matlines(age.grid, se.bands, lwd = 1, col = "blue", lty = 3) 34 | 35 | preds2 <- predict(fit2, newdata = list(age = age.grid), se = TRUE) 36 | max(abs(preds$fit - preds2$fit)) 37 | 38 | fit.1 <- lm(wage ~ age, data = Wage) 39 | fit.2 <- lm(wage ~ poly(age, 2), data = Wage) 40 | fit.3 <- lm(wage ~ poly(age, 3), data = Wage) 41 | fit.4 <- lm(wage ~ poly(age, 4), data = Wage) 42 | fit.5 <- lm(wage ~ poly(age, 5), data = Wage) 43 | anova(fit.1, fit.2, fit.3, fit.4, fit.5) 44 | 45 | coef(summary(fit.5)) 46 | 47 | (-11.983)^2 48 | 49 | fit.1 <- lm(wage ~ education + age, data = Wage) 50 | fit.2 <- lm(wage ~ education + poly(age,2), data = Wage) 51 | fit.3 <- lm(wage ~ education + poly(age,3), data = Wage) 52 | anova(fit.1, fit.2, fit.3) 53 | 54 | fit <- glm(I(wage > 250) ~ poly(age, 4), data = Wage, family = binomial) 55 | 56 | preds <- predict(fit, newdata = list(age = age.grid), se = TRUE) 57 | 58 | pfit <- exp(preds$fit)/(1 + exp(preds$fit)) 59 | se.bands.logit <- cbind(preds$fit + 2*preds$se.fit, preds$fit - 2*preds$se.fit) 60 | se.bands <- exp(se.bands.logit)/(1 + exp(se.bands.logit)) 61 | 62 | preds <- predict(fit, newdata = list(age = age.grid), type = "response", se = TRUE) 63 | 64 | plot(age, I(wage > 250), xlim = agelims, type = "n", ylim = c(0, 0.2)) 65 | points(jitter(age), I((wage > 250)/5), cex = 0.5, pch = "|", col = "darkgrey") 66 | lines(age.grid, pfit, lwd = 2, col = "blue") 67 | matlines(age.grid, se.bands, lwd = 1, col = "blue", lty = 3) 68 | 69 | table(cut(age, 4)) 70 | fit <- lm(wage ~ cut(age, 4), data = Wage) 71 | coef(summary(fit)) 72 | 73 | # 7.8.2 Splines 74 | 75 | library(splines) 76 | fit <- lm(wage ~ bs(age, knots = c(25, 40, 60)), data = Wage) 77 | pred <- predict(fit, newdata = list(age = age.grid), se = TRUE) 78 | plot(age, wage, col = "gray") 79 | lines(age.grid, pred$fit , lwd = 2) 80 | lines(age.grid, pred$fit + 2*pred$se, lty = "dashed") 81 | lines(age.grid, pred$fit - 2*pred$se, lty = "dashed") 82 | 83 | dim(bs(age,knots=c(25,40,60))) 84 | dim(bs(age,df=6)) 85 | attr(bs(age,df=6),"knots") 86 | 87 | fit2 <- lm(wage ~ ns(age, df = 4), data = Wage) 88 | pred2 <- predict(fit2, newdata = list(age = age.grid), se = TRUE) 89 | lines(age.grid, pred2$fit, col = "red", lwd = 2) 90 | 91 | plot(age, wage, xlim = agelims, cex = 0.5, col = "darkgrey") 92 | title("Smoothing Spline") 93 | fit <- smooth.spline(age, wage, df = 16) 94 | fit2 <- smooth.spline(age, wage, cv = TRUE) 95 | fit2$df 96 | lines(fit , col = "red" , lwd = 2) 97 | lines(fit2, col = "blue", lwd = 2) 98 | legend("topright", legend = c("16 DF", "6.8 DF"), col = c("red", "blue"), 99 | lty = 1, lwd = 2, cex = 0.8) 100 | 101 | plot(age, wage, xlim = agelims, cex = 0.5, col = "darkgrey") 102 | title("Local Regression") 103 | fit <- loess(wage ~ age, span = 0.2, data = Wage) 104 | fit2 <- loess(wage ~ age, span = 0.5, data = Wage) 105 | lines(age.grid, predict(fit , data.frame(age = age.grid)), col = "red" , lwd = 2) 106 | lines(age.grid, predict(fit2, data.frame(age = age.grid)), col = "blue", lwd = 2) 107 | legend("topright", legend = c("Span = 0.2", "Span = 0.5"), col = c("red", "blue"), 108 | lty = 1, lwd = 2, cex = 0.8) 109 | 110 | # 7.8.3 GAMs 111 | 112 | gam1 <- lm(wage ~ ns(year, 4) + ns(age, 5) + education, data = Wage) 113 | 114 | library(gam) 115 | gam.m3 <- gam(wage ~ s(year, 4) + s(age, 5) + education, data = Wage) 116 | par(mfrow = c(1, 3)) 117 | plot(gam.m3, se = TRUE, col = "blue") 118 | 119 | plot.gam(gam1, se = TRUE, col = "red") 120 | 121 | gam.m1 <- gam(wage ~ s(age, 5) + education, data = Wage) 122 | gam.m2 <- gam(wage~ year + s(age, 5) + education, data = Wage) 123 | anova(gam.m1, gam.m2, gam.m3, test = "F") 124 | 125 | summary(gam.m3) 126 | 127 | preds <- predict(gam.m2, newdata = Wage) 128 | 129 | gam.lo <- gam(wage ~ s(year, df = 4) + lo(age, span = 0.7) + education, data = Wage) 130 | plot.gam(gam.lo, se = TRUE, col = "green") 131 | 132 | gam.lo.i <- gam(wage ~ lo(year, age, span = 0.5) + education, data = Wage) 133 | 134 | library(akima) 135 | plot(gam.lo.i) 136 | 137 | gam.lr <- gam(I(wage > 250) ~ year + s(age, df = 5) + education, 138 | family = binomial, data = Wage) 139 | par(mfrow = c(1, 3)) 140 | plot(gam.lr, se = TRUE, col = "green") 141 | 142 | table(education, I(wage > 250)) 143 | 144 | gam.lr.s <- gam(I(wage > 250) ~ year + s(age, df = 5) + education, 145 | family = binomial, data = Wage, 146 | subset = (education != "1. < HS Grad")) 147 | plot(gam.lr.s, se = TRUE, col = "green") -------------------------------------------------------------------------------- /ISLR08.R: -------------------------------------------------------------------------------- 1 | # An Introduction to Statistical Learning with Applications in R 2 | # by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 3 | 4 | # Chapter 8: Tree-Based Methods 5 | 6 | # 8.3 Lab: Decision Trees 7 | 8 | # 8.3.1 Fitting Classification Trees 9 | 10 | library(tree) 11 | 12 | library(ISLR) 13 | attach(Carseats) 14 | High <- ifelse(Sales <= 8, "No", "Yes") 15 | 16 | Carseats <- data.frame(Carseats, High) 17 | 18 | tree.carseats <- tree(High ~ . -Sales, Carseats) 19 | 20 | summary(tree.carseats) 21 | 22 | plot(tree.carseats) 23 | text(tree.carseats, pretty = 0) 24 | 25 | tree.carseats 26 | 27 | set.seed(2) 28 | train <- sample(1:nrow(Carseats), 200) 29 | Carseats.test <- Carseats[-train, ] 30 | High.test <- High[-train] 31 | tree.carseats <- tree(High ~ . -Sales, Carseats, subset = train) 32 | tree.pred <- predict(tree.carseats, Carseats.test, type = "class") 33 | table(tree.pred, High.test) 34 | (86 + 57)/200 35 | 36 | set.seed(3) 37 | cv.carseats <- cv.tree(tree.carseats, FUN = prune.misclass) 38 | names(cv.carseats) 39 | cv.carseats 40 | 41 | par(mfrow = c(1, 2)) 42 | plot(cv.carseats$size, cv.carseats$dev, type = "b") 43 | plot(cv.carseats$k, cv.carseats$dev, type = "b") 44 | 45 | prune.carseats <- prune.misclass(tree.carseats, best = 9) 46 | plot(prune.carseats) 47 | text(prune.carseats, pretty = 0) 48 | 49 | tree.pred <- predict(prune.carseats, Carseats.test, type = "class") 50 | table(tree.pred, High.test) 51 | (94 + 60)/200 52 | 53 | prune.carseats <- prune.misclass(tree.carseats, best = 15) 54 | plot(prune.carseats) 55 | text(prune.carseats, pretty = 0) 56 | tree.pred <- predict(prune.carseats, Carseats.test, type = "class") 57 | table(tree.pred, High.test) 58 | (86 + 62)/200 59 | 60 | # 8.3.2 Fitting Regression Trees 61 | 62 | library(MASS) 63 | set.seed(1) 64 | train <- sample(1:nrow(Boston), nrow(Boston)/2) 65 | tree.boston <- tree(medv ~ ., Boston, subset = train) 66 | summary(tree.boston) 67 | 68 | plot(tree.boston) 69 | text(tree.boston, pretty = 0) 70 | 71 | cv.boston <- cv.tree(tree.boston) 72 | plot(cv.boston$size, cv.boston$dev, type = "b") 73 | 74 | prune.boston <- prune.tree(tree.boston, best = 5) 75 | plot(prune.boston) 76 | text(prune.boston,pretty = 0) 77 | 78 | yhat <- predict(tree.boston, newdata = Boston[-train, ]) 79 | boston.test <- Boston[-train, "medv"] 80 | plot(yhat, boston.test) 81 | abline(0, 1) 82 | mean((yhat - boston.test)^2) 83 | 84 | # 8.3.3 Bagging and Random Forests 85 | 86 | library(randomForest) 87 | set.seed(1) 88 | bag.boston <- randomForest(medv ~ ., data = Boston, subset = train, 89 | mtry = 13, importance = TRUE) 90 | bag.boston 91 | 92 | yhat.bag <- predict(bag.boston, newdata = Boston[-train, ]) 93 | plot(yhat.bag, boston.test) 94 | abline(0, 1) 95 | mean((yhat.bag - boston.test)^2) 96 | 97 | bag.boston <- randomForest(medv ~ ., data = Boston, subset = train, 98 | mtry = 13, ntree = 25) 99 | yhat.bag <- predict(bag.boston, newdata = Boston[-train, ]) 100 | mean((yhat.bag - boston.test)^2) 101 | 102 | set.seed(1) 103 | rf.boston <- randomForest(medv ~ ., data = Boston, subset = train, 104 | mtry = 6, importance = TRUE) 105 | yhat.rf <- predict(rf.boston, newdata = Boston[-train, ]) 106 | mean((yhat.rf - boston.test)^2) 107 | 108 | importance(rf.boston) 109 | 110 | varImpPlot(rf.boston) 111 | 112 | # 8.3.4 Boosting 113 | 114 | library(gbm) 115 | set.seed(1) 116 | boost.boston <- gbm(medv ~ ., data = Boston[train, ], distribution = "gaussian", 117 | n.trees = 5000, interaction.depth = 4) 118 | 119 | summary(boost.boston) 120 | 121 | par(mfrow = c(1, 2)) 122 | plot(boost.boston, i = "rm") 123 | plot(boost.boston, i = "lstat") 124 | 125 | yhat.boost <- predict(boost.boston, newdata = Boston[-train, ], n.trees = 5000) 126 | mean((yhat.boost - boston.test)^2) 127 | 128 | boost.boston <- gbm(medv ~ ., data = Boston[train, ], distribution = "gaussian", 129 | n.trees = 5000, interaction.depth = 4, shrinkage = 0.2, 130 | verbose = F) 131 | yhat.boost <- predict(boost.boston, newdata = Boston[-train, ], n.trees = 5000) 132 | mean((yhat.boost - boston.test)^2) -------------------------------------------------------------------------------- /ISLR09.R: -------------------------------------------------------------------------------- 1 | # An Introduction to Statistical Learning with Applications in R 2 | # by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 3 | 4 | # Chapter 9: Support Vector Machines 5 | 6 | # 9.6 Lab: Support Vector Machines 7 | 8 | # 9.6.1 Support Vector Classifier 9 | 10 | set.seed(1) 11 | x=matrix(rnorm(20*2), ncol=2) 12 | y=c(rep(-1,10), rep(1,10)) 13 | x[y==1,]=x[y==1,] + 1 14 | 15 | plot(x, col=(3-y)) 16 | 17 | dat=data.frame(x=x, y=as.factor(y)) 18 | library(e1071) 19 | svmfit=svm(y~., data=dat, kernel="linear", cost=10,scale=FALSE) 20 | 21 | plot(svmfit, dat) 22 | 23 | svmfit$index 24 | summary(svmfit) 25 | svmfit=svm(y~., data=dat, kernel="linear", cost=0.1,scale=FALSE) 26 | plot(svmfit, dat) 27 | svmfit$index 28 | set.seed(1) 29 | tune.out=tune(svm,y~.,data=dat,kernel="linear",ranges=list(cost=c(0.001, 0.01, 0.1, 1,5,10,100))) 30 | summary(tune.out) 31 | bestmod=tune.out$best.model 32 | summary(bestmod) 33 | xtest=matrix(rnorm(20*2), ncol=2) 34 | ytest=sample(c(-1,1), 20, rep=TRUE) 35 | xtest[ytest==1,]=xtest[ytest==1,] + 1 36 | testdat=data.frame(x=xtest, y=as.factor(ytest)) 37 | ypred=predict(bestmod,testdat) 38 | table(predict=ypred, truth=testdat$y) 39 | svmfit=svm(y~., data=dat, kernel="linear", cost=.01,scale=FALSE) 40 | ypred=predict(svmfit,testdat) 41 | table(predict=ypred, truth=testdat$y) 42 | x[y==1,]=x[y==1,]+0.5 43 | plot(x, col=(y+5)/2, pch=19) 44 | dat=data.frame(x=x,y=as.factor(y)) 45 | svmfit=svm(y~., data=dat, kernel="linear", cost=1e5) 46 | summary(svmfit) 47 | plot(svmfit, dat) 48 | svmfit=svm(y~., data=dat, kernel="linear", cost=1) 49 | summary(svmfit) 50 | plot(svmfit,dat) 51 | 52 | # Support Vector Machine 53 | 54 | set.seed(1) 55 | x=matrix(rnorm(200*2), ncol=2) 56 | x[1:100,]=x[1:100,]+2 57 | x[101:150,]=x[101:150,]-2 58 | y=c(rep(1,150),rep(2,50)) 59 | dat=data.frame(x=x,y=as.factor(y)) 60 | plot(x, col=y) 61 | train=sample(200,100) 62 | svmfit=svm(y~., data=dat[train,], kernel="radial", gamma=1, cost=1) 63 | plot(svmfit, dat[train,]) 64 | summary(svmfit) 65 | svmfit=svm(y~., data=dat[train,], kernel="radial",gamma=1,cost=1e5) 66 | plot(svmfit,dat[train,]) 67 | set.seed(1) 68 | tune.out=tune(svm, y~., data=dat[train,], kernel="radial", ranges=list(cost=c(0.1,1,10,100,1000),gamma=c(0.5,1,2,3,4))) 69 | summary(tune.out) 70 | table(true=dat[-train,"y"], pred=predict(tune.out$best.model,newx=dat[-train,])) 71 | 72 | # ROC Curves 73 | 74 | library(ROCR) 75 | rocplot=function(pred, truth, ...){ 76 | predob = prediction(pred, truth) 77 | perf = performance(predob, "tpr", "fpr") 78 | plot(perf,...)} 79 | svmfit.opt=svm(y~., data=dat[train,], kernel="radial",gamma=2, cost=1,decision.values=T) 80 | fitted=attributes(predict(svmfit.opt,dat[train,],decision.values=TRUE))$decision.values 81 | par(mfrow=c(1,2)) 82 | rocplot(fitted,dat[train,"y"],main="Training Data") 83 | svmfit.flex=svm(y~., data=dat[train,], kernel="radial",gamma=50, cost=1, decision.values=T) 84 | fitted=attributes(predict(svmfit.flex,dat[train,],decision.values=T))$decision.values 85 | rocplot(fitted,dat[train,"y"],add=T,col="red") 86 | fitted=attributes(predict(svmfit.opt,dat[-train,],decision.values=T))$decision.values 87 | rocplot(fitted,dat[-train,"y"],main="Test Data") 88 | fitted=attributes(predict(svmfit.flex,dat[-train,],decision.values=T))$decision.values 89 | rocplot(fitted,dat[-train,"y"],add=T,col="red") 90 | 91 | # SVM with Multiple Classes 92 | 93 | set.seed(1) 94 | x=rbind(x, matrix(rnorm(50*2), ncol=2)) 95 | y=c(y, rep(0,50)) 96 | x[y==0,2]=x[y==0,2]+2 97 | dat=data.frame(x=x, y=as.factor(y)) 98 | par(mfrow=c(1,1)) 99 | plot(x,col=(y+1)) 100 | svmfit=svm(y~., data=dat, kernel="radial", cost=10, gamma=1) 101 | plot(svmfit, dat) 102 | 103 | # Application to Gene Expression Data 104 | 105 | library(ISLR) 106 | names(Khan) 107 | dim(Khan$xtrain) 108 | dim(Khan$xtest) 109 | length(Khan$ytrain) 110 | length(Khan$ytest) 111 | table(Khan$ytrain) 112 | table(Khan$ytest) 113 | dat=data.frame(x=Khan$xtrain, y=as.factor(Khan$ytrain)) 114 | out=svm(y~., data=dat, kernel="linear",cost=10) 115 | summary(out) 116 | table(out$fitted, dat$y) 117 | dat.te=data.frame(x=Khan$xtest, y=as.factor(Khan$ytest)) 118 | pred.te=predict(out, newdata=dat.te) 119 | table(pred.te, dat.te$y) 120 | 121 | -------------------------------------------------------------------------------- /Income1.csv: -------------------------------------------------------------------------------- 1 | "","Education","Income" 2 | "1",10,26.6588387834389 3 | "2",10.4013377926421,27.3064353457772 4 | "3",10.8428093645485,22.1324101716143 5 | "4",11.2441471571906,21.1698405046065 6 | "5",11.6454849498328,15.1926335164307 7 | "6",12.0869565217391,26.3989510407284 8 | "7",12.4882943143813,17.435306578572 9 | "8",12.8896321070234,25.5078852305278 10 | "9",13.2909698996656,36.884594694235 11 | "10",13.7324414715719,39.666108747637 12 | "11",14.133779264214,34.3962805641312 13 | "12",14.5351170568562,41.4979935356871 14 | "13",14.9765886287625,44.9815748660704 15 | "14",15.3779264214047,47.039595257834 16 | "15",15.7792642140468,48.2525782901863 17 | "16",16.2207357859532,57.0342513373801 18 | "17",16.6220735785953,51.4909192102538 19 | "18",17.0234113712375,61.3366205527288 20 | "19",17.4648829431438,57.581988179306 21 | "20",17.866220735786,68.5537140185881 22 | "21",18.2675585284281,64.310925303692 23 | "22",18.7090301003344,68.9590086393083 24 | "23",19.1103678929766,74.6146392793647 25 | "24",19.5117056856187,71.8671953042483 26 | "25",19.9130434782609,76.098135379724 27 | "26",20.3545150501672,75.77521802986 28 | "27",20.7558528428094,72.4860553152424 29 | "28",21.1571906354515,77.3550205741877 30 | "29",21.5986622073579,72.1187904524136 31 | "30",22,80.2605705009016 32 | -------------------------------------------------------------------------------- /Income2.csv: -------------------------------------------------------------------------------- 1 | "","Education","Seniority","Income" 2 | "1",21.5862068965517,113.103448275862,99.9171726114381 3 | "2",18.2758620689655,119.310344827586,92.579134855529 4 | "3",12.0689655172414,100.689655172414,34.6787271520874 5 | "4",17.0344827586207,187.586206896552,78.7028062353695 6 | "5",19.9310344827586,20,68.0099216471551 7 | "6",18.2758620689655,26.2068965517241,71.5044853814318 8 | "7",19.9310344827586,150.344827586207,87.9704669939115 9 | "8",21.1724137931034,82.0689655172414,79.8110298331255 10 | "9",20.3448275862069,88.2758620689655,90.00632710858 11 | "10",10,113.103448275862,45.6555294997364 12 | "11",13.7241379310345,51.0344827586207,31.9138079371295 13 | "12",18.6896551724138,144.137931034483,96.2829968022869 14 | "13",11.6551724137931,20,27.9825049000603 15 | "14",16.6206896551724,94.4827586206897,66.601792415137 16 | "15",10,187.586206896552,41.5319924201478 17 | "16",20.3448275862069,94.4827586206897,89.00070081522 18 | "17",14.1379310344828,20,28.8163007592387 19 | "18",16.6206896551724,44.8275862068966,57.6816942573605 20 | "19",16.6206896551724,175.172413793103,70.1050960424457 21 | "20",20.3448275862069,187.586206896552,98.8340115435447 22 | "21",18.2758620689655,100.689655172414,74.7046991976891 23 | "22",14.551724137931,137.931034482759,53.5321056283034 24 | "23",17.448275862069,94.4827586206897,72.0789236655191 25 | "24",10.4137931034483,32.4137931034483,18.5706650327685 26 | "25",21.5862068965517,20,78.8057842852386 27 | "26",11.2413793103448,44.8275862068966,21.388561306174 28 | "27",19.9310344827586,168.965517241379,90.8140351180409 29 | "28",11.6551724137931,57.2413793103448,22.6361626208955 30 | "29",12.0689655172414,32.4137931034483,17.613593041445 31 | "30",17.0344827586207,106.896551724138,74.6109601985289 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ISLR 2 | ==== 3 | 4 | An Introduction to Statistical Learning with Applications in R 5 | 6 | by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 7 | --------------------------------------------------------------------------------