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. This project aims to use data from accelerometers on the belt, forearm, arm, and dumbbell of 6 participants to predict the manner in which they performed barbell lifts.
The training and test datasets are provided from the following URLs:
train_url <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
test_url <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
training <- read.csv(train_url, na.strings = c("NA", "#DIV/0!", ""))
testing <- read.csv(test_url, na.strings = c("NA", "#DIV/0!", ""))
Clean the data by removing columns with a high proportion of missing values and irrelevant columns.
# Remove columns with more than 90% missing values
na_columns <- sapply(training, function(x) mean(is.na(x))) > 0.9
training <- training[, !na_columns]
testing <- testing[, !na_columns]
# Remove irrelevant columns (first 7 columns)
training <- training[, -(1:7)]
testing <- testing[, -(1:7)]
We will use a Random Forest model for this task due to its robustness and efficiency in handling a large number of features. We will also use cross-validation to tune the model and estimate the out-of-sample error.
control <- trainControl(method = "cv", number = 5)
model <- train(classe ~ ., data = training, method = "rf", trControl = control)
Evaluate the model using cross-validation results.
print(model)
## Random Forest
##
## 19622 samples
## 52 predictor
## 5 classes: 'A', 'B', 'C', 'D', 'E'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 15698, 15698, 15697, 15698, 15697
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 2 0.9942921 0.9927795
## 27 0.9938334 0.9921993
## 52 0.9876669 0.9843968
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
Apply the trained model to the test dataset.
predictions <- predict(model, newdata = testing)
print(predictions)
## [1] 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
In this project, we successfully built a Random Forest model to predict the quality of barbell lifts using accelerator data. Cross-validation was used to ensure the model’s robustness and estimate its out-of-sample error. The model’s predictions can be used for further analysis and improvements.