I need assistance with my code with the Lee-Carter method urgently please

I have been trying to use R to forecast future mortality rates but I keep getting an error code: "Error in pop * mx : non-conformable arrays". I really need the code to work. here is there code producing the error:

library(demography)

Create the mortality rate dataset

age_groups <- c("15", "16", "17", "18", "19", "20", "21")
years <- c("2015", "2016", "2017", "2018", "2019")

mortality_rates <- matrix(data = c(
4.3, 3.3, 2.3, 1.59, 1.34,
4.49, 3.4, 2.8, 2.39, 2.12,
5.29, 4.3, 3.6, 2.52, 2.31,
7.8, 6.2, 3.6, 3.25, 2.86,
6.46, 5.2, 4.4, 4.01, 3.79,
6.68, 6.3, 4.8, 5.35, 4.9,
6.43, 6.3, 5.1, 5.86, 5.41
), nrow = length(age_groups), ncol = length(years), byrow = TRUE, dimnames = list(age_groups, years))

population <- matrix(data = c(
63937, 71957, 86645, 89203, 91701,
41931, 55173, 63164, 97777, 100515,
29093, 35915, 35827, 93464, 96081,
35556, 46479, 53210, 80364, 82614,
27496, 31605, 36182, 60748, 62449,
14073, 19279, 22071, 37201, 38243,
10018, 11181, 13021, 21625, 22231
), nrow = length(age_groups), ncol = length(years), byrow = TRUE, dimnames = list(age_groups, years))

Create a DemogData object

data <- demogdata(mortality_rates, year = as.numeric(years), pop = population, age = age_groups,
type = "mortality", label = "Nigeria", name = "adult")

Fit the Lee-Carter model

fit <- lca(data)

Print the model summary

print(fit)

These must be numeric.

library(demography)
#> Loading required package: forecast
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> Registered S3 methods overwritten by 'demography':
#>   method      from 
#>   print.lca   e1071
#>   summary.lca e1071
#> This is demography 2.0
age_groups <- c(15, 16, 17, 18, 19, 20, 21)
years <- c(2015, 2016, 2017, 2018, 2019)
mortality_rates <- matrix(data = c(
  4.3, 3.3, 2.3, 1.59, 1.34,
  4.49, 3.4, 2.8, 2.39, 2.12,
  5.29, 4.3, 3.6, 2.52, 2.31,
  7.8, 6.2, 3.6, 3.25, 2.86,
  6.46, 5.2, 4.4, 4.01, 3.79,
  6.68, 6.3, 4.8, 5.35, 4.9,
  6.43, 6.3, 5.1, 5.86, 5.41
), nrow = length(age_groups), ncol = length(years), byrow = TRUE, dimnames = list(age_groups, years))

population <- matrix(data = c(
  63937, 71957, 86645, 89203, 91701,
  41931, 55173, 63164, 97777, 100515,
  29093, 35915, 35827, 93464, 96081,
  35556, 46479, 53210, 80364, 82614,
  27496, 31605, 36182, 60748, 62449,
  14073, 19279, 22071, 37201, 38243,
  10018, 11181, 13021, 21625, 22231
), nrow = length(age_groups), ncol = length(years), byrow = TRUE, dimnames = list(age_groups, years))

dim(population)
#> [1] 7 5
d <- demogdata(mortality_rates, year = as.numeric(years), pop = population, age = age_groups,
                  type = "mortality", label = "Nigeria", name = "adult")

fit <- lca(d)
plot(fit)

Created on 2023-07-04 with reprex v2.0.2

Thanks a lot for your insight. It runs well now

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.