[R] Odd behaviour with scale()

Anon. bob.ohara at helsinki.fi
Wed Oct 20 15:56:48 CEST 2004


Moi!

A student here has been getting a bit irritated with some side effects 
of scale() (OS is Windows XP, the behaviour occurs in R 2.0.0, but not 
1.7.1).  The problem is that she scales a variable in a data frame, then 
does a regression, and tries to get some predictions for some new data.  
However, at this point she gets an error (see the example below).  This 
seems to be because the scaled variable in the new data frame does not 
have the center and scale attributes, but the one in the old data frame 
does.

The work-around is to put the scaled variable intro a new data frame, 
which again won't have the attributes.  But it seems odd to me that 
whether a scale()'d variable has attributes depends on where it's 
placed.  I presume that this is because I'm not understanding something 
about the way R is working, rather than it being a bug.  Would anyone 
care to enlighten me?

 > Data1=data.frame(xx=1:10, yy=2.1:12)
 > Data1$xx=scale(Data1$xx)
 >
 > reg1=lm(yy~xx, data=Data1)
 > New=data.frame(xx=2:4)
 > b=predict(reg1, New, se.fit=T)
Error: variable 'xx' was fitted with nmatrix.1 but numeric was supplied
 >
 > New=data.frame(xx=scale(2:4, center=5.5, scale=3.02))
 > b=predict(reg1, New, se.fit=T)
Error: variable 'xx' was fitted with nmatrix.1 but numeric was supplied
 >

Bob

-- 
Bob O'Hara
Department of Mathematics and Statistics
P.O. Box 68 (Gustaf Hällströmin katu 2b)
FIN-00014 University of Helsinki
Finland

Telephone: +358-9-191 51479
Mobile: +358 50 599 0540
Fax:  +358-9-191 51400
WWW:  http://www.RNI.Helsinki.FI/~boh/
Journal of Negative Results - EEB: www.jnr-eeb.org




More information about the R-help mailing list