Mike3
January 5, 2024, 1:27pm
1
Hi there! I am forecasting population(each gender separately) of one territory for next 2 years using Lee Carter method in R studio.

I am facing the same problem again and again.

My R script code is:

Install the required packages
install.packages(c("demography", "forecast", "readxl"))

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)

Assuming you have read the data into a variable named 'lee_carter_data'
Removing the first unnamed column (years)
lee_carter_data <- lee_carter_data[-1]

Convert the tibble to a standard data frame
lee_carter_data <- as.data.frame(lee_carter_data)

Convert the data to a matrix
mortality_matrix <- as.matrix(lee_carter_data)

Fit the Lee-Carter model using the demography package
lc_model <- demography::lca(mortality_matrix)

Forecasting future mortality rates
'h' is the number of periods (years) to forecast
forecast_result <- forecast(lc_model, h = 2)

Plotting the forecast
plot(forecast_result)

*And the problem is

Error in demography::lca(mortality_matrix) : Not demography data

What is solution of this? Please, help me. I need your assistance as soon as possible.

Thanks.

Here is my collected data in excel.

I think we need to see some sample data.

When you have created `lee_carter_data`

try this

```
dput(head(lee_carter_data, 100))
```

Copy the output and paste it here between
```

```

Thanks

startz
January 5, 2024, 3:36pm
3
The `lca`

function wants an argument of type `demogdata`

, not a matrix.

Mike3
January 5, 2024, 3:58pm
4
Thank you for your help and time.

Should I write whole code like below:

Install the required packages
install.packages(c("demography", "forecast", "readxl"))

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)

Assuming you have read the data into a variable named 'lee_carter_data'
Removing the first unnamed column (years)
lee_carter_data <- lee_carter_data[-1]

Convert the tibble to a standard data frame
lee_carter_data <- as.data.frame(lee_carter_data)

Convert the data to a matrix
mortality_matrix <- as.matrix(lee_carter_data)

[New Line] Output a text representation of the first 100 rows of your data
dput(head(lee_carter_data, 100))

Fit the Lee-Carter model using the demography package
lc_model <- demography::lca(mortality_matrix)

Forecasting future mortality rates
'h' is the number of periods (years) to forecast
forecast_result <- forecast(lc_model, h = 2)

Plotting the forecast
plot(forecast_result)

Mike3
January 5, 2024, 4:00pm
5
Thank you for your reply.

What do you mean by that? What should I do?
Can you please, provide me where should I change?

Thank you

startz
January 5, 2024, 4:10pm
6
I can’t give you specifics because I don’t use that package. Take a look at the help information.

The `demogdata()`

function will convert a matrix into a "demogdata object suitable for plotting using plot.demogdata and fitting an LC or BMS model using lca or an FDA model using fdm." The quoted part is from the package documentation.

I also have no experience with the demography package.

system
Closed
January 26, 2024, 6:15pm
8
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.