0% found this document useful (0 votes)
3 views14 pages

Statistical Analysis in R Notes

This document provides comprehensive study notes on statistical analysis using R, covering key topics such as inferential statistics, hypothesis testing, t-tests, ANOVA, regression analysis, and clustering techniques. It includes definitions, methodologies, and R code examples for implementing various statistical tests and models. The notes serve as a reference guide for understanding and applying statistical techniques in data analysis.

Uploaded by

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

Statistical Analysis in R Notes

This document provides comprehensive study notes on statistical analysis using R, covering key topics such as inferential statistics, hypothesis testing, t-tests, ANOVA, regression analysis, and clustering techniques. It includes definitions, methodologies, and R code examples for implementing various statistical tests and models. The notes serve as a reference guide for understanding and applying statistical techniques in data analysis.

Uploaded by

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

Statistical Analysis in R

Comprehensive Study Notes


Analytics Techniques · Complete Reference Guide

1. Statistical Analysis in R — Overview


2. Inferential Statistics
3. Hypothesis Testing
Statistical Analysis in R — Study Notes Page 2

4. T-Test (One-Sample, Two-Sample, Paired)


5. ANOVA Test
6. Regression Analysis
7. Clustering Analysis

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 3

1. Statistical Analysis in R — Overview

What is Statistical Analysis?


Statistical analysis is a fundamental pillar of data science. It is used to interpret data, identify trends, and
make data-driven decisions. R is one of the most popular programming languages for statistical
computing, valued for its extensive packages, flexibility, and powerful data visualisation capabilities.

Three Core Areas


• Descriptive Statistics — Summarise and describe the main characteristics of a dataset.
• Inferential Statistics — Draw conclusions about a population from sample data.
• Regression Analysis — Model relationships between dependent and independent variables.

Descriptive Statistics in R
Descriptive statistics provide a snapshot of your data. Key R functions include:
• mean(x) — Arithmetic mean — measure of central tendency
• median(x) — Middle value — robust to outliers
• sd(x) — Standard deviation — measure of spread
• quantile(x) — Percentile values — shows data distribution

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 4

2. Inferential Statistics

Core Idea
Inferential statistics lets us make inferences about an entire population based on sample data. It uses data
from a sample to decide which of two opposing ideas (hypotheses) is more likely to be true. R provides
functions for hypothesis tests such as t-tests and ANOVA to determine whether observed differences
between groups are statistically significant.

Defining Hypotheses

Hypothesis Symbol Meaning

Default assumption — no effect or difference in the


Null Hypothesis H0
population.

Opposite of null — suggests there IS a difference or


Alternative Hypothesis H1
effect.

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 5

3. Hypothesis Testing

Key Terms
Significance Level (α) The threshold for rejecting H■. Commonly set at 0.05 (5%). Represents the
probability of rejecting H■ when it is actually true.

p-value Probability of observing the data (or something more extreme) if H■ is true.
If p-value < α → reject H■.

Test Statistic A numerical value computed from the data that helps decide whether to
reject H■.

Critical Value The cutoff value compared against the test statistic. If test statistic > critical
value → reject H■.

Degrees of Freedom A value based on sample size, used to determine the critical value.

Types of Hypothesis Tests


1. Parametric Tests — assume data follows a specific distribution; used for interval/ratio data.
• T-Test: Compares means between two groups (independent or paired).
• Z-Test: Compares a sample mean to a population mean (large samples).
• ANOVA: Compares means across three or more groups.

2. Non-Parametric Tests — no specific distribution assumed; used for ordinal or skewed data.
• Chi-Square: Tests categorical data.
• Mann-Whitney U: Compares two independent groups.

Step-by-Step Hypothesis Testing Process

1 Step 1: Define the Hypotheses


State H■ (no effect) and H■ (there is an effect).

2 Step 2: Choose the Significance Level


Select α (typically 0.05). This is the probability of incorrectly rejecting H■.

3 Step 3: Collect and Analyse the Data


Gather data from experiments or observations; calculate the test statistic.

4 Step 4: Select the Appropriate Test


Z-test (large samples, known variance) · T-test (small samples) · Chi-Square (categorical data).

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 6

5 Step 5: Make a Decision


Critical Value Approach: Reject H■ if test statistic > critical value. P-value Approach: Reject H■ if
p-value ≤ α.

6 Step 6: Interpret the Results


