469 lines
14 KiB
Plaintext
469 lines
14 KiB
Plaintext
---
|
||
title: "Statystyczna analiza cen kupna oraz najmu mieszkań w 15 największych miastach w Polsce"
|
||
author: "Michał Hiszpański, Nikola Kubiczek, Adrian Mirecki"
|
||
date: "2026-04-29"
|
||
output:
|
||
html_document: default
|
||
pdf_document: default
|
||
---
|
||
|
||
```{r setup, include=FALSE}
|
||
knitr::opts_chunk$set(echo = TRUE)
|
||
library(ggplot2)
|
||
library(patchwork)
|
||
library(dplyr)
|
||
library(ggplot2)
|
||
library(nortest)
|
||
library(tidyr)
|
||
library(purrr)
|
||
library(broom)
|
||
library(tibble)
|
||
fig.width = 10
|
||
fig.height = 10
|
||
```
|
||
|
||
# O danych
|
||
|
||
Zestaw danych zawiera oferty sprzedaży (na przestrzeni dwunastu miesięcy) oraz oferty wynajmu (na przestrzeni ośmiu miesięcy) mieszkań z 15 największych miast w Polsce (Warszawa, Łódź, Kraków, Wrocław, Poznań, Gdańsk, Szczecin, Bydgoszcz, Lublin, Katowice, Białystok, Częstochowa). Dane pochodzą z lokalnych stron internetowych z ogłoszeniami o sprzedaży mieszkań. Aby jeszcze lepiej uchwycić cechy otoczenia każdego mieszkania, każda oferta została uzupełniona o dane z OpenStreetMap, zawierające odległości do punktów zainteresowania (POI – Points of Interest).
|
||
|
||
Dane są zbierane miesięcznie i obejmują okres od sierpnia 2023 roku do czerwca 2024 roku.
|
||
|
||
|
||
## Parametry danych
|
||
1. ```city``` - nazwa miasta, w którym znajduje się mieszkanie
|
||
1. ```squareMeters``` - powierzchnia mieszkania w metrach kwadratowych
|
||
1. ```rooms``` - liczba pomieszczeń w mieszkaniu
|
||
1. ```floor``` floorCount - the floor where the apartment is located and the total number of floors in the building
|
||
1. ```buildYear``` - rok wybudowania
|
||
1. ```latitude```, ```longitude``` - wysokość oraz szerokość geograficzna
|
||
1. ```centreDistance``` - odległość od centrum mierzona w kilometrach
|
||
1. ```poiCount``` - number of points of interest in 500m range from the apartment (schools, clinics, post offices, kindergartens, restaurants, colleges, pharmacies)
|
||
1. ```[poiName]Distance``` - distance to the nearest point of interest (schools, clinics, post offices, kindergartens, restaurants, colleges, pharmacies)
|
||
1. ```condition``` - the condition of the apartment
|
||
1. ```has[features]``` - whether the property has key features such as assigned parking space, balcony, elevator, security, storage room
|
||
1. ```price``` - cena oferty w złotówkach
|
||
|
||
|
||
|
||
```{r import data}
|
||
files_rent <- list.files("rent", pattern = "\\.csv$", full.names = TRUE)
|
||
files_apartments <- list.files("apartments", pattern = "\\.csv$", full.names = TRUE)
|
||
files_rent <- sort(files_rent)
|
||
files_apartments <- sort(files_apartments)
|
||
|
||
rent_list <- lapply(seq_along(files_rent), function(i) {
|
||
d <- read.csv(files_rent[i])
|
||
d$offer_type = "rent"
|
||
d$month_id = i + 3
|
||
d
|
||
})
|
||
|
||
apartments_list <- lapply(seq_along(files_apartments), function(i) {
|
||
d <- read.csv(files_apartments[i])
|
||
d$offer_type = "apartment"
|
||
d$month_id = i
|
||
d
|
||
})
|
||
|
||
df <- do.call(rbind, c(rent_list, apartments_list))
|
||
|
||
# price/m^2
|
||
df$price_m2 <- df$price / df$squareMeters
|
||
df$log_price <- log(df$price)
|
||
df$log_price_m2 <- log(df$price_m2)
|
||
df$floor_ratio <- df$floor / df$floorCount
|
||
df$age <- 2024 - df$buildYear
|
||
|
||
df$city <- as.factor(df$city)
|
||
df$offer_type <- as.factor(df$offer_type)
|
||
df$month_id <- as.factor(df$month_id)
|
||
|
||
has_cols <- grep("^has", names(df), value = TRUE)
|
||
|
||
df[has_cols] <- lapply(df[has_cols], function(x) {
|
||
tolower(x) == "yes"
|
||
})
|
||
|
||
df <- df %>%
|
||
group_by(city) %>%
|
||
mutate(
|
||
latitude_n = as.numeric(scale(latitude)),
|
||
longitude_n = as.numeric(scale(longitude))
|
||
) %>%
|
||
ungroup() %>%
|
||
group_by(offer_type) %>%
|
||
mutate(
|
||
price_n = as.numeric(scale(price)),
|
||
price_m2_n = as.numeric(scale(price_m2)),
|
||
log_price_n = as.numeric(scale(log_price)),
|
||
log_price_m2_n = as.numeric(scale(log_price_m2))
|
||
) %>%
|
||
ungroup()
|
||
|
||
rent <- df[df$offer_type == "rent",]
|
||
rent_warsaw <- rent[rent$city == "warszawa",]
|
||
apartments <- df[df$offer_type == "apartment",]
|
||
apartments_warsaw <- apartments[apartments$city == "warszawa",]
|
||
|
||
df_city <- df %>%
|
||
count(city) %>%
|
||
mutate(
|
||
perc = n / sum(n) * 100,
|
||
label = paste0(round(perc, 1), "%")
|
||
)
|
||
|
||
rent_city <- rent %>%
|
||
group_by(city) %>%
|
||
summarise(mean_price_m2 = mean(price_m2, na.rm = TRUE))
|
||
|
||
ggplot(df_city, aes(x = reorder(city, -perc), y = perc)) +
|
||
geom_col(fill = "steelblue") +
|
||
geom_text(aes(label = paste0(round(perc, 1), "%")),
|
||
vjust = -0.3, size = 3) +
|
||
labs(
|
||
title = "Procent mieszkań według miasta",
|
||
x = "Miasto",
|
||
y = "Procent"
|
||
) +
|
||
theme_minimal() +
|
||
theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
||
|
||
ggplot(rent_city, aes(x = reorder(city, mean_price_m2),
|
||
y = mean_price_m2)) +
|
||
geom_col(fill = "steelblue") +
|
||
coord_flip() +
|
||
labs(
|
||
title = "Średnia cena m² wg miasta",
|
||
x = "Miasto",
|
||
y = "Średnia cena m²"
|
||
) +
|
||
theme_minimal()
|
||
|
||
```
|
||
|
||
# Wykresy
|
||
|
||
```{r violiny}
|
||
ggplot(rent, aes(x = city, y = price_m2)) +
|
||
geom_violin(fill = "green", alpha = 0.3) +
|
||
geom_boxplot(width = 0.1, outlier.alpha = 0.01) +
|
||
theme_minimal() +
|
||
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
|
||
labs(title = "Zależność ceny za m² od miasta (wynajem)",
|
||
x = "Miasto",
|
||
y = "Cena za m² (PLN)")
|
||
ggplot(apartments, aes(x = city, y = price_m2)) +
|
||
geom_violin(fill = "purple", alpha = 0.3) +
|
||
geom_boxplot(width = 0.1, outlier.alpha = 0.01) +
|
||
theme_minimal() +
|
||
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
|
||
labs(title = "Zależność ceny za m² w od miasta (kupno)",
|
||
x = "Miasto",
|
||
y = "Cena za m² (PLN)")
|
||
```
|
||
|
||
```{r zgodność rozkładów na przestrzeni miesięcy (kupno)}
|
||
ks_long <- apartments %>%
|
||
group_split(city) %>%
|
||
set_names(unique(apartments$city)) %>%
|
||
imap_dfr(function(df, city_name) {
|
||
|
||
months <- sort(unique(df$month_id))
|
||
|
||
expand_grid(m1 = months, m2 = months) %>%
|
||
mutate(
|
||
ks = map2_dbl(m1, m2, ~{
|
||
x <- df %>% filter(month_id == .x) %>% pull(log_price_m2)
|
||
y <- df %>% filter(month_id == .y) %>% pull(log_price_m2)
|
||
ks.test(x, y)$statistic
|
||
}),
|
||
p = map2_dbl(m1, m2, ~{
|
||
x <- df %>% filter(month_id == .x) %>% pull(log_price_m2)
|
||
y <- df %>% filter(month_id == .y) %>% pull(log_price_m2)
|
||
ks.test(x, y)$p.value
|
||
}),
|
||
city = city_name
|
||
)
|
||
})
|
||
|
||
ggplot(ks_long, aes(m1, m2, fill = ks)) +
|
||
geom_tile() +
|
||
facet_wrap(~city) +
|
||
scale_fill_viridis_c() +
|
||
theme_minimal() +
|
||
labs(
|
||
title = "Statystyka K-S D pomiędzy miesiącami (kupno)",
|
||
x = "Miesiąc", y = "Miesiąc", fill = "D"
|
||
)
|
||
|
||
ggplot(ks_long, aes(m1, m2, fill = p >= 0.05)) +
|
||
geom_tile() +
|
||
facet_wrap(~city) +
|
||
scale_fill_viridis_c() +
|
||
theme_minimal() +
|
||
labs(
|
||
title = "Kiedy odrzucamy H_0 (dystrubuanty z tego samego rozkładu)",
|
||
x = "Miesiąc", y = "Miesiąc", fill = "p >= 0.05"
|
||
)
|
||
```
|
||
|
||
```{r zgodność rozkładów na przestrzeni miesięcy (wynajem)}
|
||
ks_long <- rent %>%
|
||
group_split(city) %>%
|
||
set_names(unique(rent$city)) %>%
|
||
imap_dfr(function(df, city_name) {
|
||
|
||
months <- sort(unique(df$month_id))
|
||
|
||
expand_grid(m1 = months, m2 = months) %>%
|
||
mutate(
|
||
ks = map2_dbl(m1, m2, ~{
|
||
x <- df %>% filter(month_id == .x) %>% pull(log_price_m2)
|
||
y <- df %>% filter(month_id == .y) %>% pull(log_price_m2)
|
||
ks.test(x, y)$statistic
|
||
}),
|
||
p = map2_dbl(m1, m2, ~{
|
||
x <- df %>% filter(month_id == .x) %>% pull(log_price_m2)
|
||
y <- df %>% filter(month_id == .y) %>% pull(log_price_m2)
|
||
ks.test(x, y)$p.value
|
||
}),
|
||
city = city_name
|
||
)
|
||
})
|
||
|
||
ggplot(ks_long, aes(m1, m2, fill = ks)) +
|
||
geom_tile() +
|
||
facet_wrap(~city) +
|
||
scale_fill_viridis_c() +
|
||
theme_minimal() +
|
||
labs(
|
||
title = "Statystyka K-S D pomiędzy miesiącami (wynajem)",
|
||
x = "Miesiąc", y = "Miesiąc", fill = "D"
|
||
)
|
||
|
||
ggplot(ks_long, aes(m1, m2, fill = p >= 0.05)) +
|
||
geom_tile() +
|
||
facet_wrap(~city) +
|
||
scale_fill_viridis_c() +
|
||
theme_minimal() +
|
||
labs(
|
||
title = "Kiedy odrzucamy H_0 (dystrubuanty z tego samego rozkładu)",
|
||
x = "Miesiąc", y = "Miesiąc", fill = "p >= 0.05"
|
||
)
|
||
```
|
||
```{r heatmapy}
|
||
ggplot(rent, aes(x=longitude_n, y=latitude_n, col=price_m2_n)) +
|
||
geom_point(alpha=0.03, size=0.4) +
|
||
facet_wrap(~ city) +
|
||
scale_color_viridis_c(name = "Koszt wynajmu za m² (PLN)\n(ciemne znaczy droższe)", direction=-1) +
|
||
theme_minimal()+
|
||
labs(
|
||
title = "Dokładne położenie na mapie a koszt wynajmu",
|
||
x = "szerokość geograficzna",
|
||
y = "wysokość geograficzna")
|
||
ggplot(apartments, aes(x=longitude_n, y=latitude_n, col=price_m2_n)) +
|
||
geom_point(alpha=0.01, size=0.4) +
|
||
facet_wrap(~ city) +
|
||
scale_color_viridis_c(option="plasma", name = "Koszt kupna za m² (PLN)\n(ciemne znaczy droższe)", direction=-1) +
|
||
theme_minimal()+
|
||
labs(
|
||
title = "Dokładne położenie na mapie a koszt zakupu",
|
||
x = "szerokość geograficzna",
|
||
y = "wysokość geograficzna")
|
||
```
|
||
|
||
```{r heatmapy Warszawa}
|
||
ggplot(rent_warsaw, aes(x=longitude, y=latitude, col=log_price)) +
|
||
geom_point(alpha=0.2, size=0.4) +
|
||
scale_color_viridis_c(name = "Koszt wynajmu za m² (PLN)\n(ciemne znaczy droższe)", direction=-1) +
|
||
theme_minimal()+
|
||
labs(
|
||
title = "Dokładne położenie na mapie a koszt wynajmu",
|
||
x = "szerokość geograficzna",
|
||
y = "wysokość geograficzna")
|
||
|
||
ggplot(apartments_warsaw, aes(x=longitude, y=latitude, col=log_price)) +
|
||
geom_point(alpha=0.1, size=0.4) +
|
||
scale_color_viridis_c(option="plasma", name = "Koszt kupna za m² (PLN)\n(ciemne znaczy droższe)", direction=-1) +
|
||
theme_minimal()+
|
||
labs(
|
||
title = "Dokładne położenie na mapie a koszt zakupu",
|
||
x = "szerokość geograficzna",
|
||
y = "wysokość geograficzna")
|
||
|
||
```
|
||
|
||
#Porównanie dystrybuanty empirycznej z dystrybuantą rozkładu normalnego
|
||
#o nieobciążonych estymatorach
|
||
```{r dystrybuanty}
|
||
x<-log(rent$price_m2)
|
||
t<-log(apartments$price_m2)
|
||
z <- seq(min(x,t), max(x,t), length.out = 300000)
|
||
dystrybuanta_empiryczna_wynajem<-ecdf(x)
|
||
dystrybuanta_empiryczna_kupno<-ecdf(t)
|
||
dystrybuanta_teoretyczna_wynajem<-pnorm(z, mean=mean(x), sd=sd(x))
|
||
dystrybuanta_teoretyczna_kupno<-pnorm(z, mean=mean(t), sd=sd(t))
|
||
ggplot() +
|
||
geom_step(
|
||
aes(x = z, y = dystrybuanta_empiryczna_wynajem(z), color = "Empiryczna wyn"),
|
||
linewidth = 1,
|
||
linetype = "solid"
|
||
) +
|
||
geom_line(
|
||
aes(x = z, y = dystrybuanta_teoretyczna_wynajem, color = "Teoretyczna wyn"),
|
||
linewidth = 1,
|
||
linetype = "dashed"
|
||
) +
|
||
geom_step(
|
||
aes(x=z, y=dystrybuanta_empiryczna_kupno(z), color = "Empiryczna kupna"),
|
||
linewidth = 1,
|
||
linetype="solid"
|
||
) +
|
||
geom_line(
|
||
aes(x=z, y=dystrybuanta_teoretyczna_kupno, color = "Teoretyczna kupna"),
|
||
linewidth = 1,
|
||
linetype="dashed"
|
||
) +
|
||
scale_color_manual(
|
||
values = c("Empiryczna wyn" = "green" , "Teoretyczna wyn" = "blue" , "Empiryczna kupna" = "purple", "Teoretyczna kupna" = "red" ),
|
||
labels = c("Empiryczna kupna", "Empiryczna wyn", "Teoretyczna kupna", "Teoretyczna wyn")
|
||
) +
|
||
labs(
|
||
title = "Dystrybuanty empiryczne i teoretyczne",
|
||
x = "Wartość",
|
||
y = "Prawdopodobieństwo skumulowane",
|
||
color = "Typ dystrybuanty"
|
||
) +
|
||
theme_minimal() +
|
||
theme(plot.title = element_text(hjust = 0.5))
|
||
```
|
||
|
||
```{r prosta regresja ceny od odległości od centrum dla wynajmu}
|
||
|
||
ggplot(rent, aes(x=centreDistance, y=price_m2)) +
|
||
geom_point(color = "green",alpha=0.1, size=0.3) +
|
||
geom_smooth(method = "lm", se = TRUE, color = "orange") +
|
||
facet_wrap(~ city) +
|
||
theme_minimal() +
|
||
labs(
|
||
title = "Zależność ceny za m² od odległości od centrum (wynajem)",
|
||
x = "Odległość od centrum (km)",
|
||
y = "Cena za m² (PLN)")
|
||
|
||
```
|
||
|
||
```{r prosta regresja ceny od odległości od centrum dla kupna}
|
||
ggplot(apartments, aes(x=centreDistance, y=price_m2)) +
|
||
geom_point(color = "purple", alpha=0.1, size=0.3) +
|
||
geom_smooth(method = "lm", se = TRUE, color = "orange") +
|
||
facet_wrap(~ city) +
|
||
theme_minimal() +
|
||
labs(
|
||
title = "Zależność ceny za m² od odległości od centrum (kupno)",
|
||
x = "Odległość od centrum (km)",
|
||
y = "Cena za m² (PLN)")
|
||
|
||
```
|
||
|
||
# Czy ceny normalne?
|
||
|
||
```{r lillieford}
|
||
lillie.test(rent_warsaw$log_price_m2_n)
|
||
lillie.test(apartments_warsaw$log_price_m2_n)
|
||
|
||
df_plot <- data.frame(log_price_m2 = df$log_price_m2_n)
|
||
ggplot(df, aes(x = log_price_m2_n)) +
|
||
geom_histogram(aes(y = after_stat(density)),
|
||
bins = 50,
|
||
fill = "lightblue",
|
||
color = "white") +
|
||
facet_wrap(~ offer_type)
|
||
|
||
qqnorm(rent$log_price_m2_n)
|
||
qqline(rent$log_price_m2_n, col = "red")
|
||
|
||
qqnorm(apartments$log_price_m2_n)
|
||
qqline(apartments$log_price_m2_n, col = "red")
|
||
```
|
||
|
||
# Model liniowy
|
||
```{r linear model}
|
||
model_rent <- lm(log_price_m2 ~ centreDistance +
|
||
squareMeters +
|
||
age +
|
||
floor_ratio +
|
||
city +
|
||
hasElevator +
|
||
hasBalcony +
|
||
hasSecurity +
|
||
condition,
|
||
data = rent)
|
||
|
||
model_buy <- lm(log_price_m2 ~ centreDistance +
|
||
squareMeters +
|
||
age +
|
||
floor_ratio +
|
||
city +
|
||
hasElevator +
|
||
hasBalcony +
|
||
hasSecurity +
|
||
condition,
|
||
data = apartments)
|
||
|
||
model_compare <- lm(log_price_m2 ~ offer_type *
|
||
(centreDistance +
|
||
squareMeters +
|
||
age +
|
||
city +
|
||
hasElevator +
|
||
hasBalcony +
|
||
hasSecurity +
|
||
condition) + month_id,
|
||
data = df, na.action = na.exclude)
|
||
|
||
df$pred <- predict(model_compare)
|
||
summary(df$pred)
|
||
sum(is.na(df$pred))
|
||
|
||
plot_data <- df %>%
|
||
filter(!is.na(pred)) %>%
|
||
group_by(month_id, offer_type) %>%
|
||
summarise(mean_pred = mean(pred), .groups = "drop")
|
||
|
||
ggplot(plot_data, aes(x = month_id, y = mean_pred, color = offer_type)) +
|
||
geom_line(linewidth = 1.2) +
|
||
geom_point() +
|
||
theme_minimal() +
|
||
labs(title = "Predykowane ceny: rent vs apartments",
|
||
y = "log(price m²)")
|
||
|
||
|
||
|
||
```
|
||
|
||
```{r propozycje od chata}
|
||
monthly_means <- df %>%
|
||
group_by(month_id, offer_type) %>%
|
||
summarise(
|
||
mean_log_price = mean(log_price_m2, na.rm = TRUE),
|
||
.groups = "drop"
|
||
)
|
||
|
||
wide <- monthly_means %>%
|
||
pivot_wider(names_from = offer_type,
|
||
values_from = mean_log_price)
|
||
|
||
wide$divergence <- wide$apartment - wide$rent
|
||
|
||
ggplot(wide, aes(x = month_id, y = divergence, group = 1)) +
|
||
geom_line(linewidth = 1.2) +
|
||
geom_point() +
|
||
theme_minimal() +
|
||
labs(
|
||
title = "Dywergencja rynku mieszkań: sprzedaż vs wynajem",
|
||
subtitle = "Różnica średniej log-ceny m² (apartments - rent)",
|
||
x = "Miesiąc",
|
||
y = "Dywergencja (log price m²)"
|
||
) +
|
||
theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
||
``` |