## ----cleanup, include=FALSE---------------------------------------------------
cleanup <- TRUE
library("fxregime")
knitr::opts_chunk$set(
  engine = "R",
  collapse = TRUE,
  comment = "##",
  message = FALSE,
  warning = FALSE,
  echo = TRUE
)

## ----preliminaries------------------------------------------------------------
library("fxregime")
data("FXRatesCHF", package = "fxregime")

## ----cny-data-----------------------------------------------------------------
cny <- fxreturns("CNY", frequency = "daily",
  start = as.Date("2005-07-25"), end = as.Date("2009-07-31"),
  other = c("USD", "JPY", "EUR", "GBP"), data = FXRatesCHF)

## ----cny-fitting--------------------------------------------------------------
cny_lm <- fxlm(CNY ~ USD + JPY + EUR + GBP,
  data = window(cny, end = as.Date("2005-10-31")))
summary(cny_lm)

## ----cny-testing--------------------------------------------------------------
cny_efp <- gefp(cny_lm, fit = NULL)

## ----cny-hprocess, fig.height=11.3, fig.width=9, fig.cap="Historical fluctuation process for CNY exchange rate regime."----
plot(cny_efp, aggregate = FALSE, ylim = c(-1.85, 1.85))

## ----cny-sctest---------------------------------------------------------------
sctest(cny_efp)

## ----cny-fxmonitor, fig.height=9, fig.width=9, fig.cap="Monitoring fluctuation process for CNY exchange rate regime."----
cny_mon <- fxmonitor(CNY ~ USD + JPY + EUR + GBP,
  data = window(cny, end = as.Date("2006-05-31")),
  start = as.Date("2005-11-01"), end = 4)
plot(cny_mon, aggregate = FALSE)

## ----cny-monitorbreak---------------------------------------------------------
cny_mon

## ----cny-dating, eval=FALSE---------------------------------------------------
# cny_reg <- fxregimes(CNY ~ USD + JPY + EUR + GBP,
#   data = cny, h = 20, breaks = 10)

## ----cny-dating1, echo=FALSE--------------------------------------------------
if(file.exists("cny_reg.rda")) load("cny_reg.rda") else {
cny_reg <- fxregimes(CNY ~ USD + JPY + EUR + GBP,
  data = cny, h = 20, breaks = 10)
save(cny_reg, file = "cny_reg.rda")
}
if(cleanup) file.remove("cny_reg.rda")

## ----cny-breaks, fig.height=5, fig.width=6, fig.cap="Negative log-likelihood and LWZ information criterion for CNY exchange rate regimes."----
plot(cny_reg)

## ----cny-confint--------------------------------------------------------------
confint(cny_reg, level = 0.9)

## ----cny-coef-----------------------------------------------------------------
coef(cny_reg)

## ----cny-refit----------------------------------------------------------------
cny_rf <- refit(cny_reg)
lapply(cny_rf, summary)

