Flat three-dimensional plane (true regression surface)

I try to simulate some data (x1 and x2 are my explanatory variables), calculate y using the specified function + random noise, and build the observations and the true regression surface. Here is what I still have:

set.seed(1) library(rgl) # Simulate some data x1 <- runif(50) x2 <- runif(50) y <- sin(x1)*x2+x1*x2 + rnorm(50, sd=0.3) # 3D scatterplot of observations plot3d(x1,x2,y, type="p", col="red", xlab="X1", ylab="X2", zlab="Y", site=5, lwd=15) 

Now I'm not sure how to add a “true” regression plane. I'm basically looking for something like curve () where I can connect my (true) model formula.

Thanks!

+4
source share
2 answers

If you need an airplane, you can use planes3d .

Since your model is not linear, it is not a plane: you can use surface3d .

 my_surface <- function(f, n=10, ...) { ranges <- rgl:::.getRanges() x <- seq(ranges$xlim[1], ranges$xlim[2], length=n) y <- seq(ranges$ylim[1], ranges$ylim[2], length=n) z <- outer(x,y,f) surface3d(x, y, z, ...) } library(rgl) f <- function(x1, x2) sin(x1) * x2 + x1 * x2 n <- 200 x1 <- 4*runif(n) x2 <- 4*runif(n) y <- f(x1, x2) + rnorm(n, sd=0.3) plot3d(x1,x2,y, type="p", col="red", xlab="X1", ylab="X2", zlab="Y", site=5, lwd=15) my_surface(f, alpha=.2 ) 

rgl.snapshot

+16
source

Apologies: (I did not read this question very carefully and now I see that I burst into the assessment when you wanted to build the Truth.)

Here's a valuation approach followed by a surface build using loess :

 mod2 <- loess(y~x1+x2) grd<- data.frame(x1=seq(range(x1)[1],range(x1)[2],len=20), x2=seq(range(x2)[1],range(x2)[2],len=20)) grd$pred <- predict(mod2, newdata=grd) grd <- grd[order(grd$x1,grd$x2),] x1 <- unique(grd$x1) x2 <- unique(grd$x2) # shouldn't have used y surface3d(x1, x2, z=matrix(grd$pred,length(x1),length(x2)) ) 

Points and loess fit

+1
source

Source: https://habr.com/ru/post/1496146/


All Articles