[R] Error in model.frame

Thomas Lumley tlumley at u.washington.edu
Sat Aug 16 02:07:32 CEST 2003

On Fri, 15 Aug 2003, Ross Boylan wrote:

> I am getting an error that I don't understand, and wonder if anyone
> could explain what's going on.  I call a function defined thus:
> clogit.rds<-function(formula,data,extra.data,response.prob,
>                  na.action=getOption("na.action"),subset=NULL,
>                  control=coxph.control()){
>     method="exact"  # only option for now
>     mf<-match.call()
>     mf[[1]]<-as.name("model.frame")
>     mf$method<-mf$control<-NULL
>     mfn<-mf
>     mfn$na.action<-"I"
>     mfn$subset<-NULL
>     nrows<-NROW(eval(mfn,parent.frame()))
> etc.
> At the eval on the last line, I get
> Error in model.frame(formula, rownames, variables, varnames, extras,
> extranames,  :
> 	variable lengths differ
> This is puzzling for two reasons.  First, I don't understand where the
> arguments given in the error message are coming from.  They are not in
> my function definition or call, and they do not seem to be the arguments
> to model.frame either.

They are the arguments to .Internal(model.frame( )) inside
model.frame.default. The traceback() function will list the call stack
after an error to avoid confusions like this.

> Second, I don't know why it's not working!  This is modified from clogit
> in survival, and that works fine.  I've added two arguments, extra.data
> (which is just a column of the data) and response.prob.  The latter has
> different dimensions (response.prob = c(1, 1), in fact), but nothing in
> the description of model.frame suggests that should be a problem.

Well, it is a problem.  model.frame() requires all the variables to be the
same length, as you can readily see for yourself
R> x<-1:10
R> y<-1
R> model.frame(y~x)
Error in model.frame(formula, rownames, variables, varnames, extras,
extranames,  :
        variable lengths differ

That's why the error message is "variable lengths differ" :)

The way to stop this is to remove response.prob from the call to
model.frame, ie, do
as is already done for mf$method and mf$control.

> model.frame itself is a wrapper on a primitive, so I can't really debug
> into it.

An Internal, not a primitive (although that doesn't simplify your life
any). Quite a lot of it is interpreted code, though the bit that checks
variable lengths isn't.


More information about the R-help mailing list