[R] gam - Y axis probability scale with confidence/error lines

Patrick Breheny patrick.breheny at uky.edu
Wed Mar 14 17:39:22 CET 2012

The predict() function has an option 'se.fit' that returns what you are 
asking for.  If you set this equal to TRUE in your code:

pred <- predict(fit,data.frame(x=xx),type="response",se.fit=TRUE)

will return a list with two elements, 'fit' and 'se.fit'.  The pointwise 
confidence intervals will then be

pred$fit + 1.96*se.fit
pred$fit - 1.96*se.fit

for 95% confidence intervals (replace 1.96 with the appropriate quantile 
of the normal distribution for other confidence levels).

You can then do whatever "stuff" you want to do with them, including 
plot them.


On 03/14/2012 10:48 AM, Ben quant wrote:
> Hello,
> How do I plot a gam fit object on probability (Y axis) vs raw values (X
> axis) axis and include the confidence plot lines?
> Details...
> I'm using the gam function like this:
> l_yx[,2] = log(l_yx[,2] + .0004)
> fit<- gam(y~s(x),data=as.data.frame(l_yx),family=binomial)
> And I want to plot it so that probability is on the Y axis and values are
> on the X axis (i.e. I don't want log likelihood on the Y axis or the log of
> my values on my X axis):
> xx<- seq(min(l_yx[,2]),max(l_yx[,2]),len=101)
> plot(xx,predict(fit,data.frame(x=xx),type="response"),type="l",xaxt="n",xlab="Churn",ylab="P(Top
> Performer)")
> at<- c(.001,.01,.1,1,10)  #<-------------- I'd also like to generalize
> this rather than hard code the numbers
> axis(1,at=log(at+ .0004),label=at)
> So far, using the code above, everything looks the way I want. But that
> does not give me anything information on variability/confidence/certainty.
> How do I get the dash plots from this:
> plot(fit)
> ...on the same scales as above?
> Related question: how do get the dashed values out of the fit object so I
> can do 'stuff' with it?
> Thanks,
> Ben
> PS - thank you Patrick for your help previously.
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Patrick Breheny
Assistant Professor
Department of Biostatistics
Department of Statistics
University of Kentucky

More information about the R-help mailing list