Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I show you the principle. I am not sure I used the correct equations, but since you wrote about 4 parameters, I think this is what you mean.</p> <p>I did not check for errors and leave that to you. You should also do the usual diagnostics that should be used when doing non-linear regression.</p> <pre><code>fitfun&lt;-function(p,d,a,b,c,d1){ beta_1 &lt;- 2-b-c beta_2 &lt;- -(1-c)*(1-b) beta_3 &lt;- c*d1 + b*a beta_4 &lt;- -c*d1*(1-b) - b*a*(2-c) beta_5 &lt;- b*a*(1-c) res &lt;- as.numeric(filter(p,c(0,beta_1,beta_2),sides=1)) + #use filter to calculate lagged values as.numeric(filter(d,c(0,beta_3,beta_4,beta_5),sides=1)) + c res[-(1:3)] #to remove NAs } pfit&lt;-df$p0[-(1:3)] fit&lt;-nls(pfit ~ fitfun(p0,d1,a,b,c,d),data=df,start=list(a=2,b=1,c=1,d=1)) summary(fit) Formula: pfit ~ fitfun(p0, d1, a, b, c, d) Parameters: Estimate Std. Error t value Pr(&gt;|t|) a 0.13858 0.62064 0.223 0.823 b 0.21101 0.01594 13.237 &lt;2e-16 *** c 1.41492 0.03015 46.929 &lt;2e-16 *** d 0.09838 0.09098 1.081 0.280 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 6.799 on 1043 degrees of freedom Number of iterations to convergence: 7 Achieved convergence tolerance: 5.107e-06 </code></pre> <h1>Edit</h1> <p>I show quickly how <code>filter</code> works.</p> <pre><code>x &lt;- 1:10 #[1] 1 2 3 4 5 6 7 8 9 10 filter(x,c(0,1),sides=1) #1*x_{i-1} #[1] NA 1 2 3 4 5 6 7 8 9 filter(x,c(0,0,2),sides=1) #2*x_{i-2} #[1] NA NA 2 4 6 8 10 12 14 16 filter(x,c(1,2,3),sides=1) #1*x_i + 2*x_{i-1} + 3*x_{i-2} #[1] NA NA 10 16 22 28 34 40 46 52 </code></pre> <p><code>filter</code> is a time series function and thus pretty fast.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload