0% found this document useful (0 votes)
6 views27 pages

BCA603 DataMining R Notes

The document provides comprehensive exam notes for BCA-603, covering data mining concepts, techniques, and algorithms across three units. Key topics include data mining fundamentals, clustering methods, decision trees, and advanced mining techniques, with a focus on practical applications and challenges. It also highlights important algorithms like Apriori, FP-Growth, and CART, along with their respective advantages and limitations.

Uploaded by

nadiyanaz0607
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views27 pages

BCA603 DataMining R Notes

The document provides comprehensive exam notes for BCA-603, covering data mining concepts, techniques, and algorithms across three units. Key topics include data mining fundamentals, clustering methods, decision trees, and advanced mining techniques, with a focus on practical applications and challenges. It also highlights important algorithms like Apriori, FP-Growth, and CART, along with their respective advantages and limitations.

Uploaded by

nadiyanaz0607
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BCA-603

Data Mining with R


Complete Exam Notes — All 3 Units

Unit 1 Unit 2 Unit 3


Data Mining & Association Clustering & Decision Trees R Programming & Statistics
Rules

📝 These notes cover the complete BCA-603 syllabus in simple language. Key terms are
highlighted. Examples are provided wherever possible. Best of luck for your exam!
UNIT Introduction to Data Mining
1

▶ 1.1 What is Data Mining?


Data Mining is the process of discovering useful patterns, knowledge, and relationships from large
amounts of data stored in databases, data warehouses, or other data repositories.
In simple words: It is like digging (mining) for gold in a mountain of data. We extract valuable
information hidden inside huge datasets.

📝 Think of it like this: A supermarket collects billions of purchase records. Data mining helps find
that 'customers who buy bread also tend to buy butter.' This hidden pattern is valuable!

◆ Key Terms
Database Organized collection of data stored electronically (e.g., MySQL, Oracle).

Data Warehouse A large storage system that collects data from multiple sources for
analysis.

Pattern A hidden relationship or trend discovered in data (e.g., buying habits).

KDD Knowledge Discovery in Databases — the overall process of extracting


knowledge from data. Data Mining is one step of KDD.

◆ Steps in KDD Process


• Data Cleaning — Remove noise, inconsistent data (garbage in = garbage out)
• Data Integration — Combine data from multiple sources
• Data Selection — Select relevant data for analysis
• Data Transformation — Convert data into suitable format
• Data Mining — Apply algorithms to extract patterns
• Pattern Evaluation — Identify truly interesting patterns
• Knowledge Presentation — Visualize and present results to users

▶ 1.2 Data Mining Techniques


There are several core techniques used in data mining, each suited to different types of problems:

Classification Assigns data into predefined categories.


Example: Spam or Not Spam email.
Clustering Groups similar data together without predefined
categories. Example: Grouping customers by
buying habits.
Association Rule Mining Finds rules showing which items appear
together. Example: Bread → Butter (market
basket analysis).
Regression Predicts a numerical value. Example:
Predicting house prices.
Anomaly Detection Finds unusual patterns or outliers. Example:
Detecting credit card fraud.
Summarization Provides compact description of data. Example:
Average salary by department.

▶ 1.3 Issues and Challenges in Data Mining


◆ Major Challenges
• Scalability: Algorithms must work efficiently on very large datasets (millions/billions of records).
• High Dimensionality: Data may have thousands of attributes — makes analysis complex (the
curse of dimensionality).
• Noisy Data: Real-world data contains errors, missing values, and inconsistencies.
• Privacy: Mining personal data raises privacy and ethical concerns (GDPR, HIPAA).
• Data Heterogeneity: Data comes in many formats — text, images, numbers, time-series.
• Changing Data: Data changes over time (concept drift) — models become outdated.
• Interpretability: Complex models (like neural networks) are difficult to interpret.

📝 Remember for exam: The 3 big issues = Scalability + Privacy + High Dimensionality

▶ 1.4 Applications of Data Mining


Banking & Finance Credit scoring, fraud detection, stock market
prediction
Retail / E-commerce Market basket analysis, customer
segmentation, recommendation systems
Healthcare Disease prediction, drug discovery, patient risk
analysis
Telecommunications Churn prediction (which customers will leave),
network fault analysis
Education Student performance prediction, course
recommendation
Social Media Sentiment analysis, trend detection, fake news
detection
Manufacturing Quality control, predictive maintenance
Government Tax fraud detection, crime analysis, census
data analysis

▶ 1.5 Association Rules


Association Rule Mining finds interesting relationships (associations) between variables in large
datasets. It is most famous for Market Basket Analysis.

◆ Key Concepts
Itemset A set of items that appear together. Example: {Bread, Butter, Milk}

Support How often an itemset appears in the dataset. Support(A) = (Transactions


with A) / (Total Transactions)

