mangoily的个人博客分享 http://blog.sciencenet.cn/u/mangoily

博文

[转载]R报错之Control the Iterations in nls

已有 4714 次阅读 2016-5-13 10:08 |系统分类:科研笔记

 

 power<-table_all[,i+1]

 len <- nrow(power)

 y<-power

 ds <- data.frame(x = x, y = y)

 ds<-ds[order(ds[,1],decreasing=F),]

 plot(ds[,2])

 rhs <- function(x, b0, b1,b2,b3,b4) {b0*(b3-x)*(x<b3) + b1*(x-b4)*(x>b4)+b2 }

 m.2 <- nls(y ~ rhs(x, cold, hot,cons,L,U), data = ds, start = list(cold = 350, hot = 1600,cons = 5800,L=14,U=25), trace = T)

 parest <- as.data.frame(coef(m.2))

 b_result[i,]<-t(parest)

 summary(m.2)

 s <- seq(-10,35, length =len)

 plot(y ~ x, main = "Fitted power model, with intercept", sub = "Blue: fit; magenta: fit LSE ; green: known")

lines(s, predict(m.2, list(x = s)), lty = 1, col = "blue")



报错内容:step factor 0.000488281 reduced below `minFactor' of 0.000976563

nls.control参数解释:

Description

Allow the user to set some characteristics of the nls nonlinear least squares algorithm.

Usagenls.control(maxiter = 50, tol = 1e-05, minFactor = 1/1024,            printEval = FALSE, warnOnly = FALSE)Arguments

maxiter                            

A positive integer specifying the maximum number of iterations allowed.

tol

A positive numeric value specifying the tolerance level for the relative offset convergence criterion.

minFactor

A positive numeric value specifying the minimum step-size factor allowed on any step in the iteration. The increment is calculated with a Gauss-Newton algorithm and successively halved until the residual sum of squares has been decreased or until the step-size factor has been reduced below this limit.

printEval

a logical specifying whether the number of evaluations (steps in the gradient direction taken each iteration) is printed.

warnOnly

a logical specifying whether nls() should return instead of signalling an error in the case of termination before convergence. Termination before convergence happens upon completion of maxiter iterations, in the case of a singular gradient, and in the case that the step-size factor is reduced below minFactor.

所以nls一般报错有两个可能,一个是maxiter次数用完还没有收敛,一个是step-size已经缩小的最小的极限minfactor还是没有是的tol下降。

简单的处理方法,可以是改minfactor,也可以将warnOnly设置为Ture。

设置方法:

 m.2 <- nls(y ~ rhs(x, cold, hot,cons,L,U), data = ds, start = list(cold = 350, hot = 1600,cons = 5800,L=14,U=25), trace = T,control=nls.control(warnOnly = T))


部分参考:https://stat.ethz.ch/R-manual/R-devel/library/stats/html/nls.control.html



https://blog.sciencenet.cn/blog-3083238-976875.html


下一篇:plot dataframe in R
收藏 IP: 218.94.96.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-10-3 16:33

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部