cars package:datasets R Documentation _S_p_e_e_d _a_n_d _S_t_o_p_p_i_n_g _D_i_s_t_a_n_c_e_s _o_f _C_a_r_s _D_e_s_c_r_i_p_t_i_o_n: The data give the speed of cars and the distances taken to stop. Note that the data were recorded in the 1920s. _U_s_a_g_e: cars _F_o_r_m_a_t: A data frame with 50 observations on 2 variables. [,1] speed numeric Speed (mph) [,2] dist numeric Stopping distance (ft) _S_o_u_r_c_e: Ezekiel, M. (1930) _Methods of Correlation Analysis_. Wiley. _R_e_f_e_r_e_n_c_e_s: McNeil, D. R. (1977) _Interactive Data Analysis_. Wiley. _E_x_a_m_p_l_e_s: require(stats); require(graphics) plot(cars, xlab = "Speed (mph)", ylab = "Stopping distance (ft)", las = 1) lines(lowess(cars$speed, cars$dist, f = 2/3, iter = 3), col = "red") title(main = "cars data") plot(cars, xlab = "Speed (mph)", ylab = "Stopping distance (ft)", las = 1, log = "xy") title(main = "cars data (logarithmic scales)") lines(lowess(cars$speed, cars$dist, f = 2/3, iter = 3), col = "red") summary(fm1 <- lm(log(dist) ~ log(speed), data = cars)) opar <- par(mfrow = c(2, 2), oma = c(0, 0, 1.1, 0), mar = c(4.1, 4.1, 2.1, 1.1)) plot(fm1) par(opar) ## An example of polynomial regression plot(cars, xlab = "Speed (mph)", ylab = "Stopping distance (ft)", las = 1, xlim = c(0, 25)) d <- seq(0, 25, length.out = 200) for(degree in 1:4) { fm <- lm(dist ~ poly(speed, degree), data = cars) assign(paste("cars", degree, sep="."), fm) lines(d, predict(fm, data.frame(speed=d)), col = degree) } anova(cars.1, cars.2, cars.3, cars.4)