0
votes

I'm attempting to plot two locfit models within one plot however I am unable to get the second locfit model to plot the confidence intervals. I've created two locfit models:

1_fit = locfit(Y~Time,data=data_1)
2_fit = locfit(Y~Time,data=data_2)

Each can be plotted on their own just fine with the confidence intervals using the following:

plot(1_fit,band="local",type = "l", xlab = "Time", ylab = "Y-Axis",ylim=c(0,22), 
  col = "red",lwd = 5,font=3,main="Local Poly Fit 1",cex.lab=1.5, cex.axis=1.5, 
  cex.main=1.5, cex.sub=1.5)

However, when I attempt to plot an additional locfit model to the plot using:

lines(2_fit,col="blue")

I can only add the locfit line but not the confidence intervals. I've attempted to do:

lines(2_fit,band="local",col="blue")

But I get this message and no confidence intervals:

Warning message: In plot.xy(xy.coords(x, y), type = type, ...) : "band" is not a graphical parameter

I've also looked into using lines.locfit, but had no luck as R just says that it can't find the function lines.locfit.

I have a work around to put both plots within the same window using:

par(mfrow=c(2,1))

But would like to avoid this as it would make the plots more comparable if they were within the same plot.

1
You could always use par(new = TRUE) and then plot one on top of the other. Might need to set xlim/ylim to ensure match. 1_fit is not a legal name in R - Richard Telford
Alternatively, use predict to extract the CI and plot directly - Richard Telford
par(new = TRUE) did the trick! Thanks alot! As a side note, 1_fit was just a temporary name, not actually using it in my code, but good to know anyway that it is not legal. - user3221962

1 Answers

0
votes

Answer was provided by Richard Telford in comments. The following code was able to accomplish what I needed:

    plot(1_fit,band="local",type = "l", xlab = "Time", ylab = "Y-Axis",ylim=c(0,22), xlim=c(0,12), col = "red",lwd = 5,font=3,main="Local Poly Fit 1",cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5)`

    par(new = TRUE)

    plot(2_fit,band="local",type = "l", xlab = "Time", ylab = "Y-Axis",ylim=c(0,22), xlim=c(0,12),col = "blue",lwd = 5,font=3,main="Local Poly Fit 1",cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5)` 

I needed to be sure that ylim and xlim were equal as well as main, ylab, and xlab. Also a side note from Richard, 1_fit is not a legal name, I used it here just as a placeholder name but seems good knowledge to pass on.