Compressing TIFF files in R and Windows

Compressing TIFF files is an easy way to save disk space when working with raster files or image outputs for research papers. But this is rarely the default option causing used diskspace to quickly inflate heavily. For instance this figure 7000 by 5000 pixel image as a TIFF uses 136MB.

While after lossless compression with the libtiff command tiffcp it is only 8MB. To install libtiff on windows download and run the latest setup from To compress a tiff file you need to run the following command (assuming that tiffcp is in your path). Make sure to specify a different output file name, otherwise the original will be overwritten with a broken and incomplete copy of the original.
tiffcp -c lzw uncompressed_input.tif compressed_output.tif

In R you can also write compressed raster (geo)tiff files by adding extra options to the writeRaster function from the raster package.


x <- raster("uncompressed_input.tif")
tifoptions <- c("COMPRESS=DEFLATE", "PREDICTOR=2", "ZLEVEL=6")
writeRaster(x, "compressed_output.tif",
            options = tifoptions, overwrite = FALSE)

Geographic null models for species distribution modeling: An implementation combining BIOMOD2 and dismo [Code Dump]

Warning this is more of a code dump instead of a blogpost but I'm still putting it out there hoping it's still useful for someone else as it has been sitting in my draft folder for far too long. Anyways the paper by Robert Hijmans is really worth reading if you're interested in the evaluation of species distribution models. If you have questions, don't hesitate to contact me.

Geographic null model from paper by Robert Hijsmans (link to paper).
package dismo (maintained by Robert J. Hijmans). and the package BIOMOD2.

Load libraries:


The null_model_evaluation function takes as input a BIOMOD2 model.

null_model_evaluation <- function(models) {
  ## flow
  ## 1) create geographic null model from the points used as training data
  ## 2) compare AUC of the model with AUC of the geographic null model (use same presence/absence data)
  ## extract test data from models
  data <- get_formal_data(models)
  calib.lines <- get(load(models@calib.lines@link))
  nbOfRuns <- (length(calib.lines) / length(data@data.species))
  calib.lines.mat <- as.matrix(calib.lines, nrow=length(data@data.species), ncol=nbOfRuns)

  evaluations <- get_evaluations(models)
  dims <- dimnames(evaluations)
  rocIndex <- which(dims[1][[1]] == "ROC")
  testingDataIndex <- which( dims[2][[1]] == "") <- data@coord[which(data@data.species == 1),] <- data@coord[,]
  result <- array(dim=c(nbOfRuns, 2, length(dims[3][[1]])), dimnames=list(dims[4][[1]],c("model", "geo_null_model"), dims[3][[1]]))
  ## calculate geographic null models
  for( methodIndex in 1:length(dims[3][[1]])){
    for (runIndex in 1:nbOfRuns){
      model.auc <- evaluations[rocIndex, testingDataIndex,methodIndex,runIndex,1]
      result[runIndex,1,methodIndex] <- model.auc
    <-[calib.lines[1:nrow(,runIndex],] <-[!calib.lines[1:nrow(,runIndex],]
      if (nrow( > 0 & !{ <-[calib.lines[nrow(,runIndex],] <-[!calib.lines[nrow(,runIndex],]
        gd <- geoDist(, lonlat=TRUE)
        #p <- predict(gd, env)
        #points(, pch=16, col="purple")
        #points(, pch=16, col="blue")
        #points(, pch=16, col="green")
        #points(, pch=16, col="red")
        gd.evaluation <- evaluate(gd,,
        result[runIndex,2,methodIndex] <- gd.evaluation@auc