ADVANCED STATISTICAL
METHODS
L. NO. 1.
---
Unit 1 – Introduction to R and Basic Programming Concepts
(Detailed Notes)
---
1. Introduction to R and RStudio
A programming language and environment for statistical
computing, data analysis, and visualization.
Open-source and supported by CRAN (Comprehensive R Archive
Network).
Strong in handling large datasets, statistical models, and advanced
graphics.
Widely used in academia, research, and data science.
RStudio
An IDE (Integrated Development Environment) that makes using R
easier.
Four key panes in RStudio:
1. Source/Script Editor – write and save R programs.
2. Console – run commands interactively.
3. Environment/History – shows variables created and command
history.
4. Files/Plots/Help/Packages/Viewer – manage files, view plots, get
help, and load packages.
---
2. Installing R and RStudio
Download R from [Link]
Download RStudio from [Link]
Open RStudio → verify console is linked to R.
Check version:
[Link]
---
3. Writing and Executing First Program
Open script editor, type:
print("Hello, World!")
Save script → run using Ctrl + Enter (Windows) or Cmd + Enter
(Mac).
Console shows:
[1] "Hello, World!"
---
4. Help and Documentation
Function help: ?mean or help(mean)
Search help topics: [Link]("linear regression")
General help page: [Link]()
Vignette (examples for packages):
vignette("ggplot2-specs")
---
5. Data Types in R
Character → "apple", "data science"
Numeric → 3.14, 10.5
Integer → 5L (L tells R it is integer)
Logical → TRUE, FALSE
Complex → 3+2i
Check data type:
x <- 10
typeof(x) # "double"
class(x) # "numeric"
---
6. Data Structures in R
1. Vector
One-dimensional, same type.
v <- c(1,2,3,4)
length(v) #4
2. List
Collection of different data types.
l <- list(1,"R",TRUE,3+2i)
3. Matrix
Two-dimensional, same type.
m <- matrix(1:6, nrow=2, ncol=3)
4. Array
Multi-dimensional data.
a <- array(1:8, dim=c(2,2,2))
5. Data Frame
Table-like structure (rows + columns).
df <- [Link](Name=c("A","B"), Age=c(21,22))
6. Factor
Represents categorical data.
gender <- factor(c("Male","Female","Male"))
---
7. Operators in R
Arithmetic → +, -, *, /, ^, %% (modulus), %/% (integer division).
Relational → <, >, ==, !=, <=, >=.
Logical → & (AND), | (OR), ! (NOT).
Assignment → <-, =, ->.
Example:
x <- 10
y <- 3
x %/% y # 3
x %% y # 1
---
8. Variables and Expressions
Variable naming rules
Must start with letter, can contain numbers, _, .
Case-sensitive (Var1 ≠ var1)
Example:
a <- 5
b <- 3
c <- a + b
---
9. Flow Control Structures
If-Else
x <- -5
if (x > 0) {
print("Positive")
} else if (x == 0) {
print("Zero")
} else {
print("Negative")
}
For Loop
for(i in 1:5){
print(i)
}
While Loop
i <- 1
while(i <= 5){
print(i)
i <- i + 1
}
Repeat Loop
i <- 1
repeat {
print(i)
if(i == 5) break
i <- i + 1
}
---
10. Functions in R
Built-in:
mean(c(1,2,3)) # 2
sqrt(16) #4
sum(1:10) # 55
User-defined:
add <- function(a,b){
return(a+b)
}
add(10,5)
---
11. Web Scraping in R
Web scraping = extracting structured data from websites.
Packages used:
rvest → extract HTML data.
httr → handle HTTP requests.
xml2 → parse HTML/XML.
Example:
library(rvest)
url <- "[Link]
page <- read_html(url)
# Extract text from paragraph tags
text <- html_text(html_nodes(page, "p"))
print(text)
---
Q & A – Unit 1 (Intro to R and Basic Programming Concepts)
Q1. What is R? Why is it used?
Answer:
R is an open-source programming language designed for statistical
computing, data analysis, and visualization.
It is widely used because:
Has thousands of built-in functions and packages.
Handles large datasets and complex statistical models.
Supports data visualization (graphs, plots, dashboards).
Free and supported by a large community.
---
Q2. What is RStudio? How is it different from R?
Answer:
R is the programming language.
RStudio is an IDE (Integrated Development Environment) for R.
RStudio provides:
Script editor for writing code.
Console to run commands.
Environment/history to track variables and commands.
File/Plots/Help/Packages tabs for easy management.
---
Q3. How do you install and start R and RStudio?
Answer:
1. Download R from CRAN website.
2. Install RStudio from Posit (RStudio official site).
3. Open RStudio → console should be linked with R.
4. Verify with command: [Link].
---
Q4. Write and execute your first program in R.
Answer:
print("Hello, World!")
Output:
[1] "Hello, World!"
---
Q5. How can you use help and documentation in R?
Answer:
?functionName → Example: ?mean
help(functionName) → Example: help(sum)
[Link]("linear regression") → searches related help.
[Link]() → opens help web page.
vignette("ggplot2-specs") → shows package examples.
---
Q6. What are the basic data types in R? Give examples.
Answer:
1. Character → "apple", "data science"
2. Numeric (double) → 3.14, 12.5
3. Integer → 5L
4. Logical → TRUE, FALSE
5. Complex → 3+2i
---
Q7. What are the main data structures in R?
Answer:
1. Vector → c(1,2,3,4)
2. List → list(1,"R",TRUE)
3. Matrix → matrix(1:6, nrow=2, ncol=3)
4. Array → array(1:8, dim=c(2,2,2))
5. Data Frame → [Link](Name=c("A","B"), Age=c(21,22))
6. Factor → factor(c("Male","Female","Male"))
---
Q8. Explain operators in R with examples.
Answer:
Arithmetic: +, -, *, /, ^, %%, %/%
Example: 5 %% 2 = 1
Relational: <, >, ==, !=, <=, >=
Logical: &, |, !
Assignment: <-, =, ->
---
Q9. Explain control structures in R with examples.
Answer:
If-Else:
x <- 5
if(x > 0) print("Positive") else print("Negative")
For Loop:
for(i in 1:3){ print(i) }
While Loop:
i <- 1
while(i <= 3){ print(i); i <- i+1 }
Repeat Loop:
i <- 1
repeat { print(i); if(i==3) break; i <- i+1 }
---
Q10. What are built-in and user-defined functions in R?
Answer:
Built-in functions: Already available in R.
Examples: mean(), sum(), sqrt(), length().
User-defined functions: Created by the user.
add <- function(a,b){ return(a+b) }
add(10,5)
---
Q11. What is web scraping in R? Which packages are used?
Answer:
Web scraping = extracting data from websites.
Popular packages:
rvest (HTML extraction)
httr (HTTP requests)
xml2 (parsing HTML/XML)
Example:
library(rvest)
page <- read_html("[Link]
text <- html_text(html_nodes(page, "p"))
print(text)
---
Q12. Differentiate between Data Frame and Matrix in R.
Answer:
Matrix → Only one data type allowed (numeric, character, etc.), 2D.
Data Frame → Allows different data types in columns, table-like
structure.
---
Unit 2 – Data Handling, Manipulation, and Visualization in R
(Detailed Notes)
---
1. File Operations in R
Reading Data Files
Text files (.txt)
data <- [Link]("[Link]", header=TRUE, sep="\t")
header=TRUE → first row is column names.
sep="\t" → tab-separated.
CSV files (most common)
data <- [Link]("[Link]", header=TRUE, stringsAsFactors=FALSE)
stringsAsFactors=FALSE prevents automatic conversion to factor.
Excel files (.xlsx) → need readxl package
library(readxl)
data <- read_excel("[Link]", sheet=1)
SPSS files (.sav) → need haven package
library(haven)
data <- read_sav("[Link]")
SAS files (.sas7bdat)
library(haven)
data <- read_sas("dataset.sas7bdat")
---
Writing Data Files
Write to CSV
[Link](data, "[Link]", [Link]=FALSE)
Write to Text file
[Link](data, "[Link]", sep="\t")
---
2. Data Transformation and Exploration
Subsetting Data
Select rows/columns:
df[1:5, ] # first 5 rows
df[, "Age"] # select column Age
df[df$Age > 25, ] # rows where Age > 25
Merging Data
Combine datasets by column values.
merged <- merge(df1, df2, by="ID")
Concatenating Data
Row bind (add rows):
new_df <- rbind(df1, df2)
Column bind (add columns):
new_df <- cbind(df, newVar)
---
3. Apply Family of Functions
The apply family avoids writing loops.
apply() → works on rows/columns of matrix/data frame.
m <- matrix(1:9, nrow=3)
apply(m, 2, sum) # column sums
apply(m, 1, mean) # row means
lapply() → applies function to list, returns list.
lapply(list(1:4, 1:6), sum)
sapply() → similar to lapply(), but returns vector/matrix.
sapply(list(1:4, 1:6), sum)
tapply() → applies function over groups.
tapply(mtcars$mpg, mtcars$cyl, mean) # mean mpg by cylinder
group
---
4. Inspecting Data
Check structure:
str(df)
Class/type of object:
class(df)
Length (vector/list):
length(v)
Number of rows/columns:
nrow(df); ncol(df)
Preview data:
head(df, 10) # first 10 rows
tail(df, 10) # last 10 rows
---
5. Exploratory Data Analysis (EDA)
Definition:
EDA = Process of analyzing data sets by summarizing their main
characteristics, often with visual methods.
Importance:
Understand data structure.
Detect missing values.
Identify outliers.
Get summary statistics.
Decide preprocessing steps.
Summary Statistics
Mean, median, variance, standard deviation.
mean(df$Age)
median(df$Age)
sd(df$Age)
var(df$Age)
summary(df)
Outlier Detection
Using boxplot:
boxplot(df$Salary)
Values outside whiskers are potential outliers.
---
6. Data Visualization in R
(A) Base Graphics
Line Plot
plot(x, y, type="l", col="blue", main="Line Plot", xlab="X",
ylab="Y")
Bar Plot
barplot(c(4,6,8), [Link]=c("A","B","C"), col="green", main="Bar
Chart")
Histogram
hist(df$Age, col="orange", main="Histogram of Age", xlab="Age")
Pie Chart
pie(c(25,35,40), labels=c("A","B","C"), col=c("red","blue","green"))
---
(B) Lattice Graphics
Lattice system is for multivariate data visualization.
library(lattice)
xyplot(mpg ~ wt | cyl, data=mtcars)
---
(C) ggplot2 Graphics (most powerful & flexible)
Scatterplot
library(ggplot2)
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(color="blue") +
ggtitle("Weight vs MPG")
Histogram
ggplot(mtcars, aes(x=mpg)) +
geom_histogram(binwidth=2, fill="orange", color="black")
Bar Plot
ggplot(mtcars, aes(x=factor(cyl))) +
geom_bar(fill="steelblue") +
xlab("Cylinders") + ylab("Count")
---
7. Customizing Plots
Titles and Labels (Base R)
plot(x, y, main="My Title", xlab="X-axis", ylab="Y-axis")
Legends (Base R)
legend("topright", legend=c("Group1","Group2"),
col=c("red","blue"), lty=1)
Colors
Predefined: "red", "blue".
Palettes: rainbow(5), [Link](5), [Link](5).
Themes in ggplot2
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
theme_minimal()
---
Q&A
File Operations
Q1. How do you read a CSV file in R?
👉 Using [Link]("[Link]").
Q2. How do you write data into a CSV file in R?
👉 Using [Link](dataframe, "[Link]", [Link] = FALSE).
Q3. Which function is used to read Excel files in R?
👉 read_excel("[Link]") from the readxl package.
Q4. Can R read SPSS and SAS files? If yes, how?
👉 Yes, using the foreign or haven package ([Link](), read_sas()).
---
Data Transformation & Exploration
Q5. What is subsetting in R?
👉 Selecting a part of the dataset using index or condition. Example:
df[1:5, ] (first 5 rows).
Q6. What is the difference between cbind() and rbind()?
👉
cbind() = column bind (adds new columns).
rbind() = row bind (adds new rows).
Q7. What is the use of the merge() function?
👉 To join two datasets by common columns or row names.
Q8. What are apply family functions in R?
👉 Functions for applying operations over data:
apply() → arrays/matrices.
lapply() → list, returns list.
sapply() → list, returns vector/matrix.
tapply() → applies function over grouped subsets.
---
Inspecting Data
Q9. Which function shows the structure of a dataset?
👉 str(data)
Q10. How to check the number of rows and columns?
👉 nrow(data) and ncol(data)
Q11. What does head() and tail() do?
👉 Shows first and last few rows of a dataset.
---
Exploratory Data Analysis (EDA)
Q12. What is the importance of EDA?
👉 Helps understand data distribution, detect outliers, identify
missing values, and prepare data for modeling.
Q13. Which function gives summary statistics in R?
👉 summary(data)
Q14. How to detect outliers in R?
👉 Using boxplots or statistical methods like IQR rule.
---
Visualization in R
Q15. Which R packages are used for visualization?
👉 Base R (built-in), lattice, ggplot2.
Q16. How to create a histogram in R?
👉 hist(data$column)
Q17. How to create a bar plot in R?
👉 barplot(table(data$column))
Q18. How to create a pie chart in R?
👉 pie(table(data$column))
Q19. How to customize plots with labels and colors?
👉 Using arguments like main="Title", xlab="X-axis", ylab="Y-axis",
col="blue".
Q20. What is the difference between base graphics, lattice, and
ggplot2?
👉
Base → Simple, quick plots.
Lattice → Good for conditioning plots (multi-panel).
ggplot2 → Advanced, layered graphics for customization.
---
Unit – Hypothesis Testing & Statistical Inference (Detailed Notes)
---
1. Foundations of Hypothesis Testing
Steps in Hypothesis Testing:
1. State Null Hypothesis (H₀) and Alternative Hypothesis (H₁).
2. Choose significance level (α) (commonly 0.05 or 0.01).
3. Select appropriate test statistic (Z, t, F, Chi-square, etc.).
4. Compute the test statistic and p-value using R.
5. Compare p-value with α:
If p ≤ α → Reject H₀.
If p > α → Fail to reject H₀.
Errors in Hypothesis Testing:
Type I Error (α): Rejecting H₀ when it is true.
Type II Error (β): Failing to reject H₀ when it is false.
---
2. Z-Test
When to use:
Population variance known, or sample size n > 30.
Formula:
Z = \frac{\bar{X} - \mu}{\sigma / \sqrt{n}}
library(BSDA)
[Link](x, mu = 50, sigma.x = 10, [Link] = 0.95)
Use Case: Check if the average height of students differs from 160
cm.
---
3. t-Test
When to use:
Population variance unknown, n < 30.
Types & R Commands:
1. One-sample t-test → [Link](x, mu = 50)
2. Two-sample independent t-test → [Link](x, y, [Link] = TRUE)
3. Paired t-test → [Link](before, after, paired = TRUE)
Example: Compare average marks between two classes.
---
4. F-Test
Purpose: Compare variances of two populations.
Formula:
F = \frac{s_1^2}{s_2^2}
[Link](x, y)
Also forms the basis of ANOVA.
---
5. Chi-Square Test
When to use: For categorical data.
Formulas:
\chi^2 = \sum \frac{(O - E)^2}{E}
1. Goodness-of-fit test: [Link](table(data))
2. Test of independence: [Link](table(var1, var2))
Example: Is there an association between gender and product
preference?
---
6. Testing Proportions
One-sample proportion test:
[Link](x = 45, n = 100, p = 0.5)
Two-sample proportion test:
[Link](x = c(30, 50), n = c(100, 120))
Example: Compare proportion of voters preferring two candidates.
---
7. Correlation Testing
Types:
Pearson → linear correlation.
Spearman → rank correlation.
R Example:
[Link](x, y, method = "pearson")
[Link](x, y, method = "spearman")
Output: Correlation coefficient (r) + significance test.
---
8. ANOVA (Analysis of Variance)
(a) One-way ANOVA
Purpose: Compare means of >2 groups based on one factor.
R Example:
result <- aov(marks ~ college, data = df)
summary(result)
(b) Two-way ANOVA
Purpose: Compare means with two factors + interaction effect.
R Example:
result <- aov(marks ~ gender * college, data = df)
summary(result)
Post-hoc Test (Tukey):
TukeyHSD(result)
---
9. Cross Tabulations
Used for: Summarizing categorical data.
R Example:
table(df$Gender, df$Preference)
xtabs(~ Gender + Preference, data = df)
---
10. Domain-Specific Case Studies in R
Marketing: Compare conversion rates of two ad campaigns → two-
sample proportion test.
Healthcare: Compare recovery times for two drugs → two-sample t-
test.
Education: Test if exam scores differ across subjects → One-way
ANOVA.
Business Analytics: Association between gender & product choice
→ Chi-square test.
---
Q & A – Hypothesis Testing & Statistical Inference
---
Basics
Q1. What is a hypothesis in statistics?
👉 A statement or assumption about a population parameter that
we test using sample data.
Q2. What are the types of hypotheses?
👉
Null Hypothesis (H₀): No difference / no effect.
Alternative Hypothesis (H₁): Some difference / effect exists.
Q3. What are Type I and Type II errors?
👉
Type I Error: Rejecting H₀ when it is true (false positive).
Type II Error: Failing to reject H₀ when it is false (false negative).
---
Z-Test
Q4. When do we use Z-test?
👉 When population variance is known, or sample size > 30.
Q5. Which R function is used for Z-test?
👉 [Link]() from BSDA package.
---
t-Test
Q6. What is the purpose of t-test?
👉 To compare sample mean(s) when population variance is
unknown.
Q7. What are the types of t-tests?
👉 One-sample, Two-sample (independent), Paired t-test.
Q8. How do you run a t-test in R?
👉 [Link](x, mu=50)
---
F-Test
Q9. What does the F-test check?
👉 Whether variances of two populations are equal.
Q10. Which R function is used for F-test?
👉 [Link](x, y)
---
Chi-Square Test
Q11. When is Chi-square test used?
👉 For categorical data (goodness-of-fit or independence).
Q12. What is the formula for Chi-square statistic?
👉
\chi^2 = \sum \frac{(O - E)^2}{E}
Q13. How do you perform Chi-square test in R?
👉 [Link](table(var1, var2))
---
Proportion Tests
Q14. What is tested by a proportion test?
👉 Whether population proportions equal a hypothesized value or
differ between groups.
Q15. Which R function is used?
👉 [Link](x, n, p=...)
---
Correlation Testing
Q16. What does correlation testing do?
👉 Tests if there is a significant relationship between two variables.
Q17. Which R function is used?
👉 [Link](x, y)
Q18. Difference between Pearson and Spearman correlation?
👉
Pearson → linear relationship.
Spearman → rank-based relationship.
---
ANOVA
Q19. What is the purpose of ANOVA?
👉 To test differences between means of multiple groups.
Q20. Difference between One-way and Two-way ANOVA?
👉
One-way ANOVA → one factor.
Two-way ANOVA → two factors + possible interaction effect.
Q21. How do you run One-way ANOVA in R?
👉 aov(y ~ factor, data=df)
Q22. How do you test differences between groups after ANOVA?
👉 Post-hoc Tukey test: TukeyHSD(result)
---
Cross Tabulations
Q23. What is cross-tabulation?
👉 A table summarizing distribution of two categorical variables.
Q24. Which R functions are used?
👉 table(var1, var2) or xtabs(~ var1 + var2, data=df)
---
Case Studies
Q25. Which test would you use in Marketing to compare success
rates of two ad campaigns?
👉 Two-sample proportion test.
Q26. In Healthcare, how do you compare mean recovery times of
two treatments?
👉 Independent two-sample t-test.
Q27. In Education, how do you test if scores differ across subjects?
👉 One-way ANOVA.
Q28. In Business Analytics, how do you check if gender affects
product preference?
👉 Chi-square test of independence.
--
Unit – Regression and Classification Techniques (Detailed Notes)
---
1. Linear Regression
Concept: Predicts a continuous dependent variable (Y) using one or
more independent variables (X).
Simple Linear Regression:
Y = β_0 + β_1X + ε
R function: lm(y ~ x, data = df)
Multiple Linear Regression:
Y = β_0 + β_1X_1 + β_2X_2 + … + β_nX_n + ε
R function: lm(y ~ x1 + x2 + x3, data = df)
Assumptions of Linear Regression:
1. Linearity
2. Independence of errors
3. Homoscedasticity (equal variance of residuals)
4. Normality of residuals
5. No multicollinearity among predictors
Multicollinearity:
When predictors are highly correlated.
Detected by VIF (Variance Inflation Factor) (car::vif(model)).
Residual Analysis:
Check model fit and assumption violations.
Residual plots, Q-Q plots.
---
2. Logistic Regression
Concept: Predicts a binary outcome (0/1, Yes/No).
Model Equation:
\log\left(\frac{p}{1-p}\right) = β_0 + β_1X_1 + β_2X_2 + … + β_nX_n
Key Terms:
Odds = probability of success / probability of failure.
Odds Ratio (OR): effect of one unit change in X on odds.
Log Likelihood: used for model fit.
Model Evaluation:
Classification Table (Confusion Matrix).
ROC Curve (plot of TPR vs FPR).
AUC (Area under ROC curve) – higher = better model.
R Functions:
Logistic regression: glm(y ~ x1 + x2, data, family = binomial)
ROC curves: pROC::roc()
---
3. Discriminant Analysis
Linear Discriminant Analysis (LDA):
Used for classification when dependent variable is categorical.
Finds a linear combination of predictors that best separates groups.
R function: MASS::lda()
Classification Performance:
Confusion matrix, misclassification rate.
Cross-validation.
---
4. Stepwise Regression
Purpose: Selects the best subset of predictors automatically.
Methods:
Forward Selection – start with no predictors, add one by one.
Backward Elimination – start with all predictors, remove
insignificant ones.
Stepwise – combination of both.
R function: stepAIC(model, direction = "both")
---
5. Dummy Variable Regression
Used for categorical predictors.
Example: Gender (Male/Female) → Dummy coding: Male = 0,
Female = 1.
R automatically handles dummy variables with lm() or glm().
---
6. Dimension Reduction
a) Principal Component Analysis (PCA)
Purpose: Reduce dimensionality of data while retaining maximum
variance.
Steps:
1. Standardize data
2. Find eigenvalues & eigenvectors of covariance matrix
3. Form principal components (PC1, PC2, …).
Interpretation:
PC1 explains most variance, PC2 next, etc.
Scree plot used to decide number of components.
R function: prcomp(data, scale = TRUE)
b) Factor Analysis
Purpose: Identify underlying latent variables (factors) that explain
correlations among observed variables.
Difference from PCA: PCA focuses on variance, Factor Analysis on
correlations & latent structure.
R function: factanal(data, factors = k)
---
Regression & Classification – Q&A
1. Linear Regression
Q1. What is the difference between simple and multiple linear
regression?
Simple Linear Regression uses one predictor variable.
Multiple Linear Regression uses two or more predictors.
Q2. What are the key assumptions of linear regression?
Linearity, Independence of errors, Homoscedasticity, Normality of
residuals, No multicollinearity.
Q3. How can multicollinearity be detected?
Using Variance Inflation Factor (VIF). High VIF (> 10) indicates
multicollinearity.
Q4. What is residual analysis?
Checking the difference between actual and predicted values.
Used to verify assumptions (normality, homoscedasticity).
---
2. Logistic Regression
Q5. When is logistic regression used?
When the dependent variable is binary (e.g., Yes/No, 0/1).
Q6. What are odds and odds ratio in logistic regression?
Odds = Probability(success) / Probability(failure).
Odds Ratio (OR) shows how odds change with a one-unit increase
in a predictor.
Q7. What is the ROC curve?
Receiver Operating Characteristic curve: plots True Positive Rate vs.
False Positive Rate.
Used to evaluate classification performance.
Q8. What does AUC represent?
Area Under the Curve.
AUC close to 1 = good model, 0.5 = random guessing.
---
3. Discriminant Analysis
Q9. What is Linear Discriminant Analysis (LDA)?
A classification method that finds linear combinations of predictors
to separate classes.
Q10. How is classification performance measured in LDA?
Using confusion matrix, accuracy, and misclassification rate.
---
4. Stepwise Regression
Q11. What is stepwise regression?
An automatic method for selecting predictors.
Can be forward selection, backward elimination, or both.
Q12. Why is stepwise regression used?
To reduce overfitting and improve model simplicity by removing
unnecessary variables.
---
5. Dummy Variable Regression
Q13. Why are dummy variables used?
To include categorical variables (like gender, region) in regression
models.
Q14. How does R handle dummy variables?
Automatically converts categories into 0/1 indicators when fitting
models.
---
6. Dimension Reduction
Q15. What is PCA and why is it used?
Principal Component Analysis reduces the number of variables
while preserving maximum variance.
Q16. How is the number of principal components decided?
Using a scree plot or by choosing components that explain >80%
variance.
Q17. What is the difference between PCA and Factor Analysis?
PCA: focuses on variance in data.
Factor Analysis: focuses on underlying latent constructs
(correlations).
Q18. Give a business example of using PCA.
In marketing, PCA can reduce hundreds of customer survey
variables into a few key dimensions like satisfaction, loyalty, price
sensitivity.
---
Time Series and Predictive Analytics – Full Detailed Notes
---
1. Time Series Data in R
Definition
A time series is a sequence of data points collected at successive,
equally spaced time intervals.
Examples:
Daily stock prices
Monthly rainfall
Quarterly GDP
Hourly website traffic
Key Features of Time Series
1. Stationarity
A stationary series has constant mean, variance, and
autocorrelation over time.
Many statistical models (ARMA, ARIMA) require stationarity.
If not stationary → use differencing, log transformation, or
detrending.
2. Trend
Long-term upward/downward movement in data.
Example: GDP growth, long-term rise in sales.
3. Seasonality
Regular repeating pattern over a fixed period.
Example: Ice cream sales peak in summer, dip in winter.
4. Cycle
Long-term economic/business cycles, usually longer than seasonal
effects.
5. Residual/Noise
Unpredictable random variation left after accounting for trend &
seasonality.
---
R Data Structures for Time Series
Use ts() to create a time series object.
# Monthly sales data from Jan 2020
sales_ts <- ts(sales_data, start = c(2020,1), frequency = 12)
plot(sales_ts, main="Monthly Sales Time Series", col="blue")
frequency = 12 → monthly
frequency = 4 → quarterly
frequency = 1 → yearly
---
2. Decomposition of Time Series
Purpose
To separate the series into components:
Trend (T) → long-term movement
Seasonality (S) → repeating short-term patterns
Residual (R) → random variation
Models
1. Additive Model
Y_t = T_t + S_t + R_t
2. Multiplicative Model
Y_t = T_t \times S_t \times R_t
R Example
decomp <- decompose(sales_ts, type="additive")
plot(decomp)
Output shows trend line, seasonal effect, residual noise.
For advanced decomposition:
stl(sales_ts, [Link]="periodic")
---
3. ACF and PACF Plots
Autocorrelation Function (ACF)
Measures correlation of series with its lagged values.
Example: ACF lag=1 shows correlation between Yt and Yt-1.
Helps detect MA (moving average) terms.
Partial Autocorrelation Function (PACF)
Shows correlation between Yt and Yt-k after removing effects of
intermediate lags.
Helps detect AR (autoregressive) terms.
R Example
acf(sales_ts, main="ACF Plot")
pacf(sales_ts, main="PACF Plot")
Use ACF & PACF plots to determine p (AR order) and q (MA order)
for ARIMA.
---
4. Forecasting Methods
a) Simple Exponential Smoothing (SES)
Used for short-term forecasting without trend/seasonality.
Formula:
F_{t+1} = αY_t + (1-α)F_t
R Example:
library(forecast)
ses_model <- ses(sales_ts, h=12)
plot(ses_model)
---
b) Holt’s Linear Trend Method
Handles data with trend but no seasonality.
Two smoothing constants:
α (level)
β (trend)
Equations:
Level: Lt = αYt + (1-α)(Lt-1 + Tt-1)
Trend: Tt = β(Lt - Lt-1) + (1-β)Tt-1
Forecast: Ft+m = Lt + mTt
R Example:
holt_model <- holt(sales_ts, h=12)
plot(holt_model)
---
c) Holt-Winters Method
Suitable for data with trend + seasonality.
Three parameters:
α (level)
β (trend)
γ (seasonality)
R Example:
hw_model <- HoltWinters(sales_ts)
plot(hw_model)
forecast(hw_model, h=12)
---
5. ARMA and ARIMA Models
ARMA (Autoregressive Moving Average)
AR(p): depends on past values.
MA(q): depends on past forecast errors.
ARMA(p,q): combination of both.
ARIMA (Autoregressive Integrated Moving Average)
ARIMA(p,d,q):
p = AR order
d = differencing order (to make data stationary)
q = MA order
Steps in ARIMA Modeling
1. Check stationarity (using ADF test).
2. Apply differencing if non-stationary.
3. Identify p, q from ACF & PACF plots.
4. Fit ARIMA model.
5. Validate using residuals.
R Example
library(forecast)
fit <- [Link](sales_ts)
forecast(fit, h=12)
[Link]() selects best parameters automatically.
---
6. Model Validation
Error Metrics
MAE (Mean Absolute Error)
MAE = \frac{1}{n} \sum |Y_t - \hat{Y}_t|
MSE (Mean Squared Error)
MSE = \frac{1}{n} \sum (Y_t - \hat{Y}_t)^2
RMSE (Root Mean Squared Error) = √MSE
MAPE (Mean Absolute Percentage Error)
MAPE = \frac{100}{n} \sum \left| \frac{Y_t - \hat{Y}_t}{Y_t} \right|
R Example
accuracy(fit)
Residual Analysis
Residuals should be white noise (mean=0, no autocorrelation).
Check with Ljung-Box Test:
[Link](residuals(fit), type="Ljung-Box")
---
7. Applications of Time Series Forecasting
Finance: Predict stock prices, interest rates.
Retail: Sales forecasting → optimize inventory.
Economics: Forecast GDP, unemployment, inflation.
Healthcare: Predict hospital patient inflow, disease spread.
Weather: Rainfall, temperature, storms.
Energy & Utilities: Forecast electricity demand.
---
✅ With this detailed breakdown, you have theory + formulas + R
functions + interpretation for every part of Time Series.
---
❓ Q&A on Time Series and Predictive Analytics
1. Time Series Data Structures in R
Q1. What is a time series in statistics?
A: A time series is a sequence of data points collected at regular
time intervals (e.g., daily sales, monthly stock prices). It helps
analyze trends, seasonality, and patterns over time.
Q2. How do you create a time series object in R?
A: Using the ts() function. Example:
sales <- c(100, 120, 130, 150, 160)
ts_data <- ts(sales, start = c(2020,1), frequency = 12)
Here, frequency = 12 indicates monthly data.
Q3. What is the difference between ts and xts objects in R?
A: ts is the base R structure for regular time series, while xts (from
the xts package) handles both regular and irregular time series with
date-time indexing.
---
2. Decomposition of Time Series
Q4. What are the main components of a time series?
A:
Trend (T): Long-term direction of the data.
Seasonality (S): Regular repeating patterns (e.g., higher sales in
December).
Residual/Irregular (R): Random fluctuations.
Q5. What are additive and multiplicative models in time series?
A:
Additive model: Y = T + S + R (used when seasonal variations are
constant over time).
Multiplicative model: Y = T × S × R (used when seasonal variations
grow with the trend).
Q6. How do you decompose a time series in R?
A: Using the decompose() function for additive/multiplicative
decomposition.
---
3. ACF and PACF Plots
Q7. What is the purpose of ACF and PACF?
A:
ACF (Autocorrelation Function): Measures correlation between
current values and lagged values. Helps identify the order of MA(q)
in ARIMA.
PACF (Partial Autocorrelation Function): Shows correlation after
removing effects of intermediate lags. Helps identify the order of
AR(p).
Q8. How do you generate ACF and PACF plots in R?
A:
acf(ts_data)
pacf(ts_data)
---
4. Forecasting Methods
Q9. What is exponential smoothing in time series forecasting?
A: A forecasting method that assigns more weight to recent data
and less weight to older data. It is useful for short-term predictions.
Q10. What is Holt’s linear trend method?
A: It extends simple exponential smoothing by including a trend
component. Suitable for data with trends but no seasonality.
Q11. What is Holt-Winters method?
A: An exponential smoothing method that accounts for trend +
seasonality + level. Best for data with both trend and seasonal
variations.
---
5. ARMA & ARIMA Modelling
Q12. What is ARMA?
A:
AR (Auto-Regressive): Uses past values (lags) to predict the future.
MA (Moving Average): Uses past forecast errors to predict.
ARMA (p,q): Combines AR(p) and MA(q).
Q13. What is ARIMA?
A: Auto-Regressive Integrated Moving Average. ARIMA(p,d,q):
p: Order of autoregression
d: Differencing order to remove trend and make data stationary
q: Order of moving average
Q14. What is stationarity in time series? Why is it important?
A: A stationary series has constant mean, variance, and
autocorrelation over time. ARIMA models require stationarity for
valid forecasts.
Q15. How do you build an ARIMA model in R?
A: Using the [Link]() function from the forecast package.
Example:
library(forecast)
model <- [Link](ts_data)
forecast(model, h=12)
---
6. Validation of Models
Q16. What is residual analysis in time series?
A: After fitting a model, residuals (errors) should resemble white
noise (random, no autocorrelation). This validates the model’s
accuracy.
Q17. What metrics are used to evaluate forecasting models?
A:
Mean Absolute Error (MAE)
Mean Squared Error (MSE)
Root Mean Squared Error (RMSE)
Mean Absolute Percentage Error (MAPE)
---
7. Applications of Time Series Forecasting
Q18. Give examples of domain-specific applications of time series
forecasting.
A:
Finance: Stock price prediction, volatility analysis.
Business: Sales forecasting, demand planning, budgeting.
Healthcare: Predicting disease outbreaks, patient inflow.
Economics: GDP growth, unemployment rates, inflation.
Weather: Rainfall prediction, temperature forecasting.
---