I am very new to R and trying to fit a Lee-Carter model to a mortality dataset using both the demography and StMoMo packages in R. However, I am encountering some issues and would appreciate any help or guidance. Here is the code I am using with the demography package :
<pre><code>library(demography)
library(readxl)
men <- read.table("tauxmen.txt", header=TRUE, na.strings=",")
pop <- read.table("populationmen1.txt", header=TRUE, na.strings=",")
# Convert to demogdata object
read.demogdata <- function(
men,
pop,
type = "mortality",
label = "men"
)
# Fit Lee-Carter model
lca(
men,
series = names(men$rate)[1],
years = men$Year,
ages = men$Age,
max.age = 120
)
) And here is the code I am using with the StMoMo package:
library(StMoMo)
# Read in mortality data
men <- read.table("tauxmen.txt", header=TRUE, na.strings=",")
# Create StMoMo object
men_lc <- lc(
data = men,
series = names(men)[3],
ages = 0:120,
years = 1977:2019,
method = "logit",
basis = "pca",
k = 3
)
# Fit Lee-Carter model
men_fit <- fit(men_lc)<
` With both packages, I am encountering errors related to the data format or syntax. For the demography package, I am getting the error "Not demography data", and for the StMoMo package, I am getting errors related to unused arguments or missing objects.
With the demography package, my teacher told me to use read.demogdata function but I dont know how to link it with the lca function, as for the StMomo package one I just looked it up on Rdocumentation and it didnt work.
I have checked the data format and syntax of my code, but I am still having issues. Any suggestions or guidance would be greatly appreciated. Thank you in advance for your help.