2015-11-02 36 views
1

說我有x和一些結果變量之間的一些關係,爲三個不同的羣體,ABC繪製圍繞曲線定製置信區間符合[R

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 

我想繪製這些曲線擬合,以及每個彎道陰影誤差區域的基礎上,在的不確定性參數。所以,陰影區域將是+/-一個錯誤值。我可以生成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) 

但我怎麼能添加的自定義陰影周圍的區域,根據指定的誤差項?

enter image description here

回答

2

您可以將下面的代碼添加到您當前密碼。線的誤差計算基於係數的誤差(假定的標準誤差)。如果需要,您可以將線路錯誤的計算更改爲其他值。繪圖順序可能需要更改以使多邊形顯示在線條後面。

# 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) 

enter image description here