Skip to content

Every time I go to the supermarket, my wallet weeps a little. But how expensive is food worldwide?

Explore a time series of food prices in Rwanda based on data from the United Nations Humanitarian Data Exchange Global Food Price Database. As reported by the CIA World Factbook, agriculture constitutes over 30% of Rwanda's economy and over 60% of its export earnings, so the price of food is a critical factor in the livelihood of many Rwandans.

You have data for nine foods stored in the data folder as separate CSV files. Each of these files has the following columns:

ColumnDescription
adm0_idCountry code. Always 215 (int)
adm0_nameCountry name. Always "Rwanda" (char)
adm1_idRegion code (int)
adm1_nameRegion name (chr)
mkt_idMarket code (int)
mkt_nameMarket name (chr)
cm_idCommodity code (int)
cm_nameCommodity name (chr)
cur_idCurrency code. Always 77 (int)
cur_nameCurrency name. Always "RWF" (chr)
pt_idPrice type code. Always 15 (int)
pt_namePrice type name. Always "Retail" (chr)
um_idUnit of measurement code (int)
um_nameUnit of measurement name. Always "KG" (chr)
mp_monthMonth when price occurred (int)
mp_yearYear when price occurred. 2008 to 2015 (int)
mp_pricePrice of 1 unit of commodity in currency (dbl)
mp_commoditysourceData source. Always "MINAGRI" (chr)
# Load necessary packages
library(readr)
library(dplyr)
options(dplyr.summarise.inform = FALSE)
library(lubridate)
install.packages("forecast")
library(forecast)
library(magrittr)

# Load the data
beans <- read.csv("data/Beans (dry).csv")
cassava <- read.csv("data/Cassava.csv")
chili <- read.csv("data/Chili (red).csv")
maize <- read.csv("data/Maize.csv")
oranges <- read.csv("data/Oranges (big size).csv")
peas <- read.csv("data/Peas (fresh).csv")
potatoes <- read.csv("data/Potatoes (Irish).csv")
sorghum <- read.csv("data/Sorghum.csv")
tomatoes <- read.csv("data/Tomatoes.csv")
Hidden output
# Start coding here
# Use as many cells as you like
# Load necessary packages
library(readr)
library(dplyr)
options(dplyr.summarise.inform = FALSE)
library(lubridate)
install.packages("forecast")
library(forecast)
library(magrittr)

# Load the data
beans <- read.csv("data/Beans (dry).csv")
cassava <- read.csv("data/Cassava.csv")
chili <- read.csv("data/Chili (red).csv")
maize <- read.csv("data/Maize.csv")
oranges <- read.csv("data/Oranges (big size).csv")
peas <- read.csv("data/Peas (fresh).csv")
potatoes <- read.csv("data/Potatoes (Irish).csv")
sorghum <- read.csv("data/Sorghum.csv")
tomatoes <- read.csv("data/Tomatoes.csv")

# Q1: Write a function that imports a CSV file and returns the median price of the commodity in Rwandan Francs (RWF) on each date.

get_median_price_by_date <- function(filename) {
  commodity <- read_csv(filename) %>% 
    mutate(date = ymd(paste(mp_year, mp_month, "01", sep = "-")))
  commodity %>% 
    group_by(date) %>% 
    summarize(median_price_rwf = median(mp_price))
}

# Test it on the cassava data
get_median_price_by_date("data/Cassava.csv")

# Q2: Write a function that accepts the median price data and returns the forecasted price for that commodity over the next two years.

# This solution gives a constant, or a flat, forecast, meaning the future values stay the same over time

forecast_price <- function(median_price_by_date) {
  commodity_ts <- median_price_by_date %$% 
    ts(
      median_price_rwf, 
      start = c(year(min(date)), month(min(date))), 
      end = c(year(max(date)), month(max(date))),
      frequency = 12
    )

  forecast(commodity_ts)
}

# Test it on the cassava data
cassava_median_price_by_date <- get_median_price_by_date("data/Cassava.csv")
cassava_result <- forecast_price(cassava_median_price_by_date)

# Print the result
cassava_result

# Save the forecast for February 2017
cassava_feb2017 <- 225