[R] Odd behaviour with scale()

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Wed Oct 20 16:37:10 CEST 2004


Hi Bob,

note that `scale()' returns a matrix! This should work:

Data1$xx <- scale(Data1$xx)
sapply(Data1, data.class)
####
Data1$xx <- c(scale(Data1$xx))
sapply(Data1, data.class)

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Anon." <bob.ohara at helsinki.fi>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, October 20, 2004 3:56 PM
Subject: [R] Odd behaviour with scale()


> 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
>
> ______________________________________________
> 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