# [R] confidence interval for glm

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Dec 1 17:45:35 CET 2008

```On Sun, 2008-11-30 at 01:15 -0500, David Winsemius wrote:
> ?confint.glm   # ... in MASS

That provides confidence intervals on the parameters of the model, which
is not what the OP wanted. He wants confidence intervals on:

predict(mod, newdat)

One way to do this is to compute them in the normal way but do so on the
scale of the link function. Then transform those confidence intervals on
to the scale of the response by applying the inverse of the link
function used in the GLM. One way to do this is the following: [not
tested as no reproducible code to try it on]

## compute the predicted values on the scale of the link
preds <- predict(mod, newdata = pred.data, se.fit = TRUE)

## where mod and pred.data are your glm model and
## points at which you wish to evaluate the fitted function.
## leave out newdata = pred.data if you want the fitted values

## get inverse of link as a function

## compute standard confidence intervals (2 * se.fit)
ci.u <- with(preds, fit + (2 * se.fit))
ci.l <- with(preds, fit - (2 * se.fit))
## change 2 to be a quantile from the t distribution for your level
## of confidence (2 is approximately 95% for large n)
##
## something like for 95%, N is sample size;
## (hope I got the correction for df right here, no doubt someone will
## correct me if wrong), and use crit.t in place of 2 above.
crit.t <- qt(0.975, df = N-1)

## compute predictions on scale of response by applying ilogit
preds <- ilogit(preds\$fit)

## transform conf int on to scale of response
ci.u <- ilogit(ci.u)
ci.l <- ilogit(ci.l)

Is that what you were looking for?

G

>
> On Nov 28, 2008, at 9:29 AM, Gerard M. Keogh wrote:
>
> >
> > Hi all,
> >
> > simple Q:
> >
> > how do I extract the upper and lower CI for predicted probabilities
> > directly for a glm - I'm sure there's a one line to do it but I
> > can't find
> > it.
> > the predicted values I get with the predict (.. "response")
> >
> > Thanks
> >
> > Gerard
> >
> >
> > **********************************************************************************
> > The information transmitted is intended only for the person or
> > entity to which it is addressed and may contain confidential and/or
> > privileged material. Any review, retransmission, dissemination or
> > other use of, or taking of any action in reliance upon, this
> > information by persons or entities other than the intended recipient
> > sender and delete the material from any computer.  It is the policy
> > of the Department of Justice, Equality and Law Reform and the
> > Agencies and Offices using its IT services to disallow the sending
> > of offensive material.
> > Should you consider that the material contained in this message is
> > offensive you should contact the sender immediately and also
> > mailminder[at]justice.ie.
> >
> > Is le haghaidh an duine nó an eintitis ar a bhfuil sí dírithe, agus
> > le haghaidh an duine nó an eintitis sin amháin, a bheartaítear an
> > fhaisnéis a tarchuireadh agus féadfaidh sé go bhfuil ábhar faoi rún
> > agus/nó faoi phribhléid inti. Toirmisctear aon athbhreithniú,
> > atarchur nó leathadh a dhéanamh ar an bhfaisnéis seo, aon úsáid eile
> > a bhaint aisti nó aon ghníomh a dhéanamh ar a hiontaoibh, ag daoine
> > nó ag eintitis seachas an faighteoir beartaithe. Má fuair tú é seo
> > trí dhearmad, téigh i dteagmháil leis an seoltóir, le do thoil, agus
> > scrios an t-ábhar as aon ríomhaire. Is é beartas na Roinne Dlí agus
> > Cirt, Comhionannais agus Athchóirithe Dlí, agus na nOifígí agus na
> > nGníomhaireachtaí a úsáideann seirbhísí TF na Roinne, seoladh ábhair
> > Más rud é go measann tú gur ábhar colúil atá san ábhar atá sa
> > teachtaireacht seo is ceart duit dul i dteagmháil leis an seoltóir
> > láithreach agus le mailminder[ag]justice.ie chomh maith.
> > ***********************************************************************************
> >
> >
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

```