# --------------------------------------------------------------------------
# Quick-start example for the fjohansen R package
# Author: Merwan Roudane <merwanroudane920@gmail.com>
# --------------------------------------------------------------------------

library(fjohansen)

# 1. Activate the sunny ggplot2 theme
set_paper_theme()

# 2. Load the synthetic JGB-style dataset
data <- sample_jgb_data(T = 108, seed = 7)
print(head(data))

# 3. Pick the number of Fourier frequencies (PSY 2021)
sel <- select_frequencies(data, n_max = 5, p_d = 1, sig_level = 0.10)
print(sel)

# 4. Estimate the Johansen-Fourier CNR model
res <- johansen_fourier(data, k = 3, n = max(sel$n_selected, 1),
                        model = "CNR")
print(res)

# 5. Diagnostic plots
plot(res, which = "eigenvalues")
plot(res, which = "long_run")
plot(res, which = "residuals")
if (!is.null(res$delta)) plot(res, which = "risk_premium")

# 6. LaTeX export
cat(format_trace_latex(res), file = "section6_table.tex")
cat(format_trace_html(res),  file = "section6_table.html")
message("Wrote section6_table.tex and section6_table.html")
