-
-
Save keithmcnulty/0f2dd5967938e913fa8ca76bac9b5a08 to your computer and use it in GitHub Desktop.
Using residuals dsitributions to evaluate model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(patchwork) | |
library(rethinking) | |
data(Howell1) | |
# linear model and linear log model | |
linmodel <- lm(height ~ weight, data = Howell1) | |
logmodel <- lm(height ~ log(weight), data = Howell1) | |
# fit charts | |
g1 <- ggplot(data = Howell1, aes(x = weight, y = height)) + | |
geom_point(size = 0.8) + | |
geom_jitter(color = "blue") + | |
geom_smooth(method = "lm", color = "red") + | |
theme_minimal() + | |
labs(x = "Weight", y = "Height") | |
g2 <- ggplot(data = Howell1, aes(x = weight, y = height)) + | |
geom_point(size = 0.8) + | |
geom_jitter(color = "blue") + | |
geom_smooth(formula = "y ~ log(x)", method = "lm", color = "red") + | |
theme_minimal() + | |
labs(x = "Weight", y = "Height") | |
# density/hist | |
g3 <- ggplot() + | |
geom_histogram(aes(x = linmodel$residuals, y=..density..), fill = "lightblue", color = "pink") + | |
geom_density(aes(x = linmodel$residuals), alpha=.2, fill="#FF6666", inherit.aes = FALSE) + | |
theme_minimal() + | |
labs(x = "Residual", y = "Density") | |
g4 <- ggplot() + | |
geom_histogram(aes(x = logmodel$residuals, y=..density..), fill = "lightblue", color = "pink") + | |
geom_density(aes(x = logmodel$residuals), alpha=.2, fill="#FF6666", inherit.aes = FALSE) + | |
theme_minimal() + | |
labs(x = "Residual", y = "Density") | |
# qqplots | |
g5 <- ggplot() + | |
stat_qq(aes(sample = linmodel$residuals), color = "blue", size = 1.5) + | |
stat_qq_line(aes(sample = linmodel$residuals), color = "red") + | |
theme_minimal() + | |
labs(x = "Theoretical", y = "Sample") | |
g6 <- ggplot() + | |
stat_qq(aes(sample = logmodel$residuals), color = "blue", size = 1.5) + | |
stat_qq_line(aes(sample = logmodel$residuals), color = "red") + | |
theme_minimal() + | |
labs(x = "Theoretical", y = "Sample") | |
# combine | |
(g1 | g2) / | |
(g3 | g4) / | |
(g5 | g6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment