library(np) Tuna.df <- read.delim("Yellowfin.txt",header=T) attach(Tuna.df) Length_Pacific <- Length[Ocean=="Pacific"] Age_Pacific <- Age[Ocean=="Pacific"] (bw <- npregbw(Length_Pacific~Age_Pacific)) fit1 <-npreg(bws=bw,formula=Length_Pacific~Age_Pacific) plot(fit1,plot.errors.method="asymptotic",plot.errors.style="band",main="Kernel-smoothed") points(Age_Pacific,Length_Pacific) fit180 <- npreg(bws=80,formula=Length_Pacific~Age_Pacific) plot(fit180,plot.errors.method="asymptotic",plot.errors.style="band",main="Kernel-smoothed") points(Age_Pacific,Length_Pacific) fit2=loess(Length_Pacific~Age_Pacific) pred.Age=seq(200,1200,20) pred2=predict(fit2,pred.Age,se=TRUE) plot(pred.Age,pred2$fit,type="l",xlab="Age",ylab="Length",main="Lowess Fit") lines(pred.Age,pred2$fit-1.96*pred2$se.fit,lty=3) lines(pred.Age,pred2$fit+1.96*pred2$se.fit,lty=3) points(Age_Pacific,Length_Pacific) fit26=loess(Length_Pacific~Age_Pacific,enp.target=6) pred3=predict(fit26,pred.Age,se=TRUE) plot(pred.Age,pred3$fit,type="l",xlab="Age",ylab="Length",main="Lowess Fit") lines(pred.Age,pred3$fit-1.96*pred3$se.fit,lty=3) lines(pred.Age,pred3$fit+1.96*pred3$se.fit,lty=3) points(Age_Pacific,Length_Pacific)