{"id":380223,"date":"2023-11-22T01:04:19","date_gmt":"2023-11-22T07:04:19","guid":{"rendered":"http:\/\/datageeek.com\/?p=7119"},"modified":"2023-11-22T01:04:19","modified_gmt":"2023-11-22T07:04:19","slug":"time-series-analysis-and-understanding-a-eurobond-fund-with-prophet","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2023\/11\/time-series-analysis-and-understanding-a-eurobond-fund-with-prophet\/","title":{"rendered":"Time Series Analysis and Understanding a Eurobond Fund with Prophet"},"content":{"rendered":"<!-- \r\n<div style=\"min-height: 30px;\">\r\n[social4i size=\"small\" align=\"align-left\"]\r\n<\/div>\r\n-->\r\n\r\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\">\r\n[This article was first published on  <strong><a href=\"https:\/\/datageeek.com\/2023\/11\/22\/time-series-analysis-and-understanding-a-eurobond-fund-with-prophet\/\"> DataGeeek<\/a><\/strong>, and kindly contributed to <a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers<\/a>].  (You can report issue about the content on this page <a href=\"https:\/\/www.r-bloggers.com\/contact-us\/\">here<\/a>)\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>\n\n<p>The Eurobonds are so popular in Turkey nowadays. The reason for this is that the CDS of Turkey fell for quite a while since the new economic outlook began, according to the economists. We will look into the factors that shape this trend in this article.<\/p>\n\n\n\n<p>First, we build our data set with the components that we think are related to the Turkish Eurobonds.<\/p>\n\n\n<pre>\nlibrary(tidyverse)\nlibrary(tidymodels)\nlibrary(timetk)\nlibrary(modeltime)\nlibrary(modelStudio)\n\n#GPA(Eurobond Debt Instruments (FX) Fund of a private Turkish bank)\ndf_gpa &lt;- \n  read_csv(&quot;https:\/\/raw.githubusercontent.com\/mesdi\/blog\/main\/gpa.csv&quot;)\n\ndf_gpa_tidy &lt;- \n  df_gpa %&gt;% \n  janitor::clean_names() %&gt;% \n  mutate(date = parse_date(date, &quot;%m\/%d\/%Y&quot;)) %&gt;% \n  select(date, gpa = price)\n\n#CDS(Turkey CDS 5 Years USD)\ndf_cds &lt;- \n  read_csv(&quot;https:\/\/raw.githubusercontent.com\/mesdi\/blog\/main\/cds.csv&quot;)\n\ndf_cds_tidy &lt;- \n  df_cds %&gt;% \n  janitor::clean_names() %&gt;% \n  mutate(date = parse_date(date, &quot;%m\/%d\/%Y&quot;)) %&gt;% \n  select(date, cds = price)\n\n#USDTRY(US Dollar Turkish Lira)\ndf_usdtry &lt;- \n  read_csv(&quot;https:\/\/raw.githubusercontent.com\/mesdi\/blog\/main\/usdtry.csv&quot;)\n\ndf_usdtry_tidy &lt;- \n  df_usdtry %&gt;% \n  janitor::clean_names() %&gt;% \n  mutate(date = parse_date(date, &quot;%m\/%d\/%Y&quot;)) %&gt;% \n  select(date, usdtry = price)\n\n#Federal Funds Effective Rate\ndf_fedfunds &lt;- \n  read_csv(&quot;https:\/\/raw.githubusercontent.com\/mesdi\/blog\/main\/fedfunds.csv&quot;)\n\n##Converting Fed Funds data from monthly to weekly \nlibrary(padr)\ndf_fedfunds_tidy &lt;- \n  df_fedfunds %&gt;% \n  janitor::clean_names() %&gt;% \n  thicken(&quot;week&quot;) %&gt;% \n  pad_by_time(date_week, .by = &quot;week&quot;) %&gt;%\n  mutate(across(fedfunds, .fns = \\(x) ts_impute_vec(x, period = 1))) %&gt;% \n  select(date = date_week, fedfunds)\n\n#Transforming the variables to PPP (Purchasing power parities) \n#to see the changes(%) relative the last point\ndf_tidy &lt;- \n  df_all %&gt;%\n  mutate(gpa = (gpa\/first(gpa)*100) %&gt;% round(2),\n         cds = (cds\/first(cds)*100) %&gt;% round(2),\n         usdtry = (usdtry\/first(usdtry)*100) %&gt;% round(2),\n         fedfunds = (fedfunds\/first(fedfunds)*100) %&gt;% round(2))\n<\/pre>\n\n\n<p>Now that we have created our data set, we can examine the <em><a href=\"https:\/\/business-science.github.io\/timetk\/articles\/TK08_Automatic_Anomaly_Detection.html\" rel=\"nofollow\" target=\"_blank\">anomalies<\/a><\/em> of the variables.<\/p>\n\n\n<pre>\n#Anomaly\ndf_all %&gt;% \n  pivot_longer(cols = -date, names_to = &quot;vars&quot;) %&gt;%\n  plot_anomaly_diagnostics(date, \n                           value,\n                           .interactive = FALSE,\n                           .facet_vars = vars,\n                           .facet_ncol = 2)\n<\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png\" rel=\"nofollow\" target=\"_blank\"><img loading=\"lazy\" data-attachment-id=\"7130\" data-permalink=\"https:\/\/datageeek.com\/2023\/11\/22\/time-series-analysis-and-understanding-a-eurobond-fund-with-prophet\/anomaly\/\" data-orig-file=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png\" data-orig-size=\"1877,958\" data-comments-opened=\"1\" data-image-meta=\"{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}\" data-image-title=\"anomaly\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png?w=300\" data-large-file=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png?w=750\" src=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png?w=450\" alt=\"\" class=\"wp-image-7130\" srcset_temp=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png?w=450 1024w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png?w=150 150w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png?w=300 300w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png?w=768 768w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/anomaly.png 1877w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>We can see that all variables but <em><code>cds<\/code><\/em> have had a lot of anomalies in the last few years. So we will choose the <em><a href=\"https:\/\/otexts.com\/fpp3\/prophet.html\" rel=\"nofollow\" target=\"_blank\">prophet<\/a><\/em> model which is robust to outliers. Before moving on to our modeling, we will analyze the relations between all variables with the <em><a href=\"https:\/\/business-science.github.io\/timetk\/articles\/TK09_Clustering.html\" rel=\"nofollow\" target=\"_blank\">clustering<\/a><\/em> method.<\/p>\n\n\n<pre>\n#Clustering\ndf_tsfeature &lt;- \n  df_all %&gt;% \n  pivot_longer(cols = -date, names_to = &quot;id&quot;) %&gt;%\n  group_by(id) %&gt;%\n  tk_tsfeatures(\n    .date_var = date,\n    .value    = value,\n    .period   = 52,\n    .features = c(&quot;frequency&quot;, &quot;stl_features&quot;, &quot;entropy&quot;, &quot;acf_features&quot;, &quot;mean&quot;),\n    .scale    = TRUE,\n    .prefix   = &quot;ts_&quot;\n  ) %&gt;%\n  ungroup()\n\nset.seed(123)\ntibble(\n  cluster = df_tsfeature %&gt;% \n    select(-id) %&gt;%\n    as.matrix() %&gt;%\n    kmeans(centers = 3, nstart = 100) %&gt;%\n    pluck(&quot;cluster&quot;)\n) %&gt;%\n  bind_cols(\n    df_tsfeature\n  ) %&gt;% \n  select(cluster, id) %&gt;%\n  right_join(df_all %&gt;% pivot_longer(cols = -date, names_to = &quot;id&quot;) , \n             by = &quot;id&quot;) %&gt;%\n  group_by(id) %&gt;%\n  plot_time_series(\n    date, value, \n    .color_var   = cluster, \n    .facet_ncol  = 2, \n    .interactive = FALSE,\n    .title = &quot;&quot;\n  ) +\n  theme(strip.text = ggtext::element_markdown(face = &quot;bold&quot;))\n<\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png\" rel=\"nofollow\" target=\"_blank\"><img loading=\"lazy\" data-attachment-id=\"7144\" data-permalink=\"https:\/\/datageeek.com\/2023\/11\/22\/time-series-analysis-and-understanding-a-eurobond-fund-with-prophet\/cluster\/\" data-orig-file=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png\" data-orig-size=\"1920,992\" data-comments-opened=\"1\" data-image-meta=\"{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}\" data-image-title=\"cluster\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png?w=300\" data-large-file=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png?w=750\" src=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png?w=450\" alt=\"\" class=\"wp-image-7144\" srcset_temp=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png?w=450 1024w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png?w=150 150w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png?w=300 300w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png?w=768 768w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/cluster.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>We can say that the <em><code>gpa<\/code><\/em> and <em><code>usdtry<\/code><\/em> variables are in the same cluster and look very similar. I wonder if we will see it in our model as well.<\/p>\n\n\n<pre>\n#Split into a train and test set\nsplits &lt;- \n  df_tidy %&gt;% \n  timetk::time_series_split(assess = &quot;1 year&quot;, cumulative = TRUE)\n  \ntrain &lt;- training(splits)\ntest &lt;- testing(splits)\n\n#Modeling\ndf_rec &lt;- \n  recipe(gpa ~ ., data = train) \n\n\ndf_spec &lt;- \n  prophet_reg() %&gt;%\n  set_engine(engine = &quot;prophet&quot;)\n\n\nset.seed(12345)\ndf_wflow_fit &lt;- \n  workflow() %&gt;% \n  add_recipe(df_rec) %&gt;% \n  add_model(df_spec) %&gt;% \n  fit(train)\n\n#Calibration data\ndf_cal &lt;- \n  modeltime_calibrate(df_wflow_fit, test)\n\n#Accuracy\ndf_cal %&gt;% \n  modeltime_accuracy(metric_set = metric_set(rmse,rsq))\n\n# A tibble: 1 \u00d7 5\n#  .model_id .model_desc           .type  rmse   rsq\n#      &lt;int&gt; &lt;chr&gt;                 &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;\n#1         1 PROPHET W\/ REGRESSORS Test   2.40 0.989\n<\/pre>\n\n\n<p>Based on the <em><a href=\"https:\/\/yardstick.tidymodels.org\/reference\/rsq.html\" rel=\"nofollow\" target=\"_blank\">rsq<\/a><\/em> and <em><a href=\"https:\/\/yardstick.tidymodels.org\/reference\/rmse.html\" rel=\"nofollow\" target=\"_blank\">rmse<\/a><\/em> values, our model looks very good. Now, we can calculate the variable importance values based on the model with the <em><a href=\"https:\/\/www.business-science.io\/r\/2022\/02\/22\/my-4-most-important-explainable-ai-visualizations-modelstudio.html#:~:text=modelstudio%20tutorial\" rel=\"nofollow\" target=\"_blank\">modeltStudio<\/a><\/em> package. This will pop up the interactive dashboard on your browser, so you will be able to select variables on the right bottom for <em><a href=\"https:\/\/www.business-science.io\/r\/2022\/02\/22\/my-4-most-important-explainable-ai-visualizations-modelstudio.html#:~:text=plot%204%3A%20partial%20dependence%20plot\" rel=\"nofollow\" target=\"_blank\">partial dependence<\/a><\/em>.<\/p>\n\n\n<pre>\n#VIP(variable importance)\n##Explainer object\nlibrary(DALEXtra)\nexplainer &lt;- \n  DALEX::explain(\n    model = df_wflow_fit,\n    data  = train %&gt;% select(-gpa),\n    y     = train$gpa,\n    label = &quot;Prophet&quot;\n  )\n\n##Model Studio\nset.seed(1983)\nmodelStudio::modelStudio(explainer, \n                         B = 100,\n                         viewer = &quot;browser&quot;)\n<\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png\" rel=\"nofollow\" target=\"_blank\"><img loading=\"lazy\" data-attachment-id=\"7159\" data-permalink=\"https:\/\/datageeek.com\/2023\/11\/22\/time-series-analysis-and-understanding-a-eurobond-fund-with-prophet\/vip-5\/\" data-orig-file=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png\" data-orig-size=\"1413,563\" data-comments-opened=\"1\" data-image-meta=\"{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}\" data-image-title=\"vip\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png?w=300\" data-large-file=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png?w=750\" src=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png?w=450\" alt=\"\" class=\"wp-image-7159\" srcset_temp=\"https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png?w=450 1024w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png?w=150 150w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png?w=300 300w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png?w=768 768w, https:\/\/datageeek.files.wordpress.com\/2023\/11\/vip.png 1413w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>Despite what economists say, the CDS(<em><code>cds<\/code><\/em>) seems to have little effect on the fund based on the dashboard. Besides, it looks like the most dominant effect is the USD\/TRY(<em><code>usdtry<\/code><\/em>) rates\u2019 and it has a strong linear relation to the GPA(<code>gpa<\/code>) fund. This confirms the inference we found earlier with clustering.<\/p>\n\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\">\r\n<div style=\"text-align: center;\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"https:\/\/datageeek.com\/2023\/11\/22\/time-series-analysis-and-understanding-a-eurobond-fund-with-prophet\/\"> DataGeeek<\/a><\/strong>.<\/div>\r\n<hr \/>\r\n<a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers.com<\/a> offers <strong><a href=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=RBloggers\" rel=\"nofollow\">daily e-mail updates<\/a><\/strong> about <a title=\"The R Project for Statistical Computing\" href=\"https:\/\/www.r-project.org\/\" rel=\"nofollow\">R<\/a> news and tutorials about <a title=\"R tutorials\" href=\"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/\" rel=\"nofollow\">learning R<\/a> and many other topics. <a title=\"Data science jobs\" href=\"https:\/\/www.r-users.com\/\" rel=\"nofollow\">Click here if you're looking to post or find an R\/data-science job<\/a>.\r\n\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<div style = \"width:60%; display: inline-block; float:left; \"> The Eurobonds are so popular in Turkey nowadays. The reason for this is that the CDS of Turkey fell for quite a while since the new economic outlook began, according to the economists. We will look into the factors that shape this trend in this article. First, we build our &#8230;<\/div>\n<div style = \"width: 40%; display: inline-block; float:right;\"><\/div>\n<div style=\"clear: both;\"><\/div>\n","protected":false},"author":2348,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"aioseo_notices":[],"jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/380223"}],"collection":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/users\/2348"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=380223"}],"version-history":[{"count":8,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/380223\/revisions"}],"predecessor-version":[{"id":385380,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/380223\/revisions\/385380"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=380223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=380223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=380223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}