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)