Rejecting H■ means there is enough evidence to support H■. Otherwise, we fail to reject H■.

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 7

4. T-Test

What is the T-Test?


The T-Test is a statistical method used to determine whether there is a significant difference between the
means of two groups, or between a sample and a known value. It assumes a null hypothesis that the two
means are the same, and uses sample data to test whether any observed difference is real or simply due
to chance.

One-Sample T-Test
Tests whether a sample mean equals a hypothesised population mean.
Use when you want to compare the average of one sample against a known or assumed value. Syntax:
[Link](x, mu = hypothesised_mean)

R Code:
[Link](0)
sweetSold <- c(rnorm(50, mean = 140, sd = 5))

# mu = hypothesised mean
[Link](sweetSold, mu = 150)

Two-Sample T-Test
Compares means of two independent groups.
Use when you have two separate (unrelated) groups and want to know if their means differ significantly.
Default in R uses Welch's correction (unequal variances). Use [Link] = TRUE to assume equal
variances.

R Code:
[Link](0)
shopOne <- rnorm(50, mean = 140, sd = 4.5)
shopTwo <- rnorm(50, mean = 150, sd = 4)

[Link](shopOne, shopTwo, [Link] = TRUE)

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 8

Paired Sample T-Test


Tests the mean difference between two related/matched measurements.
Use when each subject is measured twice (before/after). The test evaluates whether the mean difference
between paired observations is zero.

R Code:
[Link](2820)
sweetOne <- c(rnorm(100, mean = 14, sd = 0.3))
sweetTwo <- c(rnorm(100, mean = 13, sd = 0.2))

[Link](sweetOne, sweetTwo, paired = TRUE)

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 9

5. ANOVA Test

What is ANOVA?
ANOVA (Analysis of Variance) is a statistical technique used to analyse the relationship between
categorical independent variables and a continuous dependent variable. It determines whether the means
of different groups are significantly different by comparing variation within groups to variation between
groups. ANOVA is widely used in business, biology, social sciences, and experimental research.

Hypotheses for ANOVA


• H■ (Null): All group means are equal — the categorical variable has no effect.
• H■ (Alternative): At least one group mean differs — the categorical variable has an effect.

Types of ANOVA

Type Independent Variables Use Case

Compare means across 3+ groups on one


One-Way ANOVA 1 categorical variable
factor.

Analyse 2 factors simultaneously; can detect


Two-Way ANOVA 2 categorical variables
interaction effects.

Implementation in R (using mtcars dataset)


Install & Load Packages
[Link]('dplyr')
library(dplyr)

View the Dataset


head(mtcars) # Displays first few rows

One-Way ANOVA — does mean displacement differ by gear level?


mtcars_aov <- aov(mtcars$disp ~ factor(mtcars$gear))
summary(mtcars_aov)

Two-Way ANOVA — influence of both gear and transmission (am) on disp


mtcars_aov2 <- aov(mtcars$disp ~ factor(mtcars$gear) * factor(mtcars$am))
summary(mtcars_aov2)

Compare Models using AIC


# Lower AIC = better model fit
library(AICcmodavg)

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 10

aictab(list(mtcars_aov, mtcars_aov2))

Visualise Results with ggplot2


library(ggplot2)
ggplot(mtcars, aes(x = factor(gear), y = disp)) +
geom_boxplot() +
labs(title = 'Displacement by Gear', x = 'Gear', y = 'Displacement') +
theme_minimal()

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 11

6. Regression Analysis

What is Regression Analysis?


Regression analysis is a widely used statistical tool for establishing a relationship model between two or
more variables. It determines the relationship between a dependent variable (outcome) and one or more
independent variables (predictors). Common uses include prediction, forecasting, and understanding how
variables influence each other.

Linear Regression
Linear regression models the relationship between one dependent variable and one independent variable
using the equation:

y = ax + b
• y — Dependent variable (response)
• x — Independent variable (predictor)
• a — Slope (coefficient)
• b — Intercept

Steps in R:
1. Collect Input Data
# e.g. height & weight of persons
height <- c(151, 174, 138, 186, 128)
weight <- c(63, 81, 56, 91, 47)

2. Build the Model with lm()


relation <- lm(weight ~ height)
print(relation)

3. View Summary
print(summary(relation))

4. Predict New Values


a <- [Link](height = 170)
result <- predict(relation, a)
print(result)

