ihmeuw-demographics / hierarchyUtils

Demographics Related Utility Functions
https://ihmeuw-demographics.github.io/hierarchyUtils/
BSD 3-Clause "New" or "Revised" License
8 stars 3 forks source link

Categorical trees question #32

Closed krpaulson closed 4 years ago

krpaulson commented 4 years ago
mapping <- data.table::data.table(
  col = rep("location", 4),
  parent = rep("all", 4),
  child = c("east", "west", "north", "south")
)
test_tree <- create_agg_tree(
  mapping,
  exists = c("east", "west", "north", "south", "all"),
  col_type = "categorical"
)

The above only gives me the top level in my tree ("all"), when I expect to get the next level down too, with the regions.

I'd add that the description for exists could be clearer. Perhaps "unique values in the hierarchy"?

krpaulson commented 4 years ago

I've tried a few modifications and it seems that in general it is omitting the lowest level.

chacalle commented 4 years ago

You only need the parent and child columns like in iran_mapping. There should be a better assertion for that

krpaulson commented 4 years ago
dt <- data.table::data.table(
  location_1 = c("east", "east", "east"),
  location_2 = c("east", "west", "all")
)
mapping <- data.table::data.table(
  col = rep("location", 4),
  parent = rep("all", 4),
  child = c("east", "west", "north", "south")
)
tree <- data.tree::FromDataFrameNetwork(mapping)
dt[, distance_location := 
     data.tree::Distance(tree[[location_1]], tree[[location_2]])]

Thanks! I might just use the FromDataFrameNetwork function directly actually. Do you have an idea of how to get this to work? Trying to use the tree within a data.table to assign the distance between locations in two columns. But getting an error on this test example that I'm not sure how to solve.

chacalle commented 4 years ago

I think your mapping is still messed up, delete the col column.

Something like data.tree::Distance(data.tree::FindNode(node = tree, name = "east"), data.tree::FindNode(node = tree, name = "north"))

krpaulson commented 4 years ago

Yeah, I got there but this function is not vectorized. We can close this issue here and I'll move over to lifetableMethods.