为什么我的计算年化变化率的函数返回R中的NA?

jtoj6r0c  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(111)

我有下面的框架,其中有一个长期运行的数据.我的目标是计算年化率的变化,我称之为'aroc'下面的功能.然而,在应用程序的功能,我得到的aroc值为NA的.我已经尝试过,并决定提交自己的帮助.如你所知,对于第一个时间点,aroc必须是0。在这种情况下,第一个时间点是1990年。代码确实按照要求粘贴了零,但没有计算aroc。我需要你的帮助。请!

df <- data.frame (year_id = c(1990, 1990, 1991, 1991, 1992, 1992, 1993, 1993, 1994, 1994, 1995,   1995, 1996, 1996, 1997, 1997, 1998, 1998, 1999, 1999, 2000, 2000, 2001, 2001, 2002, 2002, 2003, 2003, 2004, 2004, 2005, 2005, 2006, 2006, 2007, 2007, 2008, 2008, 2009, 2009, 2010, 2010, 2011, 2011, 2012, 2012, 2013, 2013, 2014, 2014, 2015, 2015, 2016, 2016, 2017, 2017, 2018, 2018, 2019, 2019, 2020, 2020, 2021, 2021, 1990, 1990, 1991, 1991, 1992, 1992, 1993, 1993, 1994, 1994, 1995, 1995, 1996, 1996, 1997, 1997, 1998, 1998, 1999, 1999, 2000, 2000, 2001, 2001, 2002, 2002),
              sex = c("male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male"  , "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male",   "female", "male"  , "female", "male"  , "female", "male"  , "female", "male",   "female", "male",   "female", "male",   "female"),
              loc  = c("C", "C", "C", "C",  "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",  "C", "C", "C", "C",  "C", "C",  "C", "C", "C",   "C","C", "C", "C", "C",  "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",   "C", "C", "C", "C", "C", "C", "C","C", "C", "C", "C",    "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",   "C",   "C", "D", "D", "D" ,"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D"),
                reg1 = c( "SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO","SOAEAO", "SOAEAO","SOAEAO","SOAEAO","SOAEAO"), reg2 = c("EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA","EA"), val = c(0.000043700, 0.000019600, 0.000049300, 0.000022900, 0.000058200 ,0.000027000, 0.000068200, 0.000031700, 0.000080100, 0.000037800, 0.000093800, 0.000044800,0.000107661, 0.000051300, 0.000123680, 0.000058200, 0.000143318, 0.000066600, 0.000167694, 0.000076700, 0.000198397, 0.000089100, 0.000235314, 0.000103256,0.000277176, 0.000118437, 0.000321671, 0.000133436, 0.000368679, 0.000148275, 0.000411686, 0.000160694, 0.000437294, 0.000165787, 0.000456603, 0.000169191, 0.000473239, 0.000172542, 0.000475592, 0.000169768 ,0.000480068, 0.000168260, 0.000480103, 0.000166006, 0.000470809, 0.000159872, 0.000473221, 0.000159643, 0.000483188, 0.000161755, 0.000486762, 0.000162135, 0.000485582, 0.000160320, 0.000479532, 0.000157872, 0.000473521, 0.000154745, 0.000465836, 0.000150780, 0.000440927, 0.000141668, 0.000414025, 0.000131982, 0.000008850, 0.000002930, 0.000011700, 0.000004080, 0.000016800, 0.000006140, 0.000024500, 0.000009350, 0.000036500, 0.000014500, 0.000052200, 0.000021300, 0.000069400, 0.000029000, 0.000089700, 0.000038200, 0.000114664, 0.000049800, 0.000144987, 0.000063900, 0.000181219, 0.000080900, 0.000222981, 0.000100655, 0.000269623, 0.000122767) )

字符串
下面是我创建的计算年化变化率的函数

#function

aroc <- function(x, t, mean = FALSE){
    t <- order(t)
    res <- numeric(length(x)-1)
    for(i in 1:(length(x)-1)){
      res[i] <- log((x[i+1]/x[i]))/(t[i+1]-t[i])
    }
    if(mean == TRUE){
    res <- mean(res)}
    return(aroc = res)
}


我在这里应用函数。

#calculations
df %>%
  select(year_id,loc,sex,val, reg1, reg2) %>%
  group_by(year_id,loc,sex) %>%
  mutate(
    aroc_pop = ifelse(
      as.numeric(year_id != 1990),
      aroc(val, as.numeric(year_id)),
      0
    )
  ) %>% 
  data.frame()


下面是新列'aroc_pop'上NA的输出。如何在新列中获得正确的值?

