首页 > 解决方案 > 特定方程中的回归

问题描述

我有来自现场测试的值。

我需要在下一个等式中进行回归拟合:

y = a x^n + b x

但我尝试使用线性回归、非线性等但不起作用。

示例数据:

+-----+---------+
|  X  |    Y    |
+-----+---------+
|  1  |      7  |
|  3  |    255  |
|  5  |   1895  |
|  8  |  12320  |
+-----+---------+

标签: r

解决方案


首先使用来自您的问题的数据轻轻编辑的 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()

在此处输入图像描述


推荐阅读