【发布时间】:2014-10-05 23:34:12
【问题描述】:
8,27268
17,7308
29,5468
59,4136
149,3741
299,1438
599,297
749,113
750,44
这些是我的数据,我想找到多项式拟合并将其放入函数中。
【问题讨论】:
-
多项式的什么阶?见this post。
标签: r curve-fitting
8,27268
17,7308
29,5468
59,4136
149,3741
299,1438
599,297
749,113
750,44
这些是我的数据,我想找到多项式拟合并将其放入函数中。
【问题讨论】:
标签: r curve-fitting
如果这是您的示例数据
dd<-data.frame(
x = c(8, 17, 29, 59, 149, 299, 599, 749, 750),
y = c(27268, 7308, 5468, 4136, 3741, 1438, 297, 113, 44)
)
您可以使用poly() 函数以任何顺序拟合多项式。这里我拟合了一个三阶多项式
fit <- lm(y~poly(x,3),dd)
# Call:
# lm(formula = y ~ poly(x, 3), data = dd)
#
# Coefficients:
# (Intercept) poly(x, 3)1 poly(x, 3)2 poly(x, 3)3
# 5535 -14073 8504 -6091
现在我可以绘制结果了
plot(dd)
x <- seq(min(dd$x), max(dd$x), length.out=100)
lines(x, predict(fit, newdata=data.frame(x=x)))
【讨论】: