Insert equation using nlm in R

180 Views Asked by At

I need to estimate parameters by a non-linear fitting procedure. In particular, I'm trying to fit the following equation:

enter image description here

I thought that nlm could be a good solution, using:

#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)

However I got the error: Error in v - fy : non-numeric argument to binary operator. I'm sure that it's a basic thing but I'm struggling to understand the problem.

0

There are 0 best solutions below