[Rd] Error in funcion distancia() in package dprep(v1.0) (PR#9745)
kyousan at sina.com
kyousan at sina.com
Wed Jun 20 04:26:13 CEST 2007
Full_Name: Kang Yousan
Version: 2.5.0
OS: Windows XP
Submission from: (NULL) (211.137.211.67)
There is a bug in function distancia() in package dprep. See the description
below.
> distancia
1 function (x, y)
2 {
3 if (class(y) == "matrix") {
4 distancia = drop(sqrt(colSums((x - t(y))^2)))
5 distancia = t(distancia)
6 }
7 else distancia = sqrt(sum((x - y)^2))
8 distancia
9 }
The t(y) in line 4 should be modified to y. Before the change, see example:
> (x=rnorm(4))
[1] 0.02670846 1.24512505 -0.22145290 1.99760521
> (y=matrix(rnorm(12),4,3))
[,1] [,2] [,3]
[1,] 0.4181184 1.8980350 -1.7398861
[2,] -0.1906483 -0.1777747 0.6666913
[3,] 0.2239468 -0.2631944 -0.3586917
[4,] -1.0316230 -0.7121346 0.3587542
> distancia(x,y)
[,1] [,2] [,3] [,4]
[1,] 1.698568 2.272631 2.336264 2.847838
After the change:
a<-function (x, y)
{
if (class(y) == "matrix") {
distancia = drop(sqrt(colSums((x - y)^2)))
distancia = t(distancia)
}
else distancia = sqrt(sum((x - y)^2))
distancia
}
> a(x,y)
[,1] [,2] [,3]
[1,] 3.404299 3.587609 2.481957
The latter result is resonable according to the description of the function
return value: "distancia numeric value representing the Euclidean distance
between x and y, or a row matrix representing the Euclidean distance between x
and each column of y."
Thanks for attention.
More information about the R-devel
mailing list