[R] zigzag confidence interval in a plot

Peter Ehlers ehlers at ucalgary.ca
Fri May 17 17:23:04 CEST 2013


On 2013-05-17 06:03, Ozgul Inceoglu wrote:
> Dear All,
>
> When I plot the values and linear regression line for one data set, it is fine. But for another one I see zigzags, when I plot the confidence interval
>
>> cd
> Depth	CHAOsep12RNA
> 9,94	804
> 25,06	1476,833333
> 40,04	1540,561404
> 50,11	1575,166667
> 52,46	349,222222
> 54,92	1941,5
> 57,29	1053,507042
> 60,11	1535,1
> 70,04	2244,963303
> 79,97	1954,507042
> 100,31	2679,140625
>
>
>> plot(cd$CHAOsep12RNA,cd$Depth, ylim = rev(range(0:100)), xlab="CHAO", ylab="Depth", pch=15, las=2, main="Sep12-RNA", cex.main=1)
>> lmR <- lm(cd$Depth~cd$CHAOsep12RNA)
>> abline(lmR)
> pconfR <- predict(lmR,interval="confidence")
> matlines(cd$CHAOsep12RNA,pconfR[,c("lwr","upr")], col=1, lty=2)
>
> I also tried
>
>> newx <- seq(min(cd$CHAOsep12RNA), max(cd$CHAOsep12RNA), length.out=11)
>> a <- predict(lmR, newdata=data.frame(CHAO=newx), interval=c("confidence"))
>> plot(cd$CHAOsep12RNA,cd$Depth, ylim = rev(range(0:100)), xlab="CHAO", ylab="Depth", pch=15, las=2, main="Sep12-RNA", cex.main=1)
>> abline(lmR)
>> lines(cd$CHAOsep12RNA, a[,2], lty=2)
>
> But I see both cases kind of zigzags. What can it be the reason? thank you!
>

Sort your dataframe by x-values (CHAOsep12RNA).

Peter Ehlers



More information about the R-help mailing list