2015-12-17 96 views
0

此的平方之和是,我想中的R的代碼的功能,寫一個程序,以儘量減少遞歸指數函數

enter image description here

i = 1,2,3,....j-1 

A,B,C,F,克將被從NLS確定(與起始值任意設定爲7,30,15,1,2)

S且Y是在數據集中

功能可以在一個更大的計算量被呈現人友好的遞推方程,

enter image description here

這裏是我的代碼的嘗試,但我無法得到它的收斂,

S=c(235,90,1775,960,965,1110,370,485,667,140,588,10,0,1340,600,0,930,1250,930,120,895,825,0,935,695,270,0,610,0,0,445,0,0,370,470,819,717,0,0,60,0,135,690,0,825,730,1250,370,1010,261,0,865,570,1425,150,1515,1143,0,675,1465,375,0,690,290,0,430,735,510,270,450,1044,0,928,60,95,105,60,950,0,1640,3960,1510,500,1135,0,0,0,181,568,60,1575,247,0,1270,870,290,510,0,540,455,120,580,420,90,525,1116,499,0,60,150,660,1080,1715,90,1090,840,975,280,850,633,30,1530,1765,880,150,225,77,1380,810,835,0,540,1017,1108,0,300,600,90,370,910,0,60,60,0,0,0,0,50,0,735,900) 

Y=c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,7.7,NA,NA,7.2,NA,NA,NA,NA,NA,NA,7.4,NA,NA,NA,NA,NA,NA,10.7,NA,NA,NA,NA,8.1,8.5,NA,NA,NA,NA,NA,9.9,NA,7.4,NA,NA,NA,9.5,NA,NA,9,NA,NA,NA,8.8,NA,NA,8.5,NA,NA,NA,6.9,NA,NA,7.9,NA,NA,NA,7.3,NA,7.9,8.3,NA,NA,NA,11.5,NA,NA,12.3,NA,NA,NA,6.1,NA,NA,9,NA,NA,NA,10.3,NA,NA,9.7,NA,NA,8.6,NA,9.1,NA,NA,11,NA,NA,12.4,11.1,10.1,NA,NA,NA,NA,11.7,NA,NA,9,NA,NA,NA,10.2,NA,NA,11.2,NA,NA,NA,11.8,NA,9.2,10,9.8,NA,9.5,11.3,10.3,9.5,10.2,10.6,NA,10.8,10.7,11.1,NA,NA,NA,NA,NA,NA,NA,NA,12.6,NA) 

mydata = data.frame(Y,S) 

f <- function(a,b,f,c,g,m) { 

    model <- matrix(NA,nrow(m)+1,3) 

    model[1,1]=0 
    model[1,2]=0 
    model[1,3]=a 

    for (i in 2:nrow(model)){ 
     model[i,1]=exp(-1/c)*model[i-1,1] + m$S[i-1] 
     model[i,2]=exp(-1/g)*model[i-1,2] + m$S[i-1] 
     model[i,3]=a+b*model[i,1]-f*model[i,2] 
    } 
    model <- as.data.frame(model) 
    colnames(model) = c('l','m','Y') 
    model$Y[which(m$Y>0)] 
} 

Y=mydata$Y 
nls(Y ~ f(a,b,f,c,g,mydata), start=list(a=7,b=5.3651,f=5.3656,c=16.50329,g=16.5006),control=list(maxiter=1000,minFactor=1e-12)) 

的錯誤,我已經得到依賴於初始值:

