The Consumer Price Index (CPI) is a widely used economic indicator that measures the average change over time in the prices paid by urban consumers for a basket of goods and services. It is a key tool for assessing inflation, which is the rate at which the general level of prices for goods and services rises, leading to a decrease in the purchasing power of a currency.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(ggplot2)
url <- "https://raw.githubusercontent.com/MRobinson112/extra_credit-2/main/CPILFESL.csv"
CPI_Data <- read.csv(url)
# Formating date
CPI_Data$DATE <- as.Date(CPI_Data$DATE, format = "%Y-%m-%d")
# Calculate year-to-date average
CPI_Data <- CPI_Data %>%
arrange(DATE) %>%
group_by(YEAR = year(DATE)) %>%
mutate(YearToDateAverage = cummean(CPILFESL)) %>%
ungroup()
# Calculate moving average
CPI_Data <- CPI_Data %>%
arrange(DATE) %>%
mutate(MovingAverage = zoo::rollmean(CPILFESL, k = 6, fill = NA, align = "right"))
CPI_Data
## # A tibble: 800 × 5
## DATE CPILFESL YEAR YearToDateAverage MovingAverage
## <date> <dbl> <dbl> <dbl> <dbl>
## 1 1957-01-01 28.5 1957 28.5 NA
## 2 1957-02-01 28.6 1957 28.6 NA
## 3 1957-03-01 28.7 1957 28.6 NA
## 4 1957-04-01 28.8 1957 28.6 NA
## 5 1957-05-01 28.8 1957 28.7 NA
## 6 1957-06-01 28.9 1957 28.7 28.7
## 7 1957-07-01 29 1957 28.8 28.8
## 8 1957-08-01 29 1957 28.8 28.9
## 9 1957-09-01 29.1 1957 28.8 28.9
## 10 1957-10-01 29.2 1957 28.9 29
## # ℹ 790 more rows
ggplot(data = CPI_Data, aes(x = YEAR, y = MovingAverage)) +
geom_line() + # You can use other geoms like geom_point(), geom_bar(), etc., depending on your data and preference.
labs(
title = "Year vs. MovingAverage",
x = "Year",
y = "MovingAverage"
)
## Warning: Removed 5 rows containing missing values (`geom_line()`).
ggplot(data = CPI_Data, aes(x = YEAR, y = MovingAverage)) +
geom_bar(stat = "identity", fill = "blue") +
labs(
title = "Bar Graph of Year vs. Data",
x = "Year",
y = "MovingAverage"
)
## Warning: Removed 5 rows containing missing values (`position_stack()`).