Computation of Measures of Variability Using R

 

Experiment

Computation of Measures of Variability Using R

Aim

To compute and analyze various measures of variability (dispersion) such as range, interquartile range, variance, standard deviation, mean absolute deviation, coefficient of variation, and quartiles using R.


Objectives

  1. To understand the concept of variability or dispersion.
  2. To compute range, variance, and standard deviation.
  3. To determine quartiles and interquartile range.
  4. To calculate mean absolute deviation and coefficient of variation.
  5. To compare different measures of variability.
  6. To visualize the spread of data using appropriate graphs.

Theory

Measures of variability describe the spread or dispersion of data around the central value. They indicate how scattered the observations are.

A smaller variability indicates that the observations are closely clustered, whereas a larger variability indicates greater spread.

Common measures of variability are:

  1. Range
  2. Quartiles
  3. Interquartile Range (IQR)
  4. Variance
  5. Standard Deviation
  6. Mean Absolute Deviation (MAD)
  7. Coefficient of Variation

1. Range

Range is the difference between the largest and smallest observations.

Formula:

Range = Maximum Value − Minimum Value

In R:

range(x)

Difference:

max(x)-min(x)

2. Quartiles

Quartiles divide the ordered data into four equal parts.

  • Q1 : First Quartile (25%)
  • Q2 : Median (50%)
  • Q3 : Third Quartile (75%)

In R:

quantile(x)

3. Interquartile Range (IQR)

Interquartile Range measures the spread of the middle 50% of observations.

Formula:

IQR = Q3 − Q1

In R:

IQR(x)

4. Variance

Variance measures the average squared deviation from the mean.

For sample data,

s2=∑(xi−xˉ)2n−1s^2=\frac{\sum(x_i-\bar x)^2}{n-1}

where

  • xix_i= observation

  • ˉ
    \bar x
    = mean
  • nn= number of observations

In R:var(x)


5. Standard Deviation

Standard deviation is the square root of variance.

s=∑(xi−xˉ)2n−1s=\sqrt{\frac{\sum(x_i-\bar x)^2}{n-1}}

In R:

sd(x)

6. Mean Absolute Deviation

It measures the average absolute distance from the mean.

Formula:

The formula is

MAD=1n∑i=1n∣xi−xˉ∣

In R:

mean(abs(x-mean(x)))

7. Median Absolute Deviation

Median absolute deviation is based on the median and is robust against outliers.

The formula is

MAD=1n∑i=1n∣xi−xˉmed∣MAD=\frac{1}{n}\sum_{i=1}^{n}|x_i-\bar{x}|

In R, it can be computed as:

In R:

mad(x)

8. Coefficient of Variation

Coefficient of Variation (CV) measures variability relative to the mean.

Formula:

CV = (Standard Deviation / Mean) × 100

In R:

(sd(x)/mean(x))*100

Algorithm

  1. Create a numeric vector.
  2. Find maximum and minimum values.
  3. Compute range.
  4. Find quartiles and interquartile range.
  5. Calculate variance.
  6. Calculate standard deviation.
  7. Find mean absolute deviation.
  8. Find median absolute deviation.
  9. Compute coefficient of variation.
  10. Visualize the data using histogram and box plot.

Program

# Sample data
x <- c(10,20,15,25,20,18,22,30,20,25)

# Minimum and Maximum
minimum <- min(x)
maximum <- max(x)

# Range
range_value <- maximum - minimum
# or use range(x) which will give min and max use diff(range(x))

# Quartiles
quartiles <- quantile(x)

# Interquartile Range
iqr_value <- IQR(x)

# Variance
variance_value <- var(x)

# Standard Deviation
sd_value <- sd(x)

# Mean Absolute Deviation
mad_mean <- mean(abs(x-mean(x)))

# Median Absolute Deviation
mad_median <- mad(x)

# Coefficient of Variation
cv <- (sd_value/mean(x))*100

# Display results
cat("Minimum =", minimum,"\n")
cat("Maximum =", maximum,"\n")
cat("Range =", range_value,"\n")

cat("\nQuartiles:\n")
print(quartiles)

cat("Interquartile Range =", iqr_value,"\n")

cat("Variance =", variance_value,"\n")

cat("Standard Deviation =", sd_value,"\n")

cat("Mean Absolute Deviation =", mad_mean,"\n")

cat("Median Absolute Deviation =", mad_median,"\n")

cat("Coefficient of Variation =", cv,"%\n")

Sample Output

> # Display results
> cat("Minimum =", minimum,"\n")
Minimum = 10 

> cat("Maximum =", maximum,"\n")
Maximum = 30 

> cat("Range =", range_value,"\n")
Range = 20 

> cat("\nQuartiles:\n")

Quartiles:

> print(quartiles)
   0%   25%   50%   75%  100% 
10.00 18.50 20.00 24.25 30.00 

> cat("Interquartile Range =", iqr_value,"\n")
Interquartile Range = 5.75 

> cat("Variance =", variance_value,"\n")
Variance = 31.16667 

> cat("Standard Deviation =", sd_value,"\n")
Standard Deviation = 5.582711 

> cat("Mean Absolute Deviation =", mad_mean,"\n")
Mean Absolute Deviation = 4 

> cat("Median Absolute Deviation =", mad_median,"\n")
Median Absolute Deviation = 5.1891 

> cat("Coefficient of Variation =", cv,"%\n")
Coefficient of Variation = 27.23274 %

Visualization

Histogram

hist(x,
col="skyblue",
main="Histogram",
xlab="Values")

Shows the distribution of observations.




Box Plot

boxplot(x,
col="lightgreen",
main="Box Plot")

Displays median, quartiles, and outliers.



Strip Chart

stripchart(x,
method="stack",
pch=19,
col="blue",
main="Strip Chart")

Shows individual observations.




Multiple Graphs

par(mfrow=c(1,3))

hist(x,
col="yellow",
main="Histogram")

boxplot(x,
col="pink",
main="Box Plot")

stripchart(x,
method="stack",
pch=19,
col="red",
main="Strip Chart")

par(mfrow=c(1,1))

Functions Used

FunctionPurpose
min()    Minimum value
max()    Maximum value
range()    Minimum and maximum values
quantile()    Quartiles
IQR()    Interquartile range
var()    Variance
sd()    Standard deviation
mean()    Mean
abs()    Absolute value
mad()    Median absolute deviation
hist()    Histogram
boxplot()        Box plot
stripchart()    Strip chart
par()    Multiple plots


Result

The various measures of variability namely range, quartiles, interquartile range, variance, standard deviation, mean absolute deviation, median absolute deviation, and coefficient of variation were successfully computed using R. The histogram, box plot, and strip chart were used to visualize the spread and dispersion of the data.



Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Programs to try using Functions in R - Assignment 3