2
$\begingroup$

I'm trying to develop a non-linear model, but I'd like to have the values for the parameters vary by group. To give you an example, a section of my data (just random numbers here) looks like:

  MONTH FUTURE.PC FUTURE.SALES
1   APR        35         1498
2   APR        22         1124
3   MAY        24          744
4   MAY        45          453
5   MAY        13         1024  
6   JUN        26          689

Say I wanted to make a non-linear model that looked like

nls(FUTURE.SALES ~ a + b * FUTURE.PC ^ c)

Is there a way to change the values of $a$, $b$, and $c$ based on which month the record belongs to? Alternatively, is there a way to incorporate factor variables in non-linear regression in $R$?

$\endgroup$
2
$\begingroup$

Is there a way to change the values of a, b, and c based on which month the record belongs to?

Sure, there are a variety of ways, depending on your model for how they change.

is there a way to incorporate factor variables in non-linear regression in R?

Certainly. Here's an example of one way (there may be better ways):

 x=10:30
 xe=exp((x-18)/3)
 y=50*xe/(1+xe)+rnorm(21)
 plot(x,y)

 b=rep(1:3,times=7)   # these value will be used for my factor variable
 bf=model.matrix(~as.factor(b)) # get the dummies

nls(y~a*exp(b0+b1*x)/(1+exp(b0+b1*x))+b2*bf[,2]+b3*bf[,3],
      start=list(a=52,b0=-5,b1=.3,b2=-.2,b3=-.1))
Nonlinear regression model
  model: y ~ a * exp(b0 + b1*x)/(1 + exp(b0 + b1*x)) + b2 * bf[,2] + b3 * bf[,3]
   data: parent.frame()
      a      b0      b1      b2      b3 
50.4555 -5.8314  0.3245 -0.3729 -0.2740 
 residual sum-of-squares: 18.83

Number of iterations to convergence: 4 
Achieved convergence tolerance: 9.468e-06

In either case, take care, however, with trying to use nls with many parameters.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.