简体   繁体   English

绘制 3D 平面(真实回归曲面)

[英]Plot 3D plane (true regression surface)

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 true regression surface.我正在尝试模拟一些数据(x1 和 x2 - 我的解释变量),使用指定的函数 + 随机噪声计算 y 并绘制结果观察值和真实回归曲面。 Here's what I have so far:这是我到目前为止所拥有的:

   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 I can add the "true" regression plane.现在我不确定如何添加“真实”回归平面。 I'm basically looking for something like curve() where I can plug in my (true) model formula.我基本上是在寻找类似 curve() 的东西,我可以在其中插入我的(真实)模型公式。

Thanks!谢谢!

If you wanted a plane, you could use planes3d .如果你想要一架飞机,你可以使用planes3d

Since your model is not linear, it is not a plane: you can use surface3d instead.由于您的模型不是线性的,因此它不是平面:您可以改用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

Apologies: ( I didn't read the question very carefllly and now see that I rushed into estimation when you wanted to plot the Truth.)道歉:(我没有仔细阅读问题,现在看到当你想绘制真相时,我匆忙进行了估计。)

Here's an approach to estimation followed by surface plotting using loess :这是一种估计方法,然后使用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)) )

点和黄土适合

IRTFM's somewhat imperfect answers above let me to a thread on the CRAN help pages. IRTFM 上面有些不完美的答案让我看到了 CRAN 帮助页面上的一个线程。
https://stat.ethz.ch/pipermail/r-help/2013-December/364037.html https://stat.ethz.ch/pipermail/r-help/2013-December/364037.html
I extracted the relevant bits of code and turned them into a function like so:我提取了相关的代码位并将它们转换为如下函数:

require(rgl)
pred.surf.3d <- function(df, x.nm,y.nm,z.nm, ...){
  x <- df[,x.nm]; y <- df[,y.nm]; z<-df[,z.nm]
  fit <- lm(z ~ x + y + x*y + x^2 + y^2)
  xnew <- seq(range(x)[1],range(x)[2],len=20)
  ynew <- seq(range(y)[1],range(y)[2],len=20)
  df <- expand.grid(x=xnew, y=ynew)
  df$z <- predict(fit, newdata=df)
  with(df, surface3d(xnew, ynew, z=df$z))
}

I may end up bundling this into my CRAN utility package at some point.我最终可能会在某个时候将它捆绑到我的 CRAN 实用程序包中。
In the mean time, I hope you find it useful!同时,我希望你觉得它有用! (Run it on IRTFM's first code chunk like so:) (像这样在 IRTFM 的第一个代码块上运行它:)

pred.surf.3d(data.frame(x1,x2,y),'x1','x2','y')

理论多项式二维拟合替代

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM