Created
December 14, 2013 01:24
-
-
Save drsnyder/7954464 to your computer and use it in GitHub Desktop.
explorations with correlation
This file contains hidden or 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
# negative correlation x = seq(0,10,0.5) | |
fX = function(x) { return(x) } | |
fY = function(x) { return(-x) } | |
df = data.frame(x=x, fX=fX(x), fY=fY(x)) | |
ggplot(df, aes(x=x)) + geom_line(aes(y=fX), color="green") + geom_line(aes(y=fY), color="blue") | |
cov(df$fX, df$fY) | |
cor(df$fX, df$fY) | |
fX = function(x) { return(x - 5) } | |
fY = function(x) { return(-x + 5) } | |
df = data.frame(x=x, fX=fX(x), fY=fY(x))ggplot(df, aes(x=x)) + geom_line(aes(y=fX), color="green") + geom_line(aes(y=fY), color="blue") | |
cov(df$fX, df$fY) | |
cov(df$fX, df$fY)/(sd(df$fX)*sd(df$fY)) # == cor(x,y) | |
cor(df$fX, df$fY) | |
# how do we generate a plot with a cor 1 > x > -1? | |
x = seq(0,100,5)[0:20] | |
df = data.frame(x=x, fX=rnorm(20,50,25), fY=rnorm(20,50,12)) | |
ggplot(df, aes(x=x)) + geom_point(aes(y=fX), color="green") + geom_point(aes(y=fY), color="blue")cov(df$fX, df$fY)cov(df$fX, df$fY)/(sd(df$fX)*sd(df$fY)) # == cor(x,y)cor(df$fX, df$fY) | |
df <- data.frame(x = 1:20 + rnorm(20,sd=3), y = 1:20 + rnorm(20,sd=3)) | |
ggplot(df, aes(x=x,y=y)) + geom_point(color="blue") + geom_smooth(method=lm, se=F) | |
cor(df$x,df$y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment