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()

# loop while loading the data, injecting noise, and creating the model
Count=1
while (Count<20) # loop injecting different noise each time through the loop
{
	# Read the data each time because we will be modifying it below
	TheData = read.csv("D:\\jimg\\Classes\\GSP 570\\GSP 570\\Cali_5Minute/DougFir_CA_Predict 4.csv")
	
	# Remove any NA (null) values
	TheData=na.omit(TheData)
	
	# Get the number of rows in the data to create a vector with random data
	NumRows=length(TheData$AnnualPrecip)
	
	# Add some random data
	TheData$AnnualPrecip=TheData$AnnualPrecip+rnorm(NumRows, mean=0, sd=10)
	
	# 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)
plot(AICs)
hist(AICs)