Skip to content
Prev 323554 / 398503 Next

Broken line questions

Hi,
You may also replace the NAs using ?na.approx() from library(zoo).


library(zoo)
?mydata2<- mydata
?mydata2$MW01<-na.approx(mydata2$MW01)
?plot(MW01~Year,data=mydata2,col=ifelse(D_MW01,"black","red"),ylab="END (mg/L)",pch=ifelse(D_MW01,19,24),cex=1)
with(mydata2,lines(Year,MW01,lty=c(1),col="black"))
plx<-predict(loess(MW01 ~ Year, data=mydata2), se=T)
lines(mydata2$Year,plx$fit+2*plx$s, lty=2)
lines(mydata2$Year,plx$fit-2*plx$s, lty=2)
y.loess <- loess(y ~ x, span=0.8, data.frame(x=mydata2$Year,y=mydata2$MW01))
y.predict <- predict(y.loess, data.frame(x=mydata2$Year))
lines(mydata2$Year,y.predict, lty=2, lwd=2)
legend ("topleft", 
c("Smoothing Curve","Detect","Non-Detect"), 
col = c(1,1,1),
cex = 1, 
text.col = "black", 
lty = c(2,-1,-1), 
lwd = c(2,-1,-1),
pch = c(-1,19,24), 
merge = TRUE, bg = 'gray90')

A.K.

----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: David Doyle <kydaviddoyle at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Tuesday, May 14, 2013 11:52 PM
Subject: Re: [R] Broken line questions

Hi,
It is because of the unequal lengths of x and y.
I was able to plot without the errors.? But, not sure if this is what you wanted.

mydata <- read.table("TestData.csv", header=TRUE, sep="\t")
reg1<-lm(MW01~Year,data=mydata)
?plot(MW01~Year,data=mydata[!is.na(mydata$MW01),],col=ifelse(D_MW01,"black","red"),ylab="END (mg/L)",pch=ifelse(D_MW01,19,24),cex=1)
with(mydata[!is.na(mydata$MW01),],lines(Year,MW01,lty=c(1),col="black"))

plx1<-predict(loess(MW01 ~ Year, data=mydata[!is.na(mydata$MW01),]), se=T)
#rough & ready CI by adding and subtracting 2 times the standard error to the mean
lines(mydata[!is.na(mydata$MW01),"Year"],plx1$fit+2*plx1$s, lty=2) 
lines(mydata[!is.na(mydata$MW01),"Year"],plx1$fit-2*plx1$s, lty=2) 
mydata1<-mydata[!is.na(mydata$MW01),2:3]
y.loess <- loess(y ~ x, span=0.8, data.frame(x=mydata1$Year,y=mydata1$MW01))

# Compute loess smoothed values for all points along the curve
y.predict <- predict(y.loess, data.frame(x=mydata1$Year))

# Plots the curve.
lines(mydata1$Year,y.predict, lty=2, lwd=2)

BTW,? "Non-detect" in the legend is not clear.
A.K.

#Add line between the points
lines(mydata1$Year,mydata1$MW01)