首页 文章

R中的非线性回归显示误差

提问于
浏览
2

我正在使用包 minpack.LM 中的R函数 nlsLM ,我有以下错误 .

我用噪声生成自己的信号,所以我知道所有参数,我试图使用相同的函数进行回归分析,我习惯于生成信号 .

问题是, nlsLM 函数运行正常,它甚至可以找到正确的参数值,但最后,当它找到它们时,错误显示如下:

它 . 23,RSS = 14.4698,Par . = 42.6727 0.78112 1 65.2211 15.6065 1

它 . 24,RSS = 14.4698,Par . = 42.671 0.781102 1 65.2212 15.6069 1

Error in stats:::nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates

而且我不知道该怎么做 . 请解释它可能是什么,以及我如何解决它!

附加信息:

#This is how i generate my signal (it is convolution of gaussian with exp(-kt)

set.seed(100)

Yexp=sim_str_exp(error=10)

time=Yexp[[1]]

y=Yexp[[2]]

dataset_nls=data.frame(time,y)

start=c(tau1=.5,beta1=.5,exp_A1=.5,gaus_pos=.5,gaus_width=.5,gaus_A=0.5)

lower=c(tau1=0.01,beta1=0.01,exp_A1=0.01,gaus_pos=0.01,gaus_width=0.01,gaus_A=0.01)

upper=c(tau1=100,beta1=1,exp_A1=1,gaus_pos=100,gaus_width=850,gaus_A=1)

#here i do fitting

FIT=nlsLM(y ~ str_exp_model(time,tau1,beta1,exp_A1,gaus_pos,gaus_width,gaus_A),data=dataset_nls,start=start,lower=lower,upper=upper,trace=TRUE,algorithm="LM",na.action=na.pass,control=nls.lm.control(maxiter=200,nprint=1))

#Model_function

str_exp_model<-function(time, tau1,beta1,exp_A1,gaus_pos,gaus_width,gaus_A){
F_gen_V<-vector(length=length(time))

F_gaus_V=vector(length=length(time))
F_exp_V=vector(length=length(time))
for (i in 1:length(time)) {
F_gaus_V[i]=gaus_A*exp(-2.77*((i-gaus_pos)/gaus_width)^2)
F_exp_V[i]=exp_A1*exp(-1*(i/tau1)^beta1)
}

convolve(F_gaus_V, F_exp_V,FALSE)
}

用于生成信号的功能

sim_str_exp<- function(num_points=512,time_scale=512,tau1=45,beta1=.80,exp_A1=1,gaus_pos=65,

gaus_width = 15,gaus_A = 1,Y0 = 0,错误= 2.0,show_graph = TRUE,norm =“False”){

F_gen_V<-vector(length=num_points)
time_gen_V<-vector(length=num_points)
F_gaus_V=vector(length=num_points)
F_exp_V=vector(length=num_points)
ts=time_scale/num_points
sigma=vector(length=num_points)


for (i in 1:num_points) {
F_gaus_V[i]=gaus_A*exp(-2.77*((i*ts-gaus_pos)/gaus_width)^2)
F_exp_V[i]=exp_A1*exp(-1*(i*ts/tau1)^beta1)
time_gen_V[i]=i*ts
}

F_gen_V<-(convolve(F_gaus_V, F_exp_V,FALSE))+Y0

if(norm==TRUE){
F_gen_V=F_gen_V/max(F_gen_V)}
else{;}

error_V=runif(512,-1*error, error)

for(i in 1:num_points){
F_gen_V[i]=error_V[i]/100*F_gen_V[i]+F_gen_V[i]
sigma[i]=(error_V[i]/100*F_gen_V[i])
}

RETURN=list(time=time_gen_V,y=F_gen_V,sigma=sigma)

if (show_graph==TRUE){
plot(RETURN[[1]],RETURN[[2]], type="l", main="Generated signal with noise",xlab="time,        pixel",ylab="Intensity");}
else {;}

return(RETURN)

}

1 回答

  • 2

    你还没有向我们展示 sim_str_exp ,所以这个例子不会在这里猜测 . 你说"I generate my own signal with noise",但你使用 Yexp=sim_str_exp(error=0) 来生成数据,所以看起来你实际上并没有添加任何噪音 . (另外,您在最后一步报告的RSS是 1.37e-28 ...)

    我的猜测是你遇到了 ?nls 中记录的问题,即当噪音为零时 nls() 不能正常工作 . ?nlsLM 中没有记录这一点,但如果它也存在,我也不会感到惊讶 .

    为方便起见,这里是我从 ?nls 引用的部分:

    不要在人工“零残留”数据上使用'nls' . 'nls'函数使用相对偏移收敛标准
    比较当前参数的数值不精确度
    估计剩余的平方和 . 这表现很好
    表格的数据

    y = f(x,theta)eps

    ('var(eps)> 0') . 它未能表明数据趋同
    形式

    y = f(x,theta)

    因为该标准相当于比较了两个组成部分
    舍入错误 . 如果你想在人工数据上测试'nls'
    请添加噪声组件,如下例所示 .

    如果我的假设是正确的,那么如果您将噪声幅度设置为大于零,那么您应该能够无误地得到拟合 .

相关问题