如果模型数据中缺少值,您可能需要使用 na.action = na.exclude
重新拟合模型。
值
带有额外列的原始数据:
- .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 ggplot2 facet_wrap 将 1d 面板带包成 2d 面板
- R ggplot2 facet_grid 将面板布置在网格中
- R ggplot2 annotation_logticks 注释:记录刻度线
- R ggplot2 vars 引用分面变量
- R ggplot2 position_stack 将重叠的对象堆叠在一起
- R ggplot2 geom_qq 分位数-分位数图
- R ggplot2 geom_spoke 由位置、方向和距离参数化的线段
- R ggplot2 geom_quantile 分位数回归
- R ggplot2 geom_text 文本
- R ggplot2 get_alt_text 从绘图中提取替代文本
- R ggplot2 annotation_custom 注释:自定义grob
- R ggplot2 geom_ribbon 函数区和面积图
- R ggplot2 stat_ellipse 计算法行数据椭圆
- R ggplot2 resolution 计算数值向量的“分辨率”
- R ggplot2 geom_boxplot 盒须图(Tukey 风格)
- R ggplot2 lims 设置规模限制
- R ggplot2 geom_hex 二维箱计数的六边形热图
- R ggplot2 scale_gradient 渐变色阶
- R ggplot2 scale_shape 形状比例,又称字形
- R ggplot2 geom_bar 条形图
- R ggplot2 draw_key 图例的关键字形
- R ggplot2 annotate 创建注释层
- R ggplot2 label_bquote 带有数学表达式的标签
- R ggplot2 annotation_map 注释:Map
- R ggplot2 scale_viridis 来自 viridisLite 的 Viridis 色标
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Supplement the data fitted to a linear model with model fit statistics.。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。