Getting Started with Data Science
=======================================
This short tutorial will not only guide you through some basic data analysis methods but it will also show you how to implement some of the more sophisticated techniques available today. We will look into traffic accident data from the National Highway Traffic Safety Administration and try to predict fatal accidents using state-of-the-art statistical learning techniques. If you are interested, download the code at the bottom and follow along as we work through a real world data set. This post is in R while a companion post covers the same techniques in Python.
## Getting started in R
The `swirl` package is designed to teach people R. You can visit their [website](http://swirlstats.com/students.html) to see links to YouTube videos on installing R on both Mac and Windows.
You can download R directly using these links for the [Windows installer](http://cran.r-project.org/bin/windows/base/release.htm) or the [Mac package](http://cran.r-project.org/bin/macosx/R-latest.pkg). The R install packages are hosted by the [CRAN project](http://cran.r-project.org/). Just bear in mind that these are large files (60-70MB) and will take some time to download and install.
We suggest you install and view this file in [RStudio](https://www.rstudio.com/). After you have installed R, download and run the [installer for Rstudio](http://www.rstudio.com/ide/download/desktop). We recommend using RStudio as the IDE, but you can also use the R console directly if you choose.
If you open this file in RStudio, you can see the code is stored in "cells"" or "chunks" like this:
```{r}
# This is a code cell
```
You can also enter the code in the cells directly at the R command prompt.
If you click in a cell, you can run the code in that cell by selecting "Run Current Chunk" under the Chunks menu.
Try running the code in the following cell, which will load the libraries needed for the rest of this tutorial.
```{r}
library('glmnet') # For ridge regression fitting. It also supports elastic-net and LASSO models
library('gbm') # For Gradient-Boosting
library('rpart') # For building decision trees
```
If you get an error, delete the `#` and run the following code chunk to install the needed packages.
```{r}
#install.packages(c('glmnet', 'gbm', 'rpart'))
```
If you prefer, you can download a version with just the R code here, which you can load into R via the `source` command.
Now that you have R installed, we can start with our analysis. Inside RStudio, select "Run Next Chunk" under the "Chunks" menu to run the examples one at a time.
## Get some data
Being able to play with data requires having the data available, so let's take care of that right now. The National Highway Traffic Safety Administration ([NHTSA](http://www.nhtsa.gov/)) has some really cool data that they make public. The following code snippet will take care of downloading the data to a temporary file, and extract the file we are interested in, "PERSON.TXT", from the zipfile. Finally, it loads the data into R. The zip is 14.9 MB so it might take some time to run - it is worth the wait! This is really cool data.
```{r}
temp <- tempfile()
download.file("ftp://ftp.nhtsa.dot.gov/GES/GES12/GES12_Flatfile.zip",temp, quiet=TRUE)
accident_data_set <- read.delim(unz(temp, "PERSON.TXT"))
unlink(temp)
```
With our data downloaded and readily accessible, we can start to play around and see what we can learn from the data. Many of the columns have an encoding that you will need to read the manual in order to understand; it might be useful to [download the PDF](ftp://ftp.nhtsa.dot.gov/GES/GES12/GES%20Analytical%20Users%20Manual%201988-2012_FINAL-2013-10-31.pdf "GES 2012 Manual") so you can easily refer to it. Again, we will be looking at ``PERSON.TXT``, which contains information about individuals involved in road accidents.
```{r}
print(sort(colnames(accident_data_set)))
```
## Clean up the data
One prediction task you might find interesting is predicting whether or not a crash was fatal. The column ``INJSEV_IM`` contains imputed values for the severity of the injury, but there is one value that might complicate analysis - level 6 indicates that the person died prior to the crash.
```{r}
table(accident_data_set$INJSEV_IM)
```
Forntunately, there are only four of those cases within the dataset, so it is not unreasonable to ignore them during our analysis. However, we will find that a few of these columns have missing values:
```{r}
accident_data_set <- accident_data_set[accident_data_set$INJSEV_IM !=6,]
for (i in 1:ncol(accident_data_set)){
if (sum(as.numeric(is.na(accident_data_set[,i]))) > 0) {
num_missing <- sum(as.numeric(is.na(accident_data_set[,i])))
print(paste0(colnames(accident_data_set)[i], ": ", num_missing))
}
}
```
For this analysis, we will just drop these rows (they are all the same rows) - but you certainly don't have to do that. In fact, maybe there is a systematic data entry error that is causing them to be interpreted incorrectly. Regardless of the way you cleanup this data, we will most assuredly want to drop the column INJ_SEV, as it is the non-imputed version of INJSEV_IM and is a pretty severe data leak - there are others as well.
```{r}
rows_to_drop <- which(apply(accident_data_set, 1, FUN=function(X){return(sum(is.na(X)) > 0)}))
data <- accident_data_set[-rows_to_drop,]
data$INJ_SEV <- NULL
```
One more preprocessing step we'll do is to transform the response. If you flip to the manual it shows that category ``4`` is a fatal injury - so we will encode our target as such.
```{r}
data$INJSEV_IM <- as.numeric(data$INJSEV_IM == 4)
target <- data$INJSEV_IM
```
## Now we model!
We have predictors, we have a target, now it is time to build a model. We will be using ordinary least squares, [Ridge Regression](http://en.wikipedia.org/wiki/Ridge_regression) and [Lasso Regression](http://en.wikipedia.org/wiki/Lasso_%28statistics%29#Lasso_method "wikipedia"), both forms of regularized Linear Regression, a [Gradient Boosting Machine](http://en.wikipedia.org/wiki/Gradient_boosting "wikipedia") (GBM), and a [CART](http://en.wikipedia.org/wiki/Predictive_analytics#Classification_and_regression_trees) decision tree, to have some variety in modeling methods. These are just some representatives from the many packages available in R, which gives you access to quite a few machine learning techniques.
Don't be alarmed if this cell block takes quite a bit of time to run - the data is of non-negligible size. Additionally the ridge classifier is running several times to compute an optimal penalty parameter, and the gradient boosting classifier is building many trees in order to produce its ensembled decisions. There is a lot of computation going on under the hood, so get up and take a break if you need.
```{r}
# Split the data into test and train sets
train_rows <- sample(nrow(data), round(nrow(data) * 0.5))
traindf <- data[train_rows,]
testdf <- data[-train_rows,]
````
First the linear model:
```{r}
OLS_model <- lm(INJSEV_IM ~ ., data=traindf)
```
Then the GBM:
```{r}
print("Started Training GBM")
# GBM is easier to process as a data matrix
response_column <- which(colnames(traindf) == "INJSEV_IM")
trainy <- traindf$INJSEV_IM
gbm_formula <- as.formula(paste0("INJSEV_IM ~ ", paste(colnames(traindf[,-response_column]), collapse=" + ")))
gbm_model <- gbm(gbm_formula, traindf, distribution="bernoulli", n.trees=500, bag.fraction=0.75, cv.folds=5, interaction.depth=3)
print("Finished Training GBM")
# For glmnet we make a copy of our dataframe into a matrix
trainx_dm <- data.matrix(traindf[,-response_column])
print("Started fitting LASSO")
lasso_model <- cv.glmnet(x=trainx_dm, y=traindf$INJSEV_IM, alpha=1)
print("Finished fitting LASSO")
print("Started fitting RIDGE")
ridge_model <- cv.glmnet(x=trainx_dm, y=traindf$INJSEV_IM, alpha=0)
print("Finished fitting RIDGE")
```
And finally, we make a decison tree:
```{r}
dtree_model <- rpart(INJSEV_IM ~ ., traindf)
```
Now we can make predictions. For the GBM, we need to decide on how many trees to predict with. The following will plot how well the GBM performs at each of the 500 iterations, one for each additional tree. We want to minimize the green line, which represents the model performance on test data.
```{r}
gbm_perf<-gbm.perf(gbm_model,method="cv")
```
Notice that since the curve is still decreasing, we could try training more trees at the expense of more computation.
Now we can make predictions using our trained models:
```{r}
predictions_ols <- predict(OLS_model, testdf[,-response_column])
predictions_gbm <- predict(gbm_model, newdata=testdf[,-response_column], n.trees=gbm_perf, type="response")
testx_dm <- data.matrix(testdf[,-response_column])
predictions_lasso <- predict(lasso_model, newx=testx_dm, type="response", s='lambda.min')[,1]
predictions_ridge <- predict(ridge_model, newx=testx_dm, type="response", s='lambda.min')[,1]
predictions_dtree <- predict(dtree_model, testdf[,-response_column])
```
We can now assess model performance on the test set. We will be using the metric of area under the [ROC curve](http://en.wikipedia.org/wiki/Receiver_operating_characteristic). A perfect classifier would score 1.0 while purely random guessing would score 0.5.
```{r}
print("OLS: Area under the ROC curve:")
auc(testdf$INJSEV_IM, predictions_ols)
print("Rdige: Area under the ROC curve:")
auc(testdf$INJSEV_IM, predictions_ridge)
print("LASSO: Area under the ROC curve:")
auc(testdf$INJSEV_IM, predictions_lasso)
print("GBM: Area under the ROC curve:")
auc(testdf$INJSEV_IM, predictions_gbm)
print("Decision Tree: Area under the ROC curve:")
auc(testdf$INJSEV_IM, predictions_dtree)
```
# What else can I do?
We have a [blogpost](http://www.datarobot.com/blog/regularized-linear-regression-with-scikit-learn "Sage Wisdom") that goes into more detail about regularized linear regression, if that is what you are interested in. It would also be good to look at the various other packages in R, listed on CRAN under the task "MachineLearning" available [here](http://cran.r-project.org/web/views/MachineLearning.html). Beyond that, here are a few challenges that you can undertake to help you hone your data science skills.
## Data Prep
If it wasn't obvious in the blog post, the column ``STRATUM`` is a data leak (it encodes the severity of the crash). Which other columns contain data leaks? Can you come up with a rigorous method to generate candidates for deletion without having to read the entire GES manual?
And while we are considering data preparation, consider the column ``REGION``. Any regression model will consider the West region to be 4 times more ``REGION``-y than the Northeast - that just doesn't make sense. Which columns could benefit from being encoded as factor levels, rather than as numeric? To change a column into a factor, use the ``as.factor`` command.
## Which is the best model?
How good of a model can you build for predicting fatalities from car crashes? First you will need to settle on a metric of "good" - and be prepared to reason why it is a good metric. How bad is it to be wrong? How good is it to be right?
In order to avoid [overfitting](http://en.wikipedia.org/wiki/Overfitting) you will want to separate some of the data and hold it in reserve for when you evaluate your models - some of these models are expressive enough to memorize all the data!
## Which is the best story?
Of course, data science is more than just gathering data and building models - it's about telling a story backed up by the data. Do crashes with alcohol involved tend to lead to more serious injuries? When it is late at night, are there more convertibles involved in crashes than other types of vehicles (this one involves looking at a different dataset with the GES data)? Which is the safest seat in a car? And how sure can you be that your findings are statistically relevant?
Good luck coming up with a great story!