Set the "diagonal" diagonal to 1

Is it possible to make a distance object from a statistics packet be something other than 0?

Here I am trying to force it something else, and I can change the upper or lower, but not the diagonals:

set.seed(10) x <- matrix(rnorm(25), ncol = 5) y <- dist(x, diag =TRUE) z <- 1 - as.matrix(y) as.dist(z, diag =TRUE) 

gives:

  1 2 3 4 5 1 0.0000000 2 -0.9030066 0.0000000 3 -0.9803571 -1.9319785 0.0000000 4 -1.5249747 -2.3673155 -1.5928891 0.0000000 5 -2.7903980 -2.8020380 -2.2491893 -1.5839067 0.0000000 

not expected:

  1 2 3 4 5 1 1.0000000 2 -0.9030066 1.0000000 3 -0.9803571 -1.9319785 1.0000000 4 -1.5249747 -2.3673155 -1.5928891 1.0000000 5 -2.7903980 -2.8020380 -2.2491893 -1.5839067 1.0000000 

Perhaps I need to output it as a matrix object instead, because there is something about the fact that there should not be 0 for diagonals, which makes it not correspond to the way dist objects are processed.

+4
source share
2 answers

Not with dist() ; it does not save the diagonal, just a flag indicating whether to print it using the print() method.

This is not unexpected; dist() is a compact way to store distance matrices, not symmetric matrices in general. In the distance matrix, by definition, the distance between the observation and itself is 0. Therefore, dist() considers the diagonal as a trivial thing and does not save it.

If I wanted to do what you want, I would use the guts of dist() and save the data as dist() in a function, say mydist() with the class "mydist" , but then write print.mydist() taking the code from the method print.dist() , but using a different value for the diagonal and write as.matrix.mydist() to do the conversion to the matrix. Your class could either save the values ​​for the diagonal (if they changed), or just the single value that you want the diagonal to be.

Essentially, all you have to do is save the diagonal values ​​you want as an additional attribute, then provide the print() and as.matrix() methods, which are extracted from this attribute to print or fill the matrix.

+6
source

The dist object is not a matrix, although there is a coercive function (which you already know) to create it. If you needed a matrix object, such as a dist object, you could do this:

 > z <- as.matrix(y) > diag(z) <- 1 > z[upper.tri(z)] <- NA > z 1 2 3 4 5 1 1.000000 NA NA NA NA 2 2.515850 1.000000 NA NA NA 3 2.093508 2.443131 1.000000 NA NA 4 2.734773 1.985341 2.652412 1.000000 NA 5 1.107235 2.012257 2.134162 1.913537 1 
+3
source

All Articles