[R] 2D contour predictions

Spencer Graves spencer.graves at pdf.com
Sun Jul 17 04:53:46 CEST 2005


	  It's not so simple, but consider the following:


x=rep(1:6, each=2, length=24)
y=rep(1:6, each=4)
z=rep(0:1, length=24)
set.seed(1)
tstDF <- data.frame(x=x, y=y, z=z,
            w=(x-3.5)^2+(y-3.5)+z+0.1*rnorm(24))
fit <- lm(w~x+y+z+I(x^2)+I(y^2), tstDF)
x0 <- seq(1, 5, .5)
y0 <- seq(1, 6, .5)
Grid <- expand.grid(x=x0, y=y0)
Grid$z <- rep(0.5, dim(Grid)[1])
pred <- predict(fit, Grid)

contour(x0, y0, array(pred, dim=c(length(x0), length(y0))))

	  This kind of thing is discussed in Venables and Ripley (2002) Modern 
Applied Statistics with S, 4th ed. (Springer).  I highly recommend this 
book.

	  spencer graves

Michael Hopkins wrote:

> 
> Hi All
> 
> I have been fitting regression models and would now like to produce some
> contour & image plots from the predictors.
> 
> Is there an easy way to do this?  My current (newbie) experience with R
> would suggest there is but that it's not always easy to find it!
> 
> f3 <- lm( fc ~ poly( speed, 2 ) + poly( torque, 2 ) + poly( sonl, 2 ) +
> poly( p_rail, 2 ) + poly( pil_sep, 2 ) + poly( maf, 2 ) + (speed + torque +
> sonl + p_rail + pil_sep + maf)^2 )
> 
> hat <- predict( f3 )
> 
> contour( sonl, maf, hat )
> 
> Error in contour.default(sonl, maf, hat) :
>     increasing 'x' and 'y' values expected
> 
> image(sonl, maf, hat)
> 
> Error in image.default(sonl, maf, hat) : increasing 'x' and 'y' values
> expected
> 
> I have tried naïve sorting but no luck with that.
> 
> I suspect I may need to produce a data grid of some kind but I'm not clear
> how I would use R to specify such a 2D slice in the 6D design space.
> 
> TIA
> 
> Michael
> 
> P.S.  Whilst I'm asking the list - is there an easier way of expressing a
> full 2nd order model than the one I used above?  I'm sure there is but
> finding it etc etc...
> 
> 
> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
> 
>         _/    _/   _/_/_/             Hopkins Research Ltd
>        _/    _/   _/    _/
>       _/_/_/_/   _/_/_/          http://www.hopkins-research.com/
>      _/    _/   _/   _/
>     _/    _/   _/     _/               'touch the future'
>                    
> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
> 
> ______________________________________________
> 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

-- 
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915




More information about the R-help mailing list