/
blr-residual-diagnostics.R
145 lines (127 loc) · 3.72 KB
/
blr-residual-diagnostics.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#' Influence diagnostics plot
#'
#' Reisudal diagnostic plots for detecting influential observations.
#'
#' @param model An object of class \code{glm}.
#' @param print_plot logical; if \code{TRUE}, prints the plot else returns a plot object.
#'
#' @return A panel of influence diagnostic plots.
#'
#' @references
#'
#' Fox, John (1991), Regression Diagnostics. Newbury Park, CA: Sage Publications.
#'
#' Cook, R. D. and Weisberg, S. (1982), Residuals and Influence in Regression, New York: Chapman & Hall.
#'
#' @examples
#' model <- glm(honcomp ~ female + read + science, data = hsb2,
#' family = binomial(link = 'logit'))
#'
#' blr_plot_diag_influence(model)
#'
#' @importFrom gridExtra grid.arrange
#'
#' @family diagnostic plots
#'
#' @export
#'
blr_plot_diag_influence <- function(model, print_plot = TRUE) {
blr_check_model(model)
p1 <- blr_plot_pearson_residual(model)
p2 <- blr_plot_deviance_residual(model)
p3 <- blr_plot_diag_c(model)
p4 <- blr_plot_diag_cbar(model)
p5 <- blr_plot_diag_difdev(model)
p6 <- blr_plot_diag_difchisq(model)
p7 <- blr_plot_leverage(model)
myplots <- list(pearson_residual = p1,
deviance_residual = p2,
diag_c = p3,
diag_cbar = p4,
diag_difdev = p5,
diag_difchisq = p6,
leverage = p7)
if (print_plot) {
do.call(grid.arrange, c(myplots, list(ncol = 2)))
}
invisible(myplots)
}
#' Fitted values diagnostics plot
#'
#' Diagnostic plots for fitted values.
#'
#' @param model An object of class \code{glm}.
#' @param print_plot logical; if \code{TRUE}, prints the plot else returns a plot object.
#'
#' @return A panel of diagnostic plots for fitted values.
#'
#' @references
#'
#' Fox, John (1991), Regression Diagnostics. Newbury Park, CA: Sage Publications.
#'
#' Cook, R. D. and Weisberg, S. (1982), Residuals and Influence in Regression, New York: Chapman & Hall.
#' @examples
#' model <- glm(honcomp ~ female + read + science, data = hsb2,
#' family = binomial(link = 'logit'))
#'
#' blr_plot_diag_fit(model)
#'
#' @family diagnostic plots
#'
#' @export
#'
blr_plot_diag_fit <- function(model, print_plot = TRUE) {
blr_check_model(model)
p1 <- blr_plot_difdev_fitted(model)
p2 <- blr_plot_difchisq_fitted(model)
p3 <- blr_plot_leverage_fitted(model)
p4 <- blr_plot_c_fitted(model)
myplots <- list(difdev_fitted = p1,
difchisq_fitted = p2,
leverage_fitted = p3,
c_fitted = p4)
if (print_plot) {
do.call(grid.arrange, c(myplots, list(ncol = 2)))
}
invisible(myplots)
}
#' Leverage diagnostics plot
#'
#' Diagnostic plots for leverage.
#'
#' @param model An object of class \code{glm}.
#' @param print_plot logical; if \code{TRUE}, prints the plot else returns a plot object.
#'
#' @return A panel of diagnostic plots for leverage.
#'
#' @references
#'
#' Fox, John (1991), Regression Diagnostics. Newbury Park, CA: Sage Publications.
#'
#' Cook, R. D. and Weisberg, S. (1982), Residuals and Influence in Regression, New York: Chapman & Hall.
#'
#' @examples
#' model <- glm(honcomp ~ female + read + science, data = hsb2,
#' family = binomial(link = 'logit'))
#'
#' blr_plot_diag_leverage(model)
#'
#' @family diagnostic plots
#'
#' @export
#'
blr_plot_diag_leverage <- function(model, print_plot = TRUE) {
blr_check_model(model)
p1 <- blr_plot_difdev_leverage(model)
p2 <- blr_plot_difchisq_leverage(model)
p3 <- blr_plot_c_leverage(model)
p4 <- blr_plot_fitted_leverage(model)
myplots <- list(difdev_leverage = p1,
difchisq_leverage = p2,
c_leverage = p3,
fitted_leverage = p4)
if (print_plot) {
do.call(grid.arrange, c(myplots, list(ncol = 2)))
}
invisible(myplots)
}