I am trying to reproduce one of models evolved on the base of Lee-Carter model by use of RStudio. Because I am still pretty fresh in this software, my question is not really sophisticated.
Applying MLE with Poisson distribution, I got final solution: $\alpha_x + \beta_x \, \kappa_t = \log(D_{x,t} / E_{x,t})$ with x as number of ages (for instance from i = 0 to 90) and with t as year (for instance from j = 1970 to 2016). Then $D_{x,t}$ and $E_{x,t}$ are matrix x = 91 (rows) times t = 47 (columns), number of cells= 4277 in which every cell is noted either by $D_{i,j}$ or $E_{i,j}$. These values are earlier determined and we will keep them as given values since they could be estimated by singular value decomposition, what I have managed to do earlier.
As in Lee-Carter model one imposes consecutive restraints: all kappas in vector of parameters $\sum_t \kappa_t = 0$ and all betas in vector of parameters $\sum_x \beta_x = 1$, one gets a constrained problem.
Moreover, one can notice that number of parameters to be changed equals to $2x + t$ as $\alpha_i$ is not time-dependent, what gives in my case 229 (91*2 + 47) parameters. In that way one wants to minimize the difference between LHS (to be adjusted) and RHS (earlier calculated deterministic values).
I have tried many functions not finding the solution. My last version of code follows:
ages <- 91 #in description called "x"
years <-47 #in description called "t"
DX_Expo_POLF <- matrix(0,ages, years) #deaths are divided by exposure and taken into logarithm
#the operation is repeated for whole matrix
for (i in 1:ages) {
for (j in 1:years) {
DX_Expo_POLF[i,j] <- log(DX_PolandF[i,j]/Expo_PolandF[i,j])
}
}
#difference between LHS and RHS is to be minimised. Left - earlier determined values
obj_POLF <- as.vector(abs(DX_Expo_POLF - (rep(main_aPOLF,years) + main_bPOLF %*% main_kPOLF)))
mat_POLF <- matrix(0,2, ages ) #first parameter - beta, second one - kappa
for (i in 1:ages) {
mat_POLF[1,i] <- main_bPOLF[i,1]
}
for (i in 1:years) {
mat_POLF[2,i] <- main_kPOLF[1,i]
}
dir_POLF <- rep("==", 2) # constraint directions
rhs_POLF <- c(1,0) # constraint upper-bounds
opt_POLF <- Rglpk_solve_LP(obj = obj_POLF, mat = mat_POLF, dir = dir_POLF, rhs = rhs_POLF, max = FALSE)
opt_POLF.main_kPOLF <- matrix(opt_POLF$solution, 1, years)
opt_POLF.mat_POLF <- matrix(opt_POLF$solution, ages, years)
where I tried to minimize absolute difference between both matrices by keeping vectors restrained. Repeating, only vectors $\alpha_x , \beta_x , \kappa_t$ should be changed.
Basically my question is: what function and specification should I use?
Excuse me if question is trivial.