Skip to content
Project: Food Price Forecasting with Functions
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:
| Column | Description |
|---|---|
adm0_id | Country code. Always 215 (int) |
adm0_name | Country name. Always "Rwanda" (char) |
adm1_id | Region code (int) |
adm1_name | Region name (chr) |
mkt_id | Market code (int) |
mkt_name | Market name (chr) |
cm_id | Commodity code (int) |
cm_name | Commodity name (chr) |
cur_id | Currency code. Always 77 (int) |
cur_name | Currency name. Always "RWF" (chr) |
pt_id | Price type code. Always 15 (int) |
pt_name | Price type name. Always "Retail" (chr) |
um_id | Unit of measurement code (int) |
um_name | Unit of measurement name. Always "KG" (chr) |
mp_month | Month when price occurred (int) |
mp_year | Year when price occurred. 2008 to 2015 (int) |
mp_price | Price of 1 unit of commodity in currency (dbl) |
mp_commoditysource | Data 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