I would like to apply a linear extrapolation for one year within a pipe. What I would like to do is very similar to this simple example without grouping. But within a pipe and employing dplyr::group_by()
. There are some examples like this one, this one or this one. But I can't manage to get the desirable output.
Reproducible example:
test.frame <- data.frame(Country =
rep(c("Austria", "Brazil", "Canada"), each = 3, times = 3),
Entity = rep(c("CO2","CH4","N2O"), times = 9),
Year = rep(c(1990:1992), each = 9),
value = runif(27, 1,5))
test.frame2 <- data.frame(Country =
rep(c("Austria", "Brazil", "Canada"), each = 3),
Entity = rep(c("CO2","CH4","N2O"), times = 3),
Year = rep(c(1993), each = 3),
value = 0)
results_frame <- test.frame %>%
dplyr::bind_rows(test.frame2)
I have two grouping categories (Country and Entity), and I would like to use the values for years 1990 to 1992 to fill those for year 1993 using linear extrapolation. On the basis of this, I can estimate the linear model:
linear_model <- test.frame %>%
dplyr::group_by(Country, Entity) %>%
lm(value ~ Year, data=.)
results <- predict.lm(linear_model, test.frame2)
But then, results
doesn't show the desirable output. So following the solution proposed here I try the following:
results_frame <- test.frame %>%
dplyr::group_by(Country, Entity) %>%
do(lm( value ~ Year , data = test.frame)) %>%
predict.lm(linear_model, test.frame2) %>%
bind_rows(test.frame)
But it doesn't work, instead I get
Error: Results 1, 2, 3, 4, 5, ... must be data frames, not lm
Any help would be highly appreciated!