当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。