[R-sig-Geo] Fit a sine curve to raster stack
Verbesselt, Jan
jan.verbesselt at wur.nl
Mon Nov 4 16:24:52 CET 2013
Dear Sofea,
You could try something like this:
fun3 <- function(x){
time <- 1:length(x)
xc <-cos(2*pi*t/365)
xs <-sin(2*pi*t/365)
fit <- lm(x~xc+xs)
return(fit$coefficients[1])
}
s3 <- calc(s, fun = fun3)
But check for NA's within your time series as these might trigger
errors/warnings. Maybe also check na.action options like na.approx(). Can
you provide a reproducible example in that case?
Best,
Jan Verbesselt
Wageningen University
The Netherlands
On 31/10/13 21:39, "Sofea Wright" <sofeawright at gmail.com> wrote:
>Dear list,
>
>I am trying to fit a sine curve to my data. When dealing with non-raster
>data, it's quite straight forward:
>
># Fit sine curve to describe the data
>temperature<-maxtemp$temperature
>time<-1:n
>xc<-cos(2*pi*time/365)
>xs<-sin(2*pi*time/365)
>fit<-lm(temperature~xc+xs)
>summary(fit)
>
>I am not sure how to do that with rasterstack as I keep on getting error
>message.
> > s
>class : RasterStack
>dimensions : 1000, 1621, 1621000, 365 (nrow, ncol, ncell, nlayers)
>resolution : 0.04166667, 0.04166667 (x, y)
>extent : 96.5, 165.5, -16.5, 26.5 (xmin, xmax, ymin, ymax)
>coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
>+towgs84=0,0,0
>
>s <- stack(tif)
>time <- 1:nlayers(s)
>fun1 <- function(x){
> xc<-cos(2*pi*time/365)
> xs<-sin(2*pi*time/365)
> }
>
>fun2 <- function (x) {
> lm(s~xc+xs)
> }
>Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
> cannot use this function
>
>s1 <- calc(s, fun = fun1)
>s2 <- calc(s, fun = fun2)
>
>Anybody?
>
>Thank you
>Sofea
>
> [[alternative HTML version deleted]]
>
>_______________________________________________
>R-sig-Geo mailing list
>R-sig-Geo at r-project.org
>https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list