Created
December 19, 2018 23:15
-
-
Save duttashi/1cbc258fb509cc54f3872fbb613b059b to your computer and use it in GitHub Desktop.
Plot the linear regression results
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
# create function to plot linear regression results | |
# adapted from https://sejohnston.com/2012/08/09/a-quick-and-easy-function-to-plot-lm-results-in-r/ | |
ggplotRegression <- function (fit) { | |
lmdf<- data.frame(fitted_values = fit$fitted.values, actual_values = fit$model[, 1]) | |
print(names(lmdf)) | |
ggplot(lmdf, aes(x = actual_values, y = fitted_values)) + | |
geom_point() + | |
geom_abline(slope = 1, intercept = 0) + | |
labs(title = paste("Adj R2 = ", signif(summary(fit)$adj.r.squared, 4), | |
"Intercept =",signif(fit$coef[[1]],5 ), | |
" Slope =",signif(fit$coef[[2]], 5), | |
" P =",signif(summary(fit)$coef[2,4], 5) | |
), | |
x = 'observed value', y = 'predicted value') | |
} | |
fit<-lm(Sepal.Length ~ Petal.Width, data = iris) | |
ggplotRegression(fit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment