functional programming - Manual pearson correlation in r -
how create function manually calculates pearson correlation in r. know there native function called cor, if want apply below equation in r each combination of columns in data frame, how it?
i wish knew how, believe requires many for-loops, nested for-loops etc make happen , not strong @ programming yet. hope attempt such newbie me can learn. thanks
example:
set.seed(1) df = data.frame(v1 = rnorm(10), v2=rnorm(10), v3=rnorm(10), v4=rnorm(10)) # v1 v2 v3 v4 # v1 1.00 -0.38 -0.72 -0.24 # v2 -0.38 1.00 0.60 0.18 # v3 -0.72 0.60 1.00 0.08 # v4 -0.24 0.18 0.08 1.00
first write helper function calculate covariance:
v <- function(x,y=x) mean(x*y) - mean(x)*mean(y)
then use calculate correlation:
my_corr <- function(x,y) v(x,y) / sqrt(v(x) * v(y))
here's quick check works correctly:
> my_corr(df$v1, df$v2) [1] -0.3767034 > cor(df$v1, df$v2) [1] -0.3767034
note calculating correlation way numerically unstable.
edit:
to apply combinations of columns, use outer
:
> outer(df, df, vectorize(my_corr)) v1 v2 v3 v4 # v1 1.00 -0.38 -0.72 -0.24 # v2 -0.38 1.00 0.60 0.18 # v3 -0.72 0.60 1.00 0.08 # v4 -0.24 0.18 0.08 1.00
Comments
Post a Comment