Plot 3D plane (true regression surface)

后端 未结 3 2132
天命终不由人
天命终不由人 2020-12-09 07:06

I\'m trying to simulate some data (x1 and x2 - my explanatory variables), calculate y using a specified function + random noise and plot the resulting observations AND the t

3条回答
  •  暖寄归人
    2020-12-09 07:30

    If you wanted a plane, you could use planes3d.

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

    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

提交回复
热议问题