0

I have been working in R with nonlinear models such us:

Y = Alpha1*time + Alpha2*sin(2*pi*time/Alpha3) + Alpha4*(-1)^time

And I would line whether a bernoulli variable affects to the Alpha1*time and intercept or not. Such bernoulli variable could be:

varia<-rep("BEFORE","AFTER"),each=30)

Having

Y<- -2.5+rnorm(60)+2*sin(2*pi*time/8)+2.5*(-1)^time
time<-seq(1,60)

I found that nls can fit this model but without the effect of this categorical variable, I also found that the nlme package can estimate effects over variables not over terms of the nonlinear model. My question is: Which of these package could help me? and, how could I add this bernoulli variable in the code? Regards and thanks.

Hector
  • 1
  • 2

1 Answers1

1

Below .lin4 will be an amount that AFTER adds to Y:

# test data
set.seed(123)
time<-seq(1,60)
Y<- -2.5+rnorm(60)+2*sin(2*pi*time/8)+2.5*(-1)^time
varia <- rep(c('BEFORE', 'AFTER'), each = 30)

fm <- nls(Y ~ cbind(1, sin(2 * pi * time/Alpha3), (-1)^time, varia == "AFTER"), 
 start = list(Alpha3 = 8), alg = "plinear")

summary(fm)

giving:

Formula: Y ~ cbind(1, sin(2 * pi * time/Alpha3), (-1)^time, (varia == 
    "AFTER"))

Parameters:
       Estimate Std. Error t value Pr(>|t|)    
Alpha3  8.03367    0.02583 310.984  < 2e-16 ***
.lin1  -2.55242    0.16740 -15.247  < 2e-16 ***
.lin2   1.88528    0.16726  11.272 6.45e-16 ***
.lin3   2.53734    0.11828  21.453  < 2e-16 ***
.lin4   0.23992    0.23673   1.013    0.315    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.9159 on 55 degrees of freedom

Number of iterations to convergence: 3 
Achieved convergence tolerance: 6.226e-07

so in this case all parameters are significant except the last one, as expected.

REVISED Revised after re-reading the question.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341