[R] case weights-coxph (solved)

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Sep 18 13:51:58 CEST 2001

Nicholas Lewin-Koh <kohnicho at comp.nus.edu.sg> writes:

> Hi,
> The following function does work
> optimize.W<-function(W,k,G,Groups,cph.call,z){
>   n<-length(Groups)
>   grp.wt<-rep(0,n)
>   for(i in 1:(length(G))){
>     ind<-Groups == G[i]
>     if(G[i]!=k){
>       grp.wt[ind]<-W[i]
>     }
>     elsegrp.wt[ind]<-1
>   }
>   z<-data.frame(cbind(z,grp.wt=grp.wt)) #needed to make the case weights
>                                         #part of the data frame
>   mod<-coxph(cph.call,z,grp.wt,na.action=na.omit,singular.ok=T)
>   sum(mod$residuals^2)
> }
> I wouldn't call this a bug, but shouldn't the case weights be a separate
> vector if the user so desires?

It's a side effect of the new model environment concept, combined with
the fact that certain arguments like weight have non-standard
evaluation rules. These arguments are evaluated in the data frame
enclosed by the model environment - which is generally the environment
where you defined the model formula. 

As things are implemented currently, your solution is probably the
best one (z$grp.wt <- grp.wt might be more efficient though). Another
one would be to defeat the nonstandard evaluation with stuff like


which I'll be the first to admit is horrendously ugly but avoid's the
copying implied by adding variables to 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