Background
Using devices such as Jawbone Up, Nike FuelBand, and Fitbit it is now possible to collect a large amount of data about personal activity relatively inexpensively. These type of devices are part of the quantified self movement – a group of enthusiasts who take measurements about themselves regularly to improve their health, to find patterns in their behavior, or because they are tech geeks. One thing that people regularly do is quantify how much of a particular activity they do, but they rarely quantify how well they do it. In this project, your goal will be to use data from accelerometers on the belt, forearm, arm, and dumbell of 6 participants. They were asked to perform barbell lifts correctly and incorrectly in 5 different ways. More information is available from the website here: http://groupware.les.inf.puc-rio.br/har (see the section on the Weight Lifting Exercise Dataset).
Data
The training data for this project are available here:
https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv
The test data are available here:
https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv
The data for this project come from this source: http://groupware.les.inf.puc-rio.br/har.
Velloso, E.; Bulling, A.; Gellersen, H.; Ugulino, W.; Fuks, H. Qualitative Activity Recognition of Weight Lifting Exercises. Proceedings of 4th International Conference in Cooperation with SIGCHI (Augmented Human ’13) . Stuttgart, Germany: ACM SIGCHI, 2013.
First, we download the dataset to be used for the analysis;
## custom function for downloading and reading csv files downloadcsv <- function(url, nastrings) { temp <- tempfile() download.file(url, temp) data <- read.csv(temp, na.strings = nastrings) unlink(temp) return(data) } trainurl <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv" train <- downloadcsv(trainurl, c("", "NA", "#DIV/0!")) testurl <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv" test <- downloadcsv(testurl, c("", "NA", "#DIV/0!")) dim(train) ## [1] 19622 160 table(train$classe) ## ## A B C D E ## 5580 3797 3422 3216 3607
The training data has 19622 observations and 160 features, and the distribution of the five measured classes are shown in the table above.
Preprocessing
We partition our training data into a training set and a validation set so that we can validate our model before applying it to the test data.
set.seed(123) suppressMessages(library(caret)) trainset <- createDataPartition(train$classe, p = 0.8, list = FALSE) Training <- train[trainset, ] Validation <- train[-trainset, ]
Feature selection
First we clean up near zero variance features, columns with missing values and descriptive fields.
# exclude near zero variance features nzvcol <- nearZeroVar(Training) Training <- Training[, -nzvcol] # exclude columns with 40% more missing values exclude descriptive columns cntlength <- sapply(Training, function(x) { sum(!(is.na(x) | x == "")) }) nullcol <- names(cntlength[cntlength < 0.6 * length(Training$classe)]) descriptcol <- c("X", "user_name", "raw_timestamp_part_1", "raw_timestamp_part_2", "cvtd_timestamp", "new_window", "num_window") excludecols <- c(descriptcol, nullcol) Training <- Training[, !names(Training) %in% excludecols]
Model Train
We will use random forest as our model in the random forest package.
suppressMessages(library(randomForest)) rfModel <- randomForest(classe ~ ., data = Training, importance = TRUE, ntrees = 10)
Model Validation
Let’s test our model performance on the training set itself and the cross validation set.
training set accuracy
ptraining <- predict(rfModel, Training) print(confusionMatrix(ptraining, Training$classe)) ## Confusion Matrix and Statistics ## ## Reference ## Prediction A B C D E ## A 4464 0 0 0 0 ## B 0 3038 0 0 0 ## C 0 0 2738 0 0 ## D 0 0 0 2573 0 ## E 0 0 0 0 2886 ## ## Overall Statistics ## ## Accuracy : 1 ## 95% CI : (0.9998, 1) ## No Information Rate : 0.2843 ## P-Value [Acc > NIR] : < 2.2e-16 ## ## Kappa : 1 ## Mcnemar's Test P-Value : NA ## ## Statistics by Class: ## ## Class: A Class: B Class: C Class: D Class: E ## Sensitivity 1.0000 1.0000 1.0000 1.0000 1.0000 ## Specificity 1.0000 1.0000 1.0000 1.0000 1.0000 ## Pos Pred Value 1.0000 1.0000 1.0000 1.0000 1.0000 ## Neg Pred Value 1.0000 1.0000 1.0000 1.0000 1.0000 ## Prevalence 0.2843 0.1935 0.1744 0.1639 0.1838 ## Detection Rate 0.2843 0.1935 0.1744 0.1639 0.1838 ## Detection Prevalence 0.2843 0.1935 0.1744 0.1639 0.1838 ## Balanced Accuracy 1.0000 1.0000 1.0000 1.0000 1.0000
We see our model performing very accurate with all predictions correct against the training set.
validationg set accuracy
pvalidation <- predict(rfModel, Validation) print(confusionMatrix(pvalidation, Validation$classe)) ## Confusion Matrix and Statistics ## ## Reference ## Prediction A B C D E ## A 1116 0 0 0 0 ## B 0 758 1 0 0 ## C 0 1 683 6 0 ## D 0 0 0 637 0 ## E 0 0 0 0 721 ## ## Overall Statistics ## ## Accuracy : 0.998 ## 95% CI : (0.996, 0.9991) ## No Information Rate : 0.2845 ## P-Value [Acc > NIR] : < 2.2e-16 ## ## Kappa : 0.9974 ## Mcnemar's Test P-Value : NA ## ## Statistics by Class: ## ## Class: A Class: B Class: C Class: D Class: E ## Sensitivity 1.0000 0.9987 0.9985 0.9907 1.0000 ## Specificity 1.0000 0.9997 0.9978 1.0000 1.0000 ## Pos Pred Value 1.0000 0.9987 0.9899 1.0000 1.0000 ## Neg Pred Value 1.0000 0.9997 0.9997 0.9982 1.0000 ## Prevalence 0.2845 0.1935 0.1744 0.1639 0.1838 ## Detection Rate 0.2845 0.1932 0.1741 0.1624 0.1838 ## Detection Prevalence 0.2845 0.1935 0.1759 0.1624 0.1838 ## Balanced Accuracy 1.0000 0.9992 0.9982 0.9953 1.0000
Prediction for the cross validation also shows our model perfroms almost accurate with few misses
Test set prediction
ptest <- predict(rfModel, test) ptest ## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ## B A B A A E D B A A B C B A E E A B B B ## Levels: A B C D E
Save the test prediction to a file
writeLines(as.character(ptest),"prediction.txt",sep="\n")
We are now able to classify the excercise quality and any new data we receive can be used in predicting the quality of the work out performed by the individual.