LOESS, or LOWESS Smoothing Curves

The following will add a locally weighted scatterplot smoothing (LOESS, or LOWESS ) curve for the data. It is based on the code found at loess Smoothingand Data Imputation. To read more about LOESS see the Wikipedia article

Note: Lines with # in front of them are just for your information. They are ignored by R.

_________________________________________________________

#SIMPLE CODE

# Compute loess smoothed values for all points along the curve

y.predict <- predict(y.loess, data.frame(x=Year))

# Plots the curve.

lines(Year,y.predict)

#Add title

title(main="Simple Code Example")

#Done

_________________________________________________________

#Plots Detects and NonDetects and adds legend)

# Apply loess smoothing using the default span value of 0.8. You can change the curve by changing the span value.

y.loess <- loess(y ~ x, span=0.8, data.frame(x=Year, y=EMD))

#Plots the data

plot(Year, EMD)

#Load your data.

mydata <-read.csv("http://doylesdartden.com/Example-ND.csv", sep=",")

attach(mydata)

# Apply loess smoothing using the default span value of 0.8. You can change the curve by changing the span value.

y.loess <- loess(y ~ x, span=0.8, data.frame(x=Year, y=EMD))

# Compute loess smoothed values for all points along the curve

y.predict <- predict(y.loess, data.frame(x=Year))

# Plots the curve.

lines(Year,y.predict)

#Add Legend to graph. You can change the size of the box by changing cex = 0.75 Large # makes it larger.

legend("topleft", c("Smoothing Curve", "Detected", "NonDetect"), col = c(1, "black","red"), cex = 0.75,

text.col = "black", lty = c(1 ,-1, -1), pch = c(-1, 19, 17),

merge = TRUE, bg = 'gray90')

#Add title

title(main="Detects and NonDetects with Curve")

# Done

_________________________________________________________

#Plots the data but makes nondetects a different color and type based on column D_EMD being a 0 for ND and 1 for detect.

plot(Year, EMD, col=ifelse(D_EMD, "black", "red"),ylab = "EMD mg/L", pch=ifelse(D_EMD, 19, 17), cex = 0.7)

mydata <-read.csv("http://doylesdartden.com/Example-ND.csv", sep=",")

attach(mydata)

reg1 <- lm(EMD~Year)

par(cex=1)

#Load your data. Is located on the web at the address below

mydata <- read.csv("http://doylesdartden.com/Example-ND.csv", sep=",")

attach(mydata)

reg1 <- lm(EMD~Year)

par(cex=1)

#Plots the data but makes nondetects a different color and type based on column D_EMD being a 0 for ND and 1 for detect.

plot(Year, EMD, col=ifelse(D_EMD, "black", "red"),ylab = "EMD", pch=ifelse(D_EMD, 19, 17), cex = 0.7)

plx<-predict(loess(EMD ~ Year, data=mydata), se=T)

#rough & ready CI by adding and subtracting 2 times the standard error to the mean

lines(mydata$Year,plx$fit+2*plx$s, lty=2)

lines(mydata$Year,plx$fit-2*plx$s, lty=2)

# Apply loess smoothing using the default span value of 0.8. You can change the curve by changing the span value.

y.loess <- loess(y ~ x, span=0.8, data.frame(x=Year, y=EMD))

# Compute loess smoothed values for all points along the curve

y.predict <- predict(y.loess, data.frame(x=Year))

# Plots the curve.

lines(Year,y.predict)

#Add Legend to graEMD. You can change the size of the box by changing cex = 0.75 Large # makes it larger.

legend ("topleft",

c("Smoothing Curve", "Detected", "NonDetect"),

col = c(1,"black","red"),

cex = 0.75,

text.col = "black",

lty = c(1 ,-1, -1),

pch = c(-1, 19, 17),

merge = TRUE, bg = 'gray90')

#Add title

title(main="Locally Weighted Scatterplot Smoothing Curve")

# Done

_________________________________________________________

#Smoothing Curve with Confidence Interval Detects and NonDetects Together - does one line and ci for detects and another for NDs. Special thanks to Kaori (Groton) Ito from the ggplot group for helping me on this one.

ggplot(data = mydata, aes(x=Year, y=EMD, col=Detections)) +

geom_point(aes(shape=Detections)) +

geom_smooth(span=2)+

scale_colour_manual(values=c("black","red")) +

theme(legend.position=c(0.2,0.9))+

ggtitle("Detects and NonDetects with Separate Curves and CIs")

#done

