當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R cars 汽車的速度和停車距離


R語言 cars 位於 datasets 包(package)。

說明

這些數據給出了汽車的速度和停車所需的距離。請注意,數據記錄於 20 世紀 20 年代。

用法

cars

格式

包含 2 個變量 50 個觀測值的 DataFrame 。

[,1] speed numeric 速度(英裏/小時)
[,2] dist numeric 停止距離(英尺)

例子

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)

來源

Ezekiel, M. (1930) 相關分析方法。威利。

參考

McNeil, D. R. (1977) Interactive Data Analysis. Wiley.

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Speed and Stopping Distances of Cars。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。