dimanche 21 juin 2015

optimization/programming with R

I'm new with R. I'm trying to write a function with parameter a that gives me a certain output for n<4 and another output for n>=4. Actually, my function should be the sum of all the output because then I have to maximize wrt the parameter a. Which is my error??? Thank you

f= function(a,Y) { 
 Y = rep(NA, 10)
 for (n in 1:9)
 {if(n<4){
 Y[1]=5
 Y[n+1]<-2+(Y[n])}
 else{Y[n+1]<-a*n}
 return(Y)}}
> f(1)

[1] 5 7 NA NA NA NA NA NA NA NA

Aucun commentaire:

Enregistrer un commentaire