R Interclass distance matrix

This question is a continuation of how to extract intergroup and intergroup distances from a distance matrix? in R . In this question, they first calculated the distance matrix for all points, and then simply retrieved the interclass distance matrix. I have a situation where I would like to bypass the initial calculations and skip the extraction right, i.e. I want to directly calculate the interclass distance matrix. Drawing from a related example, with tricks, let's say I have some data in a data frame df:

values<-c(0.002,0.3,0.4,0.005,0.6,0.2,0.001,0.002,0.3,0.01)
class<-c("A","A","A","B","B","B","B","A","B","A")
df<-data.frame(values, class)

What I need is a distance matrix:

    1    2    3    8   10
4 .003 .295 .395 .003 .005
5 .598 .300 .200 .598 .590
6 .198 .100 .200 .198 .190
7 .001 .299 .399 .001 .009
9 .298 .000 .100 .298 .290

Is there an already elegant and fast way to do this in R?

1D- , : , , df :

values1<-c(0.002,0.3,0.4,0.005,0.6,0.2,0.001,0.002,0.3,0.01)
values2<-c(0.001,0.1,0.1,0.001,0.1,0.1,0.001,0.001,0.1,0.01)
class<-c("A","A","A","B","B","B","B","A","B","A")
df<-data.frame(values1, values2, class)

B A.

+2
2

n - ( R, ):

square_dist(b,a) = sum_i(b[i]*b[i]) + sum_i(a[i]*a[i]) - 2*inner_prod(b,a)

a b i=[1,n]. a b - a b. , a b.

:

## First split the data with respect to the class
n <- 2   ## the number of dimensions, for this example is 2
tmp <- split(df[,1:n], df$class)

d <- sqrt(matrix(rowSums(expand.grid(rowSums(tmp$B*tmp$B),rowSums(tmp$A*tmp$A))),
                 nrow=nrow(tmp$B)) - 
          2. * as.matrix(tmp$B) %*% t(as.matrix(tmp$A)))

:

  • rowSums sum_i(b[i]*b[i]) sum_i(a[i]*a[i]) b b a a .
  • expand.grid b a.
  • rowSums sum_i(b[i]*b[i]) + sum_i(a[i]*a[i]) .
  • matrix. , - b .
  • . tmp$B %*% t(tmp$A), .
  • , .

:

print(d)
##          1         2         3         8         10
##4 0.0030000 0.3111688 0.4072174 0.0030000 0.01029563
##5 0.6061394 0.3000000 0.2000000 0.6061394 0.59682493
##6 0.2213707 0.1000000 0.2000000 0.2213707 0.21023796
##7 0.0010000 0.3149635 0.4110985 0.0010000 0.01272792
##9 0.3140143 0.0000000 0.1000000 0.3140143 0.30364453

, n > 1. 1- , n 1 rowSums ( tmp$A tmp$B):

n <- 1   ## the number of dimensions, set this now to 1
tmp <- split(df[,1:n], df$class)

d <- sqrt(matrix(rowSums(expand.grid(tmp$B*tmp$B,tmp$A*tmp$A)),
                 nrow=length(tmp$B)) - 
          2. * as.matrix(tmp$B) %*% t(as.matrix(tmp$A)))
print(d)
##      [,1]  [,2]  [,3]  [,4]  [,5]
##[1,] 0.003 0.295 0.395 0.003 0.005
##[2,] 0.598 0.300 0.200 0.598 0.590
##[3,] 0.198 0.100 0.200 0.198 0.190
##[4,] 0.001 0.299 0.399 0.001 0.009
##[5,] 0.298 0.000 0.100 0.298 0.290
+3

, :

abs(matrix(Reduce(`-`, expand.grid(split(df$values, df$class))), nrow=5, byrow=TRUE))
#      [,1]  [,2]  [,3]  [,4]  [,5]
#[1,] 0.003 0.295 0.395 0.003 0.005
#[2,] 0.598 0.300 0.200 0.598 0.590
#[3,] 0.198 0.100 0.200 0.198 0.190
#[4,] 0.001 0.299 0.399 0.001 0.009
#[5,] 0.298 0.000 0.100 0.298 0.290
+2

Source: https://habr.com/ru/post/1654996/


All Articles