将多项式模型拟合到R中的数据 [英] Fitting polynomial model to data in R

查看:160
本文介绍了将多项式模型拟合到R中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关问题的答案,它们非常有用,但是我需要帮助,尤其是在R中。

I've read the answers to this question and they are quite helpful, but I need help particularly in R.

我在R中有一个示例数据集,如下所示:

I have an example data set in R as follows:

x <- c(32,64,96,118,126,144,152.5,158)  
y <- c(99.5,104.8,108.5,100,86,64,35.3,15)

我想对这些数据拟合模型,以便 y = f(x)。我希望它成为三阶多项式模型。

I want to fit a model to these data so that y = f(x). I want it to be a 3rd order polynomial model.

如何在R中做到这一点?

How can I do that in R?

此外,R可以帮助我找到最合适的模型吗?

Additionally, can R help me to find the best fitting model?

推荐答案

要获取x(x ^ 3)中的三阶多项式,您可以

To get a third order polynomial in x (x^3), you can do

lm(y ~ x + I(x^2) + I(x^3))

lm(y ~ poly(x, 3, raw=TRUE))

您可以拟合10阶多项式并获得接近完美的拟合,但是应该吗?

You could fit a 10th order polynomial and get a near-perfect fit, but should you?

编辑:
poly(x,3)可能是一个更好的选择(请参见下面的@hadley)。

poly(x, 3) is probably a better choice (see @hadley below).

这篇关于将多项式模型拟合到R中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