timriffe / DemoTools

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

OPAG doesn't run with 5-year data in PAS sample #236

Open peterdavjohnson opened 3 years ago

peterdavjohnson commented 3 years ago

I tried to reproduce the results in the sample data in the PAS OPAG workbook, but it crashes in groupAges. Trying to track down the error there seem to be points in the chain of functions where the ages passed go down from age 45 instead of up.

Pop <- c(88962, 73756, 51990, 55395, 48562)
Age <- seq(45,65,5)

Lx <- c(280032, 255560, 226333, 191419, 150907,
        107242, 64867, 38472)
AgeLx <- seq(45,80,5)

opag_out <- OPAG(Pop,Age, c(rep(5,4),Inf), nLx=Lx, Age_nLx = AgeLx,
                 Age_fit = seq(45,80,5),
                 Redistribute_from = 65,
                 OAnew = 80)
timriffe commented 3 years ago

Now should work. Just a note, nLx is fit to Pop, so Age_fit should be with the range of Age_Pop. For now Age_fit is silently filtered down to cover cases like this one. Thanks for reporting, as always

peterdavjohnson commented 3 years ago

Hi Tim, I finally got the new version installed but my test code still won't run. The error I get is: Error in Age_fit[1]:(max(Age_fit) + AgeInt_fit[N] - 1) : NA/NaN argument

I don't really understand why the AgeInt values are needed as inputs since the assumption is that you are giving pops (and nLx) with the last age group open, so the intervals should be able to be computed from the age values.

I will download the full code so I can debug and see where the problem occurs.

Thanks, pj

On Sat, May 8, 2021 at 6:38 PM Tim Riffe @.***> wrote:

Now should work. Just a note, nLx is fit to Pop, so Age_fit should be with the range of Age_Pop. For now Age_fit is silently filtered down to cover cases like this one. Thanks for reporting, as always

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/timriffe/DemoTools/issues/236#issuecomment-835551133, or unsubscribe https://github.com/notifications/unsubscribe-auth/APAK5QW57RRNQPPZPDYQT2TTMW4M7ANCNFSM44ENAHVQ .

timriffe commented 3 years ago

There are two rather fringe reasons why I made AgeInt_fit necessary. 1. to ensure the higher age group is bounded, and 2. to allow for overlapping age ranges when comparing age ranges (although I'm not sure why this would be interesting honestly). So hypothetically, one could compare e.g. age 60-85 and 75-95 in the given and stable populations. That is, if 5 year age data is given, the fitting can still be against fatter age groups. Of course, one can also just keep the 5-year age groups and use more data points as in the example. Anyway, I'll check your example again, which I though was running on my machine before

timriffe commented 3 years ago

Hi Peter, I verified the example runs properly for me, so I think this boils down to the installation issues for now, will examine

peterdavjohnson commented 3 years ago

If only the Age_fit is given, then the AgeInt_fit is assumed to be the same as the PopInt for that age group. This causes the age groups to be fit to be 45-49 and 55-59, which the current logic in OPAG_r_min doesn't handle correctly. The solution is to use the same approach used to compute Pop_fit in the main OPAG function, something like:

w1Lx_fit <- rep(NA, length(Age_fit))
for (i in 1:length(Age_fit)){
  ind <- Age_Lx1 >= Age_fit[i] & Age_Lx1 < (Age_fit[i] + AgeInt_fit[i])
  w1Lx_fit[i] <- sum(w1Lx[ind])
}

  # 5) rescale standard and Pop_fit to sum to 1  
  stand   <- rescale_vector(w1Lx_fit, scale = 1)
peterdavjohnson commented 3 years ago

Sorry, didn't mean to close the Issue!

peterdavjohnson commented 3 years ago

The test data now runs with many different options. However, now that Age_fit and AgeInt_fit is not required, I think it would be useful to return the values that were used in the output list.