Skip to content

Instantly share code, notes, and snippets.

@janeshdev
Last active December 18, 2015 11:19
Show Gist options
  • Save janeshdev/5774475 to your computer and use it in GitHub Desktop.
Save janeshdev/5774475 to your computer and use it in GitHub Desktop.
Regression
# GET EQUATION AND R-SQUARED AS STRING
# SOURCE: http://goo.gl/K4yh
lm_eqn = function(df){
m = lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}
p1 = p + geom_text(aes(x = 25, y = 300, label = lm_eqn(df)), parse = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment