Galera, estou com um problema que não estou entendendo o motivo do erro que está na função filtro_kalman. Estou realizando um exercício que é para construir uma função que realiza o filtro de kalman. Utilizei o modelo de tendencia linear local no filtro de kalman:<br>
<br><br><div><br></div><div>#Função para gerar as matrizes do sistema para o</div><div>#Modelo de tendencia linear local</div><div><br></div><div>matriz_Tll<-function(fi,sig2_eta,sig2_eps,sig2_qui,n){</div><div><br></div>
<div>#Definindo as dimensões das matrizes do sistema</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_Z<-array( ,dim=c(1,2,n)) </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_T<-array( ,dim=c(2,2,n))</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_C<-array( ,dim=c(2,1,n))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_D<-array( ,dim=c(1,1,n))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_R<-array( ,dim=c(2,2,n))</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_H<-array( ,dim=c(1,1,n))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_Q<-array( ,dim=c(2,2,n))</div><div><br></div><div>
#Iniciando um 'for' para 'construir' as matrizes do sistema</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>for(t in 1:n){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_Z[ , ,t]<-matrix(c(1,0),nc=2)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_T[ , ,t]<-matrix(c(1,0,1,fi),nc=2)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_C[ , ,t]<-matrix(c(0,0))</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>ar_D[ , ,t]<-matrix(0)<span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_R[ , ,t]<-matrix(c(1,0,0,1),nc=2)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_H[ , ,t]<-matrix(sig2_eps,nc=1)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_Q[ , ,t]<-matrix(c(sig2_eta,0,0,sig2_qui),nc=2)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return(list(ar_Z,ar_T,ar_C,ar_D,ar_R,ar_H,ar_Q))</div><div>}</div><div><br></div><div>
fi<-0.5 #com |fi|<1 temos um modelo de tendencia amortecida</div><div>sig2_eta<-4 </div><div>sig2_eps<-6</div><div>sig2_qui<-10</div><div>n<-100 #número pequeno apenas para teste</div><div><br></div><div>
mat_sist<-matriz_Tll(fi,sig2_eta,sig2_eps,sig2_qui,n)</div><div><br></div><div>ar_Z<-mat_sist[[1]]</div><div>ar_T<-mat_sist[[2]]</div><div>ar_C<-mat_sist[[3]]</div><div>ar_D<-mat_sist[[4]]</div><div>ar_R<-mat_sist[[5]]</div>
<div>ar_H<-mat_sist[[6]]</div><div>ar_Q<-mat_sist[[7]]</div><div><br></div><div><br></div><div>#Função para modelo em espaço de estado em geral</div><div><br></div><div>gerador_EE<-function(ar_Z,ar_T,ar_C,ar_D,ar_R,ar_H,ar_Q,a1,p1){</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>n<-dim(ar_Z)[3] #nº de matrizes do array Z ou D ou H</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>p<-dim(ar_H)[1] #nº linhas de H, D ou Z</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>m<-dim(ar_T)[1] #nº linhas de T, C ou R</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>r<-dim(ar_Q)[1] #nº linhas de Q</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>ar_Y<-array(,dim=c(p,1,n))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>eps<-array(,dim=c(p,1,n))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>eta<-array(,dim=c(r,1,n))</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>alfa<-array(,dim=c(m,1,n+1))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>alfa[,,1]<-a1+chol(p1)%*%matrix(rnorm(m,0,1),nc=1)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>for(t in 1:n){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>eps[,,t]<-0+chol(ar_H[,,t])%*%matrix(rnorm(p,0,1),nc=1)</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>ar_Y[,,t]<-ar_Z[,,t]%*%alfa[,,t]+ar_D[,,t]+eps[,,t]</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>eta[,,t]<-0+chol(ar_Q[,,t])%*%matrix(rnorm(r,0,1),nc=1)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>alfa[,,t+1]<-ar_T[,,t]%*%alfa[,,t]+ar_C[,,t]+ar_R[,,t]%*%eta[,,t]</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return(ar_Y)</div>
<div>}</div><div><br></div><div>a1<-matrix(1:2,nc=1)</div><div>p1<-matrix(c(3,1,1,2),nc=2)</div><div>geracao<-gerador_EE(ar_Z,ar_T,ar_C,ar_D,ar_R,ar_H,ar_Q,a1,p1)</div><div><br></div><div>#Gráfico dos valores gerados para Y</div>
<div>plot(geracao,type='l')</div><div><br></div><div>#Filtro de Kalman</div><div><br></div><div>install.packages('MASS')</div><div>require(MASS)</div><div><br></div><div>Filtro_Kalman<-function(ar_Z,ar_T,ar_C,ar_D,ar_R,ar_H,ar_Q,a1,p1){</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>n<-dim(ar_Z)[3] #nº de matrizes do array Z ou D ou H</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>p<-dim(ar_H)[1] #nº linhas de H, D ou Z</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>m<-dim(ar_T)[1] #nº linhas de T, C ou R</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>r<-dim(ar_Q)[1] #nº linhas de Q</div><div>
<br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_Y<-array(,dim=c(p,1,n))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_U<-array(,dim=c(p,1,n))<span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_F<-array(,dim=c(p,p,n))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_AA<-array(,dim=c(m,1,n))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_PP<-array(,dim=c(m,m,n))</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_A<-array(,dim=c(m,1,n+1))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_P<-array(,dim=c(m,m,n+1))</div><div><br></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_A[,,1]<-a1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_P[,,1]<-p1<span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_Y<-gerador_EE(ar_Z,ar_T,ar_C,ar_D,ar_R,ar_H,ar_Q,a1,p1)<span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>for(t in 1:2){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_U[,,t]<-ar_Y[,,t]-ar_Z[,,t,drop=FALSE]%*%ar_A[,,t,drop=FALSE]-ar_D[,,t,drop=FALSE]</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_F[,,t]<-ar_Z[,,t,drop=FALSE]%*%ar_P[,,t,drop=FALSE]%*%t(ar_Z[,,t,drop=FALSE])+ar_H[,,t,drop=FALSE]</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_AA[,,t]<-ar_A[,,t,drop=FALSE]+ar_P[,,t,drop=FALSE]%*%ar_Z[,,t,drop=FALSE]%*%ginv(ar_F[,,t,drop=FALSE])%*%ar_U[,,t,drop=FALSE]</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_PP[,,t]<-ar_P[,,t,drop=FALSE]-ar_P[,,t,drop=FALSE]%*%t(ar_Z[,,t,drop=FALSE])%*%ginv(ar_F[,,t,drop=FALSE])%*%ar_Z[,,t,drop=FALSE]%*%ar_P[,,t]</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>ar_A[,,t+1]<-ar_T[,,t,drop=FALSE]%*%ar_AA[,,t,drop=FALSE]+ar_C[,,t,drop=FALSE]</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ar_P[,,t+1]<-ar_T[,,t,drop=FALSE]%*%ar_PP[,,t,drop=FALSE]%*%t(ar_PP[,,t,drop=FALSE])+ar_R[,,t,drop=FALSE]%*%ar_Q[,,t,drop=FALSE]%*%t(ar_R[,,t,drop=FALSE])</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return(list(ar_A,ar_P))</div>
<div>}</div><div><br></div><div>Filtro_Kalman(ar_Z,ar_T,ar_C,ar_D,ar_R,ar_H,ar_Q,a1,p1)</div>