Learn R Programming

extracat (version 1.6-3)

wdcor: Weighted Distance Correlation

Description

An efficient implementation of the distance correlation for two variables with the additional option to weight the observations. The main application for the weights is to use frequencies according to ordinal variables which can be represented by a contingency table (wdcor.table)). Another idea is to make the distance correlation more robust by assigning small weights to observations which are far from the rest of the data. For large datasets the distance correlation is often said to be too inefficient to be of any great use. The function approx.dcor offers a pretty good approximation of the distance correlation via binning and wdcor.table). wdcor.data.frame computes a distance correlation matrix. Factor variables are transformed to integer via data.matrix.

Usage

wdcor(x,...)
## S3 method for class 'default':
wdcor(x,y,w = NULL,ep = 1,\dots)
## S3 method for class 'table':
wdcor(x,ep = 1,\dots)
## S3 method for class 'data.frame':
wdcor(x,approx = TRUE,\dots)

Arguments

x
A data crosstable or a numeric vector.
y
A numeric vector.
w
Weights, typically frequencies. The default weights all cases the same, which leads to the standard distance correlation.
ep
The euclidean (absolute) distances can be taken to the power of ep.
approx
Whether or not to use approx.dcor instead of wdcor for the data.frame method.
...
dots.

Value

  • The correlation value which is between 0 and 1.

References

Szekely, G. J. Rizzo, M. L. and Bakirov, N. K. (2007). "Measuring and testing independence by correlation of distances", Annals of Statistics, 35/6, 2769-2794

See Also

approx.dcor

Examples

Run this code
# repeat and change N for different results and computation times.
N <- 2000
x1 <- rnorm(N,mean=10,sd=3)
x2 <- runif(N,0,40)
x3 <- rnorm(N,mean=30,sd=4)
x <- sample(c(x1,x2,x3),N)		
			
y <- rnorm(1,sd=0.0001)*(x-mean(x))^4+ rnorm(1,sd=0.01)*(x-mean(x))^3
 + rnorm(1,sd=0.1)*(x-mean(x))^2 
 + rnorm(1)*(x-mean(x))+rnorm(N,sd=runif(N,3,10))
 + runif(N,0,20)*sin(abs(scale(x))*2*pi)

plot(x,y,pch=19,col=alpha("black",0.2))
system.time(dd<-wdcor(x,y))

y2 <- runif(2000)
system.time(dde<-wdcor(x,y2))
dd
dde

Run the code above in your browser using DataLab