[R] Curve crosses back to origin in plot

Robert A. LaBudde ral at lcfltd.com
Mon May 28 06:32:41 CEST 2007


Another sample problem: In the Windows version of R-2.5.0,

data(GHQ,package='HSAUR')
layout(1)
GHQ_glm_1<- glm(cbind(cases,non.cases) ~ GHQ, data=GHQ, family=binomial())
summary(GHQ_glm_1)
yfit_glm_1<- predict(GHQ_glm_1, type='response')
layout(1)
plot(probs ~ GHQ,pch=1,col=1,ylab='Probability(Case)', data=GHQ)
lines(yfit_glm_1 ~ GHQ, pch=3,col=3, data=GHQ)
legend("topleft", c("linear", "logistic"), pch=c(2,3), col=c(2,3))

Everything is fine, but the predicted values curve in the lines() 
statement becomes closed by a straight line segment connecting the 
last point to the first.

How can this be avoided? It appears to be wrapping due to the change 
from GHQ=10 for female to GHQ=0 again for male, bring the curve back 
to the beginning value.

One way to avoid this is to plot each sex's data in a separate lines() call.

Is there a way of doing this in a single lines() statement? If not, 
I'd like to know. If two lines() are needed, I'd like to know an 
efficient syntax. My attempt would be

lines(yfit_glm_1[0:10] ~ GHQ[0:10], pch=3,col=3, data=GHQ)
lines(yfit_glm_1[11:22] ~ GHQ[11:22], pch=3,col=3, data=GHQ)

which seems inelegant, as it involves numerical ranges that have to 
be determined by inspection.

Thanks again for answering these simple questions that seem to be the 
hardest to find answers for.
================================================================
Robert A. LaBudde, PhD, PAS, Dpl. ACAFS  e-mail: ral at lcfltd.com
Least Cost Formulations, Ltd.            URL: http://lcfltd.com/
824 Timberlake Drive                     Tel: 757-467-0954
Virginia Beach, VA 23464-3239            Fax: 757-467-2947

"Vere scire est per causas scire"



More information about the R-help mailing list