library(tidyverse)
options(timeout = 300) # the BEA file is 36 MB
hpi <- read_csv("https://www.fhfa.gov/hpi/download/monthly/hpi_master.csv") |>
filter(hpi_type == "traditional", hpi_flavor == "purchase-only", frequency == "quarterly",
place_id == "USA" | level == "MSA") |>
transmute(place_id, place_name, level, quarter = yq(paste(yr, period)), hpi = index_sa) |>
drop_na(hpi)
txt <- tempfile(fileext = ".txt")
download.file("https://apps.bea.gov/national/Release/TXT/NipaDataQ.txt", txt, quiet = TRUE)
bea <- read_csv(txt, skip = 1, col_names = c("code", "quarter", "value"), col_types = "ccc") |>
mutate(quarter = yq(quarter), value = parse_number(value))
pce <- bea |> filter(code == "DPCERG") |> select(quarter, pce = value)
homes <- hpi |>
inner_join(pce, by = "quarter") |>
mutate(real = hpi / pce * pce[quarter == max(quarter)][1]) # in latest-quarter dollars
peak <- homes |> filter(place_id == "USA") |> slice_max(real, n = 1) |> pull(quarter)
Bu maqolada R 4.6.1 versiyasi, readr 2.2.0 va dplyr 1.2.1 kutubxonalari ishlatilgan. Quyidagi kodlar bilan ma'lumotlar yuklanadi va tahlil qilinadi:
library(tidyverse)
options(timeout = 300) # BEA fayli 36 MB
hpi <- read_csv("https://www.fhfa.gov/hpi/download/monthly/hpi_master.csv") |>
filter(hpi_type == "traditional", hpi_flavor == "purchase-only", frequency == "quarterly",
place_id == "USA" | level == "MSA") |>
transmute(place_id, place_name, level, quarter = yq(paste(yr, period)), hpi = index_sa) |>
drop_na(hpi)
Keyingi qadamda BEA ma'lumotlari yuklanadi va tahlil qilinadi:
dsp_colors <- c("#0066CC", "#E8862D", "#159A6C", "#7D5BD6",
"#D64580", "#2AA9B8", "#C9A227")
dsp_theme <- theme_minimal(base_size = 13) +
theme(plot.background = element_rect(fill = "#ECECEF", color = NA),
panel.background = element_rect(fill = "#ECECEF", color = NA),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color = "grey78"),
axis.ticks = element_blank(),
plot.title = element_text(face = "bold"),
strip.text = element_text(face = "bold"),
legend.position = "top")
homes |>
filter(place_id == "USA") |>
select(quarter, `In dollars of the day` = hpi, `After inflation, in latest-quarter dollars` = real) |>
pivot_longer(-quarter, names_to = "measure") |>
ggplot(aes(quarter, value, color = measure)) +
geom_line(linewidth = 1) +
scale_color_manual(values = dsp_colors[1:2], name = NULL) +
labs(x = NULL, y = "FHFA House Price Index (1991 Q1 = 100 in dollars)") +
dsp_theme
txt <- tempfile(fileext = ".txt")
download.file("https://apps.bea.gov/national/Release/TXT/NipaDataQ.txt", txt, quiet = TRUE)
bea <- read_csv(txt, skip = 1, col_names = c("code", "quarter", "value"), col_types = "ccc") |>
mutate(quarter = yq(quarter), value = parse_number(value))
pce <- bea |> filter(code == "DPCERG") |> select(quarter, pce = value)
Keyingi qadamda uy narxlari va inflyatsiya indekslari birlashtiriladi va haqiqiy narxlar hisoblanadi:
homes <- hpi |>
inner_join(pce, by = "quarter") |>
mutate(real = hpi / pce * pce[quarter == max(quarter)][1]) # oxirgi chorak dollarlarida
AQSh uy narxlari 1991-yildan buyon
Quyidagi grafik AQSh uy narxlarining 1991-yildan buyon o'zgarishini ko'rsatadi:
metros <- homes |>
filter(level == "MSA") |>
group_by(place_name) |>
summarise(dollars_2022 = hpi[quarter == max(quarter)] / hpi[quarter == as.Date("2022-04-01")] - 1,
real_2022 = real[quarter == max(quarter)] / real[quarter == as.Date("2022-04-01")] - 1,
dollar_record = hpi[quarter == max(quarter)] == max(hpi),
real_record = real[quarter == max(quarter)] == max(real),
vs_boom = real[quarter == max(quarter)] / max(real[year(quarter) < 2010]) - 1)
metros |>
summarise(up_in_dollars = sum(dollars_2022 > 0), up_in_real_terms = sum(real_2022 > 0),
records_in_dollars = sum(dollar_record), records_in_real_terms = sum(real_record))
## # A tibble: 1 × 4
## up_in_dollars up_in_real_terms records_in_dollars records_in_real_terms
## <int> <int> <int> <int>
## 1 87 52 43 11
homes |>
filter(place_id == "USA") |>
select(quarter, `In dollars of the day` = hpi, `After inflation, in latest-quarter dollars` = real) |>
pivot_longer(-quarter, names_to = "measure") |>
ggplot(aes(quarter, value, color = measure)) +
geom_line(linewidth = 1) +
scale_color_manual(values = dsp_colors[1:2], name = NULL) +
labs(x = NULL, y = "FHFA House Price Index (1991 Q1 = 100 in dollars)") +
dsp_theme
Bu grafikda ko'rinib turibdiki, dollarlar bo'yicha chiziq 2022-yildagi to'xtashdan keyin yana ko'tarilmoqda. Inflyatsiyadan keyin esa bu chiziq 2024-yil 4-choragida cho'qqiga yetdi va hozirgi kunda 27% o'sib ketdi.
Yuzta metropoliyaning ikki javobi
Har bir metropoliyada eng oxirgi chorakni 2022-yil 2-choragiga solishtirib, eng oxirgi chorak eng yuqori bo'lishini tekshiradi:
bind_rows(slice_min(metros, real_2022, n = 10), slice_max(metros, real_2022, n = 10)) |>
mutate(place_name = str_remove(place_name, " \\(MSAD\\)"),
place_name = fct_reorder(place_name, real_2022)) |>
ggplot(aes(y = place_name)) +
geom_vline(xintercept = 0, color = "grey50") +
geom_segment(aes(x = dollars_2022, xend = real_2022), color = "grey65") +
geom_point(aes(x = dollars_2022, color = "In dollars"), size = 2.8) +
geom_point(aes(x = real_2022, color = "After inflation"), size = 2.8) +
scale_color_manual(values = c("In dollars" = dsp_colors[2], "After inflation" = dsp_colors[1]),
breaks = c("In dollars", "After inflation"), name = NULL) +
scale_x_continuous(labels = scales::percent) +
labs(x = "Change in home prices since 2022 Q2", y = NULL) +
dsp_theme +
theme(panel.grid.major.x = element_line(color = "grey78"),
panel.grid.major.y = element_blank())
metros <- homes |>
filter(level == "MSA") |>
group_by(place_name) |>
summarise(dollars_2022 = hpi[quarter == max(quarter)] / hpi[quarter == as.Date("2022-04-01")] - 1,
real_2022 = real[quarter == max(quarter)] / real[quarter == as.Date("2022-04-01")] - 1,
dollar_record = hpi[quarter == max(quarter)] == max(hpi),
real_record = real[quarter == max(quarter)] == max(real),
vs_boom = real[quarter == max(quarter)] / max(real[year(quarter) < 2010]) - 1)
Natijalar shuni ko'rsatadiki, 100 ta metropoliyadan 87 tasi dollarlar bo'yicha o'sdi va 52 tasi inflyatsiyadan keyin o'sdi. Eng katta o'sishlar Shimoliy-Sharq va Markaziy AQShda kuzatiladi, masalan, Rochester, NY (20% inflyatsiyadan keyin) va Hartford, CT (19%). Eng katta pasayishlar esa Kaliforniya, Kolorado va Vashington shtatlarida kuzatiladi.
Qaysi narx indeksi?
Iste'molchi xarajatlari uylarni o'z ichiga oladi, shuning uchun uy narxlarini PCE ga bo'lib, uylarni uy bilan bo'lib chiqish mumkin. BEA shuningdek, oziq-ovqat, energiya va uy-joyni o'z ichiga olmagan PCE ni ham taqdim etadi:
core <- bea |> filter(code == "IA001176") |> select(quarter, core = value)
us_core <- homes |>
filter(place_id == "USA") |>
inner_join(core, by = "quarter") |>
mutate(real_core = hpi / core)
core <- bea |> filter(code == "IA001176") |> select(quarter, core = value)
Bu maqolada R tilidan foydalanilgan holda uy narxlarini inflyatsiyaga moslashtirish usullari va natijalari taqdim etildi. Bu usullar AQSh uy bozoridagi o'zgarishlarni tushunish uchun foydali bo'lishi mumkin.
Asl manba: datascienceplus.com