#output
year_id loc    sex         val   reg1 reg2 aroc_pop1     1990   C   male 0.000043700 SOAEAO   EA        02     1990   C female 0.000019600 SOAEAO   EA        03     1991   C   male 0.000049300 SOAEAO   EA       NA4     1991   C female 0.000022900 SOAEAO   EA       NA5     1992   C   male 0.000058200 SOAEAO   EA       NA6     1992   C female 0.000027000 SOAEAO   EA       NA7     1993   C   male 0.000068200 SOAEAO   EA       NA8     1993   C female 0.000031700 SOAEAO   EA       NA9     1994   C   male 0.000080100 SOAEAO   EA       NA10    1994   C female 0.000037800 SOAEAO   EA       NA11    1995   C   male 0.000093800 SOAEAO   EA       NA12    1995   C female 0.000044800 SOAEAO   EA       NA13    1996   C   male 0.000107661 SOAEAO   EA       NA14    1996   C female 0.000051300 SOAEAO   EA       NA15    1997   C   male 0.000123680 SOAEAO   EA       NA16    1997   C female 0.000058200 SOAEAO   EA       NA17    1998   C   male 0.000143318 SOAEAO   EA       NA18    1998   C female 0.000066600 SOAEAO   EA       NA19    1999   C   male 0.000167694 SOAEAO   EA       NA20    1999   C female 0.000076700 SOAEAO   EA       NA21    2000   C   male 0.000198397 SOAEAO   EA       NA22    2000   C female 0.000089100 SOAEAO   EA       NA23    2001   C   male 0.000235314 SOAEAO   EA       NA24    2001   C female 0.000103256 SOAEAO   EA       NA25    2002   C   male 0.000277176 SOAEAO   EA       NA26    2002   C female 0.000118437 SOAEAO   EA       NA27    2003   C   male 0.000321671 SOAEAO   EA       NA28    2003   C female 0.000133436 SOAEAO   EA       NA29    2004   C   male 0.000368679 SOAEAO   EA       NA30    2004   C female 0.000148275 SOAEAO   EA       NA31    2005   C   male 0.000411686 SOAEAO   EA       NA32    2005   C female 0.000160694 SOAEAO   EA       NA33    2006   C   male 0.000437294 SOAEAO   EA       NA34    2006   C female 0.000165787 SOAEAO   EA       NA35    2007   C   male 0.000456603 SOAEAO   EA       NA36    2007   C female 0.000169191 SOAEAO   EA       NA37    2008   C   male 0.000473239 SOAEAO   EA       NA38    2008   C female 0.000172542 SOAEAO   EA       NA39    2009   C   male 0.000475592 SOAEAO   EA       NA40    2009   C female 0.000169768 SOAEAO   EA       NA41    2010   C   male 0.000480068 SOAEAO   EA       NA42    2010   C female 0.000168260 SOAEAO   EA       NA43    2011   C   male 0.000480103 SOAEAO   EA       NA44    2011   C female 0.000166006 SOAEAO   EA       NA45    2012   C   male 0.000470809 SOAEAO   EA       NA46    2012   C female 0.000159872 SOAEAO   EA       NA47    2013   C   male 0.000473221 SOAEAO   EA       NA48    2013   C female 0.000159643 SOAEAO   EA       NA49    2014   C   male 0.000483188 SOAEAO   EA       NA50    2014   C female 0.000161755 SOAEAO   EA       NA51    2015   C   male 0.000486762 SOAEAO   EA       NA52    2015   C female 0.000162135 SOAEAO   EA       NA53    2016   C   male 0.000485582 SOAEAO   EA       NA54    2016   C female 0.000160320 SOAEAO   EA       NA55    2017   C   male 0.000479532 SOAEAO   EA       NA56    2017   C female 0.000157872 SOAEAO   EA       NA57    2018   C   male 0.000473521 SOAEAO   EA       NA58    2018   C female 0.000154745 SOAEAO   EA       NA59    2019   C   male 0.000465836 SOAEAO   EA       NA60    2019   C female 0.000150780 SOAEAO   EA       NA61    2020   C   male 0.000440927 SOAEAO   EA       NA62    2020   C female 0.000141668 SOAEAO   EA       NA63    2021   C   male 0.000414025 SOAEAO   EA       NA64    2021   C female 0.000131982 SOAEAO   EA       NA65    1990   D   male 0.000008850 SOAEAO   EA        066    1990   D female 0.000002930 SOAEAO   EA        067    1991   D   male 0.000011700 SOAEAO   EA       NA68    1991   D female 0.000004080 SOAEAO   EA       NA69    1992   D   male 0.000016800 SOAEAO   EA       NA70    1992   D female 0.000006140 SOAEAO   EA       NA71    1993   D   male 0.000024500 SOAEAO   EA       NA72    1993   D female 0.000009350 SOAEAO   EA       NA73    1994   D   male 0.000036500 SOAEAO   EA       NA74    1994   D female 0.000014500 SOAEAO   EA       NA75    1995   D   male 0.000052200 SOAEAO   EA       NA76    1995   D female 0.000021300 SOAEAO   EA       NA77    1996   D   male 0.000069400 SOAEAO   EA       NA78    1996   D female 0.000029000 SOAEAO   EA       NA79    1997   D   male 0.000089700 SOAEAO   EA       NA80    1997   D female 0.000038200 SOAEAO   EA       NA81    1998   D   male 0.000114664 SOAEAO   EA       NA82    1998   D female 0.000049800 SOAEAO   EA       NA83    1999   D   male 0.000144987 SOAEAO   EA       NA84    1999   D female 0.000063900 SOAEAO   EA       NA85    2000   D   male 0.000181219 SOAEAO   EA       NA86    2000   D female 0.000080900 SOAEAO   EA       NA87    2001   D   male 0.000222981 SOAEAO   EA       NA88    2001   D female 0.000100655 SOAEAO   EA       NA89    2002   D   male 0.000269623 SOAEAO   EA       NA90    2002   D female 0.000122767 SOAEAO   EA       NA


如上所述,我编写了aroc函数,并希望它能计算aroc_pop值。此外,在将aroc函数应用于女性人口时,如下所示:

aroc(df[df$sex == 'female', ]$val,as.numeric(df[df$sex == 'female', ]$year_id))%>% data.frame()


在这种情况下,我得到了女性的aroc_pop的正确值如下:
我可以将“female”改为“male”,但仍然可以计算这些值。

nnsrf1az

nnsrf1az1#

格式使得这很难理解,但我很确定这就是答案:你的group_by(year_id, loc, sex)导致每组n=1个观测值。如果你将其导入你的aroc函数,该函数被编写为期望向量的长度至少为2,则会发生以下情况(例如数据集中的第一个观测值):

x <- 4.37e-05
x[2]
> NA

字符串
你要么删除分组变量,要么在组中添加更多的记录。你没有解释这个函数应该做什么,但是基于它在时间上的工作,我认为你不应该按year_id分组。

相关问题