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


R ggplot2 fortify.lm 使用模型拟合统计数据补充拟合到线性模型的数据。


如果模型数据中缺少值,您可能需要使用 na.action = na.exclude 重新拟合模型。

用法

# S3 method for lm
fortify(model, data = model$model, ...)

参数

model

线性模型

data

数据集,默认为用于拟合模型的数据

...

不使用该方法

带有额外列的原始数据:

.hat

帽子矩阵的对角线

.sigma

从模型中删除相应观测值时残差标准差的估计

.cooksd

库克距离,cooks.distance()

.fitted

模型的拟合值

.resid

残差

.stdresid

标准化残差

例子

mod <- lm(mpg ~ wt, data = mtcars)
head(fortify(mod))
#>                    mpg    wt       .hat   .sigma      .cooksd  .fitted
#> Mazda RX4         21.0 2.620 0.04326896 3.067494 1.327407e-02 23.28261
#> Mazda RX4 Wag     21.0 2.875 0.03519677 3.093068 1.723963e-03 21.91977
#> Datsun 710        22.8 2.320 0.05837573 3.072127 1.543937e-02 24.88595
#> Hornet 4 Drive    21.4 3.215 0.03125017 3.088268 3.020558e-03 20.10265
#> Hornet Sportabout 18.7 3.440 0.03292182 3.097722 7.599578e-05 18.90014
#> Valiant           18.1 3.460 0.03323551 3.095184 9.210650e-04 18.79325
#>                       .resid   .stdresid
#> Mazda RX4         -2.2826106 -0.76616765
#> Mazda RX4 Wag     -0.9197704 -0.30743051
#> Datsun 710        -2.0859521 -0.70575249
#> Hornet 4 Drive     1.2973499  0.43275114
#> Hornet Sportabout -0.2001440 -0.06681879
#> Valiant           -0.6932545 -0.23148309
head(fortify(mod, mtcars))
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#>                         .hat   .sigma      .cooksd  .fitted     .resid
#> Mazda RX4         0.04326896 3.067494 1.327407e-02 23.28261 -2.2826106
#> Mazda RX4 Wag     0.03519677 3.093068 1.723963e-03 21.91977 -0.9197704
#> Datsun 710        0.05837573 3.072127 1.543937e-02 24.88595 -2.0859521
#> Hornet 4 Drive    0.03125017 3.088268 3.020558e-03 20.10265  1.2973499
#> Hornet Sportabout 0.03292182 3.097722 7.599578e-05 18.90014 -0.2001440
#> Valiant           0.03323551 3.095184 9.210650e-04 18.79325 -0.6932545
#>                     .stdresid
#> Mazda RX4         -0.76616765
#> Mazda RX4 Wag     -0.30743051
#> Datsun 710        -0.70575249
#> Hornet 4 Drive     0.43275114
#> Hornet Sportabout -0.06681879
#> Valiant           -0.23148309

plot(mod, which = 1)


ggplot(mod, aes(.fitted, .resid)) +
  geom_point() +
  geom_hline(yintercept = 0) +
  geom_smooth(se = FALSE)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


ggplot(mod, aes(.fitted, .stdresid)) +
  geom_point() +
  geom_hline(yintercept = 0) +
  geom_smooth(se = FALSE)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


ggplot(fortify(mod, mtcars), aes(.fitted, .stdresid)) +
  geom_point(aes(colour = factor(cyl)))


ggplot(fortify(mod, mtcars), aes(mpg, .stdresid)) +
  geom_point(aes(colour = factor(cyl)))


plot(mod, which = 2)

ggplot(mod) +
  stat_qq(aes(sample = .stdresid)) +
  geom_abline()


plot(mod, which = 3)

ggplot(mod, aes(.fitted, sqrt(abs(.stdresid)))) +
  geom_point() +
  geom_smooth(se = FALSE)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


plot(mod, which = 4)

ggplot(mod, aes(seq_along(.cooksd), .cooksd)) +
  geom_col()


plot(mod, which = 5)

ggplot(mod, aes(.hat, .stdresid)) +
  geom_vline(linewidth = 2, colour = "white", xintercept = 0) +
  geom_hline(linewidth = 2, colour = "white", yintercept = 0) +
  geom_point() + geom_smooth(se = FALSE)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


ggplot(mod, aes(.hat, .stdresid)) +
  geom_point(aes(size = .cooksd)) +
  geom_smooth(se = FALSE, linewidth = 0.5)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


plot(mod, which = 6)

ggplot(mod, aes(.hat, .cooksd)) +
  geom_vline(xintercept = 0, colour = NA) +
  geom_abline(slope = seq(0, 3, by = 0.5), colour = "white") +
  geom_smooth(se = FALSE) +
  geom_point()
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


ggplot(mod, aes(.hat, .cooksd)) +
  geom_point(aes(size = .cooksd / .hat)) +
  scale_size_area()

源代码:R/fortify-lm.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Supplement the data fitted to a linear model with model fit statistics.。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。