Module-2
STATISTICAL DATA ANALYSIS
Dr. Sonali Mahure
Contents:
Explanatory Data Analysis and Data Visualization:
Creating
tables of frequencies and proportions,
Cross tabulations of categorical variables,
Descriptive statistics for continuous variables,
Graphs and charts in R.
Comparison & Association Tests in R:
One Sample T Test,
One-way analysis of variance (ANOVA),
Chi-Square test of independence,
Pearson’s Correlation,
Spearman’s Rank-Order Correlation.
Predictive Regression Models:
Linear Regression,
Multiple Linear Regression,
Binary Logistic Regression,
Ordinal Logistic Regression.
Dr. Sonali Mahure
Explanatory Data Analysis and Data Visualization
Creating tables of frequencies and proportions
# Create Data
A frequency table is: data<-c('G','E','E','T','A',
a list of objects with the frequency of each item shown in 'N','S','H','S','A','H','N','I')
the table.
used when evaluating categorical data to determine # Use table() to get the
how frequently a variable appears in their data set frequency table
table <- table(data)
# Printing table
Frequency Tables in R
One way frequency: print(table)
simply use the table() function from the base R, # Use barplot to visualize
simply pass data as its parameter to the function barplot(table)
and this function will further generate the frequency table.
Syntax: table(x)
Dr. Sonali Mahure
Can we pass a list?
my_list <- list('A','B','A','C')
table(my_list)
convert List into Vector
my_list <- list('A','B','A','C')
table(unlist(my_list))
Dr. Sonali Mahure
Can we Pass Arrays? No
arr <- array(c('A','B','A','C','B','A'))
table(arr)
For 2d Array:
arr <- array(c('A','B','A','C','B','A'))
table(arr)
Dr. Sonali Mahure
Create Frequency Table with Proportions(count of one value / total
count)
create a frequency table with proportions:
using the sum() function with the table() function,
here table() function will simply create the frequency table, and
sum() function:returns the addition of the values passed as arguments to the function.
data <- c('G','E','E','T','A','N','S','H','S','A','H','N','I’)
freq <- table(data)
print(freq)
total <- sum(freq)
print(total)
prop <- freq / total
print(prop)
Dr. Sonali Mahure
Create Cumulative Frequency Table
create a Cumulative frequency table with the table() function,
table function will simply create the frequency table, and
use cumsum() function to get the Cumulative sum of all values and setting it into
the table.
cumsum() function:
cumsum() function is used to calculate the cumulative sum of the vector passed as an
arg and its adds step by step.
Syntax: cumsum(x)
Dr. Sonali Mahure
Difference between Sum() and sumsum()
Function Meaning
sum() Adds all values once and gives single output
cumsum() Adds values step by step and gives multiple outputs (running total)
x <- c(2, 4, 6, 8) x <- c(2, 4, 6, 8)
Sum(x) cumsum(x)
Dr. Sonali Mahure
Ex:
Create Data
data<-c('G','E','E','T','A', 'N','S','H','S','A','H','N','I’)
# Use table() to get the frequency table
table<-table(data)
print("Simple Frequency Table")
print(table)
# Use cumsum function to # Create cumulative frequency table
cumsum_table <- cumsum(table)
print("cumulative Frequency Table")
print(cumsum_table)
Dr. Sonali Mahure
Cross tabulations of categorical variables
Two-Way Frequency Tables in R
Cross-tabulation
It is a table used to compare two categorical variables also called a contingency
table
It helps to see relationship between variables.
This arrangement generally involves one categorical variable to define the rows of the
table and another categorical variable to define the columns
the intersections of the rows and columns contain the frequency or count of
observations corresponding to the combinations of the variables.
Example: •Rows → one category (e.g., employee)
•Columns → another category (e.g., complaints)
•Values → count (frequency)
These are essential tools in data analysis when we want to explore the relationships
between two categorical variables.
Dr. Sonali Mahure
# Set seed (same random values every time)
[Link](50)
# Create data
data <- [Link](
employee = c('A','B','A','A','B','C','A','B','C'),
sales = round(runif(9, 2000, 5000), 0),
complaints = c('Yes','No','Yes','Yes','Yes','Yes','No','No','Yes')
)
# runif : generates 9 random sales values
#round(...,0) → removes decimals
# Print data
print(data)
# Cross-tabulation
table(data$employee, data$complaints) #creates cross table
Dr. Sonali Mahure
Calculate total sales of employee using group_by()
# 1. Load the library
library(dplyr)
# 2. CREATE the data first (This fixes the error)
data <- [Link](
employee = c("Alice", "Bob", "Alice", "Bob", "Charlie"),
sales = c(200, 300, 150, 400, 250)
)
# 3. Process the data
total_sales <- data %>%
group_by(employee) %>%
summarize(total_sales = sum(sales))
# 4. Print the result
print(total_sales)
Dr. Sonali Mahure
Descriptive statistics for continuous variables,
Descriptive statistics help us understand numerical data by showing:
Central tendency → average value (mean, median)
Dispersion → how spread the data is (sd, variance)
Distribution → overall data shape
1. Using Base R Functions:
•summary(): This function provides a quick overview for each variable in a data
frame. For numerical variables, it returns the minimum, 1st quartile, median, mean, 3rd
quartile, and maximum. For factor variables, it provides a frequency table.
Ex: summary(my_data)
data<-c(1,2,3,4,4,5,6,7)
summary(data)
Dr. Sonali Mahure
Individual Functions:
One can calculate specific statistics using functions like
mean(), sd() (standard deviation), var() (variance), min(), max(),
median(), range(), and quantile()
Dr. Sonali Mahure
Examples:
x<-c(1,2,3,4,4,5,6,7)
mean(x)
sd(x)
var(x)
min(x)
max(x)
median(x)
sapply(): To apply a specific function to multiple variables in a data frame,
use sapply().
Eg: sapply(my_data, mean, [Link] = TRUE)
Dr. Sonali Mahure
#
Using boxplot()
create the vector with 10 elements
data=c(1:10)
# get five summary
print(boxplot(data))
2. Using Packages:
packages offer more comprehensive or specialized descriptive statistics functions:
•psych package: The describe() function provides a wide range of statistics, including
central tendency, variability, skewness, and kurtosis
Dr. Sonali Mahure
[Link]("psych")
library(psych)
describe(my_data)
pastecs package: The [Link]() function offers a detailed set of descriptive statistics,
including confidence intervals for the mean, standard error, and coefficient of variation.
[Link]("pastecs")
library(pastecs)
[Link](my_data)
dplyr package: Functions like group_by() and summarize() offer a powerful way to
perform group-wise calculations.(tidyverse package)
[Link]("dplyr")
library(dplyr)
my_data %>%
group_by(grouping_variable) %>%
summarize(mean_value = mean(variable_to_summarize, [Link] = TRUE))
Dr. Sonali Mahure
Practice Question
[Link] is a frequency table? Give one example.
[Link] is the use of the table() function in R?
[Link] is a cumulative frequency?
4. What is the difference between frequency and
proportion?
Dr. Sonali Mahure
[Link] does the sum() function do?
[Link] does the cumsum() function do?
[Link] is the difference between sum() and cumsum()?
Dr. Sonali Mahure
8. Can we pass a list directly to table()? Why or why not?
9. How do you convert a list into a vector in R?
Dr. Sonali Mahure
10. What is cross-tabulation?
11. Which function is used to create a cross table in R?
12. What do rows and columns represent in a cross table?
13. Calculate total sales of employee using group_by()
Dr. Sonali Mahure
14. What does the summary() function return?
Dr. Sonali Mahure
16. Write a simple R code to create a frequency table.
17. Write a code to calculate mean of a column.
18. Write a code to create a cumulative sum.
Dr. Sonali Mahure
#
Graphs and charts in R.
R language is mostly used for statistics and data analytics purposes to represent the
data graphically in the software.
To represent those data graphically, charts and graphs are used in R.
There are number of charts and graphs present in R.
Types of R - Charts
Bar Plot or Bar Chart
Pie Diagram or Pie Chart
Histogram
Scatter Plot
Box Plot
Dr. Sonali Mahure
Bar plot
# 1. Create a simple list of numbers (Sales)
sales <- c(50, 80, 30, 90)
# 2. Create a list of names for those numbers
departments <- c("IT", "HR", "Sales", "Admin")
# 3. Create the bar plot
barplot(sales,
[Link] = departments,
col = "orange",
main = "Company Sales by Department",
xlab = "Departments",
ylab = "Sales Score")
Dr. Sonali Mahure
Pie Chart
# 1. Create a list of numbers (Values)
slices <- c(40, 20, 15, 25)
# 2. Create a list of labels for each slice
labels <- c("Apples", "Bananas", "Cherries", "Dates")
# 3. Create the pie chart
pie(slices, labels = labels, col = rainbow(4), main = "Fruit
Distribution")
Dr. Sonali Mahure
Histogram
# 1. Create a list of 10 student exam scores
scores <- c(55, 67, 72, 74, 78, 82, 85, 89, 92, 95)
# 2. Create the histogram
hist(scores, col = "lightgreen", main = "Distribution of
Exam Scores", xlab = "Score Ranges", ylab = "Number of
Students")
Dr. Sonali Mahure
Scatter Plot
# 1. Create data for Study Hours (X-axis)
hours <- c(2, 3, 5, 7, 8, 10, 12)
# 2. Create data for Exam Scores (Y-axis)
scores <- c(50, 55, 70, 75, 88, 92, 95)
# 3. Create the scatter plot
plot(hours, scores, col = "darkblue", pch = 19, main =
"Study Hours vs. Exam Scores", xlab = "Hours Spent
Studying", ylab = "Final Exam Score")
Dr. Sonali Mahure
Box Plot
data=c(1:10)
# get five summary
print(boxplot(data))
Dr. Sonali Mahure
Comparison & Association Tests in R:
1. One Sample T Test,
2. One-way analysis of variance (ANOVA),
3. Chi-Square test of independence,
4. Pearson’s Correlation,
5. Spearman’s Rank-Order Correlation.
Dr. Sonali Mahure
1. One Sample T Test:
One Sample T Test: The one-sample t-test is a statistical method for determining if a
sample's mean significantly varies from an assumed or known population mean.
OR
The One-Sample T-Test is the math tool we use to see if your small group (sample) is
actually different from what everyone assumes (population mean).
Is based on the t-distribution and commonly used when dealing with small sample
size.
Used when the We don't know the "Standard Deviation" of the whole world, only of for
small group.
Real time example: Imagine someone tells you, "The average height of people in this city
is 170 cm." You don't believe them, so you measure 15 people yourself.
Dr. Sonali Mahure
The formula for the one-sample t-test statistic is:
Where:
•xˉ(Sample Mean): The average of your 15 people.
•μ (Population Mean): The "assumed" average (170 cm).
•s (Standard Deviation): How much the heights in your group vary.
•n (Sample Size): How many people you measured (15).
▪ The Two Guesses (Hypotheses)
Before we run the code, we make two guesses:
[Link] Hypothesis (H0): There is no difference. Your group average is basically 170 cm.
[Link] Hypothesis (Ha): There is a difference. Your group is significantly taller
or shorter than 170 cm.
Dr. Sonali Mahure
)
EXAMPLE: determine if the sample mean height, collected from 15 individuals,
significantly differs from the known population mean height of 170 cm.
heights <- c(165, 168, 172, 170, 169, 171, 174,
168, 166, 170, 175, 172, 169, 167, 170)
pop_mean <- 170
result <- [Link](heights, mu = pop_mean)
print(result)
P-value >0.05 then choose Null Hypothesis
P-value < 0.05 then choose Alternative Hypothesis
Dr. Sonali Mahure
The t-statistic is -0.37025, showing a small difference between the sample mean and
170.
The p-value is 0.7167, which is greater than 0.05, so we do not have enough evidence
to reject the null hypothesis.
The 95% confidence interval is [168.19, 171.28], meaning the true population mean
is likely to fall within this range.
The sample mean is 169.73.
There is no strong evidence to suggest that the sample mean height is significantly
different from 170 cm
Dr. Sonali Mahure
2. One-way analysis of variance (ANOVA)
Analysis of Variance, is a statistical method for comparing means among three or more
groups, crucial in understanding group differences and relationships in diverse
fields.
Real Time Example : Imagine you are testing three different fertilizers (A, B, and C)
on plants. You want to know: "Does it matter which fertilizer I use, or do they all
give the same result?"
• The Two Guesses (Hypotheses)
[Link] Hypothesis (H0): All group means are equal. (Fertilizers A, B, and C all work the
same).
[Link] Hypothesis (Ha): At least one group mean is different from the others.
Dr. Sonali Mahure
Example:
We are checking if students perform the same in all three subjects, or if one
subject is much harder/easier than the others."
Dr. Sonali Mahure
1. The Rules (Assumptions): Before we start, we assume:
Independence: A student’s mark in English doesn’t change their mark in Math.
Normality: Most students score near the middle, not just at the extremes.
Equal Variance: The "spread" of marks is similar for all three subjects.
2. Step 1: Set the Guess (Hypothesis)
•H0 (Null): All subjects have the same average marks (\mu_e = \mu_m = \mu_s).
•Ha (Alternative): At least one subject has a different average.
Dr. Sonali Mahure
3. Steps 2: The Calculations (Simplified)
We need to find two types of differences:
[Link] Groups (SS_{between}): How different are English, Math, and Science from
each other?
[Link] Groups (SS_{within}): How much do individual students vary inside the same
subject?
Degrees of Freedom (df):
•df_{between} = k - 1: (3 subjects - 1) = 2
•df_{within} = n - k: (9 total marks - 3 subjects) = 6
Dr. Sonali Mahure
4. Step 3: The F-Value (The Final Result)
We calculate F_{calc}. If this number is big, it means the subjects are very different.
We compare it to F_{table} (the "cut-off" point from a statistics book).
Rule: If F_{calc} > F_{table}, we Reject the Null. The subjects are different!
Dr. Sonali Mahure
Sample Code:
# 1. Create the data based on your table
marks <- c(2, 4, 2, # English
2, 3, 4, # Math
1, 2, 5) # Science
subject <- c(rep("English", 3), rep("Math", 3), rep("Science", 3))
# Put into a data frame
df <- [Link](subject, marks)
# 2. Run ANOVA
results <- aov(marks ~ subject, data = df)
# 3. View the summary
summary(results)
Dr. Sonali Mahure
When you run this, R will show you a p-value.
English Mean: 2.66
Math Mean: 3.00
Science Mean: 2.66
In Conclusion : Because these averages (2.66, 3.0, 2.66) are so close to each other,
your F_{calc} will be very small and your p-value will be greater than 0.05. "Since
the p-value is large, we cannot reject the null. This means there is no significant
difference in how students performed across these three subjects!"
Dr. Sonali Mahure
Practice program
1. A trainer wants to know if three different types of workouts (Yoga, Cardio, and
Weights) burn the same amount of calories. He tracks 4 students in each group.
Determine if there is a significant difference in calories burned between the three
workouts.
Yoga: 150, 160, 155, 145
Cardio: 300, 310, 295, 305
Weights: 250, 260, 245, 255
Dr. Sonali Mahure
Dr. Sonali Mahure
P-value is less than 0.05 so what is the answer?
Null or Alternative.
Dr. Sonali Mahure
Chi-Square test of independence,
The chi-square statistic is used to check if the distributions of categorical variables are
different from each other.
The chi-square test of independence helps to find out if there's a relationship between the
categories of two variables.
There are two main types of data:
numerical (numbers) and
categorical (categories) on which it is performed.
Dr. Sonali Mahure
2. The Two Guesses (Hypotheses)
Before we look at the data, we set the rules:
•H_0 (Null Hypothesis): There is no link. Gender does not affect snack choice.
•H_a (Alternative Hypothesis): There is a link. Gender does affect snack choice.
Dr. Sonali Mahure
Imagine you ask 100 students what their favorite snack is.
•If boys and girls choose snacks at roughly the same rate, they are Independent (No
link).
•If almost all girls choose Chocolate and almost all boys choose Chips, they are Dependent
(There is a link).
Gender Chips Chocolate Total
Boys 25 25 50
Girls 24 26 50
Total 49 51 100
Dr. Sonali Mahure
# 1. Create a "Table" of our survey results
# Rows: Boys, Girls | Columns: Chips, Chocolate
snack_data <- matrix(c(20, 30, # Boys: 20 like Chips, 30 like Chocolate
45, 15), # Girls: 45 like Chips, 15 like Chocolate
nrow = 2, byrow = TRUE)
# Add names so the table is easy to read
colnames(snack_data) <- c("Chips", "Chocolate")
rownames(snack_data) <- c("Boys", "Girls")
# 2. Print the table to show the students
print(snack_data)
# 3. Run the Chi-Square Test
test_result <- [Link](snack_data)
# 4. Show the result
print(test_result)
Dr. Sonali Mahure
p-value:
If p-value < 0.05: The link is REAL. (Gender matters!)
If p-value > 0.05: The link is ACCIDENTAL. (Gender doesn't matter).
Output: If the p-value is 0.000423 the answer is Reject the Null Hypothesis and Accept
the Alternative Hypothesis
Why use this instead of a T-Test?
•T-Test/ANOVA: Use these for Numbers (like Height, Marks, Weight).
•Chi-Square: Use this for Categories (like Gender, Color, Yes/No, Snack Type)
Dr. Sonali Mahure
Visualizing the "Link"
# Create a bar plotbar
plot(snack_data, beside = TRUE, col = c("skyblue", "pink"),
main = "Snack Preference by Gender",
legend = rownames(snack_data))
In conclusion : If the blue and pink bars look very
different across the categories, the Chi-Square test
will give us a low p-value, proving that the snack
choice depends on the gender!"
Dr. Sonali Mahure
Practice QP
1. We want to see if a student's Grade (Junior vs. Senior) affects their favorite Snack
(Fruit vs. Pizza).
Juniors: 40 like Pizza, 10 like Fruit.
Seniors: 15 like Pizza, 35 like Fruit.
Dr. Sonali Mahure
Dr. Sonali Mahure
Pearson’s Correlation,
Pearson correlation is a parametric statistical method used to measure the linear
relationship between two continuous variables.
It indicates both the strength and direction of the and returns a value between -1 and +1.
relationship
There are mainly two types of correlation:
Parametric Correlation: It measures a linear dependence between two variables (x and
y) is known as a parametric correlation test because it depends on the distribution of the
data.
Non-Parametric Correlation: They are rank-based correlation coefficients and are known
as non-parametric correlation. Parameters:
•r : pearson correlation coefficient
•xand y: two vectors of length n
•mx and my: corresponds to the means of x and y,
Dr. Sonali Mahure
respectively.
Implementation of Pearson Correlation Testing
Performing Correlation Test Using [Link]()
We perform the Pearson correlation test which returns the coefficient, p-value and confidence
interval.
[Link]: Performs a test of association between paired samples.
t: Test statistic used to calculate the p-value.
p-value: Indicates the probability of observing the data under the null hypothesis.
alternative hypothesis: States the direction of the correlation (not equal to zero by default).
sample estimates: Returns the computed correlation coefficient.
Dr. Sonali Mahure
Practice Program
# 1. Turn off scientific notation for clear decimals
options(scipen = 999)
# 2. Define the data
hours <- c(1, 2, 3, 4, 5, 6, 7) # Number of hours studing
marks <- c(35, 45, 50, 65, 75, 85, 95) # Marks of 7 students
# 3. Perform the Correlation Test
result <- [Link](hours, marks, method = "pearson")
# 4. Print result
print(result)
Dr. Sonali Mahure
Output:
Correlation Coefficient (r): This will be close to 0.99.
Since it is close to +1, there is a very strong positive link(no of hours increases then
marks will increases.
•p-value: It will be very small ( 0.00001).
•Since p < 0.05, this relationship is Significant. It's not a coincidence."
Dr. Sonali Mahure
the mtcars dataset to show a Negative Correlation
Example: Heavier cars get fewer miles per gallon
# Load necessary libraries
library(ggplot2)
# Scatter plot with a Regression Line
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "blue", size = 2) +
geom_smooth(method = "lm", color = "red", se = FALSE)
+
labs(title = "Weight vs. Mileage",
x = "Weight of Car", y = "Miles per Gallon") +
theme_minimal()
Dr. Sonali Mahure
Spearman’s Rank-Order Correlation
is a statistical method used to evaluate the strength and direction of a monotonic
relationship between two ranked variables.
Unlike Pearson correlation, it does not assume normal distribution or linearity,
making it ideal for ordinal data and non-linear associations.
often denoted as Spearman’s rho (ρ)
•Instead of using raw numbers, this test uses the Rank (1st, 2nd, 3rd) of the data.
•Use this when the relationship looks like a curve rather than a straight line.
• Perfect for things like "Star Ratings" (1 to 5 stars) or "Rankings" (1st place to 10th
place).
Dr. Sonali Mahure
It ranges from -1 to +1:
+1: A perfect positive monotonic relationship.
0: No monotonic relationship.
-1: A perfect negative monotonic relationship.
Where:
ρ is the Spearman Correlation coefficient
di is the difference between the ranks of corresponding variables.
n is the number of observations.
Dr. Sonali Mahure
Dr. Sonali Mahure
Correlation coeff
S is the value of the test statistic (S = 10.871)
p-value is the significance level of the test statistic (p-value = 0.4397).
sample estimates is the correlation coefficient. For Spearman correlation coefficient it’s
named as rho ([Link] = 0.4564).
Dr. Sonali Mahure
Comparsion of all Assumption and sample Tests
Statistical
Core Assumptions (Key Requirements)
Test
ANOVA Normality, Independence, Equal Variance, No Overlap
Chi-Square Categorical Data, Independent Observations, No Numbers
Pearson Linear Trend, Continuous Data, Normal Distribution
Spearman Ranked Data, Monotonic Trend, Non-Parametric
Dr. Sonali Mahure
Pratice Questions
1. A coffee shop claims their large coffee has 95 mg of caffeine. You test 10 cups and find
the caffeine levels are slightly different.
Goal: Determine if the sample mean significantly differs from the known mean of 95 mg.
caffeine <- c(92, 94, 96, 93, 95, 94, 97, 91, 95, 93)
2. A company claims their batteries last for 800 hours. A student tests 12 batteries to see
if the claim is true.
Goal: Determine if the sample mean significantly differs from the known mean of 800
hours.
battery_life <- c(790, 810, 805, 795, 800, 785, 815, 798, 802, 792, 808, 799)
Dr. Sonali Mahure
3. A tech teacher wants to check if three different brands of smartphones (Brand A, Brand
B, and Brand C) have the same battery life. He tests 3 phones from each brand. Goal:
Determine if the average battery life is the same for all three brands.
Brand A: 12, 13, 11 (hours)
Brand B: 13, 12, 14 (hours)
Brand C: 12, 12, 13 (hours)
Dr. Sonali Mahure
[Link] want to see if a student's Gender (Boy vs. Girl) affects their favorite Pen Color
(Blue vs. Black). The Data:
Boys: 25 prefer Blue, 25 prefer Black.
Girls: 24 prefer Blue, 26 prefer Black.
Dr. Sonali Mahure
5. Determine if there is a linear relationship between the number of hours a student spends
studying and their final exam percentage. Since both variables are continuous and expected to
follow a straight-line trend, Pearson is the correct model.
Input: hours <- c(2, 5, 8, 10, 12) marks <- c(40, 55, 70, 80, 92)
Dr. Sonali Mahure
6. A trainer wants to see if there is a consistent linear link between a person’s body
weight and their running speed. We use Pearson here to measure the strength of this
straight-line numeric connection.
Input: weight <- c(60, 70, 80, 90, 100) speed <- c(15, 13, 11, 9, 7)
Dr. Sonali Mahure
7. Two judges rank 5 singers from 1st place to 5th place. Because the data is already in
the form of "ranks" (ordinal) and we want to see if the judges agree, Spearman is the
required model.
Input: judge_A <- c(1, 2, 3, 4, 5) judge_B <- c(2, 1, 3, 5, 4)
Dr. Sonali Mahure
8. A company compares the "Star Rating" (1 to 5 stars) of a product against the "Price
Rank" (Cheapest to Most Expensive). Since star ratings are categories with a specific
order, we use Spearman to find the trend.
Input: price_rank <- c(1, 2, 3, 4, 5) stars <- c(2, 3, 3, 4, 5)
Dr. Sonali Mahure
Predictive Regression Models:
Linear Regression,
Linear regression is a statistical approach used to model the relationship between a
dependent variable and one or more independent variables.
A Real-Time Example: Electricity Bills
The Input (Independent Variable(X)): The number of units of electricity we use.
The Output (Dependent Variable(Y)): The total cost of your monthly bill.
The Logic: If you use 0 units, you might have a small fixed "service fee" (the Intercept).
For every extra unit you use, the bill goes up by a fixed amount (the Slope).
Dr. Sonali Mahure
Key Points to Remember:
Variables: You use X (Units) to find Y (Cost).
Predictive: It helps you guess a future value (like "What will my bill be if I use 500
units?").
Linear: It assumes the relationship looks like a straight line, not a curve.
There are two main types of linear regression:
Simple Linear Regression (single dependent variable(Output), single independent
variable(Input))
Multiple Linear Regression (single dependent variable(Output), multiple independent
variables(Input))
Dr. Sonali Mahure
Linear regression algorithm assumes the following:
Linear relationship: The dependent and independent variables are linearly related.
No multicollinearity: Independent variables should not be highly correlated.
Normal distribution of error terms: Error terms should follow a normal distribution.
No autocorrelation: The error terms should not show patterns.
The linear regression equation is: Y=β0+β1X+ϵ
Where:
Y is the dependent variable. X is the independent variable.
β0 is the intercept. β1 is the slope of the line.
ϵ is the error term.
Dr. Sonali Mahure
1. Example: Years of Experience vs. Salary
In this example, we want to see if we can predict a person's Salary based on their Years
of Experience.
Dr. Sonali Mahure
# 1. Create the dataset
experience <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # Add the "Best Fit" line (red)
salary <- c(30000, 35000, 38000, 45000, abline(model, col = "red", lwd = 2)
51000, 55000, 62000, 68000, 75000, 80000)
# 4. Make a Prediction for 12 years of
# 2. Build the Linear Regression Model experience
model <- lm(salary ~ experience)
new_data <- [Link](experience = 12)
# 3. Create the Visualization (Scatter Plot + predicted_val <- predict(model, new_data)
Regression Line)
# pch = 16 makes the dots solid, col sets the # Print the result clearly
color
print(paste("Predicted Salary for 12 Years:",
plot(experience, salary,
main = "Salary vs Experience", round(predicted_val, 2)))
xlab = "Years of Experience",
ylab = "Salary",
pch = 16,
col = "blue")
Dr. Sonali Mahure
Dr. Sonali Mahure
Easy to Implement: Built-in functions like lm() make it straightforward.
Useful for Prediction: It can predict numeric variables like salary, price, etc.
It cannot handle categorical or non-numeric data.
REAL TIME EXAMPLE:
Dr. Sonali Mahure
Multiple Linear Regression
In Simple Linear Regression, we predicted Salary using only one variable (Experience).
But in the real world, your salary depends on many things at once. Multiple Linear
Regression is like a formula that calculates your value based on a multiple factors:
Experience (How many years?)
Position (Are you a Junior or a Senior?)
Location (Do you work in a high-cost city?)
The model gives each factor a "weight" to see which one impacts your paycheck the most.
In the real world, your salary isn't just about how long you've worked (Experience). It also
depends on your job level (Position) and where you live (Location). Multiple Linear
Regression allows us to look at all these factors at the same time to see which one has the
biggest impact on your paycheck.
Dr. Sonali Mahure
# Load visualization library
library(ggplot2) # 3. Print the Summary (Output Table)
summary(model)
# 1. Create the Dataset
# Position: 1 = Junior, 2 = Senior # 4. Professional Visualization
# Location: 1 = Rural, 2 = City ggplot(salary_data, aes(x = Experience, y =
salary_data <- [Link](
Salary, color = Position, shape = Location))
Experience = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 6, 8,
10), + geom_point(size = 4) +
Position = factor(c(1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, geom_smooth(method = "lm", se = FALSE)
2, 2)),
+ labs(title = "Salary Model: Experience,
Location = factor(c(1, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2,
1, 2)), Position, and Location", subtitle = "Dots =
Salary = c(30000, 32000, 41000, 46000, 58000, Actual People | Lines = Predicted Trends", x
52000, 65000, 62000, 78000, 85000, 38000,
48000, 61000, 66000, 88000) = "Years of Experience", y = "Annual Salary
) ($)") + theme_minimal()
# 2. Build the Multiple Linear Regression Model
# This formula means: Salary depends on Exp + Pos
+ Loc
model <- lm(Salary ~ Experience + Position +
Location, data = salary_data)
Dr. Sonali Mahure
Dr. Sonali Mahure
Binary Logistic Regression,
While Linear Regression predicts a continuous number (like Salary), Binary Logistic
Regression predicts a Yes/No outcome. It calculates the probability of something
belonging to one of two categories.
•What it does: It predicts an outcome that has only two possibilities.
•Simple Words: It calculates the probability (0% to 100%) of something happening.
•Example: Predicting if a student will Pass (1) or Fail (0).
•The Shape: Instead of a straight line, it uses an S-Curve (Sigmoid).
•If the probability is > 0.5, the model says "Yes."
•If the probability is < 0.5, the model says "No."
Dr. Sonali Mahure
Ordinal Logistic Regression.
What it does: It predicts an outcome with multiple categories that have a natural order
or ranking.
Simple Words: It is for when "the order matters, but the distance between them doesn't."
Example: Predicting a student’s performance as Low, Medium, or High.
"Medium" is clearly better than "Low," but we don't know exactly "how much" better it is
in numbers.
Dr. Sonali Mahure
Hands on/ Lab Program:
1. Two Categorical Variables – Discover relationships within a dataset(chia square)
OR
A university health department wants to know if there is a relationship between a
student's Smoking Habits (Never, Occasional, Regular, Heavy) and their Exercise
Frequency (Frequent, Some, None). Use the survey dataset to determine if smoking
habits are independent of how much a student exercises.
Input Data:
Variable 1: Smoke (Categorical)
Variable 2: Exer (Categorical)
Dr. Sonali Mahure
Dr. Sonali Mahure
2. Create Two Dimensional Tables from Multi-Dimensional Cross-Tabulations
OR
A university research team is analyzing a survey of recent graduates to understand the
diversity of their career starts. The team has collected three categorical data points for every
individual: Gender (Male, Female), Age Group (18-30, 31-40, 41-50), and Employment
Status (Employed, Unemployed). The objective is to perform a multi-dimensional cross-
tabulation to see how these factors overlap. By "flattening" the 3D data cube into 2D tables,
the researchers want to isolate specific relationships—such as checking if Employment
Status differs by Gender, or if older graduates have higher employment rates than younger
ones.
Dr. Sonali Mahure
Dr. Sonali Mahure
Dr. Sonali Mahure
3. Create a model for crop yield as a function of the type of fertilizer used. First use aov()
to run the model, then use summary() to print the summary of the model..
# Example dataset: crop yield under different fertilizers
fertilizer_data <- [Link](
Fertilizer = factor(c("A","A","A","B","B","B","C","C","C")),
Yield = c(20, 22, 21, 25, 27, 26, 30, 32, 31))
# View dataset
print(fertilizer_data)
# Build ANOVA model: Yield ~ Fertilizer
model <- aov(Yield ~ Fertilizer, data = fertilizer_data)
# Print summary of the model
summary(model)
Dr. Sonali Mahure
4. Fit a simple linear regression model using the lm() function.
OR
An agricultural researcher wants to understand how the amount of Fertilizer (X)
applied to a field affects the resulting Crop Yield (Y). Using the recorded data.
Fertilizer (10, 20, 30, 40, 50, 60, 70) and Yield (15, 18, 22, 27, 30, 35, 40) .Solve the
problem by building a simple linear regression model in R. Your script must draw a
scatter plot with a red regression line and provide a summary of the model to
determine the relationship.
Dr. Sonali Mahure
# Step 1: Create a dataset # Step 5: Plot the data with regression line
crop_data <- [Link]( plot(crop_data$Fertilizer, crop_data$Yield,
Fertilizer = c(10, 20, 30, 40, 50, 60, 70), main = "Crop Yield vs Fertilizer",
Yield = c(15, 18, 22, 27, 30, 35, 40) xlab = "Fertilizer Amount",
) ylab = "Crop Yield",
print(crop_data) pch = 19, col = "blue")
abline(model, col = "red", lwd = 2)
# Step 3: Fit a simple linear regression model
model <- lm(Yield ~ Fertilizer, data = crop_data)
# Step 4: Print the summary of the model
summary(model)
Dr. Sonali Mahure
Dr. Sonali Mahure