Masthead

Lab: Creating Spatial Models with GLMs

This lab will introduce you to modeling presence/absence data with GLMs. This is also the first full-modeling lab. A key element of this lab is examining the response of the data vs. the predicted output of the model and how both relate to the predictor variables.

Note: Remember to include the glm2 library in the code below.

GLMs in R

The Data

The function below will create synthetic presence/absence data for evaluating GLMs. Copy it into R now and compile them.

############################################################################ 
# Creates a data frame with Xs ranging from 1 to the number of entries
# and "Measures" with 1/2 set to 0 and 1/2 set to 1.  The lower have of
# the X values have measures at 0.
# ProportionRandom - amount of uniform randomness to add
############################################################################ 

Categories1D_Random=function(NumEntries=10,ProportionRandom=0.4)
{
  Range=NumEntries*ProportionRandom/2
  Ys=as.vector(array(1:NumEntries))
  Xs=as.vector(array(1:NumEntries))
  Measures=as.vector(array(1:NumEntries))
  for (Index in 1:NumEntries)
  {
    Xs[Index]=Index #runif(1,0,100)
    Ys[Index]=Index #runif(1,0,100)
    Threshold=0.5
    Random=0
    if (ProportionRandom!=0) Random=runif(1,-Range,Range)
    if (Xs[Index]>NumEntries/2+Random) Measures[Index]=1
    else Measures[Index]=0
  }
  TheDataFrame = data.frame(Ys, Xs, Measures)
}

The code below will create a synthetic data set for a logistic model using the function above. Try it now in R. Note that the data contains some randomness to make the values overlap a bit.

TheData=Categories1D_Random(100) # create a set of binary data 

plot(TheData$Xs,TheData$Measures) # plot the data

GLMs with Real Data

GLMs are often used with presence/absence data to create models with a logistic trend. Creating presence/absence data from occurrence data is fast in BlueSpray.

The steps below will create a point dataset that represents a grid over a sample area. It will include values for your covariates and counts of the number of occurrences in each cell in the grid. This can be converted to a presence/absence value in R.

  1. Open BlueSpray
  2. Drag and drop one of your covariate layers into BlueSpray
  3. Right click on the layer in the table-of-contents (TOC) and select General Transforms -> Sample
  4. Enter 10 for the X-Direction sampling rate. This will reduce the size of the raster cells and thus the number of cells to make building our models fast.
  5. Click OK and you should see a down sampled version of your covariate. This will provide the structure for our dataset.
  6. Right click on the down sampled covariate layer and select General Transforms -> To Point
  7. When you click okay you should see a point layer. If you open the attributes you should see a "Value" attribute with the values from the raster.
  8. You can rename attributes in BlueSpray by right clicking on the attribute title and selecting "Settings...".
  9. Change the name to whatever you want but it is recommended to keep names to 10 characters or less and to not use symbols or other software packages may have errors on the name.
  10. Now, load up your occurrence point layer.
  11. Right click on the layer and select "General Transforms -> To Raster".
  12. Select "Count" for the "Pixel Source". This will make every cell (pixel) of the raster contain a number of points in the point dataset.
  13. Select your down sampled covariate raster in the "Set To" menu. This will set the extent and resolution of the point raster to exactly the same as your down sampled covariate raster.
  14. When you click "OK" you should see a raster with pixels that vary in color based on the number of occurrences they contain.
  15. In your point layer's attribute table, right click on an attribute and select "Insert Column Right..."
  16. Give the new attribute a good name like "Count" and set the "Data Type" to float.
  17. When you click "OK" you should see the new attribute column be added to the attribute table.
  18. Right click on the attribute title for the new column and select "Extract".
  19. Select your count raster as the "Raster Layer" and click "OK". You should see your counts appear in the attribute table.
  20. You can now repeat steps 15 through 19 for each of your other covariate rasters.

A count value can be converted to a presence/absence value in R with:

PresAbs=TheData$count>=1 # create a vector with TRUE/FALSE values
PresAbs=as.numeric(PresAbs) # convert the TRUE/FALSE values to 1/0s
TheData$PresAbs=PresAbs # add the vector to the original datatable

For the remainder of the lab, please use the GLM web page in the R for Spatial Stats website.

© Copyright 2018 HSU - All rights reserved.