From describing data to drawing conclusions
The previous lesson summarised data: averages, counts, tables. Statistics goes one step further and asks what the data says beyond itself. Orange juice seems to make guinea pigs' teeth grow more than ascorbic acid; is the difference larger than what chance variation among sixty animals would produce? Heavier cars use more fuel; by how much per extra thousand pounds, and how sure can we be? These are questions of inference, and R was built to answer them.
R's design makes statistical modelling unusually direct. A formula such as mpg ~ wt + cyl describes a model in a line, the same formula works with dozens of modelling functions, and every result is an object you can print, summarise, extract parts from and pass to other functions. This lesson covers the core tools that come with every R installation: t-tests, linear regression, analysis of variance and logistic regression, plus simulation as a way of checking what a method does. It explains the statistics briefly as it goes; the aim is to use these tools correctly and read their output honestly, not to replace a statistics course. Lumenary Math's introduction to statistical inference covers the ideas underneath.
Model formulas
Definition— Formula
A formula is an R object written with a tilde, response ~ predictors, that describes a model without evaluating it. The left side is the variable to explain; the right side lists the variables that explain it, combined with special operators. Modelling functions take a formula and a data argument, and look the names up as columns of that data frame.
| Formula | Meaning |
|---|---|
y ~ x | y depends on x (with an intercept) |
y ~ x1 + x2 | two predictors, effects added together |
y ~ x * g | x, g and their interaction x:g (the effect of x differs by g) |
y ~ . | all other columns of the data frame |
y ~ x - 1 | without an intercept |
log(y) ~ x + I(x^2) | transformations; I() protects arithmetic that would otherwise mean something else in a formula |
y ~ g with g a factor | one effect per level of g, compared with a baseline level |
Formulas are also used outside modelling, as you saw with aggregate(len ~ supp, …) and as plotting functions use them. They are a compact language for "this, explained by that".
Comparing two groups
A t-test asks whether the difference between two means is larger than chance variation would plausibly produce. It reports a p-value: if the two groups truly had the same mean, how often would samples like these show a difference at least this large? It also reports a confidence interval for the difference, which is usually the more informative number. The ToothGrowth data set records the tooth length of 60 guinea pigs given vitamin C either as orange juice (OJ) or as ascorbic acid (VC), at three doses.
# ToothGrowth: tooth length in 60 guinea pigs given vitamin C as orange juice (OJ)
# or as ascorbic acid (VC), at three doses.
str(ToothGrowth)
aggregate(len ~ supp, data = ToothGrowth, FUN = mean)
# Is the difference between the two supplements larger than chance would explain?
tt <- t.test(len ~ supp, data = ToothGrowth)
tt
# A test result is a list: take out exactly what you need.
round(tt$p.value, 4)
round(tt$conf.int, 2)
# Paired data: the sleep data set records extra hours of sleep for the same
# 10 people under two drugs, so compare within each person.
drug1 <- sleep$extra[sleep$group == 1]
drug2 <- sleep$extra[sleep$group == 2]
t.test(drug2, drug1, paired = TRUE)$p.value'data.frame': 60 obs. of 3 variables:
$ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
$ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
$ dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
supp len
1 OJ 20.66333
2 VC 16.96333
Welch Two Sample t-test
data: len by supp
t = 1.9153, df = 55.309, p-value = 0.06063
alternative hypothesis: true difference in means between group OJ and group VC is not equal to 0
95 percent confidence interval:
-0.1710156 7.5710156
sample estimates:
mean in group OJ mean in group VC
20.66333 16.96333
[1] 0.0606
[1] -0.17 7.57
attr(,"conf.level")
[1] 0.95
[1] 0.00283289By default R runs Welch's t-test, which does not assume that the two groups have equal variances (hence the fractional degrees of freedom, 55.3); pass var.equal = TRUE for the classical version. The orange juice group's mean was 3.7 longer, but the 95% confidence interval for the difference runs from slightly below zero (−0.17) to 7.57, and the p-value is 0.06. A careful summary is: "orange juice may produce somewhat longer teeth, but this sample cannot rule out no difference; the plausible range of the effect is from about zero to about 7.6." Notice that the result is a list (of class htest), from which $p.value and $conf.int extract numbers for further use; the confidence interval even carries an attribute recording its level.
The sleep data, published by William Sealy Gosset under the pen name "Student" in 1908, measures the same ten people under two drugs, so each person serves as their own control. A paired test analyses the ten within-person differences, which removes the large variation between people and makes the comparison much more precise (p ≈ 0.003). Treating paired data as two independent groups is a common and costly mistake.
Linear regression
A linear model describes a response as a straight-line function of predictors plus random error: for one predictor, . lm() estimates the coefficients (the intercept) and (the slope) by least squares, choosing the line that minimises the sum of squared vertical distances from the points. The mtcars data set describes 32 car models from a 1974 US magazine; mpg is fuel economy in miles per US gallon, wt the weight in thousands of pounds.
# mtcars: 32 car models from a 1974 US magazine. Does weight predict fuel economy?
# mpg is miles per US gallon; wt is weight in thousands of pounds.
fit <- lm(mpg ~ wt, data = mtcars)
summary(fit)
# The pieces, for use in code.
coef(fit)
round(confint(fit), 2)
round(summary(fit)$r.squared, 3)
# Predictions for two new weights, with 95% prediction intervals.
new_cars <- data.frame(wt = c(2.5, 4.0))
round(predict(fit, newdata = new_cars, interval = "prediction"), 1)
Call:
lm(formula = mpg ~ wt, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.5432 -2.3647 -0.1252 1.4096 6.8727
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
wt -5.3445 0.5591 -9.559 1.29e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
(Intercept) wt
37.285126 -5.344472
2.5 % 97.5 %
(Intercept) 33.45 41.12
wt -6.49 -4.20
[1] 0.753
fit lwr upr
1 23.9 17.6 30.3
2 15.9 9.5 22.3How to read the summary, section by section:
- Residuals: the spread of the differences between observed and fitted values. Roughly symmetric around zero is what you hope for.
- Coefficients: each estimate with its standard error, t statistic and p-value. The slope says that each extra thousand pounds goes with about 5.3 fewer miles per gallon; its 95% confidence interval, from
confint(), is −6.49 to −4.20. The intercept, 37.3 mpg for a car weighing nothing, is a mathematical anchor for the line, not a meaningful prediction. - Residual standard error: the typical size of a residual, about 3 mpg.
- R-squared: the share of the variation in
mpgthat the model accounts for, here 75%.
The last lines show the difference between two kinds of interval. A confidence interval describes uncertainty about the average fuel economy of all cars of a given weight. A prediction interval describes where a single new car is likely to fall, which also includes the car-to-car scatter, so it is much wider: a 2,500-pound car is predicted at 23.9 mpg, but anywhere from 17.6 to 30.3 would be unsurprising.
Factors in models, and comparing models
Predictors can be categories. When a factor appears in a formula, R creates one coefficient for each level except the first, the baseline, and each coefficient is a difference from the baseline. The number of cylinders in mtcars is stored as a number, so it must be converted with factor(); otherwise R would treat it as a quantity and fit a single slope, as if 6 cylinders were exactly halfway between 4 and 8 in effect.
To ask whether adding predictors improves a model, compare nested models (where one contains all the terms of the other) with anova(). For comparing the means of several groups, the classical tool is analysis of variance, aov(), which is a linear model with a factor as its only predictor.
cars <- mtcars
cars$cyl <- factor(cars$cyl) # 4, 6 or 8 cylinders: a category, not a number
# Two predictors: weight and number of cylinders.
fit2 <- lm(mpg ~ wt + cyl, data = cars)
round(coef(summary(fit2)), 3)
# Does adding cylinders improve on weight alone? Compare nested models.
fit1 <- lm(mpg ~ wt, data = cars)
anova(fit1, fit2)
# The baseline level is the first level of the factor; relevel() changes it.
cars$cyl <- relevel(cars$cyl, ref = "8")
round(coef(lm(mpg ~ wt + cyl, data = cars)), 3)
# One-way analysis of variance: plant weights under a control and two treatments.
summary(aov(weight ~ group, data = PlantGrowth)) Estimate Std. Error t value Pr(>|t|)
(Intercept) 33.991 1.888 18.006 0.000
wt -3.206 0.754 -4.252 0.000
cyl6 -4.256 1.386 -3.070 0.005
cyl8 -6.071 1.652 -3.674 0.001
Analysis of Variance Table
Model 1: mpg ~ wt
Model 2: mpg ~ wt + cyl
Res.Df RSS Df Sum of Sq F Pr(>F)
1 30 278.32
2 28 183.06 2 95.263 7.2856 0.002835 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Intercept) wt cyl4 cyl6
27.920 -3.206 6.071 1.815
Df Sum Sq Mean Sq F value Pr(>F)
group 2 3.766 1.8832 4.846 0.0159 *
Residuals 27 10.492 0.3886
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1With 4 cylinders as the baseline, cyl6 and cyl8 say that, at the same weight, six- and eight-cylinder cars average 4.3 and 6.1 fewer miles per gallon than four-cylinder cars. Notice that the weight coefficient shrank from −5.3 to −3.2 once cylinders were included: heavier cars tend to have more cylinders, so part of what looked like a weight effect was an engine-size effect. Every coefficient in a multiple regression is "holding the other predictors fixed", and its value depends on which other predictors are in the model. The anova() comparison (p ≈ 0.003) says cylinders add real explanatory power beyond weight. Releveling to 8 cylinders changed how the coefficients are expressed, not the model: the fitted values are identical.
The analysis of variance on PlantGrowth (dried plant weights under a control and two treatments) finds that the group means differ (p ≈ 0.016). It does not say which groups differ; TukeyHSD() compares every pair with a correction for making several comparisons.
Logistic regression and simulation
When the response is yes or no, a straight line is the wrong model, since it can predict probabilities below 0 or above 1. Logistic regression models the log-odds of "yes" as a linear function of the predictors. It is one member of the family of generalised linear models, fitted with glm() and a family argument (binomial for yes/no, poisson for counts).
Simulation is the second idea in this program. Instead of trusting a formula, you can generate data where you know the truth and see what a method does with it. set.seed() fixes the random number generator so that the result is the same every time the script runs, which is essential for reproducible work.
# Logistic regression: the probability of a manual gearbox (am = 1) by weight.
logit <- glm(am ~ wt, data = mtcars, family = binomial)
round(coef(summary(logit)), 3)
# Coefficients are on the log-odds scale; exp() gives odds ratios.
round(exp(coef(logit)["wt"]), 4)
# Predicted probabilities for cars of 2,000, 3,000 and 4,000 pounds.
round(predict(logit, newdata = data.frame(wt = c(2, 3, 4)), type = "response"), 3)
# Simulation: if two groups really have the same mean, how often does a t-test
# at the 5% level still report a "significant" difference?
set.seed(2026)
p_values <- replicate(2000, t.test(rnorm(20), rnorm(20))$p.value)
mean(p_values < 0.05) Estimate Std. Error z value Pr(>|z|)
(Intercept) 12.040 4.510 2.670 0.008
wt -4.024 1.436 -2.801 0.005
wt
0.0179
1 2 3
0.982 0.492 0.017
[1] 0.0525The weight coefficient, −4.02 on the log-odds scale, is hard to read directly. Exponentiated, it is an odds ratio: each extra thousand pounds multiplies the odds of a manual gearbox by about 0.018, a very strong effect in this small 1974 sample. predict(…, type = "response") gives probabilities: 98% for a light car, about even at 3,000 pounds, 2% for a heavy one.
The simulation drew 2,000 pairs of samples from the same normal distribution, so every "significant" result is a false alarm. About 5% of them were (5.25% here), exactly as the 5% significance level promises. That is the meaning of a p-value threshold, and also its danger: run twenty tests on data with no real effects, and you should expect about one "discovery". When an analysis makes many comparisons, adjust for it: p.adjust(p, method = "holm") or method = "BH" (which controls the false discovery rate) turn a vector of p-values into adjusted ones, and analysis plans written before looking at the data keep the number of comparisons honest.
Reading results honestly
Misconception
"p < 0.05 means the effect is real, and p > 0.05 means there is no effect." A p-value measures how surprising the data would be if there were no effect; it is not the probability that the effect is real, and it says nothing about the effect's size. A huge sample can make a trivial difference "significant", and a small one can miss an important effect, as with the guinea pigs. Report estimates with confidence intervals and judge whether the size matters. The American Statistical Association's 2016 statement on p-values makes these points in more detail.
Misconception
"The model fitted, so its assumptions hold." Linear models assume that the relationship is roughly linear, that errors are independent, and that their spread does not change systematically; tests and intervals rely on these. Always look at the residuals: plot(fit) draws four standard diagnostic plots, and curved patterns or a funnel shape tell you to transform a variable or choose a different model.
Misconception
"A strong coefficient shows that the predictor causes the response." Observational data shows association. Heavier cars use more fuel partly because of their weight and partly because of everything that comes with weight. Causal claims need a randomised experiment, as in ToothGrowth, or careful causal reasoning about what else could explain the pattern.
Beyond these base functions, R's package ecosystem covers nearly every modelling approach in use. Mixed-effects models for repeated measurements and grouped data are in lme4 and nlme (nlme comes with R); generalised additive models for smooth non-linear effects are in mgcv (also included); survival analysis is in survival (included); Bayesian models are in brms and rstanarm, built on the Stan language; and the broom package turns model results into tidy data frames for reporting. The CRAN Task Views linked below list the packages for each field.
Exercises
1. Statistical models: t-tests, regression and beyond
Warm-upComputationalThe points lie exactly on a line. What does coef() print? Write the two lines separated by a single space.
d <- data.frame(x = c(1, 2, 3, 4), y = c(3, 5, 7, 9))
coef(lm(y ~ x, data = d))
2. Statistical models: t-tests, regression and beyond
CoreHumanThe lesson's model lm(mpg ~ wt, data = mtcars) has intercept 37.285 and slope −5.345. What fuel economy does it predict for a car weighing 3,000 pounds (wt = 3)?
3. Statistical models: t-tests, regression and beyond
CoreComputationalPlantGrowth$group is a factor with three levels: ctrl, trt1 and trt2. How many coefficients does lm(weight ~ group, data = PlantGrowth) have?
4. Statistical models: t-tests, regression and beyond
CoreHumanTwenty students take a reading test before and after a six-week course. Which call compares the scores appropriately, with before and after in the same student order?
5. Statistical models: t-tests, regression and beyond
CoreHumanIn lm(mpg ~ wt + cyl, data = mtcars), the column cyl holds the numbers 4, 6 and 8. What does the model assume about cylinders if you do not convert cyl to a factor?
6. Statistical models: t-tests, regression and beyond
StretchHumanIn a logistic regression, the linear predictor (the log-odds) for a particular case is exactly 0. What probability does the model give for "yes"?
7. Statistical models: t-tests, regression and beyond
CoreHumanA t-test comparing two teaching methods gives p = 0.03. Which statement is correct?
Further reading and resources
- An Introduction to R, chapter 11: Statistical models in R · R Core TeamFormulas, linear models, generalized linear models and nonlinear models, from the people who wrote them.
- CRAN Task ViewsCurated guides to the packages for each field, from Bayesian inference to survival analysis.
- R for Data Science (2e) · Hadley Wickham, Mine Çetinkaya-Rundel and Garrett Grolemund