Skip to content

Gower distance calculations may be incorrect #48

Description

@Nowosad

Dear @HajkD -- I think that the Gower distance is not correctly implemented in the package. As far as I understand it -- for categorical features it should either return 0 (if identical) or 1 (if different). Moreover, this distance should be bounded between 0 and 1. However, the current version of the code has no understanding of categorical variables and returns values larger than 1.

reference <- data.frame(cat = factor(c("A","B", "A", "C"), levels = c("A","B", "C")))

# Compute Gower distance with cluster::daisy
gower_dist <- cluster::daisy(reference, metric = "gower")
as.matrix(gower_dist)
#>   1 2 3 4
#> 1 0 1 0 1
#> 2 1 0 1 1
#> 3 0 1 0 1
#> 4 1 1 1 0

# Compute Gower distance with philentropy::distance
reference2 <- reference
reference2$cat <- as.integer(reference2$cat)
gower_dist2 <- philentropy::distance(reference2, method = "gower")
#> Metric: 'gower' with unit: 'log'; comparing: 4 vectors
gower_dist2
#>    v1 v2 v3 v4
#> v1  0  1  0  2
#> v2  1  0  1  1
#> v3  0  1  0  2
#> v4  2  1  2  0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions