Making a working Maxent bias file

1,946 views
Skip to first unread message

Heather Robinson

unread,
Aug 4, 2015, 1:39:27 AM8/4/15
to Maxent
I wanted to share how I managed to get produce a biasfile in R which worked with Maxent, as it took me weeks, and a lot of trawling through R help pages to do, using purely R as I do not have an ArcGis license. I read several papers explaining how important it was to use a biasfile, but none of them helped to write one, so hopefully someone will benefit from me putting all of this information together.

Firstly, I wrote a simple table of pseudoabsence data and saved it in csv format. I used locations where samples were taken from species 2, a closely related species to my species of interest (species1). At these sites over 100 samples were taken using methods that should also have successfully recovered species1 if it had been present. In each case species2 was present, but species1 was absent. I gave each a score reflecting how many samples were obtained at that site, and divided this by 10:

LONGITUDE LATITUDE SAMPLINGWEIGHT
-0.6178 51.42428 17
-0.6377 51.40813 21
-71.2431 46.81268 28
-79.8873 43.24028 12
-79.9193 43.26191 12
-79.9207 43.26286 36
37.4639 55.5149 99
38.3456 54.72669 99
-68.7205 47.89317 10

Next I used the following code adapted from Robert Hijman's suggestions on one of the R help boards. In this case I am making a bias file for use with WorldClim BIOCLIM environmental files available from www.worldclim.org/bioclim (Hijmans et al. 2005):

setwd("C:/Users/filepath")   #Set working directory to where your files are

library(raster)

library(rgdal)

library(SDMTools)

library(spatial.tools)       #Load necessary packages (Install first if necessary)

 

 

ts = read.table("Bias1.csv",header=T,sep=",")     #Read in your bias file (CSV) which should include columns LONGITUDE and LATITUDE in WSG84 format. If you are using other coordinate formats for your environmental files then match these instead.


e <- extent(c(-180,180,-60,90))                 #Define an extent that matches your environmental rasters

rast <- raster(e,ncol = 100, nrow = 100)        #Create a new raster with the dimensions of the environmental files

x <- rasterize(ts[, 1:2], rast, ts[,3], fun=mean)     #Add your data to the blank raster. 

#Where there is more than one value per pixel this code line averages them. 

#If you have more than 3 columns in your CSV amend this in the second set of square brackets



x[is.na(x)] <- 1                          #Fill in the non sampled pixels with "1" 

#None of the raster values should be zero, this causes errors

plot(x)                                         #Check the coloured pixels are in roughly the right geographical location

bioclim<-raster('bio5.bil')                     #Load one of your environmental bil files as a raster to create a template for trimming the new raster

bioclim                                         #Check the attributes of the environmental raster. Note that XDIM and YDIM are rounded to fewer decimal places when called in R.

 

r2 = raster(x)

res(r2) = min(res(x))

r2 <- resample(x, r2, method='bilinear')        #This will reshape the pixels of your new raster from oblong to square

projection(r2)<-"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs" 

#This matches the projection of your new raster to the environmental raster


r3<-spatial_sync_raster(r2, bioclim, method = "ngb",

    size_only = FALSE, raster_size, verbose = FALSE)  #Spatially sync your new raster to the environmental raster

 

writeRaster(r3, "~/biasfile.bil", format = "BIL",overwrite=TRUE)   #Write your biasfile in BIL so the format matches the env. rasters


Now that the new bias file is written in bil format, open the accompanying hdr file. Copy and paste the ULXMAP, ULYMAP, XDIM, YDIM and NBITS values from one of the hdr files of your environmental bil files over those of your new bias file hdr file. You will get a warning telling you that the geographic extents of your files mismatch if you do not do this.

Attempt to run MaxEnt. If you receive warnings that any of your presence points have missing data, go to settings>experimental, and check 'use samples with some missing data', or omit that point from your input csv file. The bias file will be ignored if you do not do this.

Make sure none of the fields or cells in your input csv files contain spaces, commas, symbol characters or full-stops, this causes the analysis to fail, and confusingly will also give you an error about your bias file being ignored even though the input file is the problem. 

Make sure only one species is selected in the left hand window of the main Maxent interface.

Make sure that all numeric fields in your input file are set as numeric and all text fields as text. None of the cells should contain more than 13 characters, and this also causes the analysis to fail. LATITUDE and LONGITUDE should be the first 2 fields, followed by any others.

I am by no means an expert at MaxEnt so there may be neater ways, this one worked for me. 


Joaquín Aldabe

unread,
Aug 4, 2015, 10:56:58 AM8/4/15
to max...@googlegroups.com
Thankyou very much Heather. I also had read serveral papers on the topic but none explained how to include the bias in the analysis. I´ll revise your procedure and try to apply to my problem. Thanks again. 
Regards.
Joaquín. 

--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maxent+un...@googlegroups.com.
To post to this group, send email to max...@googlegroups.com.
Visit this group at http://groups.google.com/group/maxent.
For more options, visit https://groups.google.com/d/optout.



--
Joaquín Aldabe

Grupo Biodiversidad, Ambiente y Sociedad
Centro Universitario de la Región Este, Universidad de la República
Ruta 15 (y Ruta 9), Km 28.500, Departamento de Rocha  
                                                           
Departamento de Conservación
Aves Uruguay
BirdLife International
Canelones 1164, Montevideo


https://sites.google.com/site/joaquin.aldabe

rayan emad

unread,
Aug 14, 2015, 11:17:37 AM8/14/15
to Maxent
Dear
when i apply your codes to get bias file i face this error below
ts = read.table("C:/Users/plxedk/Desktop/species records/records.csv",header=T,sep=",")
> e <- extent(c(-180,180,-60,90)) 
> rast <- raster(e,ncol = 100, nrow = 100)
> x <- rasterize(ts[, 1:2], rast, ts[,3], fun=mean) 
Error in .pointsToMatrix(xy) : 
  Highly suspect column names (longitude and latitude reversed?)
please do you any information how to solve this error.

Regards

emad 

Ahmed El-Gabbas

unread,
Aug 14, 2015, 12:37:31 PM8/14/15
to Maxent
Hello Emad,
Be sure that your .csv file has 3 columns in this order: Lonitude, Latitude, SAMPLINGWEIGHT (or something similar). It seems you use wrong order of coordinates.

Chers,
Ahmed

Mara Hernandez

unread,
Sep 29, 2018, 7:04:26 AM9/29/18
to Maxent
Hello Heather,

Your post has been very helpful in my project. It seems to work fine, but when I run this code:
r3<-spatial_sync_raster(kdl, climdat, method = "ngb",
                        size_only = FALSE, raster_size, verbose = FALSE)
It returns:
Warning message:
In .local(x, ...) :
  this does not look like an appropriate object for this function
Any ideas?

Thank you,
Mara

Amanda Perin Marcon

unread,
Apr 12, 2019, 11:25:29 PM4/12/19
to Maxent
Hi, Heather!

Please, a little help hahaha

I did everything right, but how in heavens do I open a .hdr file to edit such information?? Hahaha

I am so close of finally making my bias work, but I cannot figure out how to open a .hdr in a way I can visualize its values to edit them.

Also, thank you for your script! It really helped a lot!
Reply all
Reply to author
Forward
0 new messages