# Extract trend from WX shadow rate using an UC model

setwd("D:/HM/JMCB/US4/compare rstar/WX shadow rate")
library(rucm)
library(Matrix)

WXdata <- read.csv(file = "WX_sr.csv", header = F)
wx_ts <- ts(WXdata[WXdata$V1<=202204,2], start = c(1990,1), frequency = 12)
UC_wx <- ucm(wx_ts~0, data = wx_ts, level = T, cycle = T, irregular = T, cycle.period =120)
# UC_wx <- ucm(wx_ts~0, data = wx_ts, level = T, cycle = T,  cycle.period =24)
# UC_wx <- ucm(wx_ts~0, data = wx_ts, level = T)

plot(wx_ts, ylab = "Rate")
lines(UC_wx$s.level, col = "blue")
legend("topright", legend = c("WX Shadow Rate","Trend"), col = c("black","blue"), lty = 1)

plot(UC_wx$s.level)


temp <- matrix(0, nrow = 606, ncol = 2)
temp[,1] <- WXdata[,1]
temp[,2] <- UC_wx$s.level
  
write.csv(temp, file="WX_trend.csv", row.names = F)  
