0

我需要通过非线性拟合程序来估计参数。特别是,我试图拟合以下等式:

在此处输入图像描述

我认为这nlm可能是一个很好的解决方案,使用:

#example data
df <- data.frame(var= cumsum(sort(rnorm(100, mean=20, sd=4))),
                 time= seq(strptime("2018-1-1 0:0:0","%Y-%m-%d %H:%M:%S"), by= dseconds(200),length.out=100))
#write function
my_fun <- function(v, vin, t,theta){
  fy <- v ~ (theta[1]-(theta[1]- vin)*exp((-theta[2]/10000)*(t-theta[3])))
  ssq<-sum((v-fy)^2)
  return(ssq)
}
#run nlm
th.start <- c(7000, 1000, 10)
my_fit <- nlm(f=my_fun, vin=400, v = df$var,
           t=df$time,p=th.start)

但是我得到了错误:Error in v - fy : non-numeric argument to binary operator。我确信这是一件基本的事情,但我很难理解这个问题。

4

0 回答 0