Fit multiple Gompertz curves and skip errors in R (nlsList and SSgompertz)

China☆狼群 提交于 2021-01-28 05:11:04

问题


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:


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


来源:https://stackoverflow.com/questions/35828768/fit-multiple-gompertz-curves-and-skip-errors-in-r-nlslist-and-ssgompertz

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!