timriffe / DemoTools

Tools for the evaluation, adjustment, and standardization of demographic data
https://timriffe.github.io/DemoTools
Other
59 stars 31 forks source link

mig_beta and interp_coh fail when span bw date1 and date2 is exactly one year #229

Closed sarahertog closed 3 years ago

sarahertog commented 3 years ago

Hi Tim, When I try to use mig_beta() with a 1x1 population matrix, I get the following error: Aggregate function missing, defaulting to 'length'

However, if I increase date2 by one day, it works fine.

I tried with interp_coh and have the same problem, so I think it's coming from rup()?

Is there a way to fix so that I can use with 1x1 populations?

Thanks!

sarahertog commented 3 years ago

Some more info: It seems to only be a problem for Jan 1 to Jan 1. For example

This fails:

jan1 <- mig_beta( location = "Russian Federation", sex = "male", c1 = pop1m_rus2002, c2 = pop1m_rus2010, date1 = "2002-01-01", date2 = "2003-01-01", age1 = 0:100)

But this works:

nov1 <- mig_beta( location = "Russian Federation", sex = "male", c1 = pop1m_rus2002, c2 = pop1m_rus2010, date1 = "2002-11-01", date2 = "2003-11-01", age1 = 0:100)

timriffe commented 3 years ago

Hi @sarahertog these examples work for me, can you reinstall and then re-run in a fresh R session? If the error persists, can I get the output of sessionInfo() ? Thanks!

timriffe commented 3 years ago

Actually quite possibly a tidy-up tinker while testing is what lead to success for these cases :man_facepalming: , but either way I expect a reinstall should fix it.

Trivia: The likely culprit was in maintaining a matrix class when the number of columns drops to 1. By default R turns a matrix to a vector when this happens, but adding the arg pxt[, , drop = FALSE] keeps it as a matrix even when it shrinks to just a column. Mentioning as this isn't the only time I've been stung by this, so maybe you'll recognize it if it ever happens to you.

timriffe commented 3 years ago

Please note also mig_beta() only makes it half way through PJ's steps, it's not the full package yet. This makes it to the step called Prelim in the Migs tab of the worksheet called ResidualMigBetaZA.xls. The remaining steps appear to be touch-up for i) children and ii) optional smoothing. (i) appears to be specific to this method and so I think it makes sense to implement, and (ii) draws on pre-existing smoothing options, so it might be considered optional.

sarahertog commented 3 years ago

Thanks, Tim! I re-installed DemoTools and now Jan1 -> Jan 1 works. Thanks also for the heads up on the mig_beta().