r - StMoMo 死亡率包中的完整 PLAT 模型
问题描述
我正在使用 R 包 StMoMo 进行随机死亡率建模。描述符号的论文可以在这里找到:https ://openaccess.city.ac.uk/id/eprint/17378/7/StMoMoVignette.pdf
论文描述了完整的 PLAT 模型:
和一个简化的 PLAT 模型:
然后它为简化的 PLAT 模型提供(见 p13-14)代码。这段代码工作正常。
#to get data
ages.fit = 12:84
years.fit = 2008:2017
gender = "male"
JPNdata = hmd.mx(country="JPN",username=username,password=password,label="Japan")
JPNStMoMo = StMoMoData(JPNdata, series = gender,type="initial")
#the reduced Plat model
f2 <- function(x, ages) mean(ages) - x
constPlat <- function(ax, bx, kt, b0x, gc, wxt, ages){
nYears <- dim(wxt)[2]
x <- ages
t <- 1:nYears
c <- (1 - tail(ages, 1)):(nYears - ages[1])
xbar <- mean(x)
phiReg <- lm(gc ~ 1 + c + I(c ^ 2), na.action = na.omit)
phi <- coef(phiReg)
gc <- gc - phi[1] - phi[2] * c - phi[3] * c ^ 2
kt[2, ] <- kt[2, ] + 2 * phi[3] * t
kt[1, ] <- kt[1, ] + phi[2] * t + phi[3] * (t ^ 2 - 2 * xbar * t)
ax <- ax + phi[1] - phi[2] * x + phi[3] * x ^ 2
ci <- rowMeans(kt, na.rm = TRUE)
ax <- ax + ci[1] + ci[2] * (xbar - x)
kt[1, ] <- kt[1, ] - ci[1]
kt[2, ] <- kt[2, ] - ci[2]
list(ax = ax, bx = bx, kt = kt, b0x = b0x, gc = gc)
}
reducedPlat <- StMoMo(link = "logit", staticAgeFun = TRUE,
periodAgeFun = c("1", f2), cohortAgeFun = "1", constFun = constPlat)
reducedPlat %>% fit(data=JPNStMoMo,ages.fit = ages.fit,years.fit=years.fit)
但是,当我尝试稍微修改代码以获取完整的 Plat 模型时,出现以下错误:
The parameter transformation function does not preserve the fitted rates.
Check the 'constFun' argument of StMoMo."
这是修改后的代码:
f2 <- function(x, ages) mean(ages) - x
f3 <- function(x, ages) max(f2(x,ages),0) #added
constPlat <- function(ax, bx, kt, b0x, gc, wxt, ages){
nYears <- dim(wxt)[2]
x <- ages
t <- 1:nYears
c <- (1 - tail(ages, 1)):(nYears - ages[1])
xbar <- mean(x)
phiReg <- lm(gc ~ 1 + c + I(c ^ 2), na.action = na.omit)
phi <- coef(phiReg)
gc <- gc - phi[1] - phi[2] * c - phi[3] * c ^ 2
kt[2, ] <- kt[2, ] + 2 * phi[3] * t
kt[1, ] <- kt[1, ] + phi[2] * t + phi[3] * (t ^ 2 - 2 * xbar * t)
ax <- ax + phi[1] - phi[2] * x + phi[3] * x ^ 2
ci <- rowMeans(kt, na.rm = TRUE)
ax <- ax + ci[1] + ci[2] * (xbar - x) + ci[3] * max(xbar - x,0) #modified
kt[1, ] <- kt[1, ] - ci[1]
kt[2, ] <- kt[2, ] - ci[2]
kt[3, ] <- kt[3, ] - ci[3] #added
list(ax = ax, bx = bx, kt = kt, b0x = b0x, gc = gc)
}
fullPlat <- StMoMo(link = "logit", staticAgeFun = TRUE,
periodAgeFun = c("1", f2, f3), cohortAgeFun = "1", constFun = constPlat) #modified
fullPlat %>% fit(data=JPNStMoMo,ages.fit = ages.fit,years.fit=years.fit)
虽然我的变化很小,但我没有发现我的错误。如果有人发现了什么,请提前感谢您!
解决方案
在我的代码中,max 必须更改为 pmax。此外,包的作者为完整模型提供了以下代码:
library(StMoMo)
f2 <- function(x, ages) mean(ages) - x
f3 <- function(x, ages) pmax(mean(ages)-x,0)
constPlat <- function(ax, bx, kt, b0x, gc, wxt, ages){
nYears <- dim(wxt)[2]
x <- ages
t <- 1:nYears
c <- (1 - tail(ages, 1)):(nYears - ages[1])
xbar <- mean(x)
#\sum g(c)=0, \sum cg(c)=0, \sum c^2g(c)=0
phiReg <- lm(gc ~ 1 + c + I(c^2), na.action = na.omit)
phi <- coef(phiReg)
gc <- gc - phi[1] - phi[2] * c - phi[3] * c^2
kt[2, ] <- kt[2, ] + 2 * phi[3] * t
kt[1, ] <- kt[1, ] + phi[2] * t + phi[3] * (t^2 - 2 * xbar * t)
ax <- ax + phi[1] - phi[2] * x + phi[3] * x^2
#\sum kt[i, ] = 0
ci <- rowMeans(kt, na.rm = TRUE)
ax <- ax + ci[1] + ci[2] * (xbar - x) + ci[3] * pmax(xbar - x, 0)
kt[1, ] <- kt[1, ] - ci[1]
kt[2, ] <- kt[2, ] - ci[2]
kt[3, ] <- kt[3, ] - ci[3]
list(ax = ax, bx = bx, kt = kt, b0x = b0x, gc = gc)
}
PLAT <- StMoMo(link = "log", staticAgeFun = TRUE,
periodAgeFun = c("1", f2, f3), cohortAgeFun = "1",
constFun = constPlat)
ages.fit <- 0:100
wxt <- genWeightMat(ages = ages.fit, years = EWMaleData$years, clip = 3)
PLATfit <- fit(PLAT, data = EWMaleData, ages.fit = ages.fit, wxt = wxt)
plot(PLATfit, parametricbx = FALSE)
推荐阅读
- sql-server - 如何在 SQL Server 2005 中捕获转储事务
- azure - 逻辑应用程序:如何在特定时间段内处理来自服务总线队列的消息?
- angular - 在 for-if 语句 Angular 中进行单元测试
- r - 如何更好地可视化具有大范围值的条形图(R Plotly)?
- php - 带有 laravel 的子文件夹中的香草 php 文件 - 找不到页面错误
- reactjs - 子组件重新渲染中的道具值
- javascript - $$ 在 html 中工作正常,但在获取后设置 innerHtml 时却不行
- dom-events - vuejs 3.0:为什么组件事件处理程序会被递归调用一次?
- python - 超级能够访问父类的属性
- azure - 如何查找数据包到达 Azure IOT 中心的时间