使用基本 R 包将非线性方程拟合到数据
Fitting non-linear equation to data using base R package
假设我有以下数据
Data = structure(list(col1 = c(31, 66, 88, 123, 249, 362, 488, 610,
730, 842), col2 = c(2101.58953918969, 2103.57391509821, 2100.3292541732,
2101.64107993765, 2100.51743895393, 2100.16708521627, 2102.1992412748,
2101.06516854423, 2101.87929065226, 2101.25318636023)), row.names = c(NA,
-10L), class = "data.frame")
现在我想拟合如下所示的非线性方程 -
library(stats)
nls(col2 ~ x1 + x2 / (1 + exp(-x3 * (col1 - x4))), data = Data, start = list(x1 = 0, x2 = 0, x3 = 0, x4 = 0), algorithm = "plinear")
然而,我遇到了以下错误 -
Error in qr.qty(QR.rhs, .swts * ddot(attr(rhs, "gradient"), lin)) :
NA/NaN/Inf in foreign function call (arg 5)
能否请您帮助我了解我的方法出了什么问题?
我只想使用基础包来满足这个等式,因为我无法在我的系统中从互联网下载任何贡献的包。
任何指点将不胜感激。
如果我使用 SSfpl
与您当前的数据,我可以得到答案。
n1 <- nls(col2 ~ SSfpl(col1, A, B, m, s), data=Data)
pframe <- data.frame(col1=seq(0,900,length=101))
pframe$col2 <- predict(n1, newdata=pframe)
library(ggplot2); theme_set(theme_bw())
ggplot(Data, aes(col1,col2)) + geom_point() + geom_smooth() +
geom_line(data=pframe, colour="red")
参数化和你的不太一样:
A B m s
2001.56354 2002.06645 642.30178 20.76013
基于x1 + x2 / (1 + exp(-x3 * (col1 - x4)))
、
我相信x4
= m
(中点),x3
= s
(尺度),x1
= A
(左渐近线), x2
= B-A
(B
是右渐近线).
有几个问题:
- 当使用 plinear 时,右侧应该是一个矩阵,该矩阵的第 i 列乘以线性输入的第 i 个参数,并且这些线性参数不应有起始值。线性参数将报告为 .lin1 和 .lin2
- 需要更好的非线性参数起始值。
nls(col2 ~ cbind(1, 1 / (1 + exp(-x3 * (col1 - x4)))), data = Data,
start = list(x3 = sd(Data$col1), x4 = mean(Data$col1)), algorithm = "plinear")
给予:
Nonlinear regression model
model: col2 ~ cbind(1, 1/(1 + exp(-x3 * (col1 - x4))))
data: Data
x3 x4 .lin1 .lin2
295.3813 358.9000 2101.5302 -0.2175
residual sum-of-squares: 9.145
Number of iterations to convergence: 0
Achieved convergence tolerance: 0
假设我有以下数据
Data = structure(list(col1 = c(31, 66, 88, 123, 249, 362, 488, 610,
730, 842), col2 = c(2101.58953918969, 2103.57391509821, 2100.3292541732,
2101.64107993765, 2100.51743895393, 2100.16708521627, 2102.1992412748,
2101.06516854423, 2101.87929065226, 2101.25318636023)), row.names = c(NA,
-10L), class = "data.frame")
现在我想拟合如下所示的非线性方程 -
library(stats)
nls(col2 ~ x1 + x2 / (1 + exp(-x3 * (col1 - x4))), data = Data, start = list(x1 = 0, x2 = 0, x3 = 0, x4 = 0), algorithm = "plinear")
然而,我遇到了以下错误 -
Error in qr.qty(QR.rhs, .swts * ddot(attr(rhs, "gradient"), lin)) :
NA/NaN/Inf in foreign function call (arg 5)
能否请您帮助我了解我的方法出了什么问题?
我只想使用基础包来满足这个等式,因为我无法在我的系统中从互联网下载任何贡献的包。
任何指点将不胜感激。
如果我使用 SSfpl
与您当前的数据,我可以得到答案。
n1 <- nls(col2 ~ SSfpl(col1, A, B, m, s), data=Data)
pframe <- data.frame(col1=seq(0,900,length=101))
pframe$col2 <- predict(n1, newdata=pframe)
library(ggplot2); theme_set(theme_bw())
ggplot(Data, aes(col1,col2)) + geom_point() + geom_smooth() +
geom_line(data=pframe, colour="red")
参数化和你的不太一样:
A B m s
2001.56354 2002.06645 642.30178 20.76013
基于x1 + x2 / (1 + exp(-x3 * (col1 - x4)))
、
我相信x4
= m
(中点),x3
= s
(尺度),x1
= A
(左渐近线), x2
= B-A
(B
是右渐近线).
有几个问题:
- 当使用 plinear 时,右侧应该是一个矩阵,该矩阵的第 i 列乘以线性输入的第 i 个参数,并且这些线性参数不应有起始值。线性参数将报告为 .lin1 和 .lin2
- 需要更好的非线性参数起始值。
nls(col2 ~ cbind(1, 1 / (1 + exp(-x3 * (col1 - x4)))), data = Data,
start = list(x3 = sd(Data$col1), x4 = mean(Data$col1)), algorithm = "plinear")
给予:
Nonlinear regression model
model: col2 ~ cbind(1, 1/(1 + exp(-x3 * (col1 - x4))))
data: Data
x3 x4 .lin1 .lin2
295.3813 358.9000 2101.5302 -0.2175
residual sum-of-squares: 9.145
Number of iterations to convergence: 0
Achieved convergence tolerance: 0