5. Visualise
plot(height, weight, col='blue', main='Height & Weight Regression',
pch=16, xlab='Height (cm)', ylab='Weight (kg)')
abline(lm(weight ~ height), col='red')

Multiple Regression

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 12

An extension of linear regression with more than one predictor variable. Useful when the outcome
depends on several factors simultaneously.

y = a + b1·x1 + b2·x2 + ... + bn·xn


# Build multiple regression model
model <- lm(mpg ~ wt + hp + cyl, data = mtcars)
summary(model)

# Predict
new_data <- [Link](wt = 3.0, hp = 120, cyl = 6)
predict(model, new_data)

Logistic Regression
Used when the response variable is categorical (binary: True/False, 0/1). It models the probability of a
binary outcome based on predictor variables. Uses the logistic (sigmoid) function:

y = 1 / (1 + e−(a + b1x1 + b2x2 + ...))


Implemented in R using glm() with family = binomial:
# glm(formula, data, family)
model_log <- glm(am ~ wt + hp, data = mtcars, family = binomial)
summary(model_log)

# Predict probabilities
predicted_prob <- predict(model_log, type = 'response')

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 13

7. Clustering Analysis

What is Clustering?
Clustering is an unsupervised learning technique that organises data points into groups (clusters) based
on their similarity — without needing labelled data. It reveals hidden patterns, natural groupings, and
behaviour trends within a dataset. Clustering is also useful for dimensionality reduction, anomaly
detection, and as a preprocessing step for supervised learning.

K-Means Clustering
K-Means is a widely used unsupervised algorithm that groups data points into a specified number (k) of
clusters based on their features. It works iteratively:
• Assign each point to the nearest cluster centre (centroid).
• Update centroids as the mean of all points in the cluster.
• Repeat until convergence (centroids no longer move).
Characteristics: Simple, fast, and effective for many real-world datasets.

# Scale data first (important for distance-based methods)


df_scaled <- scale(mtcars)

# Fit K-Means with 4 clusters, 25 random starts


km <- kmeans(df_scaled, centers = 4, nstart = 25)
km$cluster # Cluster assignment for each observation

# Visualise
library(factoextra)
fviz_cluster(km, data = df_scaled)

Hierarchical Clustering
Hierarchical clustering builds a tree-like structure (dendrogram) showing how data points group together at
different levels of similarity. Unlike K-Means, you do NOT need to specify the number of clusters in
advance — you choose it after viewing the dendrogram.

Key advantages:
• No need to pre-specify number of clusters.
• Visual dendrogram helps understand data relationships at multiple scales.
• Works well for small to medium datasets.

# Compute distance matrix


dist_matrix <- dist(df_scaled, method = 'euclidean')

# Hierarchical clustering using Ward's method


hc <- hclust(dist_matrix, method = 'ward.D2')

# Plot dendrogram

Analytics Techniques in R • Comprehensive Study Notes


Statistical Analysis in R — Study Notes Page 14

plot(hc, main = 'Hierarchical Clustering Dendrogram', xlab = '', sub = '')

# Cut the tree into k clusters


clusters <- cutree(hc, k = 4)

K-Means vs Hierarchical Clustering


• K-Means: Requires k upfront · Fast · Best for large datasets · Assumes spherical clusters.
• Hierarchical: No k needed · Produces dendrogram · Slower · Better for small datasets.
• Both methods benefit from scaling the data first using scale().

Quick Reference Summary


Topic Key R Function(s) When to Use

Descriptive Stats mean(), sd(), median(), quantile() Summarise any dataset

Compare sample mean to known


One-Sample T-Test [Link](x, mu=val)
value

Two-Sample T-Test [Link](x1, x2, [Link]=TRUE) Compare 2 independent groups

Paired T-Test [Link](x1, x2, paired=TRUE) Before/after same subjects

One-Way ANOVA aov(y ~ factor(x)) 3+ groups, 1 factor

Two-Way ANOVA aov(y ~ factor(x1)*factor(x2)) 3+ groups, 2 factors

Linear Regression lm(y ~ x) Continuous outcome, 1 predictor

Multiple Regression lm(y ~ x1+x2+...) Continuous outcome, 2+ predictors

Logistic Regression glm(y ~ x, family=binomial) Binary (0/1) outcome

K-Means Clustering kmeans(data, centers=k) Group unlabelled data

Hierarchical Clust. hclust(dist(data)) Explore cluster structure visually

Analytics Techniques in R • Comprehensive Study Notes

You might also like