Search code examples
rnls

R Power Fit with nls different from excel


I have a data set (below) with a power relationship. (Y =aX^b)

The power fit in Excel and xmgrace gave me an almost identical values for the fit. (R^2 of 0.993) Y = 215.47 X^0.812

However when I try R's nls() function I get a different value. Plus it does not calculate a R^2 because it is not statistically sound.

However if i take logarithms, I can do a lm() and get a R^2 of 0.993. How do I reproduce the values excel and xmgrace produce with power fits using R..Is R nls() not correct??

Drift Time  Mass_Independent CS
2.32    407.3417277
2.32    419.1267553
2.81    503.9859708
2.92    501.0465281
3.78    640.9024985
4.00    688.7906761
4.48    776.3958584
5.67    918.9991003
6.05    949.4448047
6.86    993.9763311
6.86    1064.539603
6.97    1041.422648
7.94    1112.407393
8.42    1183.070416
9.23    1302.622263
9.29    1291.525748

Solution

  • I think you would be foolish to trust an Excel estimate over an R estimate. The failings of Excel in the domain of regression are longstanding and well documented:

     nls(Mass_Ind_CS ~a*Drift_Time^b , dat, start=list(a=100, b=1))
    #---------------------
    Nonlinear regression model
      model:  Mass_Ind_CS ~ a * Drift_Time^b 
       data:  dat 
           a        b 
    227.0176   0.7828 
     residual sum-of-squares: 10224
    
    Number of iterations to convergence: 5 
    Achieved convergence tolerance: 3.617e-06 
    #---------------------
     plot(dat, xlim=range(dat$Drift_Time), ylim=range(dat$Mass_Ind_CS) )
     par(new=T)
     curve(215.47*x^0.812, from=min(dat$Drift_Time), 
                            to=max(dat$Drift_Time),
                             ylim=range(dat$Mass_Ind_CS) )
     par(new=T)
     curve(227.0176*x^0.7828, from=min(dat$Drift_Time), 
                              to=max(dat$Drift_Time), 
                              ylim=range(dat$Mass_Ind_CS),col="red")
    

    The R estimate is plotted in red. It shows that you are wrong to focus on the parameter estimate without looking at the predictions over the range of the x=values. There is no real R-sq to estimate for solitary non-linear models, although you can do model comparisons with anova(). You are welcome to search out the reasons for the author of nls (Douglas Bates) not including them, because it is practically a FAQ on the r-help mailing list.

    enter image description here