[R] rgl 3d surface
Hello. I am trying to plot a 3d surface given its equation. The R code is written in blue. So, let's say that I have the points x,y,z and I plot them. Also, I compute its regression surface doing polynomical regression (fit) library('rgl') x <- c(-32.09652, -28.79491, -25.48977, -23.18746,-20.88934, -18.58220, -17.27919) y <- c(-32.096, -28.794, -25.489, -23.187,-20.889, -18.582, -17.279) z <- c(12.16344, 28.84962, 22.36605, 20.13733, 79.50248, 65.46150,44.52274) plot3d(x,y,z, type="s", col="red", size=1) fit <- lm(z ~ poly(x,2) + poly(y,2)) In this way, I obtain the coefficients of the surface coef(fit) (Intercept) poly(x, 2)1 poly(x, 2)2 3.900045e+01 1.763363e+06 6.683531e+05 poly(y, 2)1 poly(y, 2)2 -1.763303e+06 -6.683944e+05 So I want to repressent the surface 3.900045e+01 +1.763363e+06*x + 6.683531e+05*x*x -1.763303e+06*y-6.683944e+05*y*y How could I do it? Any idea?? Thank you very much! -- Aurora González Vidal Sección Apoyo Estadístico. Servicio de Apoyo a la Investigación (SAI). Vicerrectorado de Investigación. Universidad de Murcia Edif. SACE . Campus de Espinardo. 30100 Murcia @. aurora.gonzal...@um.es T. 868 88 7315 F. 868 88 7302 www.um.es/sai www.um.es/ae [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] rgl 3d surface
On 15/07/2015 7:16 AM, AURORA GONZALEZ VIDAL wrote: > Hello. > > I am trying to plot a 3d surface given its equation. The R code is written > in blue. > So, let's say that I have the points x,y,z and I plot them. Also, I compute > its regression surface doing polynomical regression (fit) > > library('rgl') > x <- c(-32.09652, -28.79491, -25.48977, -23.18746,-20.88934, -18.58220, > -17.27919) > y <- c(-32.096, -28.794, -25.489, -23.187,-20.889, -18.582, -17.279) > z <- c(12.16344, 28.84962, 22.36605, 20.13733, 79.50248, 65.46150,44.52274) > plot3d(x,y,z, type="s", col="red", size=1) > > fit <- lm(z ~ poly(x,2) + poly(y,2)) > > In this way, I obtain the coefficients of the surface > > coef(fit) > > (Intercept) poly(x, 2)1 poly(x, 2)2 > 3.900045e+01 1.763363e+06 6.683531e+05 > poly(y, 2)1 poly(y, 2)2 > -1.763303e+06 -6.683944e+05 > > So I want to repressent the surface > 3.900045e+01 +1.763363e+06*x + 6.683531e+05*x*x > -1.763303e+06*y-6.683944e+05*y*y > > How could I do it? Any idea?? > > Thank you very much! You need to write a function f of x and y that produces the fitted values. I haven't checked, but I'd assume it needs to take vector inputs and produce a vector of responses. Then persp3d(f) will draw the surface. See ?persp3d.function for details on setting the x and y ranges, etc. Duncan Murdoch __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] rgl 3d surface
> -Original Message- > > I compute its regression surface doing polynomical regression (fit) > > ... > > fit <- lm(z ~ poly(x,2) + poly(y,2)) > > . > > So I want to repressent the surface > > How could I do it? Any idea?? > > You need to write a function f of x and y that produces the fitted values. I > haven't checked, but I'd assume it needs to take vector inputs and produce a > vector of responses. Perhaps worth noting that since the fit was produced by lm(), predict() will generate the surface values without a separate function. For example, if we said something like [sorry, not yet checked] xvals <- seq(-35, -15, 0.5) yvals <- seq(-35, -15, 0.5) new.data <- data.frame(x=rep(xvals, each=length(yvals)), y=rep(yvals, length(xvals) ) new.data$z <- predict(fit, newdata=new.data) #This should generate the predicted surface #Then: with(new.data, persp3d(x, y, z)) #ought to do the job. > Then > > > persp3d(f) > > will draw the surface. See ?persp3d.function for details on setting the x > and y > ranges, etc. > > Duncan Murdoch > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. *** This email and any attachments are confidential. Any use...{{dropped:8}} __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.