[R] Help with efficient double sum of max (X_i, Y_i) (X & Y vectors)
Jeffrey Racine
racinej at mcmaster.ca
Thu Feb 1 19:18:03 CET 2007
Greetings.
For R gurus this may be a no brainer, but I could not find pointers to
efficient computation of this beast in past help files.
Background - I wish to implement a Cramer-von Mises type test statistic
which involves double sums of max(X_i,Y_j) where X and Y are vectors of
differing length.
I am currently using ifelse pointwise in a vector, but have a nagging
suspicion that there is a more efficient way to do this. Basically, I
require three sums:
sum1: \sum_i\sum_j max(X_i,X_j)
sum2: \sum_i\sum_j max(Y_i,Y_j)
sum3: \sum_i\sum_j max(X_i,Y_j)
Here is my current implementation - any pointers to more efficient
computation greatly appreciated.
nx <- length(x)
ny <- length(y)
sum1 <- 0
sum3 <- 0
for(i in 1:nx) {
sum1 <- sum1 + sum(ifelse(x[i]>x,x[i],x))
sum3 <- sum3 + sum(ifelse(x[i]>y,x[i],y))
}
sum2 <- 0
sum4 <- sum3 # symmetric and identical
for(i in 1:ny) {
sum2 <- sum2 + sum(ifelse(y[i]>y,y[i],y))
}
Thanks in advance for your help.
-- Jeff
--
Professor J. S. Racine Phone: (905) 525 9140 x 23825
Department of Economics FAX: (905) 521-8232
McMaster University e-mail: racinej at mcmaster.ca
1280 Main St. W.,Hamilton, URL:
http://www.economics.mcmaster.ca/racine/
Ontario, Canada. L8S 4M4
`The generation of random numbers is too important to be left to chance'
More information about the R-help
mailing list