r - 特定方程中的回归
问题描述
我有来自现场测试的值。
我需要在下一个等式中进行回归拟合:
y = a x^n + b x
但我尝试使用线性回归、非线性等但不起作用。
示例数据:
+-----+---------+
| X | Y |
+-----+---------+
| 1 | 7 |
| 3 | 255 |
| 5 | 1895 |
| 8 | 12320 |
+-----+---------+
解决方案
首先使用来自您的问题的数据轻轻编辑的 R 代码中的可重现数据集:
dat <- read.table(text="| X | Y | #you _should_ have offered output from dput(dat)
| 1 | 7 |
| 3 | 255 |
| 5 | 1895 |
| 8 | 12320 |", header=TRUE, sep="|")[2:3] # only the middle 2 columns are useful.
然后您需要知道 R 中的“^”运算符不是 R 公式表达式中的求幂。您将需要 a) 使用该I
函数并+0
省略估计的截距。
n=4; lm( Y ~ I(X^n)+X+0, data=dat)
Call:
lm(formula = Y ~ I(X^n) + X + 0, data = dat)
Coefficients:
I(X^n) X
3 4
如果将 lm 调用的结果分配给my.lm
,则可以查看拟合值plot
> png()
> plot(dat$X, predict(my.lm))
> dev.off()
推荐阅读
- sql - 如何列出所有由杰夫·高布伦主演但不包括布鲁斯·威利斯的电影?
- python - 将图像合并到 z-stack 中并在 Python 中进行最大投影 /
- javascript - 如何将数据导入到 gatbsy-config.js
- python - 如何修复所需的位置参数?
- php - 在 WP Shortcode 中显示来自 mySQL 的数据字段
- r - 'Sum' 函数返回 TRUE 或 FALSE 值而不是数字?
- spring - 更新现有 elasticsearch 实例的集群名称
- ios - UIApplicationDelegate 如何强制其“window”为“internal”而不是“private”,但是当名称更改为“door”时可以吗?
- python - 从另一个数组更新 numpy 2d 数组索引
- reactjs - 如何在 useEffect 挂钩中形成 setFieldValue