I have been trying to run a savitzky-golar filter on a rasterstack in R to no avail. a sample is below:
library (raster)
library (zoo)
library (signal)
r <- raster(ncol=10, nrow=10)
r[]=1:ncell(r)
S <- stack(r,r,r,r,r,r,r,r,r,r,r,r)
##function for filter
fun=function(x) { m = sgolayfilt(x,na.spline=T);m}
s1<-calc(S, fun)
##This was an alternative function I used:
fun <- function(x) {
v=as.vector(x)
z=substituteNA(v, type="mean")
s1.ts2 = ts(z, start=c(2004,1), end=c(2004,12), frequency=12)
x=sgolayfilt(s1.ts2)}
The error below is what I get:
Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
cannot use this function
What could I be missing from this function?
First check your functions with some example data.
Example data
d <- S[1]
d
##layer.1 layer.2 layer.3 layer.4 layer.5 layer.6 layer.7 layer.8 layer.9 layer.10 layer.11 layer.12
##[1,] 1 1 1 1 1 1 1 1 1 1 1 1
Now test
fun=function(x) { sgolayfilt(x, na.spline=T) }
fun(d)
##Error in sgolayfilt(x, na.spline = T) : unused argument (na.spline = T)
And fix
fun=function(x) { sgolayfilt(x) }
fun(d)
## [1] 1 1 1 1 1 1 1 1 1 1 1 1
Next one
fun2 <- function(x) {
v=as.vector(x)
z=substituteNA(v, type="mean")
s1.ts2 = ts(z, start=c(2004,1), end=c(2004,12), frequency=12)
sgolayfilt(s1.ts2)
# note that I removed the assignment to x
}
fun2(S)
## Error in fun2(S) : could not find function "substituteNA"
I do not know what you want to do here, so I cannot fix it.