1

假设我有一个 3D 点云,表示为:

arv = data.frame(axis_x = rnorm(n=300, mean=-0.20, sd=1.01),
             axis_y = rnorm(n=300, mean=-0.45, sd=0.97),
             elevation = rnorm(n=300, mean=-813.2, sd=13.89))

而且我还有一个抛物面模型:

model = lm(formula = elevation ~ (axis_x + axis_y)^2 + I(axis_x^2) + I(axis_y^2), data = arv)

是否有某种方法可以在 3D 图中将两者(点和模型)一起绘制?

4

1 回答 1

5

这可以通过persp()和来完成trans3d。为了提高清晰度,它有助于用线将观察值连接到函数的 3d 表面:

# data
arv = data.frame(axis_x = rnorm(n=300, mean=-0.20, sd=1.01),
             axis_y = rnorm(n=300, mean=-0.45, sd=0.97),
             elevation = rnorm(n=300, mean=-813.2, sd=13.89))
# fit             
model = lm(formula = elevation ~ (axis_x + axis_y)^2 + I(axis_x^2) + I(axis_y^2), data = arv)

# grid for plotting function
x <- seq(min(arv$axis_x), max(arv$axis_x), length.out = 20)
y <- seq(min(arv$axis_y), max(arv$axis_y), length.out = 20)

# function for predicted surface from model
f <- function(x, y) { cbind(1,x,y,x^2,y^2,x*y) %*% coef(model) }

# predicted values in form that persp() can use
z <- outer(x, y, f)

# 3d plot of surface with persp()
ele_3d <- persp(x=x,y=y,z=z, theta=40, phi=15, zlim=c(min(arv$elevation), max(arv$elevation)) )

# transform observed values into 2d space
elevation_points <- trans3d(arv$axis_x, arv$axis_y, arv$elevation, pmat=ele_3d)

# plot observed values
points(elevation_points)

# add dotted lines from surface to observed values
fit_vals <- trans3d(arv$axis_x, arv$axis_y, fitted(model), pmat = ele_3d)
segments(fit_vals$x, fit_vals$y, elevation_points$x, elevation_points$y, lty = 3)

另一种选择是在格中使用面板功能wireframe()。看到这个帖子

于 2013-09-14T19:38:23.943 回答