# [R] confint.glm in a function

Spencer Graves spencer.graves at pdf.com
Tue Jul 13 04:02:02 CEST 2004

```      I can't get confint.glm to work from within a function.  Consider
the following (using R 1.9.1, Windows 2000):

# FIRST: SOMETHING THAT WORKS FROM A COMMAND PROMPT
DF <- data.frame(y=.1, N=100)
(fit <- glm(y~1, family=binomial, data=DF,
weights=DF[,"N"]))
Call:  glm(formula = y ~ 1, family = binomial, data = DF, weights =
DF[,      "N"])

Coefficients:
(Intercept)
-2.197

Degrees of Freedom: 0 Total (i.e. Null);  0 Residual
Null Deviance:        0
Residual Deviance: -1.11e-14     AIC: 6.052

confint(fit)
> confint(fit)
2.5 % 97.5 %
(Intercept)   NaN    NaN
Warning message:
NaNs produced in: qt(p, df, lower.tail, log.p)
# The default confint thinks it knows glm, but doesn't.  This is fixed
with:
library(MASS)
confint(fit)
Waiting for profiling to be done...
2.5 %    97.5 %
-2.915193 -1.594401
# This is on the logit space.  For proportions:
Waiting for profiling to be done...
2.5 %    97.5 %
0.0514076 0.1687655

# PUT IT IN A FUNCTION:
confint.binom <- function(y="y", size="N", data.=DF){
fit <- glm(y~1, family=binomial, data=data.,
weights=data.[,size])
CI <- confint(fit)
CI
}
> confint.binom()
Waiting for profiling to be done...
Error in model.frame.default(formula = y ~ 1, data = data., weights =
data.[,  :

##To get around this, assign both data. and size to some place where
confint.glm can find them
confint.binom.pos <- function(y="y", size="N", data.=DF,
pos=0){
assign("data.", data., pos)
assign("size", size, pos)
fit <- glm(y~1, family=binomial, data=data.,
weights=data.[,size])
CI <- confint(fit)
CI
}
> confint.binom.pos()
Error in as.environment(pos) : invalid argument

> confint.binom.pos(pos=-1)
Waiting for profiling to be done...
Error in model.frame.default(formula = y ~ 1, data = data., weights =
data.[,  :
> confint.binom.pos(pos=1)
Waiting for profiling to be done...
2.5 %    97.5 %
-2.915193 -1.594401
# This works.
#  HOWEVER, THIS ASSIGNS data. AND size TO THE WORKING DIRECTORY.
# HOW CAN I GET AROUND THIS?

If I had "confint.glm", I could modify it so it could find data.
and size.  However, its hidden.