R Statistics Intro
Statistics is the science of collecting, describing and drawing conclusions from data. R was created by statisticians, so statistical tools are built into the language rather than added on.
Statistics is usually split into two parts:
| Branch | Question | Examples in R |
|---|---|---|
| Descriptive statistics | What does the data look like? | mean(), median(), sd(), quantile(), table(), summary() |
| Inferential statistics | What can we conclude beyond this sample? | t.test(), chisq.test(), cor.test(), lm() |
Describing a variable in one call
summary() gives the minimum, quartiles, mean and maximum of a numeric vector:
commute_min <- c(22, 35, 18, 41, 27, 30, 55, 24, 19, 33)
summary(commute_min) Min. 1st Qu. Median Mean 3rd Qu. Max.
18.0 22.5 28.5 30.4 34.5 55.0
Center and spread
The two most important properties of a numeric variable are its center (a typical value) and its spread (how far values stray from it):
commute_min <- c(22, 35, 18, 41, 27, 30, 55, 24, 19, 33)
mean(commute_min) # center
median(commute_min) # center, robust to extreme values
sd(commute_min) # spread: standard deviation
var(commute_min) # spread: variance (sd squared)
IQR(commute_min) # spread: interquartile range
diff(range(commute_min))[1] 30.4
[1] 28.5
[1] 11.31567
[1] 128.0444
[1] 12
[1] 37
The mean is pulled up by the one long 55-minute commute, while the median isn't. That is why the median is often preferred for skewed data like incomes or house prices.
Categorical variables
For categories, counts and proportions are the natural summaries:
transport <- c("bus", "car", "bike", "car", "bus", "car", "walk", "car", "bus", "bike")
table(transport)
prop.table(table(transport))
names(which.max(table(transport)))transport
bike bus car walk
2 3 4 1
transport
bike bus car walk
0.2 0.3 0.4 0.1
[1] "car"
Relationships between variables
cor() measures how two numeric variables move together, and table() with two arguments cross-tabulates two categorical variables:
cor(mtcars$hp, mtcars$qsec)
table(cyl = mtcars$cyl, transmission = ifelse(mtcars$am == 1, "manual", "auto"))[1] -0.7082234
transmission
cyl auto manual
4 3 8
6 4 3
8 12 2
Samples, populations and randomness
We usually only see a sample of a larger population. Different samples give slightly different statistics. You can see this by simulation: set.seed() makes the random draws reproducible.
set.seed(42)
population <- rnorm(10000, mean = 50, sd = 10)
round(mean(population), 2)
sample_means <- replicate(5, mean(sample(population, 30)))
round(sample_means, 2)[1] 49.89
[1] 47.99 49.28 50.38 49.51 48.07
Each sample of 30 gives a mean close to, but not exactly, the population mean. Inferential statistics quantifies that uncertainty.
What's next
The following lessons look at each descriptive tool in turn, using built-in datasets: finding the max and min, the mean, median and mode, and percentiles. Then we move on to inference with linear regression and hypothesis tests.
Test yourself
Which statistic is least affected by a few extreme values?
- Mean
- Median
- Range
- Variance
What does prop.table(table(x)) return?
- Counts
- Proportions that sum to 1
- Percentiles
- The mode
Inferring a population mean from a sample is an example of…
- Descriptive statistics
- Inferential statistics
- Data cleaning
- Visualisation