Error in UseMethod("predictLink") when trying to fit Lee-Carter using StMoMo

library("demography")
#> Loading required package: forecast
#> This is demography 1.20
library("StMoMo")
#> Loading required package: gnm
canada <- hmd.mx("CAN", "username", "password", "Canada")
#> Error in hmd.mx("CAN", "username", "password", "Canada"): Connection error at www.mortality.org. Please check username, password and country label.
constLC <- function(ax, bx, kt, b0x, gc, wxt, ages){
  +     + c1 <- mean(kt[1, ], na.rm = TRUE)
  +     + c2 <- sum(bx[, 1], na.rm = TRUE)
  +     + list(ax = ax + c1 * bx, bx = bx / c2, kt = c2 * (kt - c1))
  }
LC <- StMoMo(link = "logit", staticAgeFun = TRUE, periodAgeFun = "NP",
             constFun = constLC)
CANStMoMo <- StMoMoData(canada, series = "total")
#> Error in StMoMoData(canada, series = "total"): object 'canada' not found
summary(CANStMoMo)
#> Error in summary(CANStMoMo): object 'CANStMoMo' not found
CANStMoMoinitial <- central2initial(CANStMoMo)
#> Error in central2initial(CANStMoMo): object 'CANStMoMo' not found
wxt <- genWeightMat(ages = ages.fit, years = CANStMoMoinitial$years,
                    clip = 3)
#> Error in genWeightMat(ages = ages.fit, years = CANStMoMoinitial$years, : object 'ages.fit' not found
LCfit <- fit(LC, data = CANStMoMoinitial, ages.fit = ages.fit, wxt = wxt)
#> Error in fit.StMoMo(LC, data = CANStMoMoinitial, ages.fit = ages.fit, : object 'CANStMoMoinitial' not found

Created on 2018-11-14 by the reprex package (v0.2.1)

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.

Now posted! (http://localhost:22619/session/reprex190a36cecbb8/reprex_reprex.html)

localhost refers to your machine, so no one else can access that link :slightly_frowning_face:

Oh dear, sorry! Added it to original post now.

So, you don't actually make a reprex by calling the reprex library — it's something you run on the code itself that allows other people to run your code as well. Nick Tierney's magic reprex article will take you through the process quickly:

Or, if you prefer video, Jenny gives a brief overview of it in her webinar here:

It looks like you've filed an issue (helpful, since it's hard to figure out from the existing documentation what's going wrong). We just ask that you also post the issue to your community thread, so that others who may run into the same issue can follow along, too. :slightly_smiling_face:

Okay so it acts really differently when I do it the way shown in the video! In that I encounter a different error (that is 'Error in hmd.mx'), this error doesn't happen when run in the console.

It looks like this has to do with not being able to connect to the data source you're trying to use:

#> Error in hmd.mx("CAN", "username", "password", "Canada"): Connection error at www.mortality.org. Please check username, password and country label.

As a result, you don't create that first object which, then, doesn't exist for any of your subsequent analysis. You might have better luck if you can create a small example that doesn't use a remote data source, or re-create a piece of the data that you're pulling locally for the purposes of the reprex.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.