suppressPackageStartupMessages(library(dplyr))
library(gapminder)
library(magrittr)
library(ggplot2)
library(broom)
fits <- gapminder %>%
group_by(country) %>%
do(fit = lm(lifeExp ~ year +pop, .))
result<-fits %>%
augment(fit)
result
I want to fit a model that has different data for every row of a data frame. The code here does everything that I want (grouping by country) except the fact that the data for a row has to be sliced with only years before the year in the row. F.e. for row 6 the model should only use years smaller then 1977, that is the year in the year field of that row, (so here the first 5 rows) and grouped by Afghanistan (but that is already ok in the code), row 7 should use only use years smaller then 1982,….. So there will be different data for every model of every row.
country continent year lifeExp pop gdpPercap
1 Afghanistan Asia 1952 28.801 8425333 779.4453
2 Afghanistan Asia 1957 30.332 9240934 820.8530
3 Afghanistan Asia 1962 31.997 10267083 853.1007
4 Afghanistan Asia 1967 34.020 11537966 836.1971
5 Afghanistan Asia 1972 36.088 13079460 739.9811
6 Afghanistan Asia 1977 38.438 14880372 786.1134
7 Afghanistan Asia 1982 39.854 12881816 978.0114
8 Afghanistan Asia 1987 40.822 13867957 852.3959
9 Afghanistan Asia 1992 41.674 16317921 649.3414
10 Afghanistan Asia 1997 41.763 22227415 635.3414
11 Afghanistan Asia 2002 42.129 25268405 726.7341
12 Afghanistan Asia 2007 43.828 31889923 974.5803