[R] Problem: Update of glm-object cannot find where the data object is located

Lam Phung Khanh lampk at oucru.org
Wed Jun 29 11:49:10 CEST 2011


Hi everybody,

I want to ask your help to explain what is going on with my following
code:

> mydata <- data.frame(y=rbinom(100, 1, 0.5), x1=rnorm(100),
x2=rnorm(100))

> glm.fit.method <-
function(model,data,...){glm(formula=model,data=data,family="binomial",.
..)}

> fit1 <- glm(y ~ x1 + x2, data=mydata, family=binomial())
> update(fit1, .~1)

Call:  glm(formula = y ~ 1, family = binomial(), data = mydata)

Coefficients:
(Intercept)  
   -0.04001  

Degrees of Freedom: 99 Total (i.e. Null);  99 Residual
Null Deviance:       138.6 
Residual Deviance: 138.6              AIC: 140.6 

> fit2 <- glm.fit.method(y ~ x1 + x2, data=mydata)
> update(fit2, .~1)

Error in as.data.frame.default(data, optional = TRUE) : 
  cannot coerce class '"function"' into a data.frame

One might expect that model 1 and model 2 are the same. So, it is
strange when this error occured. We can fix it easily by telling
"update" which data to evaluate:

> update(fit2, .~1, data=mydata)

Call:  glm(formula = y ~ 1, family = "binomial", data = mydata)

Coefficients:
(Intercept)  
   -0.04001  

Degrees of Freedom: 99 Total (i.e. Null);  99 Residual
Null Deviance:       138.6 
Residual Deviance: 138.6              AIC: 140.6 

I guest the problem may due to the fact that "update" requires
specifying which data is updated or the dataset named "data". fit1 and
fit2 just differ in a very small point:
> fit1$call
glm(formula = y ~ x1 + x2, family = binomial(), data = mydata)
> fit2$call
glm(formula = model, family = "binomial", data = data)

I am looking forward to your response,

Thanks,

Lam Phung Khanh
PhD student
Centre for Tropical Medicine
Oxford University Clinical Research Unit
190 Ben Ham Tu, Quan 5, Ho Chi Minh City
Vietnam
E-mail: lampk at oucru.org

P/S: Detail of my current R version is

R version 2.13.0 (2011-04-13)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

loaded via a namespace (and not attached):
[1] tools_2.13.0



More information about the R-help mailing list