Error in if (nrow(pop) != p | ncol(pop) != n) stop("data and pop are of different size") : argument is of length zero

Greetings!
I need really need your help. What is problem? Which section of the code should I fix? Who has any idea? Please, help me. This is crucial for my study.

*Code starts

Required packages

install.packages("readxl")
install.packages("demography")
install.packages("forecast")

Load the packages

library(demography)
library(forecast)
library(readxl)

Specify the file path

file_path <- "D:/maledatafer.xlsx"

Read the Excel file

lee_carter_data <- readxl::read_excel(file_path, sheet = 1)

Extract the years and population data

years <- lee_carter_data$Years
population_data <- lee_carter_data[, -1] # Exclude the first column (Years)

Convert the data to a matrix

mortality_matrix <- as.matrix(population_data)

Check dimensions and transpose if needed

if (nrow(mortality_matrix) < ncol(mortality_matrix)) {
mortality_matrix <- t(mortality_matrix)
}

Convert the matrix to a 'demogdata' object

demog_data <- demogdata(mortality_matrix, pop = colSums(mortality_matrix))

Fit the Lee-Carter model using the demography package

lc_model <- lca(demog_data)

'h' is the number of periods (years) to forecast

forecast_result <- forecast(lc_model, h = 2)

plot(forecast_result)

*Code ends
Problem is:
Error in if (nrow(pop) != p | ncol(pop) != n) stop("data and pop are of different size") :
argument is of length zero

What should I do? I really appreciate your help and support.
Thank you

Please, help me. What should I do?

demogdata requires two matrices of the same size: the mortality rates and the population sizes. Your pop argument is neither a matrix, nor the population sizes.

This topic was automatically closed 54 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.