R for Spatial Statistics

 

Polynomial Regression

Below is an example of doing linear and poynomial regression in R. The "lm()" function in R is very flexible and allows Nth order polynomials of multiple predictor variables to be used for the model.

#########################################################
# Script to explore data with histograms, scatter plots,
# and regression
# Author: Jim Grama
# Date: 4/24/2012
#########################################################

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

# Make the dataset the default so we don't have to specify the dataframe
attach(Redwood_CA_Predictors)

# Histogram the height values
hist(HT,probability=TRUE,main="Tree Height",breaks=400) 

# Histogram the diameter values
hist(DIA,probability=TRUE,main="Tree Daimeter",breaks=400) 

# Histogram the precipitation values
hist(AnnualPrecip,probability=TRUE,main="Tree Precip",breaks=400) 

# Histogram the mean temperature values
hist(MeanTemp,probability=TRUE,main="Tree Mean Temp",breaks=400) 

# Plot the height values against the diameter values
plot(HT~DIA,main="Height vs. Diameter")

# Add a linear regression line to the plot
RegressionModel=lm(HT~DIA)
abline(RegressionModel,col="red")

# Create a thrid order polynomial regression
RegressionModel=lm(HT~DIA+ I(DIA^2) + I(DIA^3))

# Create a response vector based on the model
ResponsePrediction=predict(RegressionModel)

# Add the response to the plot
points(DIA, ResponsePrediction, type="p", col="red", lwd=2)

 

 

Other Resources

Presence-Absence Package Documentation