Every chapter of this book loads a data frame, draws it, fits a model or does a test, and reads the output. The examples below come from the hands-on session of the DH801 course.
A ggplot() call starts from a data frame. The aesthetics in aes() connect columns to the axes and the colour. Layers such as geom_point() then draw the data.
Figure A.1: Odontoblast length against the dose of vitamin C, for 60 guinea pigs (Crampton 1947). We move each point a little to the side, so that points do not overlap.
Length increases with dose for both supplements. At the two lower doses, orange juice gives longer cells. At 2 mg/day the means are 26.06 for orange juice and 26.14 for ascorbic acid.
A.4 A linear model
lm() fits a linear model. The formula len ~ dose * supp means: model len from dose, supp and their interaction. The interaction lets the slope for dose be different for each supplement.
model <-lm(len ~ dose * supp, data = ToothGrowth)summary(model)
Call:
lm(formula = len ~ dose * supp, data = ToothGrowth)
Residuals:
Min 1Q Median 3Q Max
-8.2264 -2.8462 0.0504 2.2893 7.9386
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 11.550 1.581 7.304 1.09e-09 ***
dose 7.811 1.195 6.534 2.03e-08 ***
suppVC -8.255 2.236 -3.691 0.000507 ***
dose:suppVC 3.904 1.691 2.309 0.024631 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 4.083 on 56 degrees of freedom
Multiple R-squared: 0.7296, Adjusted R-squared: 0.7151
F-statistic: 50.36 on 3 and 56 DF, p-value: 6.521e-16
The output has one row for each coefficient:
(Intercept): the predicted length for orange juice at dose 0, 11.55. Dose 0 is outside the range of the data.
dose: the slope for orange juice, 7.81 units per mg/day.
suppVC: the change in intercept for ascorbic acid compared with orange juice, -8.26.
dose:suppVC: the change in slope for ascorbic acid. The slope for ascorbic acid is 7.81 + 3.90 = 11.72.
The last lines of the output (Residual standard error, Multiple R-squared and the F-statistic) describe the whole model. Chapter 12 explains each line. The functions coef(), confint() and residuals() return the parts of the fit.
To check the model, plot(model) draws four diagnostic plots, which Chapter 13 explains.
A.5 Predictions on a grid
To draw the fitted lines, make a grid of new values with expand_grid(). Then use predict() with newdata. The grid must have columns with the same names as the predictors in the model.
Figure A.2: The data with the fitted lines from the model len ~ dose * supp. The two lines have different slopes, because the model includes an interaction.
predict(model, newdata = grid, interval = "confidence") also gives a confidence interval for each predicted mean.
A.6 The tests in this book
Most tests in R take vectors and return an object that prints a short report. The table lists the functions of the book, in chapter order.
The session also did a rank-sum test on the airquality data, to compare ozone in May and August in New York in 1973. na.omit() removes the missing values.
may <-na.omit(airquality$Ozone[airquality$Month ==5])august <-na.omit(airquality$Ozone[airquality$Month ==8])wilcox.test(may, august, exact =FALSE)
Wilcoxon rank sum test with continuity correction
data: may and august
W = 127.5, p-value = 0.0001208
alternative hypothesis: true location shift is not equal to 0
Each test object is a list. Use $ to get one part, for example $p.value, $statistic or $conf.int. The chapters use this to put numbers from the output into the text.
A.7 Habits that help
Call set.seed() before a simulation, so that you get the same numbers each time.
Use str() and summary() on new data before you fit anything. Look for missing values (NA) and for numbers stored as text.
Draw the data before you do a test.
Use ?function_name to read the help page. The “Value” section lists what the function returns.