1
votes

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))
1
Write a loop that calls nls with tryCatch.Roland
That's not correct usage of tryCatch. Study this: stackoverflow.com/a/12195574/1412059Roland
Btw. if you provided a minimal reproducible example I might show you how to do this.Roland
Sorry for not doing this earlier, I've added some example data now.Ian

1 Answers

1
votes

nlsList already uses try internally. Your problem appears to be the na.action setting (na.fail is the default). Use na.omit:

nlsList(y~SSgompertz(x, Asym, xmid, scal)|GROUPING, data=data, na.action = na.omit)
#Call:
#  Model: y ~ SSgompertz(x, Asym, xmid, scal) | GROUPING 
#   Data: data 
#
#Coefficients:
#       Asym     xmid     scal
#1   618.774 2.031473 0.831752
#643 618.774 2.031473 0.831752

Degrees of freedom: 16 total; 10 residual
Residual standard error: 30.44042