# self-starting nonlinear power law function in R # Written by C. Scherber, 4th December 2013 # Please acknowledge me should you use this function in publications. # note that response and explanatory variables need to be strictly positive. powermodel=function(x,a,b,c) {a+b*x^c} powermodelInit=function(mCall,LHS,data){ xy=sortedXyData(mCall[["x"]],LHS,data) lmFit1=lm(xy[,"y"]~1) #for "intercept", a lmFit2=lm(log(xy[,"y"])~log(xy[,"x"])) #for b and c coefs1=coef(lmFit1) coefs2=coef(lmFit2) a=coefs1 b=exp(coefs2[1]) c=coefs2[2] value=c(a,b,c) names(value)=mCall[c("a","b","c")] value } SSpower=selfStart(powermodel,powermodelInit,c("a","b","c")) ##