{"id":328138,"date":"2022-06-30T04:19:02","date_gmt":"2022-06-30T10:19:02","guid":{"rendered":"http:\/\/datageeek.com\/?p=3779"},"modified":"2022-06-30T04:19:02","modified_gmt":"2022-06-30T10:19:02","slug":"food-crisis-analysis-and-forecasting-with-neural-network-autoregression","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2022\/06\/food-crisis-analysis-and-forecasting-with-neural-network-autoregression\/","title":{"rendered":"Food Crisis Analysis and, Forecasting with Neural Network Autoregression"},"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\/2022\/06\/30\/food-crisis-analysis-and-forecasting-with-neural-network-autoregression\/\"> 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 war between Russia and Ukraine has affected the global food supply other than many vital things. Primarily cereal crop products have been affected the most because the imports have been provided to the world mainly through Ukraine and Russia. Let\u2019s check the situation we\u2019ve mentioned for <a href=\"https:\/\/www.wikiwand.com\/en\/G20\" rel=\"nofollow\" target=\"_blank\">G20 countries<\/a>.<\/p>\n\n\n<pre>\nlibrary(tidyverse)\nlibrary(fpp3)\nlibrary(bbplot)\nlibrary(plotly) \n  \n\nfood &lt;- read_csv(&quot;https:\/\/raw.githubusercontent.com\/mesdi\/wheat\/main\/ukraine-russia-food.csv&quot;)\n\n#G20 countries\ng20 &lt;- c(&quot;Argentina&quot;, &quot;Australia&quot;, &quot;Brazil&quot;, &quot;Canada&quot;, &quot;China&quot;, &quot;France&quot;, \n         &quot;Germany&quot;, &quot;India&quot;, &quot;Indonesia&quot;, &quot;Italy&quot;, &quot;Japan&quot;, &quot;South Korea&quot;, \n         &quot;Mexico&quot;, &quot;Saudi Arabia&quot;, &quot;South Africa&quot;, &quot;Turkey&quot;, &quot;United Kingdom&quot;,\n         &quot;United States&quot;)\n\n<\/pre>\n\n\n<p>We will get a look at the <a href=\"https:\/\/ourworldindata.org\/ukraine-russia-food#which-countries-are-most-reliant-on-food-imports-from-ukraine-and-russia\" rel=\"nofollow\" target=\"_blank\">2010-2019 import shares totals of barley, maize(corn), sunflower oil, and wheat for the domestic supply<\/a>. If we hoover at the chart bars, we can see the last year\u2019s (2019) rates of imports for the interested country and product.<\/p>\n\n\n<pre>\n#Comparing the G20 Countries' Imported Crops \n#for Domestic Supply(%) from Russia or Ukraine \np &lt;- \n  food %&gt;% \n  select(Entity, \n           Year, \n           `Barley imports from Ukraine + Russia (% supply)`,\n           `Maize imports from Ukraine + Russia (% supply)`,\n           `Sunflower oil imports from Ukraine + Russia (% supply)`,\n           `Wheat imports from Ukraine + Russia (% supply)`) %&gt;% \n    rename(Barley=`Barley imports from Ukraine + Russia (% supply)`,\n           Maize=`Maize imports from Ukraine + Russia (% supply)`,\n           `Sunflower oil`=`Sunflower oil imports from Ukraine + Russia (% supply)`,\n           Wheat=`Wheat imports from Ukraine + Russia (% supply)`) %&gt;% \n    mutate(across(where(is.numeric), ~replace_na(.,0))) %&gt;% \n    filter(Entity %in% g20) %&gt;% \n    pivot_longer(cols = -c(Entity,Year),names_to = &quot;Crops&quot;,values_to = &quot;n&quot;) %&gt;% \n    group_by(Entity,Crops) %&gt;% \n    summarise(total=sum(n),\n              #tooltip text showing the last year's values for plotly chart\n              percent=paste0(round(last(n),2),&quot;%\\nin 2019&quot;)) %&gt;% \n    arrange(desc(total),.by_group = TRUE) %&gt;% #ranking stacked fill descending\n    ggplot(aes(Entity, total, group=Entity, fill= Crops, text=percent)) +\n    geom_bar(stat = &quot;identity&quot;, position = &quot;fill&quot;)+\n    bbc_style()+\n    scale_y_continuous(labels = scales::percent)+\n    coord_flip()+\n    theme(legend.position = &quot;top&quot;, \n          legend.justification = &quot;center&quot;,\n          panel.grid.major.y = element_blank(),\n          title = element_text(size=5))\n  \n\n\n#Plotly Graph\nggplotly(p,tooltip = &quot;text&quot;) %&gt;% \n  #adjusting legend position to top and center \n  layout(legend = list(orientation = &quot;h&quot;,\n                       y =1.1,\n                       x=0.25,\n                       title=&quot;&quot;),\n         margin=list(r=50) #gives space to the hover information\n         )\n<\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"3788\" data-permalink=\"https:\/\/datageeek.com\/2022\/06\/30\/food-crisis-analysis-and-forecasting-with-neural-network-autoregression\/crops\/\" data-orig-file=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/crops.png\" data-orig-size=\"1280,678\" 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=\"crops\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/crops.png?w=300\" data-large-file=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/crops.png?w=750\" src=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/crops.png?w=578\" alt=\"\" class=\"wp-image-3788\" srcset_temp=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/crops.png?w=578 1024w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/crops.png?w=150 150w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/crops.png?w=300 300w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/crops.png?w=768 768w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/crops.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><\/figure>\n\n\n\n<p>It seems that sunflower oil is the most dominant imported cereal crop; specifically, when we hover over China, we see that %86.47 of the domestic supply was acquired from those countries in 2019. And it is clear that Brazil and Argentina do not need those crops arriving from Russia and Ukraine. <\/p>\n\n\n\n<p>We will now examine the impact of this situation on <a rel=\"nofollow\" href=\"https:\/\/www.investing.com\/commodities\/us-wheat\" target=\"_blank\">US wheat futures<\/a> prices. We will predict weekly prices for the next year with the neural network autoregression model (<a href=\"https:\/\/otexts.com\/fpp3\/nnetar.html#neural-network-autoregression\" rel=\"nofollow\" target=\"_blank\">NNAR<\/a>).<\/p>\n\n\n<pre>\nwheat_future &lt;- read_csv(&quot;https:\/\/raw.githubusercontent.com\/mesdi\/wheat\/main\/us_wheat_futures.csv&quot;)\n\nwheat_ts &lt;- \n  wheat_future %&gt;% \n  #removes comma and then converts to the proper date format\n  mutate(Date= parse_date(str_remove(Date,&quot;\\\\,&quot;),&quot;%b %d %Y&quot;)) %&gt;% \n  as_tsibble() %&gt;% \n  fill_gaps() %&gt;% #fills the gaps with NAs to provide regular time series\n  fill(Price,.direction = &quot;down&quot;) #replaces the NAs with the previous value\n\n<\/pre>\n\n\n<p>We can see from the below results that our model has 19 lagged inputs and ten nodes in the hidden layer.<\/p>\n\n\n<pre>\nset.seed(123)  \n\n#NNAR modeling\nfit_wheat &lt;- \n  wheat_ts %&gt;% \n  model(NNETAR(Price)) \n# A mable: 1 x 1\n#  `NNETAR(Price)`\n#          &lt;model&gt;\n#1   &lt;NNAR(19,10)&gt;\n\n<\/pre>\n\n\n<p><a href=\"https:\/\/otexts.com\/fpp3\/diagnostics.html#diagnostics\" rel=\"nofollow\" target=\"_blank\">To check our model<\/a>, we have to do a residual diagnostic test. So, we can see whether there is some correlation and the distribution of residuals is similar to that normally distributed.<\/p>\n\n\n<pre>\n#Model residuals diagnostics\nfit_wheat %&gt;% \n  gg_tsresiduals()\n\n<\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" data-attachment-id=\"3790\" data-permalink=\"https:\/\/datageeek.com\/2022\/06\/30\/food-crisis-analysis-and-forecasting-with-neural-network-autoregression\/diagnostics\/\" data-orig-file=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/diagnostics.jpeg\" data-orig-size=\"639,464\" 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=\"diagnostics\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/diagnostics.jpeg?w=300\" data-large-file=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/diagnostics.jpeg?w=450\" src=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/diagnostics.jpeg?w=450\" alt=\"\" class=\"wp-image-3790\" srcset_temp=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/diagnostics.jpeg 639w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/diagnostics.jpeg?w=150 150w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/diagnostics.jpeg?w=300 300w\" sizes=\"(max-width: 639px) 100vw, 639px\" data-recalc-dims=\"1\" \/><\/figure><\/div>\n\n\n<p>All the lags are between the threshold lines so that there is no correlation, as seen on the ACF graph.  When we look at the residual time plot, we can say that around zero mean and approximately constant variance across the historical data which means the distribution is similar to normal.<\/p>\n\n\n\n<p>Now, we can make forecasting; in order to do that, we will do <a href=\"https:\/\/otexts.com\/fpp3\/nnetar.html#prediction-intervals-5\" rel=\"nofollow\" target=\"_blank\">simulations<\/a> with our model.<\/p>\n\n\n<pre>\n#Projection of prices of the US wheat futures \n\nfit_wheat %&gt;% \n  generate(times = 100, h = 52) %&gt;%\n  autoplot(.sim) +\n  autolayer(wheat_ts, Price) +\n  scale_x_yearweek(date_breaks=&quot;104 week&quot;)+\n  labs(title=&quot;1-Year Projection of US Wheat Futures (ZWU2) Prices&quot;,\n       xlab=&quot;&quot;,\n       ylab=&quot;&quot;)+\n  bbc_style()+\n  theme(legend.position = &quot;none&quot;,\n        axis.text.x=element_text(size=15),\n        plot.title = element_text(size=20,hjust=0.5))\n\n<\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"3789\" data-permalink=\"https:\/\/datageeek.com\/2022\/06\/30\/food-crisis-analysis-and-forecasting-with-neural-network-autoregression\/projection-100\/\" data-orig-file=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/projection-100.jpeg\" data-orig-size=\"1129,464\" 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=\"projection-100\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/projection-100.jpeg?w=300\" data-large-file=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/projection-100.jpeg?w=750\" src=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/projection-100.jpeg?w=578\" alt=\"\" class=\"wp-image-3789\" srcset_temp=\"https:\/\/datageeek.files.wordpress.com\/2022\/06\/projection-100.jpeg?w=578 1024w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/projection-100.jpeg?w=150 150w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/projection-100.jpeg?w=300 300w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/projection-100.jpeg?w=768 768w, https:\/\/datageeek.files.wordpress.com\/2022\/06\/projection-100.jpeg 1129w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><\/figure>\n\n\n\n<p>We clearly see that the prices were up above the 1200s because of the war, and then come back to around the 900s. But it seems for the next one-year period the prices will go down to about 600-700s.<\/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\/2022\/06\/30\/food-crisis-analysis-and-forecasting-with-neural-network-autoregression\/\"> 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 war between Russia and Ukraine has affected the global food supply other than many vital things. Primarily cereal crop products have been affected the most because the imports have been provided to the world mainly through Ukraine and Russia. Let\u2019s check the situation we\u2019ve mentioned for G20 &#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\/328138"}],"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=328138"}],"version-history":[{"count":1,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/328138\/revisions"}],"predecessor-version":[{"id":328139,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/328138\/revisions\/328139"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=328138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=328138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=328138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}