0

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Gianca
  • 109
  • 10
  • 1
    Please describe in detail what "some troubles" are. Are you getting an error? You haven't provided any input to make the problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we don't know what the problem is. – MrFlick Jan 12 '15 at 17:06

1 Answers1

1

You need to study the lm function. For example to get fitted (predicted) values, you can do:

y = runif(10)
x = 1:10
m = lm(y~x)
predict(m)

but not

summary(m)$fitted.values
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63