How do I run an exponential nls with seasonal dummies in R?

前端 未结 2 367
南笙
南笙 2021-01-24 01:10

I\'m having trouble with running an nls regression with seasonal dummies in R. I\'m able to do it without the seasonal dummies, but not with. This is what I have so far:

<
2条回答
  •  一个人的身影
    2021-01-24 01:35

    I don't think dummies are implemented for nls like they are in glm due to the fact that "formula" for nls is a real mathematical formula unlike for glm.

    You can nevertheless specify if a parameter must be assessed separately for each class of a dummy:

        data(cars)
        # define the dummy
        cars$dummy <- as.factor(LETTERS[1:5])
        # code as 0/1 the dummy with a column per dummy level
        cars$A<- as.numeric(cars$dummy=="A")
        cars$B<- as.numeric(cars$dummy=="B")
        cars$C<- as.numeric(cars$dummy=="C")
        cars$D<- as.numeric(cars$dummy=="D")
        cars$E<- as.numeric(cars$dummy=="E")
    
        # precise in the formula where the dummy level should play out
        # here in the intercept:
        model <- nls(dist~beta1*speed^beta2+beta3*A+beta4*B+beta5*C+beta6*D+beta7*E,data=cars)
    
        model
    
        Nonlinear regression model
          model: dist ~ beta1 * speed^beta2 + beta3 * A + beta4 * B + beta5 * C + beta6 * D + beta7 * E
          data: cars
          beta1   beta2   beta3   beta4   beta5   beta6   beta7 
          0.2069  1.8580  2.8266  5.3973 13.0002  9.3539  2.5361 
          residual sum-of-squares: 10040
    
          Number of iterations to convergence: 8 
          Achieved convergence tolerance: 4.924e-06
    

提交回复
热议问题