r - 用于线性拟合的`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 ...我无法解释!
解决方案
推荐阅读
- java - OpenCv 3.4.1 FaceRecognizer 在 Java 中不起作用。创建对象时出现异常
- xml - Xpath:第一个前面和后面的兄弟
- python-3.x - 如何在python中验证签名文件
- php - 如何以最有效的方式读取 5gb+ .sql 文件
- python - 替换 n 维 numpy 数组中特定轴索引的元素
- typescript - [number, number] 等类型的初始值
- xpages - Xpages xe:dialog 在 domino 9.0.1 FP8 中的表单标签之外呈现
- php - 单击按钮时使用 AJAX 删除推文
- excel - 将日期增加天数、月数或年数
- c# - C# - 如何在使另一个对象可见时移动对象