R中的非线性回归显示了误差
我正在使用程序包R中的非线性回归显示了误差,r,nonlinear-optimization,R,Nonlinear Optimization,我正在使用程序包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 1
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
统计数据错误:::nlsModel(公式、mf、start、wts):
初始参数估计的奇异梯度矩阵
我不知道该怎么办。
请解释它可能是什么,以及我如何解决它
其他信息:
#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)
}
#这就是我生成信号的方式(它是高斯与exp(-kt)的卷积)
种子集(100)
Yexp=sim\u str\u exp(错误=10)
时间=Yexp[[1]]
y=Yexp[[2]]
dataset_nls=data.frame(时间,y)
起点=c(τ1=0.5,β1=0.5,经验A1=0.5,高斯位置=0.5,高斯宽度=0.5,高斯A=0.5)
下=c(tau1=0.01,beta1=0.01,exp_A1=0.01,gaus_pos=0.01,gaus_width=0.01,gaus_A=0.01)
上限=c(τ1=100,β1=1,经验A1=1,高斯位置=100,高斯宽度=850,高斯A=1)
#我在这里试衣
拟合=nlsLM(y~str_exp_模型(时间,τ1,β1,exp_A1,高斯位置,高斯宽度,高斯A),数据=dataset_nls,开始=start,下限=lower,上限=upper,trace=TRUE,算法=“LM”,na.action=na.pass,control=nls.LM.control(maxiter=200,nprint=1))
#模型函数
str_exp_model您没有向我们展示sim_str_exp
,因此这个示例不是完全可复制的,但我想在这里进行猜测。您说“我用噪声生成我自己的信号”,但您使用Yexp=sim_str_exp(error=0)
生成数据,因此看起来您实际上没有添加任何噪声。(另外,您在最后一步报告的RSS是1.37e-28
。)
我猜您遇到了?nls
中记录的一个问题,即nls()
在零噪声下工作不好。这在?nlsLM
中没有记录,但如果它也存在,我也不会感到惊讶
为方便起见,以下是我在?nls
中提到的部分:
不要在人工“零残差”数据上使用“nls”
如果我的假设是正确的,那么如果你将噪声振幅设置为大于零,你应该能够得到一个没有错误的拟合。可能它没有收敛?你能给我们看一下sim_stru exp
?我假设它只是stru exp_model
+噪声…现在,@BenBolker,你知道他们怎么说“假设”:(好的,我应该说的)“我猜……nlsLM
使用零噪声数据比nls
做得好得多:好吧,但在这种特殊情况下可能还是一个问题?我不能排除这种可能性。:)我编辑了我的问题。我对数据建模有错误,它给出了同样的结果。很抱歉在begginng提出了一个糟糕的问题。希望你能帮助我解决这个问题
sim_str_exp<- function(num_points=512,time_scale=512,tau1=45,beta1=.80,exp_A1=1,gaus_pos=65,
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)
}
The ‘nls’ function uses a relative-offset convergence criterion
that compares the numerical imprecision at the current parameter
estimates to the residual sum-of-squares. This performs well on
data of the form
y = f(x, theta) + eps
(with ‘var(eps) > 0’). It fails to indicate convergence on data
of the form
y = f(x, theta)
because the criterion amounts to comparing two components of the
round-off error. If you wish to test ‘nls’ on artificial data
please add a noise component, as shown in the example below.