1
votes

Say I have some relationships between x and some outcome variable, for three different groups, A,B and C:

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)

The estimates of the v. parameter also have associated errors:

err.A<-0.24
err.B<-0.22
err.C<-0.29

I'd like to plot these curve fits, as well as shaded error regions around each curve, based on the uncertainty in the v. parameter. So, the shaded region would be +/- one error value. I can generate the plot of the 3 curves easily enough:

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)

But how can I added custom shaded regions around them, based on specified error terms?

enter image description here

1

1 Answers

2
votes

You can add the following code to your current code. The calculation of the error of the line is based on the error (assumed standard error) of the coefficients. You can change the calculation for the error of the line to something else, if desired. The order of plotting might need to be changed to make the polygons appear behind the lines.

# 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