3

I am struggling to get hierarchical clustering, in R. Please do not downgrade this post since I have tried what is at this link How to use 'hclust' as function call in R

Yet I haven't succeeded. A sample of data is here:

structure(list(respondents_id = c("1", "2", "3", "4", "5", "6", 
"7", "8", "9", "10"), comorbidities = c("hypertension", "asthma", 
"diabetes_type_two", "hypertension", "hypertension", "lung_condition", 
"asthma", "obesity", "obesity", "obesity"), chills = structure(c(2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), 
    cough = structure(c(1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L
    ), .Label = c("No", "Yes"), class = "factor"), diarrhoea = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L), .Label = c("No", "Yes"
    ), class = "factor"), fatigue = structure(c(2L, 1L, 1L, 1L, 
    2L, 1L, 2L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), 
    headache = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L), .Label = c("No", "Yes"), class = "factor"), loss_smell_taste = structure(c(1L, 
    1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L), .Label = c("No", "Yes"
    ), class = "factor"), muscle_ache = structure(c(2L, 1L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), 
    nasal_congestion = structure(c(1L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), 
    nausea_vomiting = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), 
    shortness_breath = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 
    1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), 
    sore_throat = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("No", "Yes"), class = "factor"), sputum = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No", "Yes"
    ), class = "factor"), temperature = structure(c(1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("No", "Yes"), class = "factor"), 
    loss_appetite = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("No", "Yes"), class = "factor"), chest_pain = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No", "Yes"
    ), class = "factor"), itchy_eyes = structure(c(1L, 1L, 2L, 
    2L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), 
    joint_pain = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("No", "Yes"), class = "factor")), row.names = c(NA, 
10L), class = "data.frame")

Here is the code:

gower_distance <- cluster::daisy(data_test[,3:19], metric = "gower")
class(gower_distance)

divisive_clustering <- diana(as.matrix(gower_distance), diss = TRUE, keep.diss = TRUE)
hc_complete <- hclust(divisive_clustering, method = "complete")
GaB
  • 1,076
  • 2
  • 16
  • 29
  • 1
    The help page for `hclust` states that the data needs to be a dissimilarity structure. Interestingly, your data works with `hclust` before you use `diana` `hc_complete <- hclust(gower_distance, method = "complete")`. – Tanner33 Aug 20 '20 at 20:00

0 Answers0