Trying to interpolate rows for data over time

Hey guys,
I'm using example GPD over time data from gapminder:

I’m trying to interpolate years in the data so that each country contains a row for every year instead of every 5 years. I need the gdp, pop and life expectancy columns to be interpolated in the new rows added. The rank column is calculated by gdp values for each year, but the rank and height columns aren’t really relevant here.

Any help is greatly appreciated! Apologies for the newbie question, just want to make sure I’m doing this correctly.


plotData <- gapminder %>%
  filter(continent == "Americas") %>%
  group_by(year) %>%
  mutate(rank = min_rank(-gdpPercap) * 1) %>%
  mutate(height = gdpPercap / max(gdpPercap)) %>%

Here is one way to make a data frame with life expectancy for every year using a spline fit. You can extend this to the pop and GDP variables by making a function to fit each of them and adding another column in the mutate where I calculate PredictedLifeExp.

#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union
DF <- gapminder %>% filter(continent == "Americas")
SplFitLife <- function(x){
  FIT <- lm(lifeExp ~ bs(year, knots = seq(1957, 2002, by = 5), degree = 1), data = x)
  Pred <- predict(FIT, newdata = data.frame(year =seq(1952, 2007, by = 1)))
  data.frame(year = seq(1952, 2007), PredLifeExp = Pred)

DF2 <- DF %>% group_by(country) %>% nest() %>% 
  mutate(PredictedLife = map(data,SplFitLife)) %>% 
  select(country, PredictedLife) %>% 
  unnest(cols = PredictedLife)

#show predicted line and actual points
ggplot() + geom_line(aes(year, PredLifeExp), data = DF2) + 
  geom_point(aes(year, lifeExp), data = DF) + facet_wrap(~country) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5))

Created on 2020-01-04 by the reprex package (v0.3.0)


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.