I'm new to R and in a bit of a jam while working on a project in R, specifically trying to fit a Lee-Carter model using the StMoMo package.
I'm attempting to create a StMoMoData object using two datasets: one for mortality (males.txt) and another for population data (malespop.txt). Despite having matching years in both datasets, I'm encountering a perplexing error when trying to create a demogdata object :
"Number of years doesn't match data."
I'm seeking some guidance on how to tackle this "Number of years" error within the context of fitting a Lee-Carter model.
Here's a snippet of the code I'm working with:
library(StMoMo)
library(demography)
# Read in mortality data
men <- read.table("males.txt", header = TRUE, na.strings = ",")
# Read in population data
pop <- read.table("malespop.txt", header = TRUE, na.strings = ",")
# Create a demogdata object
men_demogdata <- demogdata(data = men, pop = pop, ages = 0:120, years = 1977:2019, type = "mortality")
# Create a StMoMoData object
mena <- StMoMoData(men_demogdata, series = "Male")
#Fit an LC model
LCfit <- fit(object = lc(),
data = men,
ages.fit = 0:120,
years.fit = 1977 : 2019)
Any help or insights you can offer would be greatly appreciated!
I did confirm that there was entries for each year for both objects men and pop.
You did not provide much information like the dataset or such. According to git the message states, that the error occours at demogdata
when:
n <- ncol(data)
if(length(years) != n)
stop("Number of years doesn't match data")
which means, that your amount of columns are not fitting the amount of years you are trying to put into the model. You need to reconfigure your dataset or your parameters. Every year does need a column.