tags:

views:

185

answers:

1

I have a situation in which I have to give a formula as input to the nls() function. I have data which is between time and variance. For example:

Time Variance

1 0.15

2 0.23

3 0.67

4 0.85

Now I am using the formula Vt = ((1-e^kt)/kt)) (q^2)/2k, where Vt is the variance at time t. I have the two variables (k,q) in the above equation. I have to determine k(hat) and q(hat). Can I define the above formula as a user-defined formula and give it as an argument to the nls() function?

A: 

Did you try it?

> d = data.frame(t=1:4,V=c(0.15,0.23,0.67,0.85))
> Vt = function(t,k,q) {((1-exp(k*t))/(k*t))*q^2/(2*k)}
> nls(V~Vt(t,k,q),data=d,start=list(k=0.1,q=0.1),trace=TRUE)
1.483693 :  0.1 0.1 
1.099885 :  -7.358501 -3.593847 
Error in numericDeriv(form[[3L]], names(ind), env) : 
  Missing value or an infinity produced when evaluating the model

Doesn't actually work, of course, but that's because I haven't sat down and figured out sensible starting values etc.. (If you're really going to try to fit a 2-parameter nonlinear model to 4 data points, you're going to have lots of hassles. I hope this is just a subsample of your data ...)

In this case of course you could also just write out the formula within nls()

Ben Bolker