【问题标题】:How can I insert a polynomial fit into a function?如何将多项式拟合插入函数?
【发布时间】:2014-10-05 23:34:12
【问题描述】:
8,27268
17,7308
29,5468
59,4136
149,3741
299,1438
599,297
749,113
750,44

这些是我的数据,我想找到多项式拟合并将其放入函数中。

【问题讨论】:

标签: r curve-fitting


【解决方案1】:

如果这是您的示例数据

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)))

【讨论】:

    猜你喜欢
    • 2016-10-11
    • 2018-03-16
    • 1970-01-01
    • 2010-09-27
    • 2020-03-18
    • 2020-01-24
    • 2010-11-05
    • 2017-02-03
    • 2018-08-18
    相关资源
    最近更新 更多