# How to calculate 90% confidence interval in a raster stack

I have a stack of grid files (23) in ASC format from a MODIS sensor and I need to calculate a 90% confidence interval, mean, min, etc, for each one in R, how can I do that? specially the interval.

``````## set working directory
FilesPath <- "C:/Users/Grettel/ "
setwd(FilesPath)
FileList <- list.files()

# Loop for all grids by julian days and create stats
for (i in  unique(substring(FileList,40,42))){

print(as.character(paste(i,sep="")))

JulianDay<-as.character(paste(i,sep=""))

# Create output directory
WriteFolder<-paste(FilesPath,"StatsGrids",sep="")
dir.create(paste(WriteFolder))

# list files by pattern of julian day

# create raster stack

# mean files
meanRaster<-mean(FilesStack)
# min files
minRaster<-min(FilesStack)
# max files
maxRaster<-max(FilesStack)
# sd files
sdRaster <-calc(FilesStack, fun = sd)

# intervalo confianza
#CIRaster <- ci(meanRaster, conf.level = 0.9)

# write new asci into output folder
writeRaster(meanRaster,
filename=paste(WriteFolder,
"/Modis_2000_2014_mean_JulDay_",
JulianDay, sep=""),
overwrite=T, "ascii")
}
``````
• Since these seem to be a time series of rasters, you ought to consider assessing the possibility of a significant serial correlation coefficient first, because that would (strongly) affect the confidence intervals. – whuber Aug 28 '14 at 14:38
• thanks a lot Whuber and yes these are a time series or raster from 2000 to 2014 (EVI) and could you guide me with that, I'm new in R.. I would appreciate it!! – Greta Pura Vida Aug 28 '14 at 14:49
• As usual, @whuber provides very solid advice. Confidence intervals on a timeseres will not mean much at all. You can write a function that coerces the raster stack to a ts object and then use the acf function to calculate the serial correlation. Keep in mind that acf returns a autocorrelation coefficient for each lag. Because of this you will have to write output to a raster stack. It is quite important to look at serial autocorrelation across lags and not as a single correlation coefficient. Let me know if you need a "nudge" to get started. – Jeffrey Evans Nov 9 '14 at 19:09