/
ols-residual-vs-regressor-plot.R
46 lines (40 loc) · 1.27 KB
/
ols-residual-vs-regressor-plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#' Residual vs regressor plot
#'
#' @description
#' Graph to determine whether we should add a new predictor to the model already
#' containing other predictors. The residuals from the model is regressed on the
#' new predictor and if the plot shows non random pattern, you should consider
#' adding the new predictor to the model.
#'
#' @param model An object of class \code{lm}.
#' @param variable New predictor to be added to the \code{model}.
#' @param print_plot logical; if \code{TRUE}, prints the plot else returns a plot object.
#'
#' @examples
#' model <- lm(mpg ~ disp + hp + wt, data = mtcars)
#' ols_plot_resid_regressor(model, 'drat')
#'
#' @seealso [ols_plot_added_variable()], [ols_plot_comp_plus_resid()]
#'
#' @export
#'
ols_plot_resid_regressor <- function(model, variable, print_plot = TRUE) {
check_model(model)
d <- ols_prep_rvsrplot_data(model)
inter <- eval(model$call$data)[variable]
x <- inter[[1]]
y <- residuals(model)
v <- names(inter)
k <- data.frame(x = x, y = y)
p <-
ggplot(k, aes(x = x, y = y)) +
geom_point(shape = 1, colour = "blue") +
geom_hline(yintercept = 0, colour = "red") +
xlab(paste(v)) +
ylab("Residual") +
ggtitle(paste("Residual vs", v))
if (print_plot) {
print(p)
}
return(p)
}