I'm trying to fit hundreds of gompertz-shaped curves using SSgompertz. The dataset has three columns with "x" and "y" values and a coded column to separate the data into different samples: "GROUPING". Later, the parameters will be used to determine x from a fixed point on the y-axis for all samples (fit point methods) using predict().
I managed to fit multiple polynomials to the data before feeding the parameters into predict() using this code:
Parameters<-lmList(x~poly(y,3,raw=TRUE)|GROUPING,data=data, na.action=na.omit)
The fit for many of them wasn't great though. Ideally, I could use non-linear regression to fit the data to a gompertz curve. So I tried this:
Parameters<-nlsList(y~SSgompertz(x, Asym, xmid, scal)|GROUPING, data=dataframe)
However, cases where a fit cannot be obtained (bad samples or atypical curve shapes) cause errors and stop the whole process.
Eg. "iterations exceeded maximum of 50"
Is there a way to ignore samples that do not model, but retain parameters for those that do?
EDIT: I have tried using a loop as suggested, but I’m having trouble getting it to work (see script below). Also the output cannot be fed into coef()
uniq <- unique(unlist(data$GROUPING))
results=list()
for (i in uniq){
Singledata <-data[which(data$GROUPING ==uniq[i]), ]
x<-Singledata$x
y<-Singledata$y
ModelSS <- tryCatch(nls(y~SSgompertz(x, Asym, xmid, scal)))
print(ModelSS)
results[i] = ModelS
}
coef(results)
Can someone please help me understand where I'm going wrong?
Example data:
data<-data.frame(x=c(0,1,2,4,8,16,32,64,0,1,2,4,8,16,32,64,0,1,2,4,8,16,32,64),
y=c(70,90,160,250,410,510,610,650,
NA,NA,NA,NA,NA,NA,NA,NA,
70,90,160,250,410,510,610,650),
GROUPING=c(1,1,1,1,1,1,1,1,
45,45,45,45,45,45,45,45,643,643,643,643,643,643,643,643))
nls
withtryCatch
. – RolandtryCatch
. Study this: stackoverflow.com/a/12195574/1412059 – Roland