[R] Is there a regression surface demo?

Greg Snow Greg.Snow at imail.org
Tue Oct 12 17:36:59 CEST 2010


For another approach you might want to look at Predict.Plot and TkPredict from the TeachingDemos package.  The code from those could be adapted to do 3d plots.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Joshua Wiley
> Sent: Monday, October 11, 2010 3:03 PM
> To: r-help at r-project.org
> Subject: Re: [R] Is there a regression surface demo?
> 
> Thanks for everyone's responses. Just to follow up, here is a working
> version of my original.  The code is not pretty, but it functions.
> Assuming you have the 'rgl' package installed and have sourced this
> function, here are some examples:
> 
> RegSurfaceDemo(mpg ~ vs + wt, data = mtcars)
> RegSurfaceDemo(mpg ~ vs * wt, data = mtcars)
> RegSurfaceDemo(qsec ~ disp * hp, data = mtcars)
> 
> It cannot handle factors, and the axes labels are hideous...I'll get
> to that eventually.
> 
> Thanks for all your help and suggestions.
> 
> Josh
> 
> RegSurfaceDemo <- function(formula, data, xlim = NULL, ylim = NULL,
>                            zlim = NULL, resolution = 10) {
>  require(rgl)
>  ## This cannot be the proper way to extract variable names from
> formula
>  vars <- rownames(attr(terms(formula), "factors"))
> 
>  ## if no limits set, make them nearest integer to
>  ## .75 the lowest value and 1.25 the highest
>  ranger <- function(x) {
>    as.integer(range(x) * c(.75, 1.25))
>  }
>  if(is.null(xlim)) {xlim <- ranger(data[, vars[2]])}
>  if(is.null(ylim)) {ylim <- ranger(data[, vars[3]])}
>  if(is.null(zlim)) {zlim <- ranger(data[, vars[1]])}
> 
>  ## This does not actually work because the data frame
>  ## does not get named properly (actually it throws an error)
>  f <- function (x, y) {
>    newdat <- data.frame(x, y)
>    colnames(newdat) <- c(vars[2], vars[3])
>    predict(my.model, newdata = newdat)
>  }
> 
>  ## Fit model
>  my.model <- lm(formula = formula, data = data)
> 
>  ## Create X, Y, and Z grids
>  X <- seq(from = xlim[1], to = xlim[2], length.out = resolution)
>  Y <- seq(from = ylim[1], to = ylim[2], length.out = resolution)
>  Z <- outer(X, Y, f)
> 
>  ## Create 3d scatter plot and add the regression surface
>  open3d()
>  with(data = data,
>       plot3d(x = get(vars[2]), y = get(vars[3]), z = get(vars[1]),
>              xlim = xlim, ylim = ylim, zlim = zlim))
>  par3d(ignoreExtent = TRUE)
>  surface3d(X, Y, Z, col = "blue", alpha = .6)
>  par3d(ignoreExtent = FALSE)
>  return(summary(my.model))
> }
> 
> 
> 
> On Mon, Oct 11, 2010 at 1:28 PM, Ista Zahn <izahn at psych.rochester.edu>
> wrote:
> > There is also wireframe() in lattice and bplot in rms.
> >
> > -Ista
> >
> > On Mon, Oct 11, 2010 at 3:49 PM, G. Jay Kerns <gkerns at ysu.edu> wrote:
> >> Dear Josh,
> >>
> >> On Mon, Oct 11, 2010 at 3:15 PM, Joshua Wiley
> <jwiley.psych at gmail.com> wrote:
> >>> Hi All,
> >>>
> >>> Does anyone know of a function to plot a regression surface for two
> >>> predictors?  RSiteSearch()s and findFn()s have not turned up what I
> >>> was looking for.  I was thinking something along the lines of:
> >>> http://mallit.fr.umn.edu/fr5218/reg_refresh/images/fig9.gif
> >>>
> >>> I like the rgl package because showing it from different angles is
> >>> nice for demonstrations.  I started to write my own, but it has
> some
> >>> issues (non functioning code start below), and I figured before I
> >>> tried to work out the kinks, I would ask for the list's feedback.
> >>>
> >>> Any comments or suggestions (about functions or preferred idioms
> for
> >>> what I tried below, or...) are greatly appreciated.
> >>>
> >>> Josh
> >>>
> >>
> >> [snip]
> >>
> >> I haven't tried to debug your code, but wanted to mention that the
> >> Rcmdr:::scatter3d function does 3-d scatterplots (with the rgl
> >> package) and adds a regression surface, one of 4 or 5 different
> types.
> >>  If nothing else, it might be a good place to start for making your
> >> own.
> >>
> >> A person can play around with the different types in the Rcmdr under
> >> the Graphs menu.  Or, from the command line:
> >>
> >> library(Rcmdr)
> >> with(rock, scatter3d(area, peri, shape))
> >>
> >> I hope that this helps,
> >> Jay
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >
> >
> > --
> > Ista Zahn
> > Graduate student
> > University of Rochester
> > Department of Clinical and Social Psychology
> > http://yourpsyche.org
> >
> 
> 
> 
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list