绘制 3D 平面(真实回归曲面) [英] Plot 3D plane (true regression surface)
问题描述
我正在尝试模拟一些数据(x1 和 x2 - 我的解释变量),使用指定的函数 + 随机噪声计算 y,并绘制结果观察值和真实回归曲面.这是我到目前为止所拥有的:
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. 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)
现在我不确定如何添加真实"回归平面.我基本上是在寻找类似 curve() 的东西,我可以在其中插入我的(真实)模型公式.
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.
谢谢!
推荐答案
如果你想要一架飞机,你可以使用 planes3d
.
If you wanted a plane, you could use planes3d
.
由于您的模型不是线性的,因此它不是平面:您可以使用 surface3d
代替.
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 )
这篇关于绘制 3D 平面(真实回归曲面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!