试图拟合三机制非线性膜污染模型,其包括积分表达式。试图从nls.lm {minpack.lm}帮助页面扩展示例1。问题:如何修改示例1以包含整数表达式?在一些搜索之后进行了各种尝试,这似乎是我能得到的最接近但仍然给我以下错误(“match.fun(f)中的错误:'exp(1)^(xx * parS $ b)'不是函数,字符或符号“)当然不会绘制曲线拟合。非常感谢任何帮助!
require(minpack.lm)
###### example 1 + 'integrate() function
## values over which to simulate data
x <- seq(0,5,length=100)
xo <- x[1]
## two models based on a list of parameters
integrand <- function(parS=pp, xx=x) exp(1)^(xx * parS$b)
getPred <- function(parS, xx) parS$a * exp(xx * parS$b) + parS$c
getPred2 = function(parS, xx) parS$a * exp(xx * parS$b)+ integrate(integrand, lower=xo, upper=xx) + parS$c
## parameter values used to simulate data
pp <- list(a=9,b=-1, c=6)
## simulated data, with noise
simDNoisy <- getPred(pp,x) + rnorm(length(x),sd=.1); simDNoisy
simDNoisy2 <- getPred2(pp,x) + rnorm(length(x),sd=.1); simDNoisy2
## plot data
plot(x,simDNoisy, main="data")
## residual functions
residFun <- function(p, observed, xx) observed - getPred(p,xx)
residFun2 <- function(p, observed, xx) observed - getPred2(p,xx)
## starting values for parameters
parStart <- list(a=3,b=-.001, c=1)
## perform fit
nls.out <- nls.lm(par=parStart, fn = residFun, observed = simDNoisy, xx = x)
nls.out2 <- nls.lm(par=parStart, fn = residFun2, observed = simDNoisy, xx = x)
## plot model evaluated at final parameter estimates
lines(x, getPred(as.list(coef(nls.out)), x), col=2, lwd=2)
lines(x, getPred2(as.list(coef(nls.out2)), x), col=3, lty=2,lwd=2)
## summary information on parameter estimates
summary(nls.out)
summary(nls.out2
)
NRussel建议修改后的代码并包含一个被积函数但仍然出错:“parS $ b:$运算符错误原子向量无效”试图找出原因。谢谢
How to pass vector to integrate function
Roland在链接1中提供的答案有助于确定如何对整体部分进行矢量化;但仍试图弄清楚如何将带参数的积分传递给nls.lm()的预测函数。 修改后的代码如下:
# vectorize integrand and plot of results
integrand <- function(parS, xx) exp(1)^(xx * parS$b)
res = c()
for(ii in 1:100){
res[ii] = integrate(Vectorize(integrand, vectorize.args="xx"), lower=xo, upper=x[ii], parS=pp)
}
inres = unlist(res)
x
plot(x, inres)