[R] invalid variable type in model.frame within a function

Gabor Grothendieck ggrothendieck at gmail.com
Thu Mar 23 17:46:27 CET 2006


Examine the source code of lm to determine the proper
way of doing this.  Following that gives:

makemodelframe <- function(formula,data,weights) {
	mf <- match.call()
	mf[[1]] <- as.name("model.frame")
	eval(mf, parent.frame())
}


On 3/23/06, Ingmar Visser <I.Visser at uva.nl> wrote:
> Dear expeRts,
>
> I came across the following error in using model.frame:
>
> # make a data.frame
> jet=data.frame(y=rnorm(10),x1=rnorm(10),x2=rnorm(10),rvar=rnorm(10))
> # spec of formula
> mf1=y~x1+x2
> # make the model.frame
> mf=model.frame(formula=mf1,data=jet,weights=rvar)
>
> Which gives the desired output:
> > mf
>            y         x1         x2  (weights)
> 1   0.8041254  0.1815366  0.4999551  1.4957814
> 2  -0.2546224  1.9368141 -2.2373186  0.7579341
> 3   0.8627935 -0.6690416  1.3948077 -0.2107092
> 4   0.3951245  0.5733776 -1.2926074 -0.3289226
> 5  -1.4805766 -0.6113256  1.1635959  0.2300376
> 6  -0.7418800 -0.1610305  0.4057340 -0.2280754
> 7  -1.1420962 -0.9363492 -0.4811192 -0.9258711
> 8   0.3507427  1.8744646  1.3227931  0.5292313
> 9   1.4196519  0.1340283 -1.3970614 -0.7189726
> 10 -1.0164708 -0.2044681 -0.6825873 -0.1719102
>
> However, doing this inside another function like this:
>
> makemodelframe <- function(formula,data,weights) {
>    mf=model.frame(formula=formula,data=data,weights=weights)
>    mf
> }
>
> produces the following error:
>
> > makemodelframe(mf1,jet,weights=rvar)
> Error in model.frame(formula, rownames, variables, varnames, extras,
> extranames,  :
>    invalid variable type
>
> Searching the R-help archives I came across bug-reports about this but
> couldn't figure out whehter the bug was solved or whether there are
> work-arounds available.
>
> platform:
> platform powerpc-apple-darwin7.9.0
> arch     powerpc
> os       darwin7.9.0
> system   powerpc, darwin7.9.0
> status
> major    2
> minor    2.1
> year     2005
> month    12
> day      20
> svn rev  36812
> language R
>
> Any hints are welcome,
> Best, Ingmar
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list