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]]

