发布于 2020-07-21 08:41:19
没有一个可重复的例子,我将首先创建一个基于内置数据集iris的模型。
df1 <- iris[1:100, 3:5]
df1$Species <- droplevels(df1$Species)
str(df1)
#'data.frame': 100 obs. of 3 variables:
# $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
# $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# $ Species : Factor w/ 2 levels "setosa","versicolor": 1 1 1 1 1 1 1 1 1 1 ...
fit <- lm(Petal.Length ~ Petal.Width*Species, df1)至于图,autoplot是一个泛型函数。包ggfortify包括类"lm"对象的方法等。
来自help("autoplot.lm")
如果需要地块的子集,则指定数字1:6的子集。
默认情况是which = c(1, 2, 3, 5)。通过对参数的所有6个值的尝试,我们发现所需的图并不是其中之一。因此,需要建立一个自定义图。
剩余值和杠杆值分别可以从stats::resid和stats::hatvalues获得。
library(ggplot2)
dflev <- data.frame(Leverage = hatvalues(fit), y = resid(fit))
ggplot(dflev, aes(Leverage, y)) +
geom_point() +
geom_hline(yintercept = 0, linetype = "dashed") +
ggtitle("Residuals vs Leverage") +
lims(y = c(-1, 1)) +
ylab("") +
theme(plot.title = element_text(hjust = 0.5, face = "bold"))

https://stackoverflow.com/questions/63000992
复制相似问题