在我之前的问题中here https://stackoverflow.com/questions/76621713/combine-2-df-with-a-for-loop-to-make-projections?noredirect=1#comment135092935_76621713,我没有考虑使用之前的计算值来生成所需的增量增长率(H)预测。两位贡献者纠正了我之前的意图。我用了:
mutate(cross_join(df1[-3], df2), across(D:F)*H, H = NULL)
但是,我现在想使用 across(D*F) 中的前一个值来估计新值乘以 H。像这样的事情:
df1:
A B year D E F
abc ab 2020 0 1 2
def cd 2020 3 4 0
ghi ef 2020 0 5 6
jkl gh 2020 7 8 0
mno ij 2020 0 9 10
df2:
year H
2021 1.1
2022 1.2
2023 1.3
2024 1.4
2025 1.5
df3 # for abc ab
A B year D E F H
abc ab 2020 0 1.00 2.00 1
abc ab 2021 0 1.10 2.20 1.1
abc ab 2022 0 1.32 2.64 1.2
abc ab 2023 0 1.72 3.43 1.3
abc ab 2024 0 2.40 4.80 1.4
abc ab 2025 0 3.60 7.21 1.5
可以看出,这些值现在根据 H 增长率而增加。我认为公式是这样的
Xn(1+Hm)
Where
m = n+1
Xn = across(D:F)
Hm = H
df4 # for def cd
A B year D E F H
def cd 2020 3 4 0 1
def cd 2021 3.3 4.4 0 1.1
def cd 2022 4.0 5.3 0 1.2
def cd 2023 5.1 6.9 0 1.3
def cd 2024 7.2 9.6 0 1.4
def cd 2025 10.8 14.4 0 1.5
我尝试过做这样的事情
mutate(cross_join(df1[-3], df2), across(D:F)[-1]*H, H = NULL)
I also tried the
lag and lead functions combined with across
但也没有奏效。
任何帮助都是感激不尽。
编辑:新尝试
Try 1:
在 df2 中,我添加了一个符合我预期的示例。
给出了 0.1(例如,来自 df1),因此它是示例列的第一个观察值。然后第二个是0.1 * (H+1),依此类推;值以交换方式与前一个值进行估计。
Time H example
2010 0.000 0.1000000
2011 0.063 0.1062585
2012 0.049 0.1114821
2013 0.061 0.1182550
2014 0.057 0.1250279
2015 0.028 0.1285086
2016 0.060 0.1361788
2017 0.058 0.1441024
2018 -0.049 0.1370985
2019 0.058 0.1449823
2020 -0.149 0.1233892
2021 0.159 0.1430246
2022 0.076 0.1538541
2023 0.168 0.1796427
2024 0.144 0.2054314
2025 0.126 0.2312200
2026 0.112 0.2570086
2027 0.100 0.2827973
2028 0.091 0.3085859
2029 0.084 0.3343745
2030 0.077 0.3601632
I tried this
df2_1 <- df2 %>% mutate(example2 = cumsum(0.1 * (H+1)))
head(df2_1)
Time H example example2
2010 0.000 0.1000000 0.1000
2011 0.063 0.1062585 0.2063
2012 0.049 0.1114821 0.3112
2013 0.061 0.1182550 0.4173
2014 0.057 0.1250279 0.5230
2015 0.028 0.1285086 0.6258
Try 2:
df2_1 <- df2 %>% mutate(example2 = accumulate(H, ~ 0.1 * ( .x +1)))
head(df2_1)
Time H example example2
2010 0.000 0.1000000 0.00000
2011 0.063 0.1062585 0.10000
2012 0.049 0.1114821 0.11000
2013 0.061 0.1182550 0.11100
2014 0.057 0.1250279 0.11110
2015 0.028 0.1285086 0.11111
还是不行。
一旦我完成这个示例,我将尝试插入 cross_join 和 across mutate 函数。