0

I am an R beginner trying to fit my data to a non-linear regression. Specifically I want to fit the rate of insect development over different temperatures to a power function. Below is my code, modified from the examples at the bottom of this page: https://docs.tibco.com/pub/enterprise-runtime-for-R/3.1.0/doc/html/Language_Reference/stats/selfStart.html

You should be able to paste the code below right into R to get what I get.

LarvaeDevelopment <- structure(list(Larvae_temp = c(10L, 10L, 10L, 10L, 10L, 10L), 
Larvae_rate = c(0.047757234, 0.04174518, 0.04174518, 0.04174518, 0.04174518, 0.04174518)), .Names = c("Larvae_temp", "Larvae_rate"), row.names = c(NA, 6L), class = "data.frame")


    SSpower<-(selfStart(~A*(Larvae_temp^B),
                initial=function(mCall,data,LHS)
                {xy<-sortedXyData(mCall[["Larvae_temp"]],mCall[["Larvae_rate"]],LarvaeDevelopment) ##I think the error appears in this line of code, or the one below
                z<-xy[["Larvae_rate"]]
                aux<-coef(lm(Larvae_temp~z,LarvaeDevelopment))
                pars<-as.vector(coef(nls(Larvae_rate~A*(Larvae_temp^B))),start=list(A=aux[1],B=aux[2]),data=LarvaeDevelopment,algorithm="power")
                value<-list(pars[1],pars[2])
                names(value)<-mCall[c("A","B")]
                value},
                parameters=c("A","B")))
    getInitial(Larvae_rate ~ SSpower(Larvae_temp, A, B), data=LarvaeDevelopment)

When I show traceback, these are the steps that appear:

     Error in tapply(y, x, mean, na.rm = TRUE) : arguments must have same length 
    9. stop("arguments must have same length") 
    8. tapply(y, x, mean, na.rm = TRUE) 
    7. sortedXyData.default(mCall[["Larvae_temp"]], mCall[["Larvae_rate"]], 
LarvaeDevelopment) 
    6. sortedXyData(mCall[["Larvae_temp"]], mCall[["Larvae_rate"]], 
LarvaeDevelopment) 
    5. (attr(object, "initial"))(mCall = mCall, data = data, LHS = LHS) 
    4. getInitial.selfStart(func, data, mCall = as.list(match.call(func, 
call = object[[3L]])), LHS = object[[2L]], ...) 
    3. getInitial(func, data, mCall = as.list(match.call(func, call = object[[3L]])), 
LHS = object[[2L]], ...) 
    2. getInitial.formula(Larvae_rate ~ SSpower(Larvae_temp, A, B), 
data = LarvaeDevelopment) 
    1. getInitial(Larvae_rate ~ SSpower(Larvae_temp, A, B), data = LarvaeDevelopment) 

I think the problem might come from the fact that the n of each treatment is unequal. For example, n= 58,165,113,26 for temperatures 10, 15, 20, 30. The corresponding y-values (Larvae_rate) match each x-values (Larvae_temp). Does anybody know how to fix this?

I'd also really appreciate it if someone could look over my code and see if it makes sense! I am really new to R still and debugging this code has taken over my life.

Please let me know if I can provide more information. Thanks!!

Martin Mächler
  • 4,619
  • 27
  • 27
buglady
  • 1
  • 1

1 Answers1

1

I've edited so now LarvaeDevelopment exists, and we can reproduce your problem.

Short answer: Your SSpower object is has several mistakes and inconsistencies in its Initial function: That should not use your global data object but rather only its arguments, and the formula should not use Larvae_temp but x etc etc..

Also you cite a tibco based mirror of a relatively old version of R (3.1.0) and where the current versions of R have a slightly updated help page for selfStart.

We do need another volunteer for rewriting your definition of SSpower, but believe what I answered to you on R-devel: tapply does work when the number "elements" differ in the ragged array! E.g.,

grps <- as.factor(c(3,5,3,3,6:5))
tapply(1:6, grps, sum)

shows 3 sums, 8,8,5 of 3 and 2 and 1 element, respectively :

3 5 6 
8 8 5 
Martin Mächler
  • 4,619
  • 27
  • 27