如何在optim函数中重复$ par,为R stat重复循环

时间:2016-04-18 15:11:46

标签: r

我只想重复$ par,重复5次。我仍然可以运行我的代码,但我有以下警告信息:

在结果[i] = m1中:   要替换的项目数不是替换长度的倍数

test = {
    "tests": {
        "testjson": {
            "key1": 10,
            "key2": "second",
            "key3": 20

        }
    }
}

我的编码输出如下。请帮助我放弃警告信息,我只能在'par'值上重复5次     [[1]]     [1] -0.2053160 0.4000491 2.3253354 1.3603900

library(evd)
results=0
for(i in 1:5)
{
trendgev=function(v,z)
{
beta0=v[1]
beta1=v[2]
mu=v[1]+v[2]*t
sig=v[3]
xi=v[4]
if (min(1+xi*(z-mu)/sig) <=0) return (10^10)
if (sig<0) return(10^10)

a=(1+xi*(z-mu))/sig
b=(-1/xi)

f=(1/sig)*(a^(b-1))*exp(-a^b)

ff=log(f)
ans=-sum(ff)  
return(ans)
}

t=c(1:1000)
beta0=1
beta1=0.4

z=rgev(50,0,1,1)
zz=z+beta1*t

starts=c(0.1, -0.9,1.1,0.5)

m1=optim(par=starts, fn=trendgev, z=zz) ; m1

results[i]=m1

print(results[i])
}
print(m1)

我可以得到如下输出(只重复并提及'par'值)?

[[1]]
[1] 0.07303119 0.39996344 1.11404626 0.93861174

[[1]]
[1] -0.01384496  0.39998626  1.14267411  1.05170904

[[1]]
[1] -1.0078745  0.4004017  1.0993075  4.3916078

[[1]]
[1] 0.06992641 0.39997072 1.05224910 1.09107480

Warning messages:
1: In results[i] = m1 :
  number of items to replace is not a multiple of replacement length
2: In results[i] = m1 :
  number of items to replace is not a multiple of replacement length
3: In results[i] = m1 :
  number of items to replace is not a multiple of replacement length
4: In results[i] = m1 :
  number of items to replace is not a multiple of replacement length
5: In results[i] = m1 :
  number of items to replace is not a multiple of replacement length
> print(m1)
$par
[1] 0.06992641 0.39997072 1.05224910 1.09107480

$value
[1] 2080.907

$counts
function gradient 
     501       NA 

$convergence
[1] 1

$message
NULL

0 个答案:

没有答案