由于 nlsModel 中的错误,for 循环中的 nls 停止
nls in for loop stops due to Error in nlsModel
如果我 运行 一个 for 循环进行多次 nlsLM() 有时由于无用数据 Rstudio 停止并显示以下错误消息:
“nlsModel(公式、mf、开始、wts)中的错误:
初始参数估计的奇异梯度矩阵"
所以我知道不是每个数据集都可以估计拟合参数,但我不希望整个过程停止。
如果没有任何错误,系数存储在
result[x]
- 所以现在我尝试了很多,如果无法拟合 NA 存储在 result[x]
- 但没有成功。
xx <- c(1:10)
result <- vector("numeric")
width<- vector("numeric")
for(x in xx){
temp <- long[ which(long$NR== x), ]
Ytemp <- temp$Yield
Ttemp <- temp$Treatment
M <- nlsLM(Ytemp ~ (maximum + (minimum - maximum)/(1+exp((Ttemp-
x0)/dx))),
start=list(x0=-10, dx=1))
print(summary(M))
result[x] <- coef(M)[1]
width[x] <- coef(M)[2]
}
round(result, digits = 1)
round(width, digits = 2)
mean <- mean(result)
sd <- sd(result)
我很乐意得到提示!
马蒂亚斯
考虑 tryCatch
将 NA
分配给您的向量,以解决模型调用中的任何错误:
xx <- c(1:10)
result <- vector("numeric")
width<- vector("numeric")
for(x in xx) {
temp <- long[which(long$NR== x),]
Ytemp <- temp$Yield
Ttemp <- temp$Treatment
tryCatch({
M <- nlsLM(Ytemp ~ (maximum + (minimum - maximum)/(1+exp((Ttemp-x0)/dx))),
start=list(x0=-10, dx=1))
print(summary(M)) # PRINT MODEL SUMMARY
result[x] <- coef(M)[1] # ASSIGN COEF
width[x] <- coef(M)[2] # ASSIGN COEF
}, error = function(e) {
print(e) # PRINT ERROR MESSAGE
result[x] <- NA # ASSIGN NA
width[x] <- NA # ASSIGN NA
})
}
round(result, digits = 1)
round(width, digits = 2)
mean <- mean(result, na.rm=TRUE) # REMOVE NA
sd <- sd(result, na.rm=TRUE) # REMOVE NA
如果我 运行 一个 for 循环进行多次 nlsLM() 有时由于无用数据 Rstudio 停止并显示以下错误消息: “nlsModel(公式、mf、开始、wts)中的错误: 初始参数估计的奇异梯度矩阵"
所以我知道不是每个数据集都可以估计拟合参数,但我不希望整个过程停止。
如果没有任何错误,系数存储在
result[x]
- 所以现在我尝试了很多,如果无法拟合 NA 存储在 result[x]
- 但没有成功。
xx <- c(1:10)
result <- vector("numeric")
width<- vector("numeric")
for(x in xx){
temp <- long[ which(long$NR== x), ]
Ytemp <- temp$Yield
Ttemp <- temp$Treatment
M <- nlsLM(Ytemp ~ (maximum + (minimum - maximum)/(1+exp((Ttemp-
x0)/dx))),
start=list(x0=-10, dx=1))
print(summary(M))
result[x] <- coef(M)[1]
width[x] <- coef(M)[2]
}
round(result, digits = 1)
round(width, digits = 2)
mean <- mean(result)
sd <- sd(result)
我很乐意得到提示!
马蒂亚斯
考虑 tryCatch
将 NA
分配给您的向量,以解决模型调用中的任何错误:
xx <- c(1:10)
result <- vector("numeric")
width<- vector("numeric")
for(x in xx) {
temp <- long[which(long$NR== x),]
Ytemp <- temp$Yield
Ttemp <- temp$Treatment
tryCatch({
M <- nlsLM(Ytemp ~ (maximum + (minimum - maximum)/(1+exp((Ttemp-x0)/dx))),
start=list(x0=-10, dx=1))
print(summary(M)) # PRINT MODEL SUMMARY
result[x] <- coef(M)[1] # ASSIGN COEF
width[x] <- coef(M)[2] # ASSIGN COEF
}, error = function(e) {
print(e) # PRINT ERROR MESSAGE
result[x] <- NA # ASSIGN NA
width[x] <- NA # ASSIGN NA
})
}
round(result, digits = 1)
round(width, digits = 2)
mean <- mean(result, na.rm=TRUE) # REMOVE NA
sd <- sd(result, na.rm=TRUE) # REMOVE NA