[R] Hint improve my code
EdBo
n.bowora at gmail.com
Mon Jul 4 01:32:33 CEST 2011
Please also consider the code below. My attempt to loop through the rows of
R_j so that I do not force if() to work on a vector.
> llik <- function(par, R_j, R_m) {
+ al_j <- par[1]
+ au_j <- par[2]
+ sigma_j <- par[3]
+ b_j <- par[4]
+
+ n=2
+ runs=5
+ est1=matrix(0,nrow=runs)
+ start.par=c(al_j=0,au_j=0,sigma_j=0.01,b_j=1)
+ out1=optim(par=start.par,llik, R_j=R_j, R_m=R_m)
+ for (i in 1: runs)
+ {
+ index_start=2*(i-1)+1
+ index_end= 2*i
+ if(R_j[i]< 0) {
+
sum(log(1/(2*pi*(sigma_j^2)))-(1/(2*(sigma_j^2))*(R_j+al_j-b_j*R_m))^2)
+ } else if(R_j[i]>0) {
+
sum(log(1/(2*pi*(sigma_j^2)))-(1/(2*(sigma_j^2))*(R_j+au_j-b_j*R_m))^2)
+ } else if(R_j[i]==0) {
+
sum(log(pnorm(au_j,mean=b_j*R_m,sd=sigma_j)-pnorm(al_j,mean=b_j*R_m,sd=sigma_j)))
+ }
+ est1[i] <- out1[index_start:index_end]
+ }
+ }
> est1
Error: object 'est1' not found
Thank you in advance
--
View this message in context: http://r.789695.n4.nabble.com/Hint-improve-my-code-tp3641354p3642707.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list