As part of the set up, R packages were installed.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dbplyr)
##
## Attaching package: 'dbplyr'
##
## The following objects are masked from 'package:dplyr':
##
## ident, sql, sql_escape_ident, sql_escape_string
library(dplyr)
Also, two databases were installed from GitHub. One contains monthly data about the American consumer price index in urban areas between January 2016 and August 2026. I created a data frame called “CPI”
url<- "https://raw.githubusercontent.com/Enrique01234/607-Fall-2026/refs/heads/main/CPI%20-%20Sheet1%20(1).csv"
CPI <- read_csv(file = url,
show_col_types = FALSE,
progress = FALSE)
CPI
## # A tibble: 11 × 13
## Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2016 237. 237. 238. 239. 240. 241. 241. 241. 241. 242. 241. 241.
## 2 2017 243. 244. 244. 245. 245. 245. 245. 246. 247. 247. 247. 247.
## 3 2018 248. 249. 250. 251. 252. 252. 252. 252. 252. 253. 252. 251.
## 4 2019 252. 253. 254. 256. 256. 256. 257. 257. 257. 257. 257. 257.
## 5 2020 258. 259. 258. 256. 256. 258. 259. 260. 260. 260. 260. 260.
## 6 2021 262. 263. 265. 267. 269. 272. 273. 274. 274. 277. 278. 279.
## 7 2022 281. 284. 288. 289. 292. 296. 296. 296. 297. 298. 298. 297.
## 8 2023 299. 301. 302. 303. 304. 305. 306. 307. 308. 308. 307. 307.
## 9 2024 308. 310. 312. 314. 314. 314. 315. 315. 315. 316. 315. 316.
## 10 2025 318. 319. 320. 321. 321. 323. 323. 324. 325. NA 324. 324.
## 11 2026 325. 327. 330. 333. 335. 334. 334. 335. NA NA NA NA
The other database has similar data, for the same months and years, about the unemployment rate. This indicator is percentage, not an index like the previous one. I created a data frame called “Unempl”
url <- "https://raw.githubusercontent.com/Enrique01234/607-Fall-2026/refs/heads/main/Unempl.xlsx%20-%20BLS%20Data%20Series.csv"
Unempl <- read_csv(file = url,
show_col_types = FALSE,
progress = FALSE)
Unempl
## # A tibble: 11 × 13
## Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2016 4.8 4.9 5 5.1 4.8 4.9 4.8 4.9 5 4.9 4.7 4.7
## 2 2017 4.7 4.6 4.4 4.4 4.4 4.3 4.3 4.4 4.3 4.2 4.2 4.1
## 3 2018 4 4.1 4 4 3.8 4 3.8 3.8 3.7 3.8 3.8 3.9
## 4 2019 4 3.8 3.8 3.7 3.6 3.6 3.7 3.6 3.5 3.6 3.6 3.6
## 5 2020 3.6 3.5 4.4 14.8 13.2 11 10.2 8.4 7.8 6.9 6.7 6.7
## 6 2021 6.4 6.2 6.1 6.1 5.8 5.9 5.4 5.1 4.7 4.5 4.1 3.9
## 7 2022 4 3.9 3.7 3.7 3.6 3.6 3.5 3.6 3.5 3.6 3.6 3.5
## 8 2023 3.5 3.6 3.5 3.4 3.6 3.6 3.5 3.7 3.7 3.9 3.7 3.8
## 9 2024 3.7 3.9 3.9 3.9 3.9 4.1 4.2 4.2 4.1 4.1 4.2 4.1
## 10 2025 4 4.2 4.2 4.2 4.3 4.1 4.3 4.3 4.4 NA 4.5 4.4
## 11 2026 4.3 4.4 4.3 4.3 4.3 4.2 4.1 4.1 NA NA NA NA
In both cases I had first downloaded the data in Excel from the original source (U. S. Bureau of Labor Statistics). Then I eliminated extraneous columns and rows (e.g. titles, subtitles, and footnotes) and converted them into CSV files
The firs step I took was to calculate means across the columns. I.e., the average across the months yields the annual average of the Consumer Price Index (Annualmean). The mean is estimated across all columns with numeric values. Then, the column “Year” needs to be excluded
CPI <- CPI %>%
mutate(Annualmean = rowMeans(pick(where(is.numeric), -Year)))
CPI
## # A tibble: 11 × 14
## Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2016 237. 237. 238. 239. 240. 241. 241. 241. 241. 242. 241. 241.
## 2 2017 243. 244. 244. 245. 245. 245. 245. 246. 247. 247. 247. 247.
## 3 2018 248. 249. 250. 251. 252. 252. 252. 252. 252. 253. 252. 251.
## 4 2019 252. 253. 254. 256. 256. 256. 257. 257. 257. 257. 257. 257.
## 5 2020 258. 259. 258. 256. 256. 258. 259. 260. 260. 260. 260. 260.
## 6 2021 262. 263. 265. 267. 269. 272. 273. 274. 274. 277. 278. 279.
## 7 2022 281. 284. 288. 289. 292. 296. 296. 296. 297. 298. 298. 297.
## 8 2023 299. 301. 302. 303. 304. 305. 306. 307. 308. 308. 307. 307.
## 9 2024 308. 310. 312. 314. 314. 314. 315. 315. 315. 316. 315. 316.
## 10 2025 318. 319. 320. 321. 321. 323. 323. 324. 325. NA 324. 324.
## 11 2026 325. 327. 330. 333. 335. 334. 334. 335. NA NA NA NA
## # ℹ 1 more variable: Annualmean <dbl>
However, precisely what I wanted to look at, the data about the column means, does not show for reasons of space. Thus, I selected it to see the columns with the years (Year) and the annual average (Annualmean) results.
subset(CPI, select = c("Year", "Annualmean"))
## # A tibble: 11 × 2
## Year Annualmean
## <dbl> <dbl>
## 1 2016 240.
## 2 2017 245.
## 3 2018 251.
## 4 2019 256.
## 5 2020 259.
## 6 2021 271.
## 7 2022 293.
## 8 2023 305.
## 9 2024 314.
## 10 2025 NA
## 11 2026 NA
Now there are missing values for the years 2025 and 2026. For 2025, October is missing and, obviously, 2026 only has data up and including to August. So, a January to August mean (meanJanAug) is calculated and added to the data frame. For 2026, it will serve as the year-to-date average
CPI <- CPI %>%
mutate(meanJanAug = rowMeans(pick("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug")))
subset(CPI, select = c("Year", "Annualmean", "meanJanAug"))
## # A tibble: 11 × 3
## Year Annualmean meanJanAug
## <dbl> <dbl> <dbl>
## 1 2016 240. 239.
## 2 2017 245. 244.
## 3 2018 251. 251.
## 4 2019 256. 255.
## 5 2020 259. 258.
## 6 2021 271. 268.
## 7 2022 293. 290.
## 8 2023 305. 303.
## 9 2024 314. 313.
## 10 2025 NA 321.
## 11 2026 NA 332.
For 2026, the average of the Consumer Price Index (CPI) from the beginning of the year up to August is 332. This is not the inflation rate, which measures changes in the CPI. Also, I have checked in Excel and I obtained the same result.
Next, three Moving Averages (meanMA1, meanMA3, and meanMA3) are calculated. The first one covers the period January to June, the second one covers February to July and the last one March to August). These are the only ones that can be calculated for this year.
I have tried to include all three of them in one chunk but I received an error message. Also, I have tried to find a way to select the six columns without having to type each and every month, but I could not find it.
Then, for the first Moving Average
CPI <- CPI %>%
mutate(meanMA1 = rowMeans(pick("Jan", "Feb", "Mar", "Apr", "May", "Jun")))
For the second Moving Average
CPI <- CPI %>%
mutate(meanMA2 = rowMeans(pick("Feb", "Mar", "Apr", "May", "Jun", "Jul")))
And the third Moving Average (including all the results)
CPI <- CPI %>%
mutate(meanMA3 = rowMeans(pick("Apr", "May", "Jun", "Jul", "Aug")))
subset(CPI, select = c("Year", "meanMA1", "meanMA2", "meanMA3", "meanJanAug"))
## # A tibble: 11 × 5
## Year meanMA1 meanMA2 meanMA3 meanJanAug
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2016 239. 239. 240. 239.
## 2 2017 244. 244. 245. 244.
## 3 2018 250. 251. 252. 251.
## 4 2019 254. 255. 256. 255.
## 5 2020 258. 258. 258. 258.
## 6 2021 266. 268. 271. 268.
## 7 2022 288. 291. 294. 290.
## 8 2023 302. 303. 305. 303.
## 9 2024 312. 313. 314. 313.
## 10 2025 320. 321. 322. 321.
## 11 2026 331. 332. 334. 332.
The results, for 2026, last row, are:
The first Moving Average is 331
The second Moving Average is 332
The third Moving Average is 334
Also, I have checked in Excel and I obtained the same results.
For this indicator I have followed exactly the same steps. Consequently, I will not repeat them in detail here. Also, I will not repeat some of the exploratory steps.
Thus, the first element to calculate is the mean unemployment rate between January and August 2026 (meanJanAugU).
Unempl <- Unempl %>%
mutate(meanJanAugU = rowMeans(pick("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug")))
subset(Unempl, select = c("Year", "meanJanAugU"))
## # A tibble: 11 × 2
## Year meanJanAugU
## <dbl> <dbl>
## 1 2016 4.9
## 2 2017 4.44
## 3 2018 3.94
## 4 2019 3.72
## 5 2020 8.64
## 6 2021 5.88
## 7 2022 3.7
## 8 2023 3.55
## 9 2024 3.98
## 10 2025 4.2
## 11 2026 4.25
Then I calculated the three moving averages. For the first Moving Average (meanMA1U):
Unempl <- Unempl %>%
mutate(meanMA1U = rowMeans(pick("Jan", "Feb", "Mar", "Apr", "May", "Jun")))
For the second Moving Average (meanMA2U):
Unempl <- Unempl %>%
mutate(meanMA2U = rowMeans(pick("Feb", "Mar", "Apr", "May", "Jun", "Jul")))
And the third Moving Average (meanMA3U):
Unempl <- Unempl %>%
mutate(meanMA3U = rowMeans(pick("Apr", "May", "Jun", "Jul", "Aug")))
The results are in the last row:
subset(Unempl, select = c("Year", "meanMA1U", "meanMA2U", "meanMA3U", "meanJanAugU"))
## # A tibble: 11 × 5
## Year meanMA1U meanMA2U meanMA3U meanJanAugU
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2016 4.92 4.92 4.9 4.9
## 2 2017 4.47 4.4 4.36 4.44
## 3 2018 3.98 3.95 3.88 3.94
## 4 2019 3.75 3.7 3.64 3.72
## 5 2020 8.42 9.52 11.5 8.64
## 6 2021 6.08 5.92 5.66 5.88
## 7 2022 3.75 3.67 3.6 3.7
## 8 2023 3.53 3.53 3.56 3.55
## 9 2024 3.9 3.98 4.06 3.98
## 10 2025 4.17 4.22 4.24 4.2
## 11 2026 4.3 4.27 4.2 4.25
The first Moving Average is 4.3
The second Moving Average is 4.27
The third Moving Average is 4.2
Also, I have checked in Excel and I obtained the same results.