[英]plotting custom confidence intervals around curve fits R
假設對於三個不同的組A
, B
和C
我在x
和某個結果變量之間有一些關系:
x<-c(0:470)/1000
#3 groups, each has a different v-max parameter value.
v.A<-5
v.B<-4
v.C<-3
C<- (v.C*x)/(0.02+x)
B<- (v.B*x)/(0.02+x)
A<-(v.A*x)/(0.02+x)
d.curve<-data.frame(x,A,B,C)
v.
參數的估計值還具有相關的錯誤:
err.A<-0.24
err.B<-0.22
err.C<-0.29
我想根據v.
參數的不確定性繪制這些曲線擬合以及每個曲線周圍的陰影誤差區域。 因此,陰影區域將為+/-一個誤差值。 我可以很容易地生成3條曲線的圖:
limx<-c(0,0.47)
limy<-c(0,5.5)
plot(A~x,data=d.curve,xlim=limx,ylim=limy,col=NA)
lines(smooth.spline(d.curve$x,d.curve$A),col='black',lwd=3)
par(new=T)
plot(B~x,data=d.curve,xlim=limx,ylim=limy,col=NA,ylab=NA,xlab=NA,axes=F)
lines(smooth.spline(d.curve$x,d.curve$B),col='black',lwd=3,lty=2)
par(new=T)
plot(C~x,data=d.curve,xlim=limx,ylim=limy,col=NA,ylab=NA,xlab=NA,axes=F)
lines(smooth.spline(d.curve$x,d.curve$C),col='black',lwd=3,lty=3)
但是如何根據指定的錯誤術語在其周圍添加自定義陰影區域?
您可以將以下代碼添加到當前代碼中。 線的誤差的計算基於系數的誤差(假定的標准誤差)。 如果需要,可以將行錯誤的計算方式更改為其他方式。 可能需要更改打印順序,以使多邊形顯示在線條后面。
# calculating the standard error of the line base on standard error of A,B,C
# could substitute another calculation
se.line.A <- ((x)/(0.02+x))*err.A
se.line.B <- ((x)/(0.02+x))*err.B
se.line.C <- ((x)/(0.02+x))*err.C
# library for polygons
library(graphics)
# plotting polygons
# colors can be changed
# polygons will be drawn over the existing lines
# may change the order of plotting for the shaded regions to be behind line
polygon(c(x,rev(x))
,c(A+se.line.A,rev(A-se.line.A))
,col='gray'
,density=100)
polygon(c(x,rev(x))
,c(B+se.line.B,rev(B-se.line.B))
,col='blue'
,density=100)
polygon(c(x,rev(x))
,c(C+se.line.C,rev(C-se.line.C))
,col='green'
,density=100)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.