使用dplyrs组运算符的NPV功能

时间:2019-06-17 17:38:14

标签: r dplyr

我正在尝试group_byID并通过FinancialMath包执行NPV计算(或自己创建NPV)。

数据如下:

    ID         CF otherVar date
1   78   83739.63     0.00 1989
2  100   21421.77     0.00 1989
3  987   89581.94 46926.38 1989
4 1934 2921150.05  5597.58 1989
5   78   83739.63     0.00 1989
6  100   21421.77     0.00 1989

其中CF是现金流量。

我正在尝试

data %>%
  group_by(ID) %>%
  mutate(npv = NPV("date == 1989" then "date == 1992"...))

但是我不知道如何在NPV计算中表示数据。

i <- c(0.03, 0.03, 0.04, 0.01, 0.02)

library(FinancialMath)
#The NPV function takes the arguments:
NPV(cf0,cf,times,i,plot=FALSE)

任何帮助都会很棒。

数据:

data <- structure(list(ID = c(78, 100, 987, 1934, 78, 100, 987, 1934, 
78, 100, 987, 1934, 78, 100, 987, 1934, 78, 100, 987, 1934, 78, 
100, 987, 1934, 2938, 78, 100, 987, 1934, 2938, 78, 100, 987, 
1934, 2938, 78, 100, 987, 1934, 2938, 78, 100, 987, 1934, 2938, 
78, 100, 987, 1934, 2938, 78, 100, 987, 1934, 2938, 78, 100, 
987, 1934, 2938, 78, 100, 987, 1934, 2938, 78, 100, 987, 1934, 
2938, 78, 100, 987, 1934, 2938, 78, 100, 987, 1934, 2938, 78, 
100, 987, 1934, 2938, 78, 100, 987, 1934, 2938, 78, 100, 987, 
1934, 2938, 78, 100, 987, 1934, 2938, 78, 100, 987, 1934, 2938, 
78, 100, 987, 1934, 2938, 78, 100, 987, 1934, 2938, 78, 100, 
987, 1934, 2938), CF = c(83739.6348934691, 21421.7670657712, 
89581.9350023158, 2921150.05442334, 83739.6348934691, 21421.7670657712, 
89581.9350023158, 2921150.05442334, 83739.6348934691, 21421.7670657712, 
89581.9350023158, 2921150.05442334, 83739.6348934691, 21421.7670657712, 
89581.9350023158, 2921150.05442334, 83739.6348934691, 21421.7670657712, 
89581.9350023158, 2921150.05442334, 36094.1785878537, 49844.341859417, 
103126.224536725, 1481580.09251095, 17187.7040894541, 36094.1785878537, 
49844.341859417, 103126.224536725, 1481580.09251095, 17187.7040894541, 
36094.1785878537, 49844.341859417, 103126.224536725, 1481580.09251095, 
17187.7040894541, 36094.1785878537, 49844.341859417, 103126.224536725, 
1481580.09251095, 17187.7040894541, 36094.1785878537, 49844.341859417, 
103126.224536725, 1481580.09251095, 17187.7040894541, 11065.7431065623, 
97827.5839855509, 259804.403371463, 19244.7706201084, 83394.0060204696, 
5613.05809753161, 97827.5839855509, 259804.403371463, 19244.7706201084, 
83394.0060204696, 9943.13148705599, 97827.5839855509, 259804.403371463, 
19244.7706201084, 83394.0060204696, 13952.4586995786, 97827.5839855509, 
259804.403371463, 19244.7706201084, 83394.0060204696, 7697.90824804335, 
97827.5839855509, 259804.403371463, 19244.7706201084, 83394.0060204696, 
483026.595362645, 16449.8221330932, 672.947269081084, 2646925.92505227, 
249738.208747869, 876326.888181145, 31404.2058904506, 672.947269081084, 
6011662.27045769, 273665.222759641, 912217.409198803, 28413.3291389791, 
672.947269081084, 3215192.50783185, 320023.812407449, 856886.189296581, 
32899.6442661863, 672.947269081084, 7058469.13347271, 158516.467827989, 
480035.718611173, 14954.3837573574, 672.947269081084, 5413486.92016339, 
608643.418924447, 58523.7787123908, 156063.409899709, 41802.6990802791, 
164423.949715765, 94752.7845819661, 57130.3554097148, 156063.409899709, 
41802.6990802791, 164423.949715765, 94752.7845819661, 58523.7787123908, 
157456.833202385, 41802.6990802791, 164423.949715765, 94752.7845819661, 
57130.3554097148, 157456.833202385, 41802.6990802791, 164423.949715765, 
94752.7845819661, 58523.7787123908, 157456.833202385, 41802.6990802791, 
164423.949715765, 94752.7845819661), otherVar = c(0, 0, 46926.3808521831, 
5597.58022093635, 0, 0, 46926.3808521831, 5597.58022093635, 0, 
0, 46926.3808521831, 5597.58022093635, 0, 0, 46926.3808521831, 
5597.58022093635, 0, 0, 46926.3808521831, 5597.58022093635, 3773.40425531915, 
58026.5721040189, 148420.567375887, 922387.706855792, 8385.34278959811, 
3773.40425531915, 58026.5721040189, 148420.567375887, 922387.706855792, 
8385.34278959811, 3773.40425531915, 58026.5721040189, 148420.567375887, 
922387.706855792, 8385.34278959811, 3773.40425531915, 58026.5721040189, 
148420.567375887, 922387.706855792, 8385.34278959811, 3773.40425531915, 
58026.5721040189, 148420.567375887, 922387.706855792, 8385.34278959811, 
0, 111186.313465784, 186197.924944812, 50112.1412803532, 96935.6732891832, 
0, 111186.313465784, 185054.741721854, 50112.1412803532, 96935.6732891832, 
0, 111186.313465784, 186260.565121413, 50112.1412803532, 96935.6732891832, 
0, 111186.313465784, 185571.523178808, 50112.1412803532, 96935.6732891832, 
0, 111186.313465784, 185133.041942605, 50112.1412803532, 96935.6732891832, 
900029.118136439, 0, 0, 462655.541805324, 0, 568051.164725458, 
0, 0, 496059.163477537, 0, 522311.980033278, 0, 0, 399829.082986689, 
0, 752483.361064892, 0, 0, 431831.757695507, 0, 522311.980033278, 
0, 0, 447496.690723794, 0, 514.843391902215, 350906.417112299, 
84000.7639419404, 208647.058823529, 0, 514.843391902215, 350906.417112299, 
84000.7639419404, 252002.291825821, 0, 514.843391902215, 350906.417112299, 
84000.7639419404, 173420.932009167, 0, 514.843391902215, 350906.417112299, 
84000.7639419404, 136839.954163484, 0, 514.843391902215, 350906.417112299, 
84000.7639419404, 283163.865546218, 0), date = c(1989, 1989, 
1989, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 
1989, 1989, 1989, 1989, 1989, 1989, 1989, 1992, 1992, 1992, 1992, 
1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 
1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1995, 
1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 
1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 
1995, 1995, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 
1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 
1998, 1998, 1998, 1998, 1998, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001)), row.names = c(NA, 
-120L), .internal.selfref = <pointer: 0x0000000002621ef0>, class = "data.frame")

1 个答案:

答案 0 :(得分:3)

这应该是可行的,方法是计算每一行的折扣因子,然后使用该因子对每个现金流量进行折扣。在这种情况下,我将折价回第一年(即1989年的净现值),但是我不清楚这是否是您想要的。如果您想用当前货币找到现值,则假设rate为常数回报,则用您选择的参考年替换min(date)

rate = 0.05
data %>%
  mutate(period = date - min(date),
         discount = 1 / (1+rate) ^ period,
         CF_disc = CF * discount) %>%
  group_by(ID) %>%
  summarize(NPV = sum(CF_disc))

# A tibble: 5 x 2
     ID       NPV
  <dbl>     <dbl>
1    78  3098073.
2   100  1204245.
3   987  1981237.
4  1934 37228043.
5  2938  1687395.