alysonvanraalte / LifeIneq

An R-package to calculate measures of lifespan variation from a life table
7 stars 6 forks source link

make all measures rigorously recalculated by age #4

Closed timriffe closed 2 years ago

timriffe commented 2 years ago

Here's my test code to try to detect which implementations give the right conditional result:

devtools::load_all()
data(LT)
measures <- c("mad", "var", "sd", "cov", "edag", "H", "theil", "mld", "gini", "aid")
results_current <- results_conditional <- matrix(0, ncol = nrow(meta), nrow = 111, dimnames = list(age=0:110, measures))

for (measure in measures){
  results_current[,measure] <- ineq(age=LT$Age,dx=LT$dx,lx=LT$lx,ex=LT$ex,ax=LT$ax,method=measure)
}

for (measure in measures){
  for (a in 1:100){
    results_conditional[a, measure] <- ineq(age = LT$Age[1:(112-a)],
                                            dx = LT$dx[a:111],
                                            lx = LT$lx[a:111],
                                            ex = LT$ex[a:111],
                                            ax = LT$ax[a:111],
                                            method = measure)[1]
  }
}
results_current      <- results_current[1:100,]
results_conditional  <- results_conditional[1:100,]

colSums(abs(results_current - results_conditional)) %>% zapsmall()

From this, my take is that we need to look at ineq_cov(), ineq_theil(), ineq_H(), and ineq_mld(). So, here's a checklist:

Worst case scenario for these is we'll possibly need to loop in order to get the right conditional result. I excluded IQR and cp from this because they return a single scalar, although these could also be handled via looping.

timriffe commented 2 years ago

I've checked off Theil and MLD because I have draft versions that match the results of the above conditional loop. These two functions are inside the file dev/theil_alternative.R. The respective functions are named ineq_theil2() and ineq_mld2(). @alysonvanraalte please examine these and tell me whether these coincide with your expectations. Namely, the higher age conditional results are very different from the previous ones. They are however qualitatively similar to e.g. gini, so that's good. if you decide that these function versions are giving the right output, then just remove the 2 and replace the current versions with them.