Using tapply when you have different n for each treatment


Using tapply when you have different n for each treatment



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.


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!!









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter