在R中查找NLS功能的起始值 [英] Find start values for NLS function in R

查看:125
本文介绍了在R中查找NLS功能的起始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的NLS函数来拟合幂函数,但是我找不到合适的起始值.

i am trying to make a fit for a power function using the NLS function in R but i am failing to find good start values.

这是我的数据"CentroM"的一部分:

This is part of my data "CentroM":

Wg  TLcm
3200    79
2650    77
2750    74
870     45
1480    52
3400    80.5
2400    76
2800    76.5
2900    77.5
2700    76
3215    76
3300    83
3100    79
3000    78.5
2800    76
2700    77
2500    74.5
2300    69
2700    73.5
3350    79

这是我使用的脚本:

plot(CentroM$TLcm,CentroM$Wg,xlab="Total Length(cm)",ylab="Total Weight(g)",pch=1,type="p")
f<-function(TLcm,a,b){a*TLcm^b}
fit<-nls(CentroM$Wg~f(CentroM$TLcm,a,b),start=list(a=0.5,b=0.5),data=CentroM)

这就是我得到的:

model.frame.default中的错误(公式=〜CentroM + Wg + TLcm,数据= CentroM): 变量"CentroM"的类型(列表)无效

Error in model.frame.default(formula = ~CentroM + Wg + TLcm, data = CentroM) : invalid type (list) for variable 'CentroM'

请帮助...

推荐答案

您可以获取日志,拟合线性模型并从此处使用coef作为起始值:

You could take the logs, fit a linear model and use the coef from there a starting values:

df <- read.table(header = TRUE, text = 'Wg  TLcm
3200    79
2650    77
2750    74
870     45
1480    52
3400    80.5
2400    76
2800    76.5
2900    77.5
2700    76
3215    76
3300    83
3100    79
3000    78.5
2800    76
2700    77
2500    74.5
2300    69
2700    73.5
3350    79')


mod1 <- lm(log(Wg) ~ log(TLcm), data = df)
fit <- nls(Wg ~ a*TLcm^b, 
         start = list(a = exp(coef(mod1)[1]),
                      b = coef(mod1)[2]), 
         data = df)

这篇关于在R中查找NLS功能的起始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