[英]Creating a Smooth Line in 3D R
I have a set of 3-dimensional points, like the sample data below.我有一组 3 维点,如下面的示例数据。 I would like to create a smooth line from it.我想从中创建一条平滑线。 There's information out there about to smooth a 2D surface in 3D space but how would I smooth a 1D line in 3D space?那里有关于在 3D 空间中平滑 2D 表面的信息,但是如何在 3D 空间中平滑 1D 线?
Z = seq(0, 1, 0.01)
X = rnorm(length(Z), mean = 0, sd = 0.1)
Y = 2 * Z ^ 2 + rnorm(length(Z), mean = 0, sd = 0.1)
data = data.frame(X = X, Y = Y, Z= Z)
This is an example of multivariate regression.这是多元回归的一个例子。 If you happen to know that the relationship with Z
should be quadratic, you can do如果你碰巧知道与Z
的关系应该是二次的,你可以这样做
fit <- lm(cbind(X, Y) ~ poly(Z, 2))
But I'm assuming you don't know that, and want some kind of general smoother.但我假设你不知道这一点,并且想要某种更流畅的东西。 I don't think loess
, lowess
, or gam
handle multivariate regression, but you can use natural splines in lm
:我不认为loess
、 lowess
或gam
处理多元回归,但您可以在lm
中使用自然样条曲线:
library(splines)
fit <- lm(cbind(X, Y) ~ ns(Z, df = 4))
The fitted values will be returned in a two-column matrix by predict(fit)
.拟合值将由predict(fit)
在两列矩阵中返回。 To plot the result, you can use rgl
:对于 plot 结果,您可以使用rgl
:
library(rgl)
plot3d(X, Y, Z, col = "red")
lines3d(cbind(predict(fit), Z))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.