如何解决R中的非线性曲线拟合
我正在 R 中进行非线性曲线拟合,但我无法弄清楚如何使用变量来约束数据... 下面是一个例子:
dat<-read.table(text="time y
1 4.62
2 13.55
3 30.82
6 93.97
12 145.93
24 179.93",header = TRUE)
plot(data);lines(data)
model <- nls(y ~ Max * (1-exp(-k * (time - Lag))),data=dat,start=list(Max = 200,k = 0.1,Lag = 0.5))
我想包括 if (time - Lag)
解决方法
右手边乘以(time > Lag)
st <- list(Max = 200,k = 0.1,Lag = 0.5)
model <- nls(y ~ (time > Lag) * Max * (1-exp(-k * (time - Lag))),data = dat,start = st)
model
## Nonlinear regression model
## model: y ~ (time > Lag) * Max * (1 - exp(-k * (time - Lag)))
## data: dat
## Max k Lag
## 185.6447 0.1523 1.6016
## residual sum-of-squares: 65.06
##
## Number of iterations to convergence: 4
## Achieved convergence tolerance: 2.016e-07
plot(dat)
lines(fitted(model) ~ time,dat,col = "red")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。