我试图将多项式拟合到我的数据集,看起来像那样(完整的数据集在帖子的末尾):
该理论预测曲线的表述为:
看起来像这样(对于x在0和1之间):
当我尝试通过以下方式在R中创建线性模型时:
mod <- lm(y ~ poly(x, 2, raw=TRUE)/poly(x, 2))
Run Code Online (Sandbox Code Playgroud)
这与我的期望大不相同.您是否知道如何根据这些数据拟合一条新曲线,以便它与理论预测的曲线类似?此外,它应该只有一个最小值.
完整数据集:
x值向量:
x <- c(0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.10, 0.11, 0.12,
0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25,
0.26, 0.27, 0.28, 0.29, 0.30, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38,
0.39, 0.40, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.50, 0.51,
0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.60, 0.61, 0.62, 0.63, …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试将多项式曲面拟合到具有3个坐标的一组点。
令数据为:
DATA <- with(mtcars, as.data.frame(cbind(1:32, wt,disp,mpg)))
Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用以下方法绘制表面:
例如:
library(scatterplot3d)
attach(mtcars)
DATA <- as.data.frame(cbind(1:32, wt,disp,mpg))
scatterplot3d(wt,disp,mpg, main="3D Scatterplot")
model <- loess(mpg ~wt + disp, data=DATA)
x <-range(DATA$wt)
x <- seq(x[1], x[2], length.out=50)
y <- range(DATA$disp)
y <- seq(y[1], y[2], length.out=50)
z <- outer(x,y,
function(wt,disp)
predict(model, data.frame(wt,disp)))
z
p <- persp(x,y,z, theta=30, phi=30,
col="lightblue",expand = 0.5,shade = 0.2,
xlab="wt", ylab="disp", zlab="mpg")
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用surf.ls函数:
surf.ls(2,DATA[,2],DATA[,3],DATA[,4])
Run Code Online (Sandbox Code Playgroud)
但是我得到的是这样的:
我真的不知道如何将其转换为3D图,更重要的是,如何获得最适合曲面的公式。
我将衷心感谢您的帮助。
PS我已经删除了我的上一篇文章,并在此文章中包含了更多细节。