# Recursive Estimation and Time Series analysis
# RLS(Recursive Linear Least Square) p.33
library(dplyr)
data=data.frame(anscombe)
char=c("x1","x2","x3","x4")
Y=data$y2
X=as.matrix(data[,colnames(data) %in% char])
# times=5
# for(j in 1:times){
# X=rbind(X,X)
# Y=c(Y,Y)
# }
a=as.matrix(rep(1,ncol(X)),dim=c(ncol(X),1))
P=diag(0,ncol(X))
for(k in 1:nrow(data)){
P=P+t(t(c(X[k,])))%*%t(c(X[k,]))
P_mat=eigen(P)$vectors%*%diag(1/eigen(P)$values)%*%t(eigen(P)$vectors)
sig=sum((Y[1:k]-X[1:k,]%*%a)^2)/(nrow(data)-ncol(data)-1)
P_star=sig*P_mat
e=Y[k]-t(c(X[k,]))%*%a
print(P_mat%*%t(t(c(X[k,]))))
a=a+P_mat%*%t(t(c(X[k,])))*as.numeric(e)
}
a_hat=eigen(t(X)%*%X)$vectors%*%diag(1/eigen(t(X)%*%X)$values)%*%t(eigen(t(X)%*%X)$vectors)%*%t(X)%*%Y
cbind(Y,X%*%a,X%*%a_hat)
cor(cbind(Y,X%*%a,X%*%a_hat))
More than 5 years have passed since last update.
Recursive Estimation and Time Series analysis
Last updated at Posted at 2020-01-14
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme