Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MJacobs1985/77fc5e3237897940dd639725b9169681 to your computer and use it in GitHub Desktop.

Select an option

Save MJacobs1985/77fc5e3237897940dd639725b9169681 to your computer and use it in GitHub Desktop.
try <- dataset %>%
dplyr::select(VO2cdi, Meting, ID) %>%
drop_na() %>% # must drop NAs for clustering to work
glimpse()
spread_try <- try %>%
spread(ID,VO2cdi) %>%
glimpse()
VO2_cluster <- t(spread_try[-1])
VO2_cluster_dist <- dist(VO2_cluster , method="euclidean")
fit <- hclust(VO2_cluster_dist, method="ward.D")
fit
plot(fit, family="Arial")
rect.hclust(fit, k=4, border="cadetblue")
ggdendrogram(fit, rotate = TRUE, theme_dendro = FALSE) +
theme_minimal() + xlab("") + ylab("")
clustered_data <- cutree(fit, k=4)
clustered_data_tidy <- as.data.frame(as.table(clustered_data)) %>% glimpse()
colnames(clustered_data_tidy) <- c("ID","cluster")
clustered_data_tidy$ID <- as.character(clustered_data_tidy$ID)
joined_clusters <- try %>%
inner_join(clustered_data_tidy, by = "ID") %>%
glimpse()
table(clustered_data_tidy$cluster)
cluster2<-joined_clusters %>%dplyr::filter(cluster==2)
ggplot(cluster2, aes(Meting, VO2cdi)) +
geom_line(color="grey") +
theme_minimal() +
ylab("VO2 cdi") + xlab("") +
geom_smooth(method="auto",color="red", se=F, size=0.5) +
facet_wrap(~ID)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment