
Rose,
 
1- Inside your loop, you have to use F1[i], not F[i] (2 occurences).  By the way, never use F (or T) as variables in R, because it also means FALSE and TRUE.
2- Your P update must be at the end of the loop. You haven't calculated your K[i] yet where you placed it.
 
y<-(Nile)  n.y<-length(y)  P<-numeric(n.y)  a<-numeric(n.y)  i<-y  P[1]<-10000000  a[1]<-0  n<-100  sigma.2.epsilon<-15099  sigma.2.eta<-1469.1  nv <- matrix(nrow=100, ncol=1)  F1<-P[1]+sigma.2.epsilon  K <-P[1]/F1      for (i in 2:n-1){  F1[i]<-P[i]+sigma.2.epsilon  K[i]<-P[i]/F1[i]  nv[i]<-y[i]-a[i]  a[i+1] <-a[i]+K[i]*nv[i]  P[i+1]<-P[i]*(1-K[i])+sigma.2.eta  }
Neuro> Date: Wed, 21 Feb 2007 15:41:48 -0800> From: rbentosa@yahoo.com> To: r-help@stat.math.ethz.ch> Subject: [R] loops in R help me please> > I am trying to make the following Kalman filter equations work and therefore produce their graphs.> v_t=y_t - a_t> a_t+1=a_t+K_t*v_t> F_t=P_t+sigma.squared.epsilon> P_t+1=P_t*(1-K_t)+sigma.squared.eta> K_t=P_t/F_t> > Given:> a_1=0,P_1=10^7,sigma.squared.epsilon=15099,> sigma.squared.eta=1469.1> > I have attached my code,which of course doesnt work.It produces NAs for the Fs,Ks and the a.> Can somebody tell me please what am I doing wrong in this loop?> Why doesnt this loop work as it should be;to produce plots of a declining filtered state variance(P_t),prediction errors(v_t),and a declining prediction variance(F_t)?> Also,how can I construct and plot the 90% confidence intervals around a_t?> Thanks for your help and look forward.> Leonard> > > > ---------------------------------> It's here! Your new message!> Get new email alerts with the free Yahoo! Toolbar.
_________________________________________________________________
Soyez parmi les premiers à essayer Windows Live Mail.

4911fb2b2e6d
	[[alternative HTML version deleted]]