Confidence How often the rule is correct. Confidence(A→B) = Support(A∪B) /


Support(A)

Lift How much better the rule is than random chance. Lift > 1 means rule is
useful.

Minimum Support Threshold: rules below this support value are ignored (too rare).

Minimum Threshold: rules below this confidence are ignored (too unreliable).
Confidence

◆ Example of Association Rule


Rule: {Bread, Butter} → {Milk}
Support = 30% → 30% of all transactions contain Bread, Butter, AND Milk
Confidence = 75% → 75% of the time when people buy Bread & Butter, they also buy Milk
Interpretation: Placing Milk near Bread & Butter in the store will increase sales!

◆ Apriori Algorithm
The Apriori algorithm is the most classic algorithm for finding frequent itemsets and association rules.

Core Principle (Apriori Property): If an itemset is frequent, then all its subsets must also be frequent.
Conversely, if an itemset is infrequent, all its supersets are also infrequent (anti-monotone property).

◆ Apriori Steps
• Step 1: Find all frequent 1-itemsets (items meeting minimum support)
• Step 2: Generate candidate 2-itemsets from frequent 1-itemsets
• Step 3: Prune candidates that have infrequent subsets
• Step 4: Scan database to count support of candidates
• Step 5: Repeat steps 2-4 for larger itemsets until no more frequent itemsets found
• Step 6: Generate association rules from frequent itemsets

📝 Apriori's main drawback: It scans the database many times, making it slow for large datasets.
FP-Growth algorithm solves this!

▶ 1.6 Prior Algorithm (Apriori in Detail)


Apriori uses a 'generate and test' approach. It has two main steps:
• Join Step: Combine two (k-1) frequent itemsets to generate a new k-itemset candidate.
• Prune Step: Remove any k-itemset candidate if any of its (k-1) subsets is not frequent.

▶ 1.7 Dynamic Itemset Counting (DIC)


DIC is an improvement over Apriori. Instead of waiting to finish scanning the database before
generating new candidates, DIC starts counting new itemset candidates during the same database
scan.

Apriori Multiple full database scans — one scan per


itemset size
DIC Fewer database scans — adds new candidates
dynamically during scanning
Advantage of DIC More efficient, fewer I/O operations needed
Concept Uses 'starter' and 'solid' itemset categories to
track counting status

▶ 1.8 FP-Tree Growth Algorithm


FP-Growth (Frequent Pattern Growth) is a much faster alternative to Apriori. It avoids repeated
database scans by compressing the database into a special tree structure called FP-Tree.

◆ Steps of FP-Growth
• Step 1: Scan database once — find frequent 1-itemsets and their support counts
• Step 2: Sort items in each transaction by descending support frequency
• Step 3: Build the FP-Tree by inserting transactions one by one
• Step 4: Mine the FP-Tree by generating conditional pattern bases
• Step 5: Build conditional FP-Trees and extract frequent patterns
◆ FP-Tree Structure
• Root Node: Empty node at the top of the tree
• Item Node: Each node stores: item name, count, and parent link
• Header Table: Links all nodes with same item — allows efficient traversal

📝 FP-Growth advantage: Only 2 database scans needed vs. many scans in Apriori! Much faster
for large datasets.

▶ 1.9 Incremental Learning


Incremental Learning (also called Online Learning) refers to updating a data mining model when new
data arrives, without retraining from scratch on the entire dataset.

◆ Why is Incremental Learning Important?


• Databases keep growing — retraining from scratch is too expensive
• Real-time applications need models that update continuously
• Examples: news classification, stock prediction, fraud detection

◆ Challenges in Incremental Learning


• Concept Drift: Distribution of data changes over time
• Memory constraints: Cannot store all old data
• Maintaining accuracy while updating incrementally

📝 Incremental mining is different from batch mining: Batch = retrain everything. Incremental =
update existing model with new data only.
UNIT Clustering, Decision Trees & Advanced
2 Mining

▶ 2.1 What is Clustering?


Clustering is an unsupervised learning technique that groups similar data points together into clusters,
without any predefined labels.
Goal: Data points within the same cluster should be very similar to each other, and data points in
different clusters should be very different from each other.

📝 Real-life example: Grouping customers based on purchase behavior — without being told in
advance who the groups are!

◆ Types of Clustering
• Partitional Clustering: Divides data into k non-overlapping clusters. Example: K-Means, K-
Medoid.
• Hierarchical Clustering: Creates a tree-like structure of clusters. Can be Agglomerative
(bottom-up) or Divisive (top-down).
• Density-based Clustering: Clusters are dense regions separated by sparse regions. Example:
DBSCAN.
• Grid-based Clustering: Divides space into a grid structure and clusters grid cells.

▶ 2.2 K-Medoid Algorithm (PAM)


