Skip to content

Error using lm(z~x+y,data)

5 messages · tomsv193@student.liu.se, Duncan Murdoch, Peter Dalgaard

#
Hi!

I am fitting a plane to a surface:

This works excellent:
---------------------------------
x<-dataframe[,1]
y<-dataframe[,2]
z<-dataframe[,3]
data<-cbind(x,y,z)

# Fit plane to surface
result<-lm(z~x+y,data)
---------------------------------

But I need to rescale the x,y parameters. So:
-----------------------------
x<-dataframe[,1]*149
y<-dataframe[,2]*149
z<-dataframe[,3]
data<-cbind(x,y,z)

# Fit plane to surface
result<-lm(z~x+y,data)
----------------------------------

And surprisingly this does not work. Error message is:

Error in eval(attr(formula, "variables"), data, env) : 
        sys.frame: not that many enclosing functions

Why does this happen?


kind regards
Tomas Svensson
EVOTEC,Hamburg


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
tomsv193 at student.liu.se writes:
I can confirm this, but not understand it. Certainly looks like a bug.
#
Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> writes:
Ah, got it! The data= argument needs to be a dataframe and you have a
matrix. Still looks weird, though.
#
I don't know why one version works and the other doesn't, but I've
noticed problems in the past when using "data" as a variable name, and
in fact even the first version fails for me when I type it exactly as
you did.

I suspect the strange behaviour you saw is related to the fact that
data is a matrix rather than a data.frame, and indeed everything is
fine if I change your code to

x<-dataframe[,1]*149
y<-dataframe[,2]*149
z<-dataframe[,3]
d<-data.frame(x,y,z)    # use data.frame, not cbind

# Fit plane to surface
result<-lm(z~x+y,d)

Duncan Murdoch

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Duncan Murdoch <murdoch at stats.uwo.ca> writes:
I got it now: The problem is that eval accept an integer as its envir
argument (as in sys.call()) so lm and friends do too. If you pass a
numeric object, like a matrix, the first element is extracted and
coerced to integer. That'll explain why the behaviour changes when a
column is multiplied by 149...

The reason the example ever worked was that x,y,z from the global env.
got picked up.