robjhyndman / demography

demography package for R
https://pkg.robjhyndman.com/demography
73 stars 25 forks source link

Forecasted kt value not according to expectation #52

Closed Wilsonnn245 closed 1 year ago

Wilsonnn245 commented 1 year ago

Hi, I'm facing an issue when forecasting mortality data using Lee-Carter. This is the R code I used:

R Code

This is the re-adjusted kt output:

image

Thus, I would expect the forecast kt at year 2001 to be around -15. However, the generated value is -0.55:

image

After doing some digging, I found that the forecast function changed my re-adjusted kt value:

image

I'm not sure what went wrong since the ax and bx values are unaffected. Would appreciate on any advice to get the expected forecast kt value. Thanks!

robjhyndman commented 1 year ago

You probably want to use jumpchoice = "actual" in forecast(). There are several variations on the Lee-Carter method, and the function tries to handle them all via the arguments.

Wilsonnn245 commented 1 year ago

Yup, I already tried changing to jumpchoice = "actual" but it would still generate the same results as when I used jumpchoice = "fit".

robjhyndman commented 1 year ago

Right, what is stored in the forecast object is an adjusted version of kt, with the last value subtracted. This makes the code simpler. It makes no difference to the final mx forecasts. If you want the forecasted kt values to match the fitted kt values, just add on the value at the end:

kt.f <- forecastItalyFemale$kt.f
last_kt <- c(tail(ItalylcaFemale$kt,1))
kt.f$x <- ItalylcaFemale$kt
kt.f$mean <- kt.f$mean + last_kt
kt.f$lower <- kt.f$lower + last_kt
kt.f$upper <- kt.f$upper + last_kt
plot(kt.f)
Wilsonnn245 commented 1 year ago

It works perfectly now, appreciate your help!

I've also had another issue using the FDM model in 'demography' package. I've tried to simulate the results and compare it with a past research. When I plot the basis functions, the shape of the graphs are similar but the scaling is off.

Reference: image

Simulated Results: image

R Code: image image

Is there something that I would have to amend? Or the scaling would not be a huge issue as long as the shape is similar? Thanks in advance!

robjhyndman commented 1 year ago

The scale factors are arbitrary -- if you multiply the basis function by k and divide the coefficient by k, you get the same result. For this reason, the basis functions are usually normalized in some way. Perhaps the normalization being used is different in the reference to the current version of the package.

Wilsonnn245 commented 1 year ago

I see, that's great! Thanks for all the help and guidance Rob, really appreciate it <3