将多项式模型拟合到 R 中的数据
我已阅读此问题的答案,它们非常有帮助,但我需要帮助。
我在 R 中有一个示例数据集,如下所示:
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) 。我希望它是一个三阶多项式模型。
我怎样才能在 R 中做到这一点?
另外,R可以帮助我找到最合适的模型吗?
I've read the answers to this question and they are quite helpful, but I need help.
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)
I want to fit a model to these data so that y = f(x)
. I want it to be a 3rd order polynomial model.
How can I do that in R?
Additionally, can R help me to find the best fitting model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要获得 x (x^3) 中的三阶多项式,您可以执行
或
您可以拟合 10 阶多项式并获得近乎完美的拟合,但您应该这样做吗?
编辑:
poly(x, 3) 可能是更好的选择(参见下面的@hadley)。
To get a third order polynomial in x (x^3), you can do
or
You could fit a 10th order polynomial and get a near-perfect fit, but should you?
EDIT:
poly(x, 3) is probably a better choice (see @hadley below).
哪个模型是“最适合的模型”取决于您所说的“最佳”的含义。 R 有提供帮助的工具,但您需要提供“最佳”的定义才能在它们之间进行选择。考虑以下示例数据和代码:
哪些模型最好?可以对其中任何一个进行论证(但我不想使用紫色的进行插值)。
Which model is the "best fitting model" depends on what you mean by "best". R has tools to help, but you need to provide the definition for "best" to choose between them. Consider the following example data and code:
Which of those models is the best? arguments could be made for any of them (but I for one would not want to use the purple one for interpolation).
关于“R 可以帮助我找到最合适的模型”这个问题,假设您可以声明要测试的模型集,那么可能有一个函数可以做到这一点,但这对于 n-1 组来说是一个很好的第一个方法次数多项式:
注释
此方法的有效性将取决于您的目标、
optimize()
和AIC()
的假设以及 AIC 是否是您的标准polyfit()
可能没有单个最小值。用类似的东西检查一下:我使用了
as.integer()
函数,因为我不清楚如何解释非整数多项式。要测试任意一组数学方程,请考虑 Andrew 审阅的'Eureqa' 程序Gelman 此处
更新
另请参阅
stepAIC
函数(在 MASS 包中)用于自动选择模型。Regarding the question 'can R help me find the best fitting model', there is probably a function to do this, assuming you can state the set of models to test, but this would be a good first approach for the set of n-1 degree polynomials:
Notes
The validity of this approach will depend on your objectives, the assumptions of
optimize()
andAIC()
and if AIC is the criterion that you want to use,polyfit()
may not have a single minimum. check this with something like:I used the
as.integer()
function because it is not clear to me how I would interpret a non-integer polynomial.for testing an arbitrary set of mathematical equations, consider the 'Eureqa' program reviewed by Andrew Gelman here
Update
Also see the
stepAIC
function (in the MASS package) to automate model selection.在 R 中找到最佳拟合的最简单方法是将模型编码为:
使用逐步 AIC 回归后
The easiest way to find the best fit in R is to code the model as:
After using step down AIC regression
例如,如果我们想要拟合 2 次多项式,我们可以直接通过求解线性方程组来实现,方法如下:
以下示例显示如何拟合抛物线 y =使用上述方程计算 ax^2 + bx + c 并将其与
lm()
多项式回归解进行比较。希望这有助于某人的理解,For example, if we want to fit a polynomial of degree 2, we can directly do it by solving a system of linear equations in the following way:
The following example shows how to fit a parabola y = ax^2 + bx + c using the above equations and compares it with
lm()
polynomial regression solution. Hope this will help in someone's understanding,