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
- To understand the concept of measures of central tendency.
- To compute arithmetic mean, median, and mode of a dataset.
- To determine geometric mean and harmonic mean.
- To compute weighted mean and trimmed mean.
- To compare different measures of central tendency.
- 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:
- Arithmetic Mean
- Median
- Mode
- Geometric Mean
- Harmonic Mean
- Weighted Mean
- 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
- Create a numeric vector.
- Compute arithmetic mean.
- Compute median.
- Find mode.
- Calculate geometric mean.
- Calculate harmonic mean.
- Calculate weighted mean.
- Calculate trimmed mean.
- Display all measures.
- 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
Box Plot
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
Function Purpose 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
Post a Comment