The problem is that dplyr does this as a set of vector operations, rather than evaluating the term one at a time. Here 1.1*lag(pop) interpreted as "calculates lagging values ββfor all pops and then multiplies them by 1.1". Since you set pop=50 lagged the values ββfor all steps were 50.
dplyr has some helper functions for sequential evaluation; standard function cumsum , cumprod etc. work, and several new ones (see ?cummean ) work within dplyr . In your example, you can model the model with:
tdf <- data.frame(time=1:5, pop=50, growth_rate = c(1, rep(1.1,times=4)) %>% mutate(pop = pop*cumprod(growth_rate)) time pop growth_rate 1 50.000 1.0 2 55.000 1.1 3 60.500 1.1 4 66.550 1.1 5 73.205 1.1
Notice that I added the growth rate as a column here, and I set the first growth rate to 1. You can also specify it as follows:
tdf <- data.frame(time=1:5, pop=50, growth_rate = 1.1) %>% mutate(pop = pop*cumprod(lead(growth_rate,default=1))
This makes it clear that the growth rate column refers to the growth rate at the current time step from the previous one.
There are restrictions on how many different simulations you can do this way, but it should be possible to build many discrete environmental models using some combination of cumulative functions and parameters listed in the columns.
source share