/
ols-response-profile.R
80 lines (70 loc) · 2.03 KB
/
ols-response-profile.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
#' Response variable profile
#'
#' Panel of plots to explore and visualize the response variable.
#'
#' @param model An object of class \code{lm}.
#' @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_response(model)
#'
#' @importFrom stats model.frame model.response
#' @importFrom ggplot2 geom_dotplot geom_histogram
#'
#' @export
#'
ols_plot_response <- function(model, print_plot = TRUE) {
check_model(model)
d <- ols_response_data(model)
p1 <- ols_response_dotplot(d$d1, d$nam)
p2 <- ols_response_trend(d$d2, d$nam)
p3 <- ols_response_hist(d$d1, d$nam)
p4 <- ols_response_box(d$d1, d$nam)
if (print_plot) {
grid.arrange(p1, p2, p3, p4, ncol = 2, top = "Response Diagnostics")
} else {
return(
list(dot_plot = p1,
trend_plot = p2,
histogram = p3,
boxplot = p4)
)
}
}
ols_response_data <- function(model) {
nam <- names(model.frame(model))
pred <- model.response(model.frame(model))
xval <- seq_len(length(pred))
d1 <- data.frame(x = pred)
d2 <- data.frame(x = xval, y = pred)
list(nam = nam, d1 = d1, d2 = d2)
}
ols_response_dotplot <- function(d, nam) {
ggplot(d, aes(x = x)) +
geom_dotplot(binwidth = 1, fill = "blue") +
xlab(nam[1]) +
ggtitle(paste("Dot Plot of", nam[1]))
}
ols_response_trend <- function(d, nam) {
ggplot(d, aes(x = x, y = y)) +
geom_point(color = "blue") +
geom_line(color = "blue") +
xlab("Observation") +
ylab(nam[1]) +
ggtitle(paste("Trend Plot of", nam[1]))
}
ols_response_hist <- function(d, nam) {
ggplot(d, aes(x = x)) +
geom_histogram(bins = 5, color = "black", fill = "blue") +
xlab(nam[1]) +
ggtitle(paste("Histogram of", nam[1]))
}
ols_response_box <- function(d, nam) {
ggplot(d, aes(x = factor(0), y = x)) +
geom_boxplot(fill = "blue") +
xlab("") +
ylab(nam[1]) +
ggtitle(paste("Boxplot of", nam[1])) +
theme(axis.text.x = element_blank())
}