Data mining is the process of discovering patterns and relationships in large datasets. It involves using techniques from a range of fields, including machine learning, statistics and database systems, to extract valuable insights and information from data.
In this article, we will provide an overview of data mining in the R Programming Language, including some of the most commonly used techniques and tools. We will begin by introducing the basics of data mining and R and then move to more advanced topics such as machine learning and text mining.
Getting Started with Data Mining in R
To begin working with data mining in R:
1. Install R and an IDE
Install R and a development environment such as RStudio, which provides an intuitive interface for coding, visualization and package management.
2. Install Key Packages
Install commonly used packages using install.packages()
and load them with library()
. We will install caret
,dplyr
and ggplot2
.
R
install.packages("caret")
install.packages("dplyr")
install.packages("ggplot2")
library(dplyr)
library(caret)
library(ggplot2)
Data Mining Techniques
We will explore some commonly used data mining techniques and there implementation.
1. Clustering
Clustering can be used for a variety of purposes, including discovering hidden patterns in the data, summarizing data and generating new features. This involves partitioning a dataset into groups (called clusters) such that the observations within each cluster are more similar to each other than they are to observations in other clusters.
R
install.packages("factoextra")
library(factoextra)
df <- mtcars
df <- na.omit(df)
df <- scale(df)
# With 4 Centers
km <- kmeans(df, centers = 4, nstart = 25)
fviz_cluster(km, data = df)
# With 5 Centers
km <- kmeans(df, centers = 5, nstart = 25)
fviz_cluster(km, data = df)
Output:
Clusters formed using 4 cluster centers
Clusters formed using 5 cluster centers2. Association Rule
Association Rule is used to identify patterns or relationships within large datasets, such as products frequently bought together in retail. Rules are expressed as if-then statements, where the presence of one item suggests the presence of another. The strength of these associations is measured by support, confidence and lift, helping businesses optimise product placement, promotions and recommendations. It is widely applied in market basket analysis and recommendation systems.
R
install.packages("arules")
install.packages("arulesViz")
library(arules)
library(arulesViz)
data("Groceries")
rules <- apriori(Groceries, parameter = list(support = 0.01, confidence = 0.5))
inspect(head(rules, 5))
plot(rules, method = "grouped")
Output:
Table
Plot3. Regression
Linear Regression is one of the most widely used regression techniques to model the relationship between two variables. It uses a linear relationship to model the regression line. There are 2 variables used in the linear relationship equation i.e., the predictor variable and the response variable.
Linear Regression Equation:
y = a x + b
where,
- x indicates predictor or independent variable
- y indicates response or dependent variable
- a and b are coefficients
R
x <- c(153, 169, 140, 186, 128, 136, 178, 163, 152, 133)
y <- c(64, 81, 58, 91, 47, 57, 75, 72, 62, 49)
model <- lm(y ~ x)
print(model)
df <- data.frame(x = 182)
res <- predict(model, df)
cat("\nPredicted value of a person with height = 182\n")
print(res)
plot(x, y, main = "Height vs Weight Regression Model")
abline(model)
Output:
Model and Prediction
Regression line with the points on a 2D plot4. Classification
Classification is a supervised learning technique used to categorize data into predefined classes based on input features. It is widely used in applications such as spam detection, disease diagnosis and image recognition.
R
install.packages("randomForest")
library(randomForest)
data(iris)
set.seed(123)
model <- randomForest(Species ~ ., data = iris, ntree = 100)
pred <- predict(model, iris)
# Confusion matrix
table(Predicted = pred, Actual = iris$Species)
Output:
RF CLassfication
Similar Reads
Organising Data in R Organizing data is a fundamental step in data analysis and manipulation, and R Programming Language provides a powerful set of tools and techniques to help you efficiently structure and manage your data. Whether you're working with small datasets or massive datasets, understanding how to organize yo
5 min read
Data Preprocessing in R Data preprocessing is an important step in data analysis and machine learning. In R, we use various tools to clean, manipulate and prepare data for analysis. In this article we will explore the essential steps involved in data preprocessing using R.1. Installing and Loading Required PackagesThe tidy
4 min read
Data Handling in R Programming R programming language is widely used for statistics and data analysis. One of the fundamental tasks when working with data is importing and exporting data from various file formats. R provides a variety of functions to handle different file types, such as CSV files, text files, Excel sheets, and mo
3 min read
Importing Data in R Script We can read external datasets and operate with them in our R environment by importing data into an R script. R offers a number of functions for importing data from various file formats. In this article, we are going to see how to Import data in R Programming Language. Importing Data in R First, let'
3 min read
Raster Data in R In this article we will discuss what is Raster Data in R Programming Language and how we use Raster Data in different work scenarios. What is Raster Data in R?Raster data, representing spatial data in a grid format, is crucial in fields such as geography, environmental science, and remote sensing. R
3 min read