K-Medoid is similar to K-Means but uses actual data points as cluster centers (called medoids), not
computed averages. This makes it more robust to outliers.

◆ Steps of K-Medoid (PAM = Partitioning Around Medoids)


• Step 1: Randomly select k data points as initial medoids
• Step 2: Assign every non-medoid data point to the nearest medoid
• Step 3: For each medoid m and each non-medoid point p:
◦ Calculate total cost if m is replaced by p
◦ If cost decreases, swap m with p (make p the new medoid)
• Step 4: Repeat steps 2-3 until no more swaps improve cost

K-Means Center Computed mean (average) — may not be an


actual data point
K-Medoid Center Actual data point — always exists in the
dataset
Advantage of Medoid Less sensitive to outliers and noise
Disadvantage Slower than K-Means because it tries all
possible swaps

▶ 2.3 Hierarchical Clustering


Hierarchical clustering creates a dendrogram (tree diagram) showing how clusters merge or split at
different levels.

◆ Agglomerative (Bottom-Up)
• Start: Each data point is its own cluster (n clusters for n points)
• Merge the two closest clusters at each step
• Continue until all points are in one cluster
• Cut the dendrogram at the desired level to get k clusters

◆ Divisive (Top-Down)
• Start: All data points in one big cluster
• Repeatedly split the cluster into smaller ones
• Continue until each point is its own cluster

◆ Linkage Methods (How to Measure Distance Between Clusters)


Method Definition Property
Single Linkage Min distance between any two Sensitive to outliers
points in clusters
Complete Linkage Max distance between any two Creates compact clusters
points in clusters
Average Linkage Average distance between all Good balance
pairs of points
Ward's Method Minimizes increase in total Most popular
variance when clusters merge

▶ 2.4 Categorical Clustering Algorithm (ROCK)


Standard clustering algorithms work with numerical data. For categorical data (like gender, color,
country), special algorithms are needed.

• ROCK (RObust Clustering using linKs): Measures similarity between categorical items using
the concept of 'links' (shared neighbors). Items with more common neighbors are more similar.
• k-Modes: Extension of K-Means for categorical data. Uses mode (most frequent value) instead
of mean.
▶ 2.5 Decision Trees
A Decision Tree is a tree-shaped model used for classification and regression. Each internal node tests
an attribute, each branch represents an outcome, and each leaf node represents a class label
(decision).

📝 Decision trees are like playing '20 Questions': Ask yes/no questions about features and follow
branches to reach a conclusion.

◆ Structure of a Decision Tree


