Ch 1 & 2: Measure of Center; Relative Position; Dispersion
Mean; Median; Mode; Variance; Standard Deviation
Travel times in minutes for 15 students at KU, chosen at random by a sociology student are:
30 20 10 40 25 20 10 60 15 40 5 30 12 10 10
We'd like to compute mean; median; variance; and standard deviation with this data.
> x <- c(30, 20, 10, 40, 25, 20, 10, 60, 15, 40, 5, 30, 12, 10, 10) > mean(x) [1] 22.46667 > median(x) [1] 20 > names(sort(-table(x)))[1] [1] "10" > var(x) [1] 231.9810 > sd(x) [1] 15.23092
The number of observation of x can be obtained:
> length(x) [1] 15
The sum of x and the sum of x-squared are:
> sum(x) [1] 337 > sum(x^2) [1] 10819
The mean absolute deviation from the mean for x is:
> sum(abs(x-mean(x)))/length(x) [1] 12.02667
You can compute SS(x) by writing the formula:
> sum(x^2)-(sum(x))^2/length(x) [1] 3247.733
Relative Position & 5 Number Summary
Percentile; Decile; Quintile; & Quartile can be obtained using the command, "quantile( )"
Note that different "type"s in R produce slightly different results for percentile.> quantile(x, .10, type=3) [1] 10 > quantile(x, .20, type=3) [1] 10
5 number summary: Minimum, 1st Quartile; Median; 3rd Quartile; Maximum
> quantile(x) 0% 25% 50% 75% 100% 5 10 20 30 60 > fivenum(x) 5 10 20 30 60
5 number summary + Mean
> summary(x) Min. 1st Qu. Median Mean 3rd Qu. Max. 5.00 10.00 20.00 22.47 30.00 60.00
Boxplot
> boxplot(x)