22title : " Practical Machine Learning Course Notes"
33author : " Xing Su"
44output :
5- pdf_document :
6- toc : yes
7- toc_depth : 3
85 html_document :
96 highlight : pygments
107 theme : spacelab
118 toc : yes
9+ pdf_document :
10+ toc : yes
11+ toc_depth : 3
1212header-includes : \usepackage{graphicx} \usepackage{mathtools}
1313---
1414
@@ -22,8 +22,11 @@ $\pagebreak$
2222 - ***Note**: choosing the right dataset and knowing what the specific question is are paramount to the success of the prediction algorithm (GoogleFlu failed to predict accurately when people's search habits changed) *
2323
2424``` {r fig.width = 4, fig.height = 3, fig.align = 'center', echo = FALSE, message = FALSE}
25- library(png);library(grid)
26- library(doMC); registerDoMC(cores = 4)
25+ # install grid and png packages if not present
26+ if (!require("pacman")) install.packages("pacman")
27+ p_load("png", "grid")
28+ p_load("doMC")
29+ registerDoMC(cores = 4)
2730grid.raster(readPNG("figures/1.png"))
2831```
2932
@@ -65,7 +68,8 @@ grid.raster(readPNG("figures/1.png"))
6568
6669``` {r message = FALSE, fig.width = 3, fig.height = 3, fig.align = 'center'}
6770# load data
68- library(kernlab); data(spam); set.seed(333)
71+ p_load("kernlab")
72+ data(spam); set.seed(333)
6973# picking a small subset (10 values) from spam data set
7074smallSpam <- spam[sample(dim(spam)[1],size=10),]
7175# label spam = 2 and ham = 1
@@ -352,7 +356,7 @@ grid.raster(readPNG("figures/13.png"))
352356
353357``` {r message = FALSE}
354358# load packages and data
355- library( caret)
359+ p_load(" caret" )
356360# create training set indexes with 75% of data
357361inTrain <- createDataPartition(y=spam$type,p=0.75, list=FALSE)
358362# subset spam data to training
@@ -487,7 +491,7 @@ args(trainControl)
487491
488492``` {r fig.width = 5, fig.height = 5, fig.align = 'center'}
489493# load relevant libraries
490- library( ISLR); library( ggplot2);
494+ p_load(" ISLR", " ggplot2")
491495# load wage data
492496data(Wage)
493497# create training and test sets
@@ -508,15 +512,15 @@ qplot(age,wage,colour=education,data=training)+geom_smooth(method='lm',formula=y
508512```
509513
510514* ` cut2(variable, g=3) ` = creates a new factor variable by cutting the specified variable into n groups (3 in this case) based on percentiles
511- - ***Note**: `cut2` function is part of the `Hmisc` package, so `library( Hmisc)` must be run first *
515+ - ***Note**: `cut2` function is part of the `Hmisc` package, so `p_load(" Hmisc" )` must be run first *
512516 - this variable can then be used to tabulate/plot the data
513517* ` grid.arrange(p1, p2, ncol=2) ` = ` ggplot2 ` function the print multiple graphs on the same plot
514- - ***Note**: `grid.arrange` function is part of the `gridExtra` package, so `library( gridExtra)` must be run first *
518+ - ***Note**: `grid.arrange` function is part of the `gridExtra` package, so `p_load(" gridExtra" )` must be run first *
515519
516520``` {r fig.width = 7, fig.height = 3, fig.align = 'center', message = FALSE}
517521# load Hmisc and gridExtra packages
518- library( Hmisc);library( gridExtra);
519- # cute the wage variable
522+ p_load(" Hmisc", " gridExtra")
523+ # cut the wage variable
520524cutWage <- cut2(training$wage,g=3)
521525# plot the boxplot
522526p1 <- qplot(cutWage,age, data=training,fill=cutWage,
@@ -595,6 +599,7 @@ rbind(train = c(mean = mean(trainCapAveS), std = sd(trainCapAveS)),
595599
596600``` {r message = FALSE, fig.width = 5, fig.height = 3, fig.align = 'center'}
597601# set up BoxCox transforms
602+ p_load("e1071")
598603preObj <- preProcess(training[,-58],method=c("BoxCox"))
599604# perform preprocessing on training data
600605trainCapAveS <- predict(preObj,training[,-58])$capitalAve
@@ -610,6 +615,7 @@ par(mfrow=c(1,2)); hist(trainCapAveS); qqnorm(trainCapAveS)
610615
611616``` {r message = FALSE}
612617# Make some values NA
618+ p_load("RANN")
613619training$capAve <- training$capitalAve
614620selectNA <- rbinom(dim(training)[1],size=1,prob=0.05)==1
615621training$capAve[selectNA] <- NA
@@ -691,7 +697,7 @@ nearZeroVar(training,saveMetrics=TRUE)
691697
692698``` {r fig.width = 4, fig.height = 3, fig.align = 'center'}
693699# load splines package
694- library( splines)
700+ p_load(" splines" )
695701# create polynomial function
696702bsBasis <- bs(training$age,df=3)
697703# fit the outcome on the three polynomial terms
@@ -767,6 +773,7 @@ plot(prComp$x[,1],prComp$x[,2],col=typeColor,xlab="PC1",ylab="PC2")
767773
768774``` {r message = FALSE, warning = FALSE, fig.width = 4, fig.height = 3, fig.align = 'center'}
769775# create train and test sets
776+ p_load("caret")
770777inTrain <- createDataPartition(y=spam$type,p=0.75, list=FALSE)
771778training <- spam[inTrain,]
772779testing <- spam[-inTrain,]
@@ -775,7 +782,8 @@ preProc <- preProcess(log10(training[,-58]+1),method="pca",pcaComp=2)
775782# calculate PCs for training data
776783trainPC <- predict(preProc,log10(training[,-58]+1))
777784# run model on outcome and principle components
778- modelFit <- train(training$type ~ .,method="glm",data=trainPC)
785+ modelFit <- train(training$type ~.,data=trainPC,method="glm")
786+
779787# calculate PCs for test data
780788testPC <- predict(preProc,log10(testing[,-58]+1))
781789# compare results
@@ -1003,6 +1011,15 @@ modFit <- train(Species ~ .,method="rpart",data=training)
10031011# print the classification tree
10041012print(modFit$finalModel)
10051013# plot the classification tree
1014+ # Note: Need to install Gtk+ 2.x before using rattle. On OS/X:
1015+ # brew install gtk+
1016+ # I should just need to run the following:
1017+ #p_load("rattle")
1018+ # But I actually need to run this, because apparently the dependencies aren't loading:
1019+ p_load("RGtk2")
1020+ p_load("rpart")
1021+ p_load("rpart.plot")
1022+ p_load("rattle")
10061023rattle::fancyRpartPlot(modFit$finalModel)
10071024# predict on test values
10081025predict(modFit,newdata=testing)
@@ -1024,7 +1041,8 @@ predict(modFit,newdata=testing)
10241041
10251042``` {r fig.width = 5, fig.height = 4, fig.align = 'center', message = FALSE, warning = FALSE, cache = FALSE}
10261043# load data
1027- library(ElemStatLearn); data(ozone,package="ElemStatLearn")
1044+ p_load("ElemStatLearn")
1045+ data(ozone,package="ElemStatLearn")
10281046# reorder rows based on ozone variable
10291047ozone <- ozone[order(ozone$ozone),]
10301048# create empty matrix
@@ -1065,7 +1083,8 @@ lines(1:155,apply(ll,2,mean),col="red",lwd=2)
10651083
10661084``` {r fig.width = 5, fig.height = 4, fig.align = 'center', message = FALSE, warning = FALSE, cache = FALSE}
10671085# load relevant package and data
1068- library(party); data(ozone,package="ElemStatLearn")
1086+ p_load("party")
1087+ data(ozone,package="ElemStatLearn")
10691088# reorder rows based on ozone variable
10701089ozone <- ozone[order(ozone$ozone),]
10711090# extract predictors
@@ -1128,6 +1147,7 @@ grid.raster(readPNG("figures/14.png"))
11281147* *** example***
11291148
11301149``` {r fig.width = 5, fig.height = 3, fig.align = 'center', message = FALSE, warning = FALSE, cache = FALSE}
1150+ p_load("randomForest")
11311151# load data
11321152data(iris)
11331153# create train/test data sets
@@ -1208,6 +1228,7 @@ grid.raster(readPNG("figures/18.png"))
12081228* *** example***
12091229
12101230``` {r message = FALSE, warning = FALSE, cache = FALSE}
1231+ p_load("gbm")
12111232# load data
12121233data(Wage)
12131234# remove log wage variable (we are trying to predict wage)
@@ -1321,6 +1342,7 @@ pred.lda
13211342 - ***example: `caret` package***
13221343
13231344``` {r message = F, warning = F}
1345+ p_load("klaR")
13241346# using the same data from iris, run naive Bayes on training data
13251347nb <- train(Species ~ ., data=training,method="nb")
13261348# predict test outcomes using naive Bayes model
@@ -1541,7 +1563,7 @@ lm(lpsa ~ .,data =small)
15411563
15421564``` {r echo = FALSE, fig.width = 5, fig.height = 5, fig.align = 'center', message = FALSE, warning = FALSE}
15431565# load MASS library for lm.ridge function
1544- library( MASS)
1566+ p_load(" MASS" )
15451567# we will run the models for 10 different values from 0 to 50
15461568lambdas <- seq(0,50,len=10)
15471569# create empty vectors for training and test residual sum squares
@@ -1640,7 +1662,7 @@ legend("topright",covnames,pch=as.character(1:8), cex = 0.5)
16401662
16411663``` {r fig.width = 5, fig.height = 5, fig.align = 'center', message = F, warning = F, results = 'hide'}
16421664# load lars package
1643- library( lars)
1665+ p_load(" lars" )
16441666# perform lasso regression
16451667lasso.fit <- lars(as.matrix(x), y, type="lasso", trace=TRUE)
16461668# plot lasso regression model
@@ -1820,7 +1842,7 @@ $\pagebreak$
18201842
18211843``` {r fig.width = 5, fig.height = 4.5, fig.align = 'center', message = FALSE, warning = FALSE, cache = FALSE}
18221844# load quantmod package
1823- library( quantmod);
1845+ p_load(" quantmod" );
18241846# specify to and from dates
18251847from.dat <- as.Date("01/01/00", format="%m/%d/%y")
18261848to.dat <- as.Date("3/2/15", format="%m/%d/%y")
@@ -1838,7 +1860,7 @@ plot(decompose(ts),xlab="Years")
18381860
18391861``` {r fig.width = 5, fig.height = 3.5, fig.align = 'center', message = FALSE, warning = FALSE}
18401862# load forecast library
1841- library( forecast)
1863+ p_load(" forecast" )
18421864# find the number of rows (years)
18431865rows <- ceiling(length(ts)/12)
18441866# use 90% of the data to create training set
0 commit comments