0

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.

Matt_4
  • 147
  • 1
  • 12
  • 2
    yes, `fy` seems to be a formula and `v` is an integer. you can't subtract them – YOLO Jul 27 '18 at 12:50
  • I think you just need to remove the `v ~ ` from `fy <- v ~ (theta[1] - ...)`. Not sure why that part is there. – MrFlick Jul 27 '18 at 14:53

0 Answers0