๐ Introduction
Machine Learning (ML) is a branch of Artificial Intelligence (AI) that enables computers to learn patterns from data and make predictions or decisions without being explicitly programmed for every task. R provides a rich ecosystem of machine learning libraries that simplify data preprocessing, model building, evaluation, and prediction.
Information
๐ฏ Why Learn Machine Learning with R?
- Predict future outcomes using data.
- Identify hidden patterns in datasets.
- Automate decision-making processes.
- Build intelligent data-driven applications.
- Apply statistical learning to real-world problems.
๐ง Types of Machine Learning
๐ Machine Learning Categories
| Category | Description | Examples |
|---|---|---|
| Supervised Learning | Models learn from labeled data. | Regression, Classification. |
| Unsupervised Learning | Models discover patterns without labels. | Clustering, Dimensionality Reduction. |
| Reinforcement Learning | Models learn through rewards and penalties. | Game AI, Robotics. |
๐ฆ Installing Machine Learning Packages
Several R packages simplify machine learning tasks. The most commonly used packages include caret, randomForest, e1071, and rpart.
Installing Packages
install.packages("caret")
install.packages("randomForest")
install.packages("e1071")
install.packages("rpart")
library(caret)
library(randomForest)
library(e1071)
library(rpart)๐ Loading a Dataset
R provides built-in datasets such as iris, which is commonly used for machine learning demonstrations.
Loading the iris Dataset
data(iris)
head(iris)
summary(iris)๐งน Data Preprocessing
Before training a model, data should be cleaned and prepared by handling missing values, scaling numeric features, and converting variables into appropriate formats.
Checking Missing Values
sum(
is.na(iris)
)Feature Scaling
scaledData <- scale(
iris[,1:4]
)
head(scaledData)โ๏ธ Splitting Data into Training and Testing Sets
Training data is used to build the model, while testing data evaluates its performance.
Train-Test Split
library(caret)
set.seed(123)
trainIndex <- createDataPartition(
iris$Species,
p = 0.7,
list = FALSE
)
trainData <- iris[
trainIndex,
]
testData <- iris[
-trainIndex,
]๐ Linear Regression
Linear regression predicts continuous numerical values.
Linear Regression
model <- lm(
Sepal.Length ~ Sepal.Width +
Petal.Length +
Petal.Width,
data = trainData
)
summary(model)๐ณ Decision Tree Classification
Decision trees classify data by learning a sequence of decision rules.
Decision Tree
library(rpart)
treeModel <- rpart(
Species ~ .,
data = trainData,
method = "class"
)
print(treeModel)๐ฒ Random Forest
Random Forest combines multiple decision trees to improve prediction accuracy and reduce overfitting.
Random Forest Model
library(randomForest)
rfModel <- randomForest(
Species ~ .,
data = trainData
)
print(rfModel)๐ Support Vector Machine (SVM)
Support Vector Machines are powerful supervised learning algorithms used for classification and regression.
Support Vector Machine
library(e1071)
svmModel <- svm(
Species ~ .,
data = trainData
)
print(svmModel)๐ฏ Making Predictions
After training a model, predictions can be generated using new or unseen data.
Prediction
predictions <- predict(
rfModel,
testData
)
head(predictions)๐ Evaluating Model Performance
The confusionMatrix() function compares predicted and actual values for classification models.
Confusion Matrix
confusionMatrix(
predictions,
testData$Species
)๐ Feature Importance
Random Forest models can estimate the importance of each feature.
Variable Importance
importance(
rfModel
)
varImpPlot(
rfModel
)๐ Cross-Validation
Cross-validation evaluates model performance using multiple train-test splits.
10-Fold Cross Validation
control <- trainControl(
method = "cv",
number = 10
)
model <- train(
Species ~ .,
data = iris,
method = "rf",
trControl = control
)
print(model)๐ Common Machine Learning Algorithms
| Algorithm | Learning Type | Typical Applications |
|---|---|---|
| Linear Regression | Supervised | Predicting continuous values. |
| Logistic Regression | Supervised | Binary classification. |
| Decision Tree | Supervised | Classification and regression. |
| Random Forest | Supervised | High-accuracy classification. |
| Support Vector Machine | Supervised | Classification of complex datasets. |
| K-Means | Unsupervised | Customer segmentation. |
๐ K-Means Clustering
K-Means groups similar observations into clusters without using labeled data.
K-Means Clustering
set.seed(123)
clusters <- kmeans(
iris[,1:4],
centers = 3
)
table(
clusters$cluster
)๐ Real-World Example
A bank wants to predict whether a customer will qualify for a loan based on financial information. A classification model can be trained using historical customer data and then used to predict the eligibility of new applicants.
Loan Eligibility Workflow
# Load data
loanData <- read.csv(
"loan_data.csv"
)
# Split data
trainRows <- createDataPartition(
loanData$Approved,
p = 0.8,
list = FALSE
)
trainSet <- loanData[
trainRows,
]
testSet <- loanData[
-trainRows,
]
# Train Decision Tree
loanModel <- rpart(
Approved ~ .,
data = trainSet,
method = "class"
)
# Predict
prediction <- predict(
loanModel,
testSet,
type = "class"
)
confusionMatrix(
prediction,
testSet$Approved
)๐ Machine Learning Workflow
๐ Common Machine Learning Packages
| Package | Purpose |
|---|---|
| caret | Unified machine learning framework. |
| randomForest | Random Forest implementation. |
| e1071 | Support Vector Machines and Naive Bayes. |
| rpart | Decision trees. |
| nnet | Neural networks. |
| xgboost | Gradient boosting algorithms. |
โ ๏ธ Common Mistakes
| Mistake | Explanation | Solution |
|---|---|---|
| Training on unclean data | Missing values and inconsistencies reduce model quality. | Clean and preprocess data before training. |
| Evaluating on training data | Produces overly optimistic performance estimates. | Always use separate testing or validation data. |
| Ignoring feature scaling | Algorithms such as SVM perform better with scaled features. | Normalize or standardize numeric variables when appropriate. |
| Overfitting the model | The model memorizes training data instead of learning general patterns. | Use cross-validation and tune model parameters. |
๐ก Best Practices
- Understand the problem before selecting an algorithm.
- Perform thorough data preprocessing.
- Use training, validation, and testing datasets appropriately.
- Evaluate models using multiple performance metrics.
- Interpret results rather than relying solely on prediction accuracy.
Best Practice
๐ Summary
Machine Learning in R combines statistical techniques with powerful libraries to build predictive models from data. You learned about supervised and unsupervised learning, data preprocessing, train-test splitting, regression, decision trees, random forests, support vector machines, clustering, prediction, model evaluation, feature importance, and cross-validation. Mastering these concepts provides a strong foundation for solving real-world prediction and classification problems using R.