Skip to content

Instantly share code, notes, and snippets.

@duttashi
Created December 19, 2018 23:15
Show Gist options
  • Save duttashi/1cbc258fb509cc54f3872fbb613b059b to your computer and use it in GitHub Desktop.
Save duttashi/1cbc258fb509cc54f3872fbb613b059b to your computer and use it in GitHub Desktop.
Plot the linear regression results
# 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