Nellicopter
Nellicopter

Reputation: 83

Interpolation/extrapolation by group in R

I have this type of database with data for variables x1 and x2 by country and year.

iso3   year  x1 x2
ATG    2010    NA    NA
ATG    2011    NA    NA
ATG    2012   144   277
ATG    2013    45    NA
ATG    2014    NA    NA
ATG    2015    NA    NA
ATG    2016    45    NA
ATG    2017    NA    NA
ATG    2018    NA   421
BEL    2010    NA   434
BEL    2011    NA   422
BEL    2012    NA   424
BEL    2013   282   429
BEL    2014    NA   435
BEL    2015    NA   NA
BEL    2016    NA   NA
BEL    2017   282   429
BEL    2018    NA   435

I would like to do linear interpolation between observed points and extrapolation assuming a flat trend (before first and after last observed point). I have tried the following...without success.

hwf_2000 <- uhc_hwf %>%
  group_by(iso3)%>%
  na.approx(uhc_hwf)

I get this error message:

Error in na.approx.default(., uhc_hwf) : 
  x and index must have the same length

Could you please help?

Thank you very much,

Upvotes: 0

Views: 792

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388962

We can use na.approx for multiple variables with na.rm = FALSE for interpolation and as @G. Grothendieck commented use rule = 2 in na.approx for extrapolation.

library(dplyr)
library(zoo)

df %>% 
  group_by(iso3)%>% 
  mutate_at(vars(x1, x2), list(inter = ~na.approx(., na.rm = FALSE), 
                               outer = ~na.approx(., rule = 2, na.rm = FALSE)))


#   iso3   year    x1    x2 x1_inter x2_inter x1_outer x2_outer
#   <fct> <int> <int> <int>    <dbl>    <dbl>    <dbl>    <dbl>
# 1 ATG    2010    NA    NA       NA       NA      144      277
# 2 ATG    2011    NA    NA       NA       NA      144      277
# 3 ATG    2012   144   277      144      277      144      277
# 4 ATG    2013    45    NA       45      301       45      301
# 5 ATG    2014    NA    NA       45      325       45      325
# 6 ATG    2015    NA    NA       45      349       45      349
# 7 ATG    2016    45    NA       45      373       45      373
# 8 ATG    2017    NA    NA       NA      397       45      397
# 9 ATG    2018    NA   421       NA      421       45      421
#10 BEL    2010    NA   434       NA      434      282      434
#11 BEL    2011    NA   422       NA      422      282      422
#12 BEL    2012    NA   424       NA      424      282      424
#13 BEL    2013   282   429      282      429      282      429
#14 BEL    2014    NA   435      282      435      282      435
#15 BEL    2015    NA    NA      282      433      282      433
#16 BEL    2016    NA    NA      282      431      282      431
#17 BEL    2017   282   429      282      429      282      429
#18 BEL    2018    NA   435       NA      435      282      435

Upvotes: 2

Related Questions