R for Spatial Statistics

 

Noise Injection

Introduction

Our data contains the trend we are looking for and some noise that should be based on a specific distribution. Since we cannot go out and sample our field sites over and over again, we can use the computer to inject different amounts of noise into our data to see what impact the noise might be having on our models.

Noise Injection in R

The code below will inject noise into one of the columns of our data table, run the model, collect statistics, and then repeat the process. We also load the data each time through to loop so our errors do not accumulate on each successive run.

# Setup the vectors for the statistics of interest
AICs=vector()
Count=1

while (Count<20) # loop injecting different noise each time through the loop
{
	# Read the data
	TheData = read.csv("D:/ProjectsModeling/Cali_5Minute/DougFir_CA_Predict 4.csv")

	# Remove any NA (null) values, the data should already be loaded
	TheData=na.omit(TheData)

	# Add some random data
	NumRows=length(TheData$AnnualPrecip)

	# Add some random data
	TheData$AnnualPrecip=TheData$AnnualPrecip+rnorm(NumRows, mean=0, sd=100)

	# Create the model
	attach(TheData) # attach is required for the "predict.gam()" function
	TheModel=gam(Height~s(AnnualPrecip),data=TheData,gamma=1.4)
	detach("TheData")

	# Add the AIC for this model to our results
	AICValue=AIC(TheModel)
	AICs=c(AICs,AICValue)

	# Incrememnt our counter so we don't keep going forever
	Count=Count+1
}
# Output stats on our model runs
summary(AICs)