r - 使用 predict() 或使用显式拟合方程计算的 y 值之间的差异
问题描述
如果我使用 predict() 函数计算特定 x 值的 y 值,我得到的值与我可以使用显式拟合方程计算的值不同。
我使用 nls(MyEquation) 拟合了下面的数据并获得了 m1, m2,... 参数。然后,我想使用 predict(m) 函数或我用于拟合的显式方程(输入所需的 x 值)来反向计算特定 x 值的 y 值。我为相同的 x 值获得不同的 y 值。哪一个是正确的?
> df
pH activity
1 3.0 0.88
2 4.0 1.90
3 5.0 19.30
4 6.0 70.32
5 7.0 100.40
6 7.5 100.00
7 8.0 79.80
8 9.0 7.75
9 10.0 1.21
x <- df$pH
y <- df$activity
m<-nls(y~(m1*(10^(-x))+m2*10^(-m3))/(10^(-m3)+10^(-x)) - (m5*(10^(-x))+1*10^(-i))/(10^(-i)+10^(-x)), start = list(m1=1,m2=100,m3=7,m5=1))
> m
Nonlinear regression model
model: y ~ (m1 * (10^(-x)) + m2 * 10^(-m3))/(10^(-m3) + 10^(-x)) - (m5 * (10^(-x)) + 1 * 10^(-i))/(10^(-i) + 10^(-x))
data: parent.frame()
m1 m2 m3 m5
-176.032 13.042 6.282 -180.704
residual sum-of-squares: 1522
Number of iterations to convergence: 14
Achieved convergence tolerance: 5.805e-06
list2env(as.list(coef(m)), .GlobalEnv)
#calculate y based on fitting parameters
# choose the 7th x value (i.e. x[7]) that corresponds to pH = 8
# (using predict)
> x_pH8 <- x[7]
> predict(m)[7]
[1] 52.14299
# (using the explicit fitting equation with the fitted parameters
> x1 <- x_pH8
> (m1*(10^(-x1))+m2*10^(-m3))/(10^(-m3)+10^(-x1)) - (m5*(10^(-x1))+1*10^(-8.3))/(10^(-8.3)+10^(-x1))
[1] 129.5284
如您所见: predict(m)[7] 给出 y = 52.14299(对于 x = 8)
尽管
(m1*(10^(-x1))+m2*10^(-m3))/(10^(-m3)+10^(-x1)) - (m5*(10^(-x1)))+ 1*10^(-8.3))/(10^(-8.3)+10^(-x1)) 给出 y = 129.5284(对于 x = 8)
解决方案
您在手动计算中使用的值i
可能与您在模型拟合中使用的值不同。我没有任何差异:
x <- df$pH
y <- df$activity
i <- 8.3
m <- nls(y~(m1*(10^(-x))+m2*10^(-m3))/(10^(-m3)+10^(-x)) - (m5*(10^(-x))+1*10^(-i))/(10^(-i)+10^(-x)), start = list(m1=1,m2=100,m3=7,m5=1))
x <- 8
with(as.list(coef(m)),
(m1*(10^(-x))+m2*10^(-m3))/(10^(-m3)+10^(-x)) - (m5*(10^(-x))+1*10^(-i))/(10^(-i)+10^(-x)))
# [1] 75.46504
predict(m)[7]
# [1] 75.46504
推荐阅读
- android - 我应该如何将 Dagger2 与 Retrofit 一起使用?
- go - 如何在 Go 中创建 AWS Lambda 来处理多个事件
- f# - How to define optional parameters in F# modules?
- c# - 如何使用 Newtonsoft.Json 反序列化 influxDB JSON 查询以使其看起来像一个表?
- docker - 从 Docker Compose 文件中的 Gitlab 注册表中提取图像
- wordpress - 我必须做什么才能在 xampp 中为我的 wordpress 实例创建一个本地域,以便为任何设备访问它(现有的 wordpress 项目)
- excel - 如何停止“复杂”宏(无法用现有主题解决此问题)
- c# - Tortoise svnversion.exe is invalid error in mvc
- node.js - URL 在浏览器中加载,但不在终端中(curl 或 Node.js)
- django - Django模型问题