-
Notifications
You must be signed in to change notification settings - Fork 2
/
residuals_base.R
130 lines (97 loc) · 3.35 KB
/
residuals_base.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
# Base code to analyse the residuals in the linear model
# Author: Alfredo Hernández <[email protected]>
# Libraries ------------------------------------------------
library(qqplotr)
library(quantreg)
library(lmtest)
library(nortest)
# Source base code -----------------------------------------
# source("regression_base.R")
# Residuals vs fitted values -------------------------------
plot_resid_vs_fitted <- function(fit) {
data <- tibble(fitted = fitted(fit),
resid = resid(fit))
alpha <- 0.05
smooth <- as_tibble(lowess(data))
gg <- ggplot(data) +
aes(x = fitted, y = resid) +
geom_point(shape = 1, size = 1.5) +
geom_line(data = smooth, aes(x = x, y = y), colour = "red")+
geom_quantile(quantiles = c(alpha, 1 - alpha), method = "rq", colour = "blue") +
geom_hline(yintercept = 0, linetype = "dashed") +
labs(x = "Fitted values", y = "Residuals") +
theme_bw()
return(gg)
}
plot_resid_vs_fitted_from_data <- function(data, formula) {
fit <- lm(data = data, eval(formula))
fit.data <- tibble(fitted = fitted(fit),
resid = resid(fit))
alpha <- 0.05
smooth <- as_tibble(lowess(fit.data))
gg <- ggplot(fit.data) +
aes(x = fitted, y = resid) +
geom_point(shape = 1, size = 1.5) +
geom_line(data = smooth, aes(x = x, y = y), colour = "red") +
geom_quantile(quantiles = c(alpha, 1 - alpha), method = "rq", colour = "blue") +
geom_hline(yintercept = 0, linetype = "dashed") +
labs(x = "Fitted values", y = "Residuals") +
theme_bw()
return(gg)
}
# QQ Plots (residuals) -------------------------------------
plot_resid_qqplot <- function(fit) {
res.data <- tibble(resid = resid(fit))
gg <- ggplot(res.data, aes( sample = resid )) +
stat_qq_line() +
stat_qq_point(shape = 1, size = 1.5) +
labs(x = "Theoretical Quantiles", y = "Sample Quantiles") +
theme_bw()
return(gg)
}
get_boot_residuals <- function(col.x, col.y, boot.stats) {
slope <- boot.stats$slope
inter <- boot.stats$inter
data <- tibble(x = log10(col.x),
y = log10(col.y))
data <- data %>%
mutate(
y.pred = inter + x * slope,
resid = y - y.pred)
return(data)
}
# Q-Q Plots (any variable) ---------------------------------
plot_normal_qqplot <- function(col, name) {
var.data <- tibble(variable = col)
gg <- ggplot(var.data, aes( sample = variable )) +
stat_qq_line() +
stat_qq_point(shape = 1, size = 1.5) +
labs(x = "Theoretical Quantiles", y = name) +
theme_bw()
return(gg)
}
# Statistical tests ----------------------------------------
perform_residual_tests <- function(col.x, col.y) {
data <- data.frame(col.x, col.y)
fit <- lm(data[[2]] ~ data[[1]])
norm.p.val <- lillie.test(residuals(fit))$p.value
inde.p.val <- cor.test(residuals(fit), fitted(fit))$p.value
homo.p.val <- bptest(fit)$p.value
round.digits <- 4
return(c(
"norm.p.val" = round(norm.p.val, digits = round.digits),
"inde.p.val" = round(inde.p.val, digits = round.digits),
"homo.p.val" = round(homo.p.val, digits = round.digits)
))
}
perform_fit_residual_tests <- function(fit) {
norm.p.val <- lillie.test(residuals(fit))$p.value
inde.p.val <- cor.test(residuals(fit), fitted(fit))$p.value
homo.p.val <- bptest(fit)$p.value
round.digits <- 4
return(c(
"norm.p.val" = round(norm.p.val, digits = round.digits),
"inde.p.val" = round(inde.p.val, digits = round.digits),
"homo.p.val" = round(homo.p.val, digits = round.digits)
))
}