# NOT RUN {
if(interactive()){
data(Scorecard)
# I'd like to build a decaying function that remembers previous earnings but at a declining rate
# Let's only use nonmissing earnings
# And let's say we're only interested in four-year colleges in Colorado
# (mutate_cascade + tlag can be very slow so we're working with a smaller sample)
Scorecard <- Scorecard %>%
dplyr::filter(
!is.na(earnings_med),
pred_degree_awarded_ipeds == 3,
state_abbr == "CO"
) %>%
# And declare the panel structure
as_pibble(.i = unitid, .t = year)
Scorecard <- Scorecard %>%
# Almost all instances involve a variable being set to a function of a lag of itself
# we don't want to overwrite so let's make another
# Note that earnings_med is an integer -
# but we're about to make non-integer decay function, so call it a double!
dplyr::mutate(decay_earnings = as.double(earnings_med)) %>%
# Now we can cascade
mutate_cascade(
decay_earnings = decay_earnings +
.5 * tlag(decay_earnings, .quick = TRUE)
)
}
# }
Run the code above in your browser using DataLab