Creating Predictive Maps with R

We now have the tools we need to create predictive maps. The process below can be used to create maps using a wide variety of modeling approaches including linear models, higher-order polynomial models, tree-based models (CART), Generalized Linear Models (GLMs), and Generalized Additive Models (GAMs.). We'll use this approach in the next three labs to create models with different methods.

  1. Create a CSV file for modeling. This file should contain your response variables and matching covariate values. There are a variety of ways to create this file but typically you'll:
    1. Obtain a shapefile of points and measured variables.
    2. Extract the values from your covariates for your points. In BlueSpray:
      1. Load the dataset into BlueSpray.
      2. Right click on the dataset and select "Attributes -> Visible".
      3. Right click on the right-most column heading and select "Insert Column Right".
      4. Give the new attribute a good name and make sure the "Data Type" is set to 64-bit floating point.
      5. When you click "OK", you should see a new attribute column appear.
      6. Right click on the column heading for the new attribute and select "Extract".
        1. Select the raster with the count values for the "Attribute".
        2. Select "Mean" for the "Function" in case the pixels do not line up perfectly.
        3. When you click OK, you should see the values from the count raster appear in the attribute.
      7. Now, or later, you can add addtional covariate values to this dataset using the steps above.
      8. Rght click on the dataset and select "Export To File" and save it to a CSV file.
  2. Create your model. Use the appropriate modeling methods to create the model.
  3. Create your predictive data. This is a CSV that represents your entire sample area and contains columns for the x and y coordinate values and your predictor variable values for those points.
    1. Load a covariate layer such as one from BioClim into BlueSpray.
    2. To make this process faster, you may want to copy and downsample the raster. After you have the process working, you can then increase the size of the raster to obtain more accurate results.
      1. To crop the raster:
        1. Select the marquee tool (dotted box icon)
        2. Draw a box with the mouse
        3. Right click on the layer and select "Transforms: General -> Crop Raster"
        4. In the "Set To" popup, select the AOI (this is the box you created with the maquee).
        5. Give the new raster a good name such as "Precip_cropped.tif".
        6. Click OK.
      2. To downsample the raster:
        1. Right click on the raster and select "Transforms: General -> Resample"
        2. Change the "Sample Rate" in the X direction until the size of the raster is about 100 x 100.
        3. When you click "OK", you should see a raster with large pixels.
    3. Right click on the raster and select "Transforms: General -> To Points". This step will create a raster with a grid of points where each point contains the value for one pixel in the raster. This is the point dataset we will use for modeling.
    4. Repeat the steps in section one above to add the values from the other predictor layers into the new grid of points.
    5. Open the CSV in Excel and change the file's column headers to exactly match the one's you used in R to create the model.
  4. Predict values for the entire sample area. This step adds predictor values to the CSV file you just created.
    1. Use code similar to the code below to create new predictor values in R.
      ThePrediction=predict(TheModel,NewData) # create a vector of predicted values
      FinalData=data.frame(NewData,ThePrediction) # combine the new data and the predicted values
      write.csv(FinalData, "C:\\Temp\\ModelOutput.csv")  # Save the prediction to a new file
    2. Load the code into a GIS application and then symbolize the points to see the model!
    3. You may need to change the file's column headers for the GIS app to use the headers.
  5. Create a predicted map. The last step is to convert the points into a raster.
    1. In BlueSpray:
      1. Right click on the point layer and select "Transforms -> To Raster".
      2. Select "Attribute" for the "Point Source".
      3. Select your attribute with the prediction for the "Attribute"
      4. A tricky part is picking the correct resolution for the raster to make sure the pixels match the spacing of the points. The resolution should be the same as the original predictor rasters. However, I've found I need to use a slightly lower resolution to have a raster that does not have breaks in it.
      5. You'll want to set the bounds of the new raster to match your point file.

Below is a digram that summarizes this process:

© Copyright 2018 HSU - All rights reserved.