发生不同的错误

时间:2017-01-12 14:53:07

标签: r nls

我是R编程的新手,我没有得到一个解决我使用nls函数时出现的错误的方法。 我尝试将ecdf中的数据(值被提取并保存在y中)与这个函数模型拟合,有四个参数:

fitsim <- nls(y ~ exp(-(((a-Abfluss)/(c*(Abfluss-b)))^d)), 
           start = list( a=max(Abfluss), b=min(Abfluss), 
                         c=3, d=1))

当我启动nls功能时,会发生以下错误:

 Error in numericDeriv(form[[3L]], names(ind), env) : 
  Fehlender Wert oder etwas Unendliches durch das Modell erzeugt

这意味着存在缺失值,通过模型生成具有无穷大的某个值。 我的向量Abfluss和y都有相同的长度。目的是获得参数估计。 也许问题是,该模型只能在这种条件下工作: c> 0,d> 0,b <= Abfluss&lt; = a。 我已经尝试了na.rm=True命令。然后出现另一个错误:

 Error in model.frame.default(formula = ~y + Abfluss, na.rm = TRUE) : 
  Variablenlängen sind unterschiedlich (gefunden für '(na.rm)')

这意味着,变量的长度是不同的。

我感谢各种帮助和建议。

为了更好地理解,我将整个代码附加到整个数据中:

time<-c(1851:2013)
Abfluss<-    c(4853,4214,5803,3430,4645,4485,3100,4797,4030,3590,5396,9864,3683,4485,4064,3420,5396,
        4895,3931,4238,3790,3520,4263,5474,3790,4700,5109,4525,4007,6340,4993,6903,8160,3600,3480,3540,
        3540,4565,3333,7764,  
        4755,7940,3112,3169,4435,5365,9422,3150,10500,4512,3790,4618,6126,3769,3704,
         5938,5669,4552,5458,5854,4867,6057,4783,5753,5736,4618,6091,5820,5007,7984, 4435,
         4645,7465,5820,5988,6022,4300,6062,3302,4877,4586,5275,4410,3174,4966,4939,4638,
         5541,5760,6495,5435,4952,4912,6092,5182,5820,5129,6436,6648,3063,5550,5160,4400,
         9600,6400,6380,6300,6180,6899,4360,5550,4580,3894,5277,7520,6780,5100,5430,4550,
         6620,4050,4560,5290,6610,8560,4943,6940,4744,6650,5700,7440,6200,4597,3697,7300,
         4644,5456,6302,3741,5398,9500,6296,5279,5923,6412,6559,6559,5891,5737,5010,5790,
         10300,4150,4870,6740,7560,8010,5120,8170,7430, 7330,5900, 11150)


#EV4-Distribution
dEV4 <- function(x, a, b, c,d) {
m<-exp(-(((a-Abfluss)/(c*(Abfluss-b)))^d))
return(m)
}

#Simulation example
Sim<-dEV4(Abfluss,a=max(Abfluss),b=min(Abfluss), c=3, d=1)
dEV4cdf<-cbind(Abfluss,Sim)

#Empirical cdf
p = ecdf(Abfluss)  
y<- p(Abfluss) #Extracting of cumulated probabilities
m<-cbind(Abfluss,y)

#plot EV4 and ecdf
plot(dEV4cdf, type="p",main="EV4")
plot(ecdf(Abfluss), add=T)

#Fitting EV4 nls
fitsim <- nls(y ~ exp(-(((a-Abfluss)/(c*(Abfluss-b)))^d)), 
          start = list( a=max(Abfluss), b=min(Abfluss), 
                        c=3, d=1), na.rm=TRUE)

1 个答案:

答案 0 :(得分:2)

不要使用可行区域边界上的起始值,而是在nlmrt中尝试nlxb(可以使用相同的参数,但data =不可选):

library(nlmrt)
fitsim <- nlxb(y ~ exp(-(((a - Abfluss) / (c * (Abfluss - b))) ^ d)), 
          data = data.frame(y, Abfluss),
          start = list(a = 2 * max(Abfluss), b = min(Abfluss) / 2, c = 3, d = 1))

plot(y ~ Abfluss, pch = 20)
o <- order(Abfluss)
fit <- y - fitsim$resid
lines(fit[o] ~ Abfluss[o], col = "red")

,并提供:

nlmrt class object: x 
residual sumsquares =  0.02908  on  163 observations
    after  5001    Jacobian and  6060 function evaluations
  name            coeff          SE       tstat      pval      gradient    JSingval   
a                20047.7            NA         NA         NA   1.119e-07        3251  
b               -1175384            NA         NA         NA   1.432e-09      0.1775  
c              0.0129414            NA         NA         NA     -0.1296   5.808e-06  
d                 12.146            NA         NA         NA  -2.097e-06   6.798e-11  

screenshot