Spline interpolation with R

14,091

Why not using splinefun:

func = splinefun(x=x, y=population, method="fmm",  ties = mean)

Then you define the point to forecast you want:

func(seq(1973, 2014, 0.25))
Share:
14,091

Related videos on Youtube

Gilles Cosyn
Author by

Gilles Cosyn

Updated on October 21, 2022

Comments

  • Gilles Cosyn
    Gilles Cosyn over 1 year

    I want to perform a (cubic) spline interpolation for population data to "transform" yearly data into quarterly data. I know that there are a fair number of flaws doing so, but I need to do it.

    Here is an example of my code (using generic input data):

    #--------------spline interpolation
    
    x = c(1973:2014)
    population = seq(500000, 600000, length.out = 42)
    list = spline(x, population, n = 4*length(x), method = "fmm",
           xmin = min(x), xmax = max(x), ties = mean)
    
    x_spline = list$x
    pop_spline = list$y
    

    How can I define that the splines are calculated "quarterly", in other words at 1973.25, 1973.5, 1973.75, 1974 etc.? Sorry for not being an expert in statistics: What would be the best method to "transform" yearly data into quarterly data: "fmm", "natural", "periodic", "monoH.FC" or "hyman"? The assumption would be that the growth of population is evenly distributed over the year.

    Best regards and many thanks in advance!

  • Gilles Cosyn
    Gilles Cosyn over 9 years
    Very neat solution! Thanks a lot! :)
  • Colonel Beauvel
    Colonel Beauvel over 9 years
    No problem! Always better to have a function than a vector since you can evaluate the first on a different vector !
  • Gilles Cosyn
    Gilles Cosyn over 9 years
    Good to know! Sounds reasonable.