Search code examples
rnon-linear-regression

singular gradient matrix at initial parameter estimates - Non Linear Regression


so I'm new to data science using R and I'm not sure what I'm doing wrong here. using the data set:

rocketBurn Oxygen Consumed Hydrogen Used Trajectory Thrust
20 20000 30000 0 500000
40 40000 60000 0 525000
60 60000 90000 0 551250
80 80000 120000 4 578813
100 100000 150000 8 593283
120 120000 180000 12 593283
140 140000 210000 16 593283
160 160000 240000 20 593283
180 180000 270000 24 593283
200 200000 300000 28 593283
220 220000 330000 32 593283
240 240000 360000 36 593283
260 260000 390000 40 593283
280 280000 420000 44 593283
300 300000 450000 46 593283
320 320000 480000 46 593283

I was able to create the linear regression model using the lm() formula in R, when I try to use the nls formula I am recieing the following error message

nlrModel <- nls(Trajectory ~ Oxygen.Consumed + Thrust,data = df,start = c(a=0,b=0,c=0))
Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff, nDcentral = nDcntr) : 
 singular gradient matrix at initial parameter estimates

After some googling I believe the error is due to the start parms, however, I don't know how to get the correct start parm. Any help on how to get this model to work would be greatly appreciated!


Solution

  • The problem is that the formula in the nls model is wrong. lm and nls do not use the same formula notation. In lm for a model with no interactions the independent variables are listed separated by plus signs whereas in nls the formula includes the coefficients. For example, this describes the same model in lm and nls.

    lm(Trajectory ~ Oxygen.Consumed + Thrust, df)
    
    st <- list(a = 1, b = 1, c = 1)
    nls(Trajectory ~ a + b * Oxygen.Consumed + c * Thrust, df, start = st)
    

    Note

    The input in reproducible form is:

    df <- 
    structure(list(rocketBurn = c(20L, 40L, 60L, 80L, 100L, 120L, 
    140L, 160L, 180L, 200L, 220L, 240L, 260L, 280L, 300L, 320L), 
        Oxygen.Consumed = c(20000L, 40000L, 60000L, 80000L, 100000L, 
        120000L, 140000L, 160000L, 180000L, 200000L, 220000L, 240000L, 
        260000L, 280000L, 300000L, 320000L), Hydrogen.Used = c(30000L, 
        60000L, 90000L, 120000L, 150000L, 180000L, 210000L, 240000L, 
        270000L, 300000L, 330000L, 360000L, 390000L, 420000L, 450000L, 
        480000L), Trajectory = c(0L, 0L, 0L, 4L, 8L, 12L, 16L, 20L, 
        24L, 28L, 32L, 36L, 40L, 44L, 46L, 46L), Thrust = c(500000L, 
        525000L, 551250L, 578813L, 593283L, 593283L, 593283L, 593283L, 
        593283L, 593283L, 593283L, 593283L, 593283L, 593283L, 593283L, 
        593283L)), class = "data.frame", row.names = c(NA, -16L))