Reputation: 174
I want to create the following equation:
So far, I have added a data frame:
df=data.frame(x0=c(0.5, rep(NA, 100)), rep(NA,100)), n=0:100)
But I don't know how to apply a function in the followings x0s that take the lagged value of the column. The idea of N is the times that I want to predict.
Upvotes: 1
Views: 45
Reputation: 3184
Since it is a recursive function you can use purrr::accumulate
library(purrr)
# Example values
r <- 0.1
k1 <- 0.2
k2 <- 3
q <- 5
Et <- 10
fn <- function(x, n) {
## n is discarded
((r *x/k1 - 1)*(1- x/k2) -q * Et) * x
}
accumulate(1:50, fn, .init = 0.5)
#>
#> [1] 0.50000 -50.33333 -100.00000 -150.00000 -201.33333
#> [6] -255.00000 -312.00000 -373.33333 -440.00000 -513.00000
#> [11] -593.33333 -682.00000 -780.00000 -888.33333 -1008.00000
#> [16] -1140.00000 -1285.33333 -1445.00000 -1620.00000 -1811.33333
#> [21] -2020.00000 -2247.00000 -2493.33333 -2760.00000 -3048.00000
#> [26] -3358.33333 -3692.00000 -4050.00000 -4433.33333 -4843.00000
#> [31] -5280.00000 -5745.33333 -6240.00000 -6765.00000 -7321.33333
#> [36] -7910.00000 -8532.00000 -9188.33333 -9880.00000 -10608.00000
#> [41] -11373.33333 -12177.00000 -13020.00000 -13903.33333 -14828.00000
#> [46] -15795.00000 -16805.33333 -17860.00000 -18960.00000 -20106.33333
#> [51] -21300.00000
Upvotes: 1