time series - R: Linear extrapolation between raster layers of different dates -


there thread dealing interpolation between raster layers of different years (2006,2008,2010,2012). tried linearly extrapolate 2020 approach suggested @ram narasimhan , approxextrap hmisc package:

library(raster) library(hmisc)  df <- data.frame("2006" = 1:9, "2008" = 3:11, "2010" = 5:13, "2012"=7:15)  #transpose since want time first col, , values columns new <- data.frame(t(df)) times <- seq(2006, 2012, by=2) new <- cbind(times, new)  # now, apply linear extrapolate each layer of raster approxextrap(new, xout=c(2006:2012), rule = 2) 

but instead of getting this:

#  times x1 x2 x3 x4 x5 x6 x7 x8 x9 #1  2006  1  2  3  4  5  6  7  8  9 #2  2007  2  3  4  5  6  7  8  9 10 #3  2008  3  4  5  6  7  8  9 10 11 #4  2009  4  5  6  7  8  9 10 11 12 #5  2010  5  6  7  8  9 10 11 12 13 #6  2011  6  7  8  9 10 11 12 13 14 #7  2012  7  8  9 10 11 12 13 14 15 #8  2013  8  9 10 11 12 13 14 15 16 #9  2014  9 10 11 12 13 14 15 16 17 #10 2015 10 11 12 13 14 15 16 17 18 #11 2016 11 12 13 14 15 16 17 18 19 #12 2017 12 13 14 15 16 17 18 19 20 #13 2018 13 14 15 16 17 18 19 20 21 #14 2019 14 15 16 17 18 19 20 21 22 #15 2020 15 16 17 18 19 20 21 22 23 

i this:

$x  [1] 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020  $y  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 

this quite confusing both approxtime , approxextrap based on approxfun.

i found way make work, although doesn't seem elegant way it. basic idea perform linear interpolation approxtime first, use lm fit linear model time-series , extrapolate using predict , final year of extrapolation. data gap between final year , end-year of first interpolation filled second linear interpolation using approxtime again.

note: first linear interpolation not necessary, although don't know if makes difference when use more sophisticated data.

library(raster) library(hmisc) library(simecol)   df <- data.frame("2006" = 1:9, "2008" = 3:11, "2010" = 5:13, "2012"=7:15)  #transpose since want time first col, , values columns new <- data.frame(t(df)) times <- seq(2006, 2012, by=2) new <- cbind(times, new)    # now, apply linear interpolate each layer of raster intp<-approxtime(new, 2006:2012, rule = 2)  #extract years data.frame tm<-intp[,1]  #define function linear model using lm lm.func<-function(i) {lm(i ~ tm)}  #define new data.frame without years intp intp.new<-intp[,-1]  #creates list of lm coefficients each column of intp.new lm.list<-apply(intp.new, margin=2, fun=lm.func)  #create data.frame of final year of extrapolation; keep name of tm data.frame new.pred<-data.frame(tm = 2020)  #make predictions final year each element of lm.list pred.points<-lapply(lm.frame, predict, new.pred)  #unlist predicted points fintime<-matrix(unlist(pred.points))  #add final year fintime matrix , transpond fintime.new<-t(rbind(2020,fintime))  #convert intp data.frame matrix intp.ma<-as.matrix(intp)  #append fintime.new intp.ma intp.wt<-as.data.frame(rbind(intp.ma,fintime.new))  #perform linear interpolation approxtime again approxtime(intp.wt, 2006:2020, rule = 2)   times x1 x2 x3 x4 x5 x6 x7 x8 x9 1   2006  1  2  3  4  5  6  7  8  9 2   2007  2  3  4  5  6  7  8  9 10 3   2008  3  4  5  6  7  8  9 10 11 4   2009  4  5  6  7  8  9 10 11 12 5   2010  5  6  7  8  9 10 11 12 13 6   2011  6  7  8  9 10 11 12 13 14 7   2012  7  8  9 10 11 12 13 14 15 8   2013  8  9 10 11 12 13 14 15 16 9   2014  9 10 11 12 13 14 15 16 17 10  2015 10 11 12 13 14 15 16 17 18 11  2016 11 12 13 14 15 16 17 18 19 12  2017 12 13 14 15 16 17 18 19 20 13  2018 13 14 15 16 17 18 19 20 21 14  2019 14 15 16 17 18 19 20 21 22 15  2020 15 16 17 18 19 20 21 22 23 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -