Here is how we get the data out:
x = c(1,2,3,4,5)
y = c(3,4,5,5.1,6.2)
pearsons_r = cor(x,y)
r_squared = pearsons_r^2
fit = lm(y~x) # notice the order of variables
y_intercept = as.numeric(fit$coeff[1])
slope = as.numeric(fit$coeff[2])
Here is how we plot it:
plot(x,y)
abline(lm(y~x)) # again, notice the order
function_string = paste("f(x) = ", slope, "x + ", y_intercept, sep="")
r_sq_string = paste("R^2 =", r_squared)
display_string = paste(function_string, r_sq_string, sep="\n")
mtext(display_string, side=3, adj=1) # top right outside of the margin
1 comment:
Found this very useful, thanks for posting!
Post a Comment