I am calculating a regression using 10 raster files. I am able to obtain a raster output for the slope and Rsquared but I have some troubles for producing the residuals and the fitted values.
Here is my script:
##########################################
require(raster)
require(rgdal)
#Create list of files
rasters <- list.files(pattern='\\.tif$')
time <- 1:nlayers(rasters)
fun <- function(x) { if (is.na(x[1])){ NA } else { lm(x ~ time)$coefficients[2] }}
slope <- calc(rasters, fun)
time <- 1:nlayers(rasters)
fun <- function(x) { if (is.na(x[1])){ NA } else { lm(x ~ time)$;summary(m)$r.squared }}
Rsquared <- calc(rasters, fun)
time <- 1:nlayers(rasters)
fun <- function(x) { if (is.na(x[1])){ NA } else { lm(x ~ time)$;summary(m)$residuals }}
residuals <- calc(rasters, fun)
time <- 1:nlayers(rasters)
fun <- function(x) { if (is.na(x[1])){ NA } else { lm(x ~ time)$;summary(m)$fitted.values }}
fitted.values <- calc(rasters, fun)
##############################
Please, can anybody help me?
Thanks a lot
gianca