###########################################################################
############################# Run this set up code: #######################
###########################################################################
# set seed:
seed=38
# Define training and test files:
qdata.trainfn = system.file("external", "helpexamples","DATATRAIN.csv", package = "ModelMap")
# Define folder for all output:
folder=getwd()
#identifier for individual training and test data points
unique.rowname="ID"
###########################################################################
############## Pick one of the following sets of definitions: #############
###########################################################################
########## Continuous Response, Continuous Predictors ############
#file name to store model:
MODELfn="RF_Bio_TC"
#predictors:
predList=c("TCB","TCG","TCW")
#define which predictors are categorical:
predFactor=FALSE
# Response name and type:
response.name="BIO"
response.type="continuous"
########## binary Response, Continuous Predictors ############
#file name to store model:
MODELfn="RF_CONIFTYP_TC"
#predictors:
predList=c("TCB","TCG","TCW")
#define which predictors are categorical:
predFactor=FALSE
# Response name and type:
response.name="CONIFTYP"
# This variable is 1 if a conifer or mixed conifer type is present,
# otherwise 0.
response.type="binary"
########## Continuous Response, Categorical Predictors ############
# In this example, NLCD is a categorical predictor.
#
# You must decide what you want to happen if there are categories
# present in the data to be predicted (either the validation/test set
# or in the image file) that were not present in the original training data.
# Choices:
# na.action = "na.omit"
# Any validation datapoint or image pixel with a value for any
# categorical predictor not found in the training data will be
# returned as NA.
# na.action = "na.roughfix"
# Any validation datapoint or image pixel with a value for any
# categorical predictor not found in the training data will have
# the most common category for that predictor substituted,
# and the a prediction will be made.
# You must also let R know which of the predictors are categorical, in other
# words, which ones R needs to treat as factors.
# This vector must be a subset of the predictors given in predList
#file name to store model:
MODELfn="RF_BIO_TCandNLCD"
#predictors:
predList=c("TCB","TCG","TCW","NLCD")
#define which predictors are categorical:
predFactor=c("NLCD")
# Response name and type:
response.name="BIO"
response.type="continuous"
###########################################################################
########################### build model: ##################################
###########################################################################
### create model ###
model.obj = model.build( model.type="RF",
qdata.trainfn=qdata.trainfn,
folder=folder,
unique.rowname=unique.rowname,
MODELfn=MODELfn,
predList=predList,
predFactor=predFactor,
response.name=response.name,
response.type=response.type,
seed=seed,
na.action="na.roughfix"
)
###########################################################################
############ Then Run this code to predict map pixels #####################
###########################################################################
# A single model was built from the training data,
# but it will be applied to two sets of image data, one from 2001 and one from 2004
####################################################################################################
### Create a list of the filenames (including paths) for the rast Look up Tables ###
rastLUTfn.2001 <- paste(system.file(package="ModelMap"),"/external/helpexamples/LUT_2001.csv",sep="")
rastLUTfn.2004 <- paste(system.file(package="ModelMap"),"/external/helpexamples/LUT_2004.csv",sep="")
### Load rast LUT tables, and add path to the filenames in column 1 ###
rastLUT.2001 <- read.table(rastLUTfn.2001,header=FALSE,sep=",",stringsAsFactors=FALSE)
rastLUT.2004 <- read.table(rastLUTfn.2004,header=FALSE,sep=",",stringsAsFactors=FALSE)
rastLUT.2001[,1] <- paste(system.file(package="ModelMap"),"external/helpexamples",rastLUT.2001[,1],sep="/")
rastLUT.2004[,1] <- paste(system.file(package="ModelMap"),"external/helpexamples",rastLUT.2004[,1],sep="/")
### Define filenames for map output ###
asciifn.2001 <- "RF_BIO_TCandNLCD_01.txt"
asciifn.2004 <- "RF_BIO_TCandNLCD_04.txt"
asciifn.2001 <- paste(folder,asciifn.2001,sep="/")
asciifn.2004 <- paste(folder,asciifn.2004,sep="/")
### Define Number of rows of raster to read in at one time ###
# if crashes with warning: "unable to assign..." lower this number
numrows=500
### Create ascii text files of predicted map data ###
model.mapmake( model.obj=model.obj,
folder=folder,
rastLUTfn=rastLUT.2001,
# Model Validation Arguments
na.action="na.roughfix",
# Mapping arguments
numrows = numrows,
asciifn=asciifn.2001
)
model.mapmake( model.obj=model.obj,
folder=folder,
rastLUTfn=rastLUT.2004,
# Model Validation Arguments
na.action="na.roughfix",
# Mapping arguments
numrows = numrows,
asciifn=asciifn.2004
)
###########################################################################
######### run this code to create maps in R (For small maps only!)#########
###########################################################################
### Define Color Ramp ###
l <- seq(100,0,length.out=101)
c <- seq(0,100,length.out=101)
col.ramp <- hcl(h = 120, c = c, l = l)
### read in map data ###
mapgrid.2001 <- read.asciigrid(asciifn.2001,as.image=TRUE)
mapgrid.2004 <- read.asciigrid(asciifn.2004,as.image=TRUE)
### create map ###
dev.new(width = 8, height = 4)
opar <- par(mfrow=c(1,2),mar=c(3,3,2,1),oma=c(0,0,3,4),xpd=NA)
zlim <- c(0,max(mapgrid.2001$z,mapgrid.2004$z,na.rm=TRUE))
legend.label<-rev(pretty(zlim,n=5))
legend.colors<-col.ramp[trunc((legend.label/max(legend.label))*100)+1]
image(mapgrid.2001, col = col.ramp,zlim=zlim,asp=1,bty="n",xaxt="n",yaxt="n")
mtext("2001 Imagery",side=3,line=1,cex=1.2)
image(mapgrid.2004, col = col.ramp,zlim=zlim,asp=1,bty="n",xaxt="n",yaxt="n")
mtext("2004 Imagery",side=3,line=1,cex=1.2)
legend( x=max(mapgrid.2004$x),y=max(mapgrid.2004$y),
legend=legend.label,
fill=legend.colors,
bty="n",
cex=1.2
)
mtext("Predictions",side=3,line=1,cex=1.5,outer=TRUE)
par(opar)
###########################################################################
##### Run this code to map predictor data in R (For small maps only!) #####
###########################################################################
### Define Color Ramps ###
l <- seq(100,0,length.out=101)
c <- seq(0,100,length.out=101)
col.ramp.1 <- hcl(h = 15, c = c, l = l)
col.ramp.2 <- hcl(h = 70, c = c, l = l)
col.ramp.3 <- hcl(h = 150, c = c, l = l)
dev.new(width = 9, height = 6)
opar <- par(mfcol=c(2,3),mar=c(3,3,2,1),oma=c(0,0,3,4),xpd=NA)
#band 1
predgrid.2001=readGDAL(rastLUT.2001[1,1],band=rastLUT.2001[1,3])
predgrid.2001=as.image.SpatialGridDataFrame(predgrid.2001)
predgrid.2004=readGDAL(rastLUT.2004[1,1],band=rastLUT.2004[1,3])
predgrid.2004=as.image.SpatialGridDataFrame(predgrid.2004)
zlim <- range(predgrid.2001$z,predgrid.2004$z)
image(predgrid.2001, col = col.ramp.1,zlim=zlim,asp=1,bty="n",xaxt="n",yaxt="n")
mtext(rastLUT.2001[1,2],side=3,cex=1.5)
mtext("2001 Imagery",side=2,cex=1.5,line=1)
image(predgrid.2004, col = col.ramp.1,zlim=zlim,asp=1,bty="n",xaxt="n",yaxt="n")
mtext("2004 Imagery",side=2,cex=1.5,line=1)
#band 2
predgrid.2001=readGDAL(rastLUT.2001[2,1],band=rastLUT.2001[2,3])
predgrid.2001=as.image.SpatialGridDataFrame(predgrid.2001)
predgrid.2004=readGDAL(rastLUT.2004[2,1],band=rastLUT.2004[2,3])
predgrid.2004=as.image.SpatialGridDataFrame(predgrid.2004)
zlim <- range(predgrid.2001$z,predgrid.2004$z)
image(predgrid.2001, col = col.ramp.2,zlim=zlim,asp=1,bty="n",xaxt="n",yaxt="n")
mtext(rastLUT.2001[2,2],side=3,cex=1.5)
image(predgrid.2004, col = col.ramp.2,zlim=zlim,asp=1,bty="n",xaxt="n",yaxt="n")
#band 3
predgrid.2001=readGDAL(rastLUT.2001[3,1],band=rastLUT.2001[3,3])
predgrid.2001=as.image.SpatialGridDataFrame(predgrid.2001)
predgrid.2004=readGDAL(rastLUT.2004[3,1],band=rastLUT.2004[3,3])
predgrid.2004=as.image.SpatialGridDataFrame(predgrid.2004)
zlim <- range(predgrid.2001$z,predgrid.2004$z)
image(predgrid.2001, col = col.ramp.3,zlim=zlim,asp=1,bty="n",xaxt="n",yaxt="n")
mtext(rastLUT.2001[3,2],side=3,cex=1.5)
image(predgrid.2004, col = col.ramp.3,zlim=zlim,asp=1,bty="n",xaxt="n",yaxt="n")
mtext("Predictor Imagery",side=3,line=1,cex=1.5,outer=TRUE)
par(opar)Run the code above in your browser using DataLab