Skip to content

Instantly share code, notes, and snippets.

@HanjoStudy
Created April 26, 2019 09:16
Show Gist options
  • Save HanjoStudy/a4f96ec06b160a0f7fc460d6586d82f9 to your computer and use it in GitHub Desktop.
Save HanjoStudy/a4f96ec06b160a0f7fc460d6586d82f9 to your computer and use it in GitHub Desktop.
Cluster_utils.R
# Get the multiclass obs and prop
xtract_multiclass <- function(ctree_res){
tab <- sapply(1:length(ctree_res), function(id) {
y <- data_party(ctree_res[id])
y <- y[["(response)"]]
table(y)
})
tab_prop <- prop.table(tab) %>% round(4) %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "outcome") %>%
gather(node, prop_total, -outcome)
tab <- tab %>% as.data.frame()%>%
tibble::rownames_to_column(var = "outcome") %>%
gather(node, num_obs, -outcome)
left_join(tab, tab_prop) %>%
tbl_df %>%
group_by(node) %>%
mutate(prop_in_node = round(num_obs/sum(num_obs), 3))
}
irisct <- ctree(Species ~ .,data = iris)
xtract_multiclass(irisct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment