[R] fitting models with the subset argument

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Dec 3 14:00:25 CET 2001


"MUGGEO VITO" <vito.muggeo at giustizia.it> writes:

> Hi all,
> I'd like to fit model where the terms both are in the data.frame, mydata
> say, and are vectors *not in the data.frame*.
> >obj<-glm(y~x, data=mydata) #works
> >Z<-pmax(mydata$x-20,0)
> >(length(Z)==length(obj$y))
> >[1] TRUE
> >update(obj,.~.+Z) #works
> 
> However for some subset it doesn't works:
> >obj<-glm(y~x, data=mydata, subset=f==1) #works
> >Z<-pmax(mydata$x[mydata$f==1]-20,0)
> >(length(Z)==length(obj$y))
> >[1] TRUE
> >update(obj,.~.+Z)  #DOESN'T WORK!!!
> Error in model.frame(formula, rownames, variables, varnames, extras,
> extranames, :
>             variable lengths differ
> 
> Of course also the following doesn't work
> >glm(y~x +Z, mydata, subset=f==1) #doesn't work
> 
> but the following works
> >glm(y[mydata$f==1]~x[mydata$f==1] +Z, mydata)
> 
> How can I solve this problem?

The problem is that subsetting assumes that the variables have the
same length *before* subsetting, but Z is already subsetted. I would
expect that it worked if you just let 

Z<-pmax(mydata$x-20,0)
obj<-glm(y~x, data=mydata, subset=f==1)
update(obj,.~.+Z)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list