所以我有一个给定的函数,我的目标是在一个数据集上运行,但它似乎不起作用,有一个"错误消息"我完全搞不清楚。我将描述数据集,因为它是扭曲的,然后编写我的函数。数据集的名称为:cohort_1x1:
Year Age Male Female
1940 18 0.001234 0.003028
1940 19 0.005278 0.002098
.... .. ........ ........ #up to age 100
1940 100 0.004678 0.006548
1941 18 0.004567 0.002753 # start new year, with age 18
1941 19 0.005633 0.001983
.... .. ........ ........
1979 100 0.003456 0.00256
该数据集包含从1940年至1979年的每个年龄组18-100的男性和女性的死亡率。进一步;我写的函数是:
gompakmean=function(theta,y_0,h){
# Returns the expected remaining number of years at age "l_0" under
# the Gomperz-Makeham model with parameter "theta" (vector).
# "h" is the time increment for the numerical integration.
l_0=y_0/h
l_e=110/h
ll=(l_0+1):l_e
hh=(theta[2]/theta[3])*(exp(theta[3]*h)-1)
p=exp(-theta[1]*h-hh*exp(theta[3]*ll*h))
P=cumprod(p)
(0.5+sum(P))*h
}
此功能返回每个年/年组的预期年数,并且应分别为男性和女性完成。 但是,如果我尝试输入如下所示的输入,我会收到以下错误消息:
Input:
s=-c(8,9,2)
theta=exp(s)
y_0 = cohort_1x1$Male
h = 1
gompakmean(theta,y_0,h)
导致出现此错误消息:
[1] 46.13826
Warning message:
In (l_0 + 1):l_e :
numerical expression has 3320 elements: only the first used
所以得到第一年(年龄?)的输出是:[1] 46.13826。 但是函数似乎停止了,因此错误消息。我的数据集有多少原因?也许在1940年以后,它必须有1941年?但那只会给我每年18年的产量吗? 因为我的目标是计算每年每个年龄组的预期年数,即:计算所有年份中每个年龄组的预期年数。
感谢所有答案!:)