#Loads ggplot. NOTE – You have to have ggplot installed

library(ggplot2)

#Loads data from internet

mydata <- read.csv("http://doylesdartden.com/Example-ND.csv", sep=",")

#Makes a new column of Detected and NonDetects based on D_EMD

mydata$Detections <- ifelse(mydata$D_EMD==1, "Detected", "NonDetect")

#Loads ggplot. NOTE – You have to have ggplot installed

library(ggplot2)

#Loads data

mydata <- read.csv("http://doylesdartden.com/Example-ND.csv", sep=",")

mydata$Detections <- ifelse(mydata$D_EMD==1, "Detected", "NonDetect")

ggplot(data = mydata, aes(x=Year, y=EMD, col=Detections)) +

geom_point(aes(shape=Detections)) +

geom_smooth(span=2, aes(group=1))+

scale_colour_manual(values=c("black","red")) +

theme(legend.position=c(0.2,0.9)) +

ggtitle("Smoothing Curve with Confidence Interval

Detects and NonDetects Together")

#Done

_________________________________________________________

#Plots Detects and NonDetects and does separate curves and CIs

_________________________________________________________

Multiple lines graphs

#Load your data. The data is in a spreadsheet named KW-spreadsheet and we are going to call it "data" in R

mydata <- read.csv("http://doylesdartden.com/example-smoothing-data.csv", sep=",")

attach(mydata)

#Plots the Y and X axis

plot( Date, dataset1,

#sets the range of the y axis

ylim=range(5.7,7.5),

#sets the symbol type, size, and color for the 1st series

pch=20,cex=0.0, col='black', xlab="Date ", ylab="pH")

#Plots the second series

points( Date, dataset2,

#sets the symbol type, size, and color for the 2nd series

col='blue',pch=20, cex=0.0)

#Plots the 3rd series

points( Date, dataset3,

#sets the symbol type, size, and color for the 3rd series

col='red',pch=20, cex=0.0)

#Plots the 4th series

points( Date, dataset4,

#sets the symbol type, size, and color for the 4th series

col='forestgreen',pch=20, cex=0.0)

#Plots the 5th series

points( Date, dataset5,

#sets the symbol type, size, and color for the 5th series

col='purple',pch=20, cex=0.0)

#Plots the 6th series

points( Date, dataset6,

#sets the symbol type, size, and color for the 6th series

col='orange',pch=2, cex=0.0)

#Plots the 7th series

points( Date, dataset7,

#sets the symbol type, size, and color for the 7th series

col='limegreen',pch=2, cex=0.0)

#Plots the 8th series

points( Date, dataset7,

#sets the symbol type, size, and color for the 8th series

col='red',pch=2, cex=0.0)

#Plots the 9th series

points( Date, dataset7,

#sets the symbol type, size, and color for the 9th series

col='forestgreen',pch=2, cex=0.0)

#draw the smooth lines. NOTE-the span will adjust how much it is smooth

lines( loess.smooth(Date,dataset1, span = 0.3),col='black', lwd=4, lty=1)

lines( loess.smooth(Date,dataset2, span = 0.3),col='blue',lwd=4, lty=1)

lines( loess.smooth(Date,dataset3, span = 0.3),col='red',lwd=4, lty=1)

lines( loess.smooth(Date,dataset4, span = 0.3),col='forestgreen',lwd=3, lty=1)

lines( loess.smooth(Date,dataset5, span = 0.3),col='purple',lwd=3, lty=1)

lines( loess.smooth(Date,dataset6, span = 0.3),col='orange',lwd=3, lty=1)

lines( loess.smooth(Date,dataset7, span = 0.3),col='limegreen',lwd=3, lty=1)

lines( loess.smooth(Date,dataset8, span = 0.3),col='gray49',lwd=3, lty=1)

lines( loess.smooth(Date,dataset9, span = 0.3),col='yellow',lwd=3, lty=1)

#Add Legend to graph. You can change the size of the box by changing cex = 0.75 Large # makes it larger.

legend("bottomleft",c("Dataset1","Dataset2", "Dataset3", "Dataset4", "Dataset5", "Dataset6", "Dataset7", "Dataset8", "Dataset9"),

col = c("black","blue", "forestgreen","red", "purple", "orange", "limegreen", "gray49", "yellow"),

cex = 0.7,text.col = "black",lty = c(1),lwd=c(3),pch = c(-1),

merge = TRUE, bg = 'gray90')

#Add title

title(main="Locally Weighted Scatterplot Smoothing Curve")

#done