錯誤NLS(Y〜F(A,b,F,C,G,MYDATA),開始=列表(= 7,:
迭代次數超過10最大00

錯誤NLS(Y〜F(A,B,F,C,G,MYDATA),開始=列表(= 7,:
奇異梯度

我卡和不知道該怎麼做,任何幫助將不勝感激。

+0

a)您必須構造MYDATA爲data.frame使用$:'MYDATA < - data.frame(Y,S)'。 b)什麼是'p0'? c)你可以運行'f(7,5,16,16,mydata)'嗎?你會得到正確的結果嗎?如果不是,nls不會工作... –

+0

對不起,p0 = a,更正了更正。 f(7,5,16,16,mydata)有效,它會生成預測的Y – mct

回答

1

試試這個:

ff <- function(a,b,f,c,g) { 
    Y <- numeric(length(S)) 
    for(i in seq(from=2, to=length(S))) { 
     j <- seq(length=i-1) 
     Y[i] <- a + sum((b*exp(-(i-j)/c) - f*exp(-(i-j)/g))*S[j]) 
    } 
    Y 
} 

S <- c(235,90,1775,960,965,1110,370,485,667,140,588,10,0,1340,600,0,930,1250,930,120,895,825,0,935,695,270,0,610,0,0,445,0,0,370,470,819,717,0,0,60,0,135,690,0,825,730,1250,370,1010,261,0,865,570,1425,150,1515,1143,0,675,1465,375,0,690,290,0,430,735,510,270,450,1044,0,928,60,95,105,60,950,0,1640,3960,1510,500,1135,0,0,0,181,568,60,1575,247,0,1270,870,290,510,0,540,455,120,580,420,90,525,1116,499,0,60,150,660,1080,1715,90,1090,840,975,280,850,633,30,1530,1765,880,150,225,77,1380,810,835,0,540,1017,1108,0,300,600,90,370,910,0,60,60,0,0,0,0,50,0,735,900) 
Y <- c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,7.7,NA,NA,7.2,NA,NA,NA,NA,NA,NA,7.4,NA,NA,NA,NA,NA,NA,10.7,NA,NA,NA,NA,8.1,8.5,NA,NA,NA,NA,NA,9.9,NA,7.4,NA,NA,NA,9.5,NA,NA,9,NA,NA,NA,8.8,NA,NA,8.5,NA,NA,NA,6.9,NA,NA,7.9,NA,NA,NA,7.3,NA,7.9,8.3,NA,NA,NA,11.5,NA,NA,12.3,NA,NA,NA,6.1,NA,NA,9,NA,NA,NA,10.3,NA,NA,9.7,NA,NA,8.6,NA,9.1,NA,NA,11,NA,NA,12.4,11.1,10.1,NA,NA,NA,NA,11.7,NA,NA,9,NA,NA,NA,10.2,NA,NA,11.2,NA,NA,NA,11.8,NA,9.2,10,9.8,NA,9.5,11.3,10.3,9.5,10.2,10.6,NA,10.8,10.7,11.1,NA,NA,NA,NA,NA,NA,NA,NA,12.6,NA) 
nls(Y ~ f(a,b,f,c,g,mydata), start=list(a=7,b=5.3651,f=5.3656,c=16.50329,g=16.5006)) 

但我無法得到NLS跑這裏來。你也可以嘗試一個通用的優化器。構造函數平方的總和(平方-sum因爲我們最大化):

SS <- function(par) { 
    a <- par[1] 
    b <- par[2] 
    f <- par[3] 
    c <- par[4] 
    g <- par[5] 
    -sum((Y - ff(a,b,f,c,g))^2, na.rm=TRUE) 
} 

最大化:

library(maxLik) 
summary(a <- maxBFGS(SS, start=start)) 

它的工作原理,但正如你看到的梯度還是蠻大的。我得到的梯度小,如果我在BFGS的輸出值重新運行NR優化:

summary(b <- maxNR(SS, start=coef(a))) 

這給結果

​​

我不知道這是有道理的。 nls和其他優化器的問題表明,您有數值不穩定性,或者與大數值有關,或者與模型公式中的指數不同。

檢查什麼是對那裏發生的:-)

+0

我輸入命令「summary(a < - maxBFGS(SS,start = start)」時出錯,不應該指定SS (?)?我也將起始值更改爲f(10,10,5,10,20,mydata)並將其收斂。數字看起來比maxNR結果更正確,b&f和c&g數字不應該那麼相似,它們應該是2-3倍的差異。此外,即使它現在與我的原始代碼匯合,如果呈現不同的數據集,我將無法猜測正確的起始值。我如何確定正確的起始值?也許你的maxNR方法更適合這個原因。 – mct

+0

你得到的錯誤信息是什麼?沒有通用的好方法來獲得良好的初始值 - 這首先就等於解決問題。你可以嘗試一個強大的全球優化器,比如SANN,或者Nelder-Mead(健壯但不是全局的)。運行幾百次,並將這些係數作爲N-R或BFGS的起始值。如果你有數值問題,分析梯度也可能有很大的幫助。 –