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


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.。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。