[R] loop in optim
EdBo
n.bowora at gmail.com
Tue Jul 5 05:21:14 CEST 2011
Hi
I have re-worked on my likelihood function and it is now working(#the code
is below#).
May you help me correct my loop function.
I want optim to estimates al_j; au_j; sigma_j; b_j by looking at 0 to 20,
21 to 40, 41 to 60 data points.
The final result should have 4 columns of each of the estimates AND 4 rows
of each of 0 to 20, 21 to 40, 41 to 60.
#likelihood function
a=read.table("D:/hope.txt",header=T)
attach(a)
a
llik = function(x)
{
al_j=x[1]; au_j=x[2]; sigma_j=x[3]; b_j=x[4]
sum(na.rm=T,
ifelse(a$R_j< 0, log(1/(2*pi*(sigma_j^2)))-
(1/(2*(sigma_j^2))*(a$R_j+al_j-b_j*a$R_m))^2,
ifelse(a$R_j>0 , log(1/(2*pi*(sigma_j^2)))-
(1/(2*(sigma_j^2))*(a$R_j+au_j-b_j*a$R_m))^2,
log(ifelse (( pnorm (au_j, mean=b_j * a$R_m, sd= sqrt(sigma_j^2))-
pnorm(al_j, mean=b_j * a$R_m, sd=sqrt (sigma_j^2)
)) > 0,
(pnorm (au_j,mean=b_j * a$R_m, sd= sqrt(sigma_j^2))-
pnorm(al_j, mean=b_j * a$R_m, sd= sqrt(sigma_j^2)
)),
1)) ))
)
}
start.par = c(-0.01,0.01,0.1,1)
out1 = optim(llik, par=start.par, method="Nelder-Mead")
out1
--
View this message in context: http://r.789695.n4.nabble.com/loop-in-optim-tp3643230p3645031.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list