Computation of Measures of Central Tendency Using R

 

Experiment

Computation of Measures of Central Tendency Using R

Aim

To compute and analyze various measures of central tendency such as mean, median, mode, geometric mean, harmonic mean, trimmed mean, and weighted mean using R.


Objectives

  1. To understand the concept of measures of central tendency.
  2. To compute arithmetic mean, median, and mode of a dataset.
  3. To determine geometric mean and harmonic mean.
  4. To compute weighted mean and trimmed mean.
  5. To compare different measures of central tendency.
  6. To visualize the measures using suitable plots.

Theory

Measures of central tendency are statistical measures that represent the central or typical value of a dataset. They summarize the entire dataset using a single representative value.

The commonly used measures are:

  1. Arithmetic Mean
  2. Median
  3. Mode
  4. Geometric Mean
  5. Harmonic Mean
  6. Weighted Mean
  7. Trimmed Mean

1. Arithmetic Mean

The arithmetic mean is obtained by dividing the sum of observations by the total number of observations.

Formula:

Mean = (x₁ + x₂ + ... + xₙ)/n

In R:

mean(x)

2. Median

Median is the middle value after arranging observations in ascending order.

If n is odd:

Median = Middle observation

If n is even:

Median = Average of two middle observations

In R:

median(x)

3. Mode

Mode is the value that occurs most frequently.

R does not provide a built-in function for statistical mode, so it can be computed manually.

mode_value <- names(sort(table(x), decreasing=TRUE))[1]
If there are multiple modes

For example:
x <- c(10, 20, 20, 30, 30, 40) frequency <- table(x) modes <- names(frequency)[frequency == max(frequency)] print(modes)

Output:
[1] "20" "30"


So both 20 and 30 are modes.

4. Geometric Mean

Geometric Mean = (x₁ × x₂ × ... × xₙ)^(1/n)

Useful in growth rates and financial analysis.

In R:

prod(x)^(1/length(x))

5. Harmonic Mean

Harmonic Mean = n / (1/x₁ + 1/x₂ + ... + 1/xₙ)

Useful in averaging ratios and rates.

In R:

length(x)/sum(1/x)

6. Weighted Mean

Weighted Mean = Σ(wx)/Σw

where

w = weights

In R:

weighted.mean(x,w)

7. Trimmed Mean

Trimmed mean removes a certain percentage of extreme values.

In R:

mean(x, trim=0.1)

removes 10% observations from both ends.


Algorithm

  1. Create a numeric vector.
  2. Compute arithmetic mean.
  3. Compute median.
  4. Find mode.
  5. Calculate geometric mean.
  6. Calculate harmonic mean.
  7. Calculate weighted mean.
  8. Calculate trimmed mean.
  9. Display all measures.
  10. Visualize the dataset using histogram and box plot.

Program

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

# Arithmetic Mean
mean_value <- mean(x)

# Median
median_value <- median(x)

# Mode
mode_value <- names(sort(table(x),
decreasing=TRUE))[1]

# Geometric Mean
geometric_mean <- prod(x)^(1/length(x))

# Harmonic Mean
harmonic_mean <- length(x)/sum(1/x)

# Weighted Mean
weights <- c(1,2,1,2,1,1,2,1,2,1)

weighted_mean_value <- weighted.mean(x,
weights)

# Trimmed Mean
trimmed_mean_value <- mean(x,
trim=0.1)

# Display values
cat("Arithmetic Mean =", mean_value,"\n")

cat("Median =", median_value,"\n")

cat("Mode =", mode_value,"\n")

cat("Geometric Mean =", geometric_mean,"\n")

cat("Harmonic Mean =", harmonic_mean,"\n")

cat("Weighted Mean =", weighted_mean_value,"\n")

cat("Trimmed Mean =", trimmed_mean_value,"\n")

Output

Arithmetic Mean = 20.5

Median = 20

Mode = 20

Geometric Mean = 19.34

Harmonic Mean = 18.16

Weighted Mean = 20.57

Trimmed Mean = 20.63

Visualization

Histogram

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



Box Plot

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


Strip Chart

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



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
mean()Arithmetic mean
median()Median
table()Frequency table
sort()Sorting
prod()Product of elements
length()Number of elements
sum()Summation
weighted.mean()Weighted mean
hist()Histogram
boxplot()Box plot
stripchart()Strip chart
par()Multiple graphs

Result

The various measures of central tendency namely arithmetic mean, median, mode, geometric mean, harmonic mean, weighted mean, and trimmed mean were computed successfully using R. The histogram, box plot, and strip chart were used to visualize the distribution 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