首页 > 解决方案 > 用于线性拟合的`polyfit`(Matlab)和`lm`(R)之间的区别

问题描述

我正在寻找适合以下数据的线条:x

1 2 3 4

y

0.0000000 0.5376671 2.3715522 0.1127053

从 Matlab 中的代码开始:

coef = polyfit(x,y,1);
best = coef(1)*x+coef(2);

我在 R 中开发了以下代码:

lmResult = lm(y ~ poly(x, 1))
intercept = coef(lmResult)["(Intercept)"]
slope = coef(lmResult)["poly(x, 1)"]
names(intercept) <- NULL
names(slope) <- NULL
best = slope*x+rep(intercept, length(x))

不幸的是,我为数组获得了两个不同的结果best

1.241155 1.726829 2.212504 2.698178

对于 R,

0.4297
0.6469
0.8641
1.0813

对于Matlab ...我无法解释!

标签: rmatlabdata-fitting

解决方案


推荐阅读