R绘制具有置信区间的多个locfit模型
R plotting multiple locfit models with confidence intervals
我试图在一个图中绘制两个 locfit 模型,但是我无法获得第二个 locfit 模型来绘制置信区间。我创建了两个 locfit 模型:
1_fit = locfit(Y~Time,data=data_1)
2_fit = locfit(Y~Time,data=data_2)
每个都可以单独绘制,使用以下置信区间:
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)
但是,当我尝试使用以下方法在绘图中绘制额外的 locfit 模型时:
lines(2_fit,col="blue")
我只能添加 locfit 线,不能添加置信区间。我尝试过:
lines(2_fit,band="local",col="blue")
但我收到此消息并且没有置信区间:
Warning message:
In plot.xy(xy.coords(x, y), type = type, ...) :
"band" is not a graphical parameter
我也研究过使用 lines.locfit,但没有成功,因为 R 只是说它找不到函数 lines.locfit。
我有一个解决方法,可以将两个地块放在同一个 window 中,使用:
par(mfrow=c(2,1))
但我想避免这种情况,因为如果它们在同一个地块内,这会使地块更具可比性。
Richard Telford 在评论中提供了答案。以下代码能够完成我所需要的:
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)`
我需要确保 ylim
和 xlim
以及 main、ylab 和 xlab 相等。还有来自 Richard 的旁注,1_fit 不是合法名称,我在这里将其用作占位符名称,但似乎可以传递很好的知识。
我试图在一个图中绘制两个 locfit 模型,但是我无法获得第二个 locfit 模型来绘制置信区间。我创建了两个 locfit 模型:
1_fit = locfit(Y~Time,data=data_1)
2_fit = locfit(Y~Time,data=data_2)
每个都可以单独绘制,使用以下置信区间:
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)
但是,当我尝试使用以下方法在绘图中绘制额外的 locfit 模型时:
lines(2_fit,col="blue")
我只能添加 locfit 线,不能添加置信区间。我尝试过:
lines(2_fit,band="local",col="blue")
但我收到此消息并且没有置信区间:
Warning message: In plot.xy(xy.coords(x, y), type = type, ...) : "band" is not a graphical parameter
我也研究过使用 lines.locfit,但没有成功,因为 R 只是说它找不到函数 lines.locfit。
我有一个解决方法,可以将两个地块放在同一个 window 中,使用:
par(mfrow=c(2,1))
但我想避免这种情况,因为如果它们在同一个地块内,这会使地块更具可比性。
Richard Telford 在评论中提供了答案。以下代码能够完成我所需要的:
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)`
我需要确保 ylim
和 xlim
以及 main、ylab 和 xlab 相等。还有来自 Richard 的旁注,1_fit 不是合法名称,我在这里将其用作占位符名称,但似乎可以传递很好的知识。