经过一番研究,我设法level
使用公式绘制轮廓sqrt(level * length(coef(model)) * (1 - leverage)/leverage)
,这是 R 用来绘制轮廓的plot.lm
。不过,我使用的方法肯定可以改进。
library(ggplot2)
library(ggfortify)
model <- glm(mpg ~ wt, data = mtcars, family = gaussian())
cd_cont_pos <- function(leverage, level, model) {sqrt(level*length(coef(model))*(1-leverage)/leverage)}
cd_cont_neg <- function(leverage, level, model) {-cd_cont_pos(leverage, level, model)}
autoplot(model, which = 5) +
stat_function(fun = cd_cont_pos, args = list(level = 0.5, model = model), xlim = c(0, 0.25), lty = 2, colour = "red") +
stat_function(fun = cd_cont_neg, args = list(level = 0.5, model = model), xlim = c(0, 0.25), lty = 2, colour = "red") +
scale_y_continuous(limits = c(-2, 2.5))