[R] predict assistance

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu May 2 00:21:25 CEST 2002


Michaell Taylor <pols1oh at bestweb.net> writes:

> I have a question regarding application of model coefficients between 
> datasets.  In particular, I have several datasets which I would like to apply 
> a model estimated from only a single dataset (sort of a crude - out of sample 
> application - to show the variances).
> 
> lets say,
> names(a)
> [1] "stdnoi"   "momentum" "tbm3"     "metcons"  "premium"  "STDrfa"   "t10" 
> "Y"
> attach(a)
> mymodel <- lm(Y ~ stdnoi+momentum+tbm3+metcons+premium+ STDrfa +   t10)
> detach(a)
> load('b')
> names(b)
> [1] "stdnoi"   "momentum" "tbm3"     "metcons"  "premium"  "STDrfa"   "t10" 
> "Y"
> attach(b)
> b$hat <- predict(mymodel)
> 
> The question really is, is b$hat properly defined in terms of (a)coefficients 
> * (b)variables?
> 
> I have attempted this, and was surprised to see b$hat of the appropriate 
> length for the a dataset - but not the b (which is much larger)
> 
> Obviously I could save out the coefficients, but there are actually several 
> such models and datasets.  Besides, I am generally curious if model objects 
> can be used in such a manner.

A fitted model embodies the data on which it was fitted, so
predict(mymodel) gives you the fitted values on your a data frame
whether you detach it or not. To predict on a new data frame use the
`newdata' argument, as in predict(myplot, newdata=b). 

You can use help(predict.lm) to learn about this and more.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list