• Root Node: The top node — represents the best splitting attribute (most informative)
• Internal Node: Tests a condition/attribute (e.g., Age > 30?)
• Branch: The outcome of a test (Yes or No, or value range)
• Leaf Node: Final decision or class label (e.g., 'Buy' or 'Don't Buy')

▶ 2.6 Best Split — Splitting Indices and Criteria


The key question in building a decision tree is: Which attribute should we split on first? We use
mathematical measures to find the best split.

◆ Information Gain (used in ID3)


Information Gain measures how much an attribute reduces uncertainty (entropy) in classifying data.
Entropy H(S) = -Σ p_i * log2(p_i) [where p_i = proportion of class i]
Information Gain IG(S, A) = H(S) - Σ (|S_v|/|S|) * H(S_v)
• Higher IG: Better split — attribute removes more uncertainty
• IG = 0: Attribute provides no useful information

📝 Entropy measures 'impurity'. A pure node (all same class) has entropy = 0. A perfectly mixed
node has maximum entropy.

◆ Gini Index (used in CART)


Gini Index measures the probability of incorrect classification when a random sample is classified
randomly.
Gini(S) = 1 - Σ p_i²
Gini Gain = Gini(parent) - weighted Gini(children)
• Gini = 0: Perfect purity (all samples belong to same class)
• Lower Gini: Better split

◆ Gain Ratio (Improved ID3 / C4.5)


Information Gain favors attributes with many values. Gain Ratio corrects this bias.
Gain Ratio = Information Gain / Split Information
Split Information = -Σ (|S_v|/|S|) * log2(|S_v|/|S|)
Criterion Used In Formula
Information Gain ID3 H(S) - H(S|A)
Gain Ratio C4.5 IG / SplitInfo
Gini Index CART 1 - Σ p_i²

▶ 2.7 ID3 Algorithm


ID3 (Iterative Dichotomiser 3) is one of the earliest and simplest decision tree algorithms, developed by
Ross Quinlan.

◆ ID3 Steps
• If all examples belong to same class → Create leaf node with that class
• If no attributes left → Create leaf node with majority class
• Otherwise: Select attribute A with highest Information Gain
• Create a decision node that splits on attribute A
• For each value of A, create a branch and recursively build subtree

◆ Limitations of ID3
• Cannot handle continuous/numerical attributes directly
• Biased towards attributes with many values (solved by Gain Ratio in C4.5)
• Does not handle missing data
• Tends to overfit (solution: pruning)

▶ 2.8 CART (Classification and Regression Trees)


CART is a powerful algorithm that can build both Classification Trees (for categorical output) and
Regression Trees (for numerical output).

◆ Key Features of CART


• Binary Splits Only: Each node splits into exactly 2 branches (unlike ID3 which can have
multiple branches)
• Gini Index: Used for classification problems
• Variance Reduction: Used for regression problems
• Pruning: Uses cost-complexity pruning to prevent overfitting

Classification Tree Output is a category (class label). Uses Gini


Index.
Regression Tree Output is a number. Uses variance reduction /
MSE.
CART vs ID3 CART → binary only, handles continuous data.
ID3 → multi-branch, only categorical.
CART vs C4.5 CART → binary, pruning built-in. C4.5 → multi-
branch, uses Gain Ratio.

▶ 2.9 Rain Forest Algorithm


Rain Forest is an algorithm designed to build decision trees on very large datasets that do not fit in
main memory. It uses a compact data structure called AVC-set (Attribute-Value, Class label set).

• AVC-Set: For each attribute, stores (attribute value, class count) pairs — much smaller than full
data.
• AVC-Group: Collection of AVC-sets for all attributes at a given node.
Rain Forest loads only the AVC-group for the current node, not the entire dataset. This makes it
scalable to very large databases.

▶ 2.10 Pruning Techniques


Pruning reduces the size of a decision tree by removing sections that have little predictive power. This
prevents overfitting (where the model memorizes training data but fails on new data).

◆ Types of Pruning
• Pre-Pruning (Early Stopping): Stop growing the tree early based on a threshold. Example:
Stop if information gain < 0.01, or if node has fewer than 5 samples.
• Post-Pruning: Grow the full tree first, then prune unnecessary branches. More common and
generally better.

◆ Post-Pruning Methods
• Reduced Error Pruning: Remove nodes that don't reduce accuracy on validation set. Simple
and effective.
• Cost-Complexity Pruning: Used in CART. Balances tree size vs. accuracy using a parameter
alpha.
• Minimum Description Length (MDL): Prune if the compressed model + compressed errors is
smaller than the original model.

📝 Overfitting vs Underfitting: Overfitting = model too complex, memorizes noise. Underfitting =


model too simple, misses patterns. Pruning fights overfitting!

▶ 2.11 Data Mining using Neural Networks


Neural Networks (NN) are computing systems inspired by the human brain. They consist of
interconnected nodes (neurons) organized in layers.
◆ Structure of a Neural Network
• Input Layer: Receives the input features (one neuron per feature)
• Hidden Layer(s): Processes information through weighted connections and activation functions
• Output Layer: Produces the final prediction (class or number)

◆ Key Concepts
• Weight: Strength of connection between two neurons — learned during training
• Bias: Additional parameter that helps the model fit data better
• Activation Function: Determines if a neuron fires — examples: Sigmoid, ReLU, Tanh
• Backpropagation: Algorithm to adjust weights by propagating error backward through the
network
• Gradient Descent: Optimization method to minimize error by adjusting weights in the direction
of steepest descent

Advantage Can learn complex non-linear patterns; very


powerful for image, text, speech
Disadvantage Black box — difficult to interpret; needs lots of
data; slow to train
Common Uses in DM Classification, prediction, pattern recognition,
anomaly detection
Deep Learning Neural networks with many hidden layers —
used in modern AI applications

▶ 2.12 Web Mining


Web Mining applies data mining techniques to extract knowledge from web data.

◆ Three Types of Web Mining


• Web Content Mining: Mines the actual content of web pages — text, images, video. Example:
Extracting product reviews, news articles.
• Web Structure Mining: Analyzes the structure (links) between web pages. Example: Google
PageRank algorithm — pages with more links pointing to them are more important.
• Web Usage Mining: Analyzes web server logs to understand user behavior. Example: Which
pages do users visit most? What is their navigation pattern?

▶ 2.13 Temporal Data Mining


Temporal Data Mining deals with data that has a time dimension — data collected over time (time-
series data).

• Time Series: Sequence of data points measured at successive time intervals. Example: Stock
prices, temperature readings.
• Sequence Mining: Finding patterns in ordered sequences of events. Example: 'Customers who
buy A, then B within 2 weeks, often buy C next.'
• Trend Analysis: Identifying upward or downward trends over time
• Periodicity Detection: Finding patterns that repeat at regular intervals (daily, weekly, seasonal)

▶ 2.14 Spatial Data Mining


Spatial Data Mining extracts knowledge from data that has geographic or spatial properties (location-
based data).

• Spatial Clustering: Grouping geographic locations — Example: Clustering crime locations to


identify hotspots.
• Spatial Association: Finding rules about proximity — Example: 'Cities near rivers tend to have
higher flood risk.'
• Spatial Classification: Classifying regions — Example: Classifying land use as agricultural,
urban, or forest.
• Applications: GIS (Geographic Information Systems), urban planning, epidemiology, satellite
image analysis
UNIT Introduction to R Programming & Statistics
3

▶ 3.1 Introduction to R
R is a free, open-source programming language and environment designed specifically for statistical
computing, data analysis, and data visualization. It was created by Ross Ihaka and Robert Gentleman
in 1993.

◆ Why Use R?
• Free and open-source
• Extremely powerful for statistics and data analysis
• Huge collection of packages (CRAN has 18,000+ packages)
• Excellent data visualization capabilities
• Popular in academia, research, and data science industry

▶ 3.2 Basic Elements of R


◆ R as a Calculator
# Basic arithmetic in R
5 + 3 # Addition → 8
10 - 4 # Subtraction → 6
6 * 7 # Multiplication → 42
20 / 4 # Division → 5
2 ^ 3 # Exponentiation → 8
17 %% 5 # Modulus (remainder) → 2
17 %/% 5 # Integer division → 3

▶ 3.3 Objects and Attributes


In R, everything is an object. Objects have attributes like class, type, and dimension.

# Checking object properties


x <- 42
class(x) # "numeric"
typeof(x) # "double"
length(x) # 1

# Attributes
attr(x, "myattr") <- "hello"
attributes(x) # Shows all attributes
◆ Data Types in R
Type Description Example
numeric Decimal numbers (real 3.14, -2.5, 100
numbers)
integer Whole numbers (add L suffix) 5L, 100L, -3L
character Text strings (in quotes) "hello", "R language"
logical Boolean values TRUE, FALSE
complex Complex numbers 3+2i, 1-4i
raw Raw bytes Used for binary data

▶ 3.4 Vectors
A vector is the most fundamental data structure in R. It is a sequence of elements of the same data
type.

# Creating vectors
v1 <- c(1, 2, 3, 4, 5) # Numeric vector
v2 <- c("apple","banana","mango") # Character vector
v3 <- c(TRUE, FALSE, TRUE) # Logical vector

# Sequence vectors
v4 <- 1:10 # 1 2 3 4 5 6 7 8 9 10
v5 <- seq(0, 1, by=0.2) # 0.0 0.2 0.4 0.6 0.8 1.0
v6 <- rep(5, times=4) # 5 5 5 5

# Vector operations
v1 + 10 # Adds 10 to every element
v1 * 2 # Multiplies every element by 2
v1[3] # Third element → 3
v1[c(1,3,5)] # Elements 1, 3, and 5
v1[v1 > 3] # Elements greater than 3 → 4 5

▶ 3.5 Arrays and Matrices


# Matrix (2-dimensional array)
m <- matrix(1:12, nrow=3, ncol=4)
# Creates:
# [,1] [,2] [,3] [,4]
# [1,] 1 4 7 10
# [2,] 2 5 8 11
# [3,] 3 6 9 12

m[2, 3] # Element at row 2, col 3 → 8


m[1, ] # First row
m[ , 2] # Second column
dim(m) # c(3, 4)
nrow(m); ncol(m) # 3 and 4
# Array (multi-dimensional)
a <- array(1:24, dim=c(2,3,4)) # 2x3x4 array

▶ 3.6 Lists
A list can hold elements of different types (unlike vectors which must be the same type). Lists are very
flexible.

# Creating a list
student <- list(
name = "Ramesh",
age = 21,
marks = c(85, 90, 78, 92),
passed = TRUE
)

# Accessing list elements


student$name # "Ramesh"
student[["age"]] # 21
student[[3]] # c(85, 90, 78, 92)
student$marks[2] # 90

# Length of list
length(student) # 4

▶ 3.7 Data Frames


Data frames are the most important structure for data analysis in R. They are like a table/spreadsheet
where each column can have a different data type.

# Creating a data frame


df <- [Link](
Name = c("Alice", "Bob", "Carol"),
Age = c(25, 30, 22),
Score = c(88.5, 76.0, 92.3),
Passed = c(TRUE, TRUE, TRUE)
)

# Accessing data frame


df$Name # Name column
df[2, ] # Second row
df[, 3] # Third column
df[df$Age > 24, ] # Rows where Age > 24

# Useful functions
nrow(df) # Number of rows
ncol(df) # Number of columns
str(df) # Structure of dataframe
summary(df) # Statistical summary
head(df, 3) # First 3 rows
▶ 3.8 Data Input and Output
◆ Reading Data from Files
# Reading CSV file
data <- [Link]("[Link]", header=TRUE, sep=",")

# Reading text file (space separated)


data <- [Link]("[Link]", header=TRUE)

# Reading Excel files (requires readxl package)


library(readxl)
data <- read_excel("[Link]", sheet=1)

# Writing data to CSV


[Link](data, "[Link]", [Link]=FALSE)

# Writing to text file


[Link](data, "[Link]", sep=" ")

▶ 3.9 Control Statements


◆ if-else Statement
# if-else
x <- 15
if (x > 10) {
print("x is greater than 10")
} else if (x == 10) {
print("x equals 10")
} else {
print("x is less than 10")
}
# Output: "x is greater than 10"

◆ switch Statement
day <- "Mon"
result <- switch(day,
Mon = "Monday",
Tue = "Tuesday",
Wed = "Wednesday",
"Unknown" # default case
)
print(result) # "Monday"

▶ 3.10 Loops in R
# for loop
for (i in 1:5) {
cat("Number:", i, "
")
}

# while loop
x <- 1
while (x <= 5) {
cat(x, " ")
x <- x + 1
}
# Output: 1 2 3 4 5

# repeat loop (with break)


count <- 0
repeat {
count <- count + 1
if (count == 3) break
}

# next (skip to next iteration, like 'continue')


for (i in 1:5) {
if (i == 3) next
print(i)
}
# Output: 1 2 4 5 (3 is skipped)

▶ 3.11 Functions in R
# Defining a function
greet <- function(name, greeting="Hello") {
message <- paste(greeting, name)
return(message)
}

# Calling the function


greet("Alice") # "Hello Alice"
greet("Bob", "Hi") # "Hi Bob"

# Function with multiple returns (use a list)


stats <- function(x) {
return(list(
mean = mean(x),
median = median(x),
sd = sd(x)
))
}

result <- stats(c(10, 20, 30, 40, 50))


result$mean # 30
result$sd # 15.81...
▶ 3.12 R Scripts
An R script is a text file with .R extension containing a series of R commands. You can run scripts from
the command line or RStudio.
# Save as my_analysis.R
# Run from command line: Rscript my_analysis.R

# Load data
data <- [Link]("[Link]")

# Process
mean_val <- mean(data$salary)
cat("Average Salary:", mean_val, "
")

# source() runs another R script inside current session


source("helper_functions.R")

▶ 3.13 Data Science Overview


Data Science is an interdisciplinary field that uses scientific methods, algorithms, and systems to
extract knowledge from data in various forms.

◆ Data Science Pipeline


• Data Collection — Gather raw data from various sources
• Data Cleaning — Handle missing values, outliers, errors
• Exploratory Data Analysis (EDA) — Understand data using statistics and visualizations
• Feature Engineering — Create new variables from existing ones
• Modeling — Build predictive/classification models
• Evaluation — Test model performance
• Deployment — Put model into production use

📝 R is especially powerful for EDA, statistical modeling, and visualization. Python is its main
competitor in data science.

▶ 3.14 Data Visualization in R


◆ Base R Graphics
# Basic plots using base R
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 1, 5, 3)

# Line plot
plot(x, y, type="l", main="Line Plot", xlab="X Axis", ylab="Y Axis",
col="blue")

# Scatter plot
plot(x, y, type="p", main="Scatter Plot", pch=16, col="red")

# Bar chart
barplot(c(3, 7, 2, 8, 5), [Link]=c("A","B","C","D","E"),
main="Bar Chart", col="steelblue")

# Histogram
hist(rnorm(100), main="Histogram", xlab="Values", col="lightgreen",
breaks=10)

# Pie chart
pie(c(30, 20, 25, 25), labels=c("A","B","C","D"), main="Pie Chart")

# Box plot
boxplot(rnorm(50), main="Box Plot", ylab="Values", col="orange")

▶ 3.15 ggplot2 — Advanced Visualization


ggplot2 is the most popular and powerful visualization package in R. It follows the Grammar of
Graphics — building plots layer by layer.

◆ Grammar of Graphics Structure


• Data: The dataset to plot
• Aesthetics (aes): Mapping of variables to visual properties (x, y, color, size, shape)
• Geometries (geom_*): The type of plot (points, lines, bars, etc.)
• Facets: Split plot into subplots by a variable
• Scales: Control axis limits, color scales
• Theme: Control non-data elements (fonts, background, gridlines)

library(ggplot2)

# Scatter plot
ggplot(data=mtcars, aes(x=wt, y=mpg, color=cyl)) +
geom_point(size=3) +
labs(title="Car Weight vs. Fuel Efficiency",
x="Weight (1000 lbs)", y="Miles Per Gallon") +
theme_minimal()

# Bar chart
ggplot(diamonds, aes(x=cut, fill=cut)) +
geom_bar() +
labs(title="Diamond Cuts Count") +
theme_bw()

# Histogram
ggplot(diamonds, aes(x=price)) +
geom_histogram(bins=30, fill="steelblue", color="white") +
labs(title="Diamond Price Distribution")

# Line plot
ggplot(economics, aes(x=date, y=unemploy)) +
geom_line(color="red") +
labs(title="US Unemployment Over Time")

geom Function Plot Type Use Case


geom_point() Scatter plot Relationship between two
numeric variables
geom_line() Line chart Trends over time
geom_bar() Bar chart Count of categories
geom_histogram() Histogram Distribution of numeric
variable
geom_boxplot() Box plot Distribution + outliers
geom_density() Density plot Smooth distribution estimate
geom_smooth() Trend line Add regression line to scatter
plot

▶ 3.16 File Formats for Graphics Output


# Saving plots in R

# PNG format (good for web)


png("[Link]", width=800, height=600, res=150)
plot(1:10)
[Link]() # Close device - IMPORTANT!

# PDF format (best quality, scalable, good for reports)


pdf("[Link]", width=8, height=6)
plot(1:10)
[Link]()

# JPEG format (compressed, good for photos)


jpeg("[Link]", width=800, height=600, quality=90)
plot(1:10)
[Link]()

# SVG format (vector, scalable for web)


svg("[Link]", width=8, height=6)
plot(1:10)
[Link]()

# Using ggplot2's ggsave (simpler)


p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
ggsave("[Link]", plot=p, width=8, height=6, dpi=300)

PNG Best for web, supports transparency, lossless


compression
PDF Best for reports and print, vector-based,
scalable
JPEG Good for photos, lossy compression, smaller
file size
SVG Scalable vector graphics, editable, great for
web
TIFF High quality for publishing, large file size
BMP Uncompressed, large files, rarely used in
practice

▶ 3.17 Introduction to Hypothesis Testing


A hypothesis is a testable statement or claim about a population that we want to verify using sample
data.

◆ Types of Hypotheses
Null Hypothesis The default claim — assumes no effect, no difference, or no relationship.
(H₀) Example: 'The new drug has no effect on blood pressure.'

Alternative The claim we want to prove — states there IS an effect, difference, or


Hypothesis (H₁ or relationship. Example: 'The new drug reduces blood pressure.'
Hₐ)

Simple Hypothesis Specifies the exact value of the parameter. Example: H₀: μ = 50

Composite Does not specify an exact value. Example: H₀: μ > 50


Hypothesis

📝 Remember: H₀ is always what we ASSUME to be true. We try to REJECT H₀ using evidence.


If we can't reject it, we 'fail to reject' (not 'accept'!) H₀.

▶ 3.18 Data Sampling


Since we cannot study an entire population, we take a sample and use it to make inferences about the
population.

◆ Sampling Methods
• Simple Random Sampling: Every item has equal chance of being selected. Example: Drawing
names from a hat.
• Systematic Sampling: Select every k-th item from a list. Example: Every 10th customer from a
database.
• Stratified Sampling: Divide population into groups (strata) and sample from each group
proportionally. Example: Sample students from each year (1st, 2nd, 3rd, 4th).
• Cluster Sampling: Divide into clusters, randomly select some clusters, then survey everyone in
those clusters.
• Convenience Sampling: Use whoever is easily available. Biased — not recommended for
research.

# Sampling in R
data <- 1:100

# Simple random sample of 10 items


sample(data, size=10, replace=FALSE)

# Stratified sampling using dplyr


library(dplyr)
df %>%
group_by(category) %>%
sample_n(size=5) # 5 samples from each group

▶ 3.19 Confidence Level and Significance Level


Confidence Level Probability that the confidence interval contains the true population
(1-α) parameter. Common values: 90%, 95%, 99%.

Significance Level Probability of incorrectly rejecting a true null hypothesis (Type I error).
(α) Common value: α = 0.05 (5%).

p-value Probability of observing the test statistic (or more extreme) assuming H₀
is true. If p-value < α, we reject H₀.

Confidence Interval A range of values that likely contains the true parameter. Example: 95%
(CI) CI for mean: x̄ ± 1.96 * (σ/√n).

Confidence Level Significance Level (α) Z Critical Value


90% 0.10 1.645
95% 0.05 1.96
99% 0.01 2.576

▶ 3.20 Hypothesis Tests — Decision Rules


◆ Steps to Perform Hypothesis Test
• Step 1: State H₀ and H₁ clearly
• Step 2: Choose significance level α (usually 0.05)
• Step 3: Select appropriate test (t-test, z-test, chi-square, etc.)
• Step 4: Calculate test statistic
• Step 5: Calculate p-value or find critical value
• Step 6: Decision — If p-value < α, reject H₀. Otherwise, fail to reject H₀.
• Step 7: Conclusion in context of the problem

◆ Types of Errors
Type I Error (α) Rejecting H₀ when it is actually TRUE (False
Positive). Controlled by significance level α.
Type II Error (β) Failing to reject H₀ when it is actually FALSE
(False Negative).
Power (1-β) Probability of correctly rejecting a false H₀.
Higher is better.
Trade-off Decreasing α reduces Type I error but
increases Type II error.

▶ 3.21 Parametric Tests


Parametric tests assume the data follows a specific distribution (usually normal distribution). They are
more powerful than non-parametric tests when assumptions are met.

◆ Z-Test
Used when: Population variance is known AND sample size is large (n > 30).
Test statistic: z = (x̄ - μ₀) / (σ / √n)
# Z-test in R (using BSDA package)
library(BSDA)
[Link](x, mu=50, sigma.x=10, alternative="[Link]")

◆ t-Test
Used when: Population variance is unknown OR sample size is small (n < 30). Assumes data is
approximately normally distributed.
# One-sample t-test
# H0: mean = 50
[Link](x, mu=50, alternative="[Link]")

# Two-sample t-test (compare two groups)


[Link](group1, group2, [Link]=TRUE)

# Paired t-test (before and after measurements)


[Link](before, after, paired=TRUE)

◆ ANOVA (Analysis of Variance)


Used to compare means of 3 or more groups simultaneously.
H₀: All group means are equal. H₁: At least one group mean is different.
# One-way ANOVA
result <- aov(score ~ group, data=df)
summary(result)

# If p-value < 0.05, reject H0 (groups differ)


# Post-hoc test to find which groups differ:
TukeyHSD(result)

◆ Correlation Test
Tests if there is a linear relationship between two numeric variables.
# Pearson correlation
[Link](x, y, method="pearson")
# r ranges from -1 to +1
# r close to ±1 = strong relationship, r near 0 = weak relationship

◆ F-Test
Used to compare the variances of two populations.
# F-test for equality of variances
[Link](group1, group2)

▶ 3.22 Non-Parametric Tests


Non-parametric tests do NOT assume any specific distribution for the data. They are used when data is
ordinal, skewed, or sample size is very small.

Non-Parametric Test Parametric Equivalent Use Case


Wilcoxon Signed-Rank Test One-sample t-test Test median against a value
Mann-Whitney U Test Two-sample t-test Compare two independent
groups
Wilcoxon Rank-Sum Test Two-sample t-test Same as Mann-Whitney
Kruskal-Wallis Test One-way ANOVA Compare 3+ independent
groups
Friedman Test Repeated measures ANOVA Compare 3+ related groups
Chi-Square Test No exact equivalent Test independence/goodness
of fit
Spearman Correlation Pearson Correlation Non-linear relationship, ordinal
data

# Non-parametric tests in R

# Wilcoxon Signed-Rank Test (one sample, median test)


[Link](x, mu=50, alternative="[Link]")
# Mann-Whitney U Test (two independent samples)
[Link](group1, group2, alternative="[Link]")

# Kruskal-Wallis (3+ groups)


[Link](score ~ group, data=df)

# Chi-Square Test of Independence


[Link](table(df$var1, df$var2))

# Spearman Correlation
[Link](x, y, method="spearman")

📝 Choosing the right test: (1) One group? → t-test or Wilcoxon. (2) Two groups, independent? →
t-test or Mann-Whitney. (3) Three+ groups? → ANOVA or Kruskal-Wallis. (4) Categorical data?
→ Chi-Square. (5) Normal distribution? → Parametric. Otherwise → Non-parametric.
▶ Quick Revision Summary — All Units

Topic Key Point Formula / Algorithm


Support Frequency of itemset in DB Support(A) = |A| / |Total|
Confidence Reliability of rule Conf(A→B) = Sup(A∪B)/Sup(A)
Apriori Find frequent itemsets Generate → Prune → Count → Repeat
FP-Growth Faster than Apriori Build FP-Tree → Mine → 2 DB scans
only
K-Medoid Clustering with actual points Select medoids → Assign → Swap →
Repeat
Entropy Impurity measure for ID3 H(S) = -Σ p_i * log2(p_i)
Gini Index Impurity measure for CART Gini = 1 - Σ p_i²
Information Attribute selection in ID3 IG = H(parent) - H(children)
Gain
Pruning Avoid overfitting Remove branches with low accuracy
p-value Hypothesis testing decision p < α → Reject H₀
t-test Compare means (small n) t = (x̄ - μ) / (s/√n)
Chi-Square Categorical data test χ² = Σ (O-E)²/E

All the best for your BCA-603 Exam!


Focus on: Association Rules • Apriori • FP-Tree • Decision Trees (ID3, CART) •
Clustering • R Programming • Hypothesis Testing

You might also like