Compare commits

...
16 Commits
Author SHA1 Message Date
michal e4b179418b Merge branch 'main' of https://git.yaemiku.dev/yaemiku/sad 2026-05-08 19:50:26 +02:00
michal d27f10bd15 nh 2026-05-08 19:25:50 +02:00
yaemiku a66909d5b7 music 2026-05-05 22:30:57 +02:00
yaemiku 2c259e4f1a on the finish line 2026-05-05 20:12:33 +02:00
michal 85e9cb1e9a gdg 2026-05-05 20:02:22 +02:00
yaemiku 72a26adaa0 can you hear the music 2026-05-05 16:33:28 +02:00
yaemiku 2f11a0a1b0 KS and LM? 2026-05-04 23:34:09 +02:00
yaemiku a2a913f1e6 little data overview 2026-05-04 23:27:54 +02:00
michal a3ec4c6ca0 bombom 2026-05-04 21:35:42 +02:00
michal ab44b66342 elemele 2026-05-04 17:39:27 +02:00
michal e4ae092371 baboj 2026-05-04 16:07:01 +02:00
yaemiku 9aa0e5e9e9 qq + linear models 2026-05-04 15:47:55 +02:00
yaemiku 53c28d05e8 rent + apartments 2026-05-04 15:45:49 +02:00
michal 2e9a54b539 Merge branch 'HEAD' of https://git.yaemiku.dev/yaemiku/sad.git 2026-05-04 15:42:18 +02:00
michal 37c9ede843 porownanie dystrybuant 2026-05-04 15:40:25 +02:00
yaemiku 52ff662ad8 sth 2026-05-04 13:52:38 +02:00
5 changed files with 1305 additions and 111 deletions

No files matched your search

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+512
View File
@@ -0,0 +1,512 @@
) +
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", "Empiryczna kupna")
) +
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))
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))
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(ggplot2)
library(patchwork)
library(dplyr)
library(ggplot2)
library(nortest)
library(tidyr)
library(purrr)
library(broom)
library(tibble)
library(binom)
library(reshape2)
fig.width = 10
fig.height = 10
var(rent$price)
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(ggplot2)
library(patchwork)
library(dplyr)
library(ggplot2)
library(nortest)
library(tidyr)
library(purrr)
library(broom)
library(tibble)
library(binom)
library(reshape2)
fig.width = 10
fig.height = 10
# Wczytujemy pliki
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))
# Dodatkowe parametry
df$price_m2 <- df$price / df$squareMeters
df$log_price <- log10(df$price)
df$log_price_m2 <- log10(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)
df$month_num <- as.numeric(as.character(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()
# Podzbiory danych
rent <- df[df$offer_type == "rent",]
rent_warsaw <- rent[rent$city == "warszawa",]
apartments <- df[df$offer_type == "apartment",]
apartments_warsaw <- apartments[apartments$city == "warszawa",]
summary(df)
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))
apartments_city <- apartments %>%
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 ofert 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 = "green") +
coord_flip() +
labs(
title = "Średnia cena/m² wg miasta (wynajem)",
x = "Miasto",
y = "Średnia cena/m²"
) +
theme_minimal()
ggplot(apartments_city, aes(x = reorder(city, mean_price_m2),
y = mean_price_m2)) +
geom_col(fill = "purple") +
coord_flip() +
labs(
title = "Średnia cena/m² wg miasta (kupno)",
x = "Miasto",
y = "Średnia cena/m²"
) +
theme_minimal()
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)")
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"
)
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(option = "plasma") +
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(option = "plasma") +
theme_minimal() +
labs(
title = "Kiedy odrzucamy H_0 (dystrubuanty z tego samego rozkładu)",
x = "Miesiąc", y = "Miesiąc", fill = "p >= 0.05"
)
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")
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")
z <- seq(min(rent$log_price_m2,apartments$log_price_m2), max(rent$log_price_m2,apartments$log_price_m2), length.out = 300000)
dystrybuanta_empiryczna_wynajem<-ecdf(rent$log_price_m2)
dystrybuanta_empiryczna_kupno<-ecdf(apartments$log_price_m2)
dystrybuanta_teoretyczna_wynajem<-pnorm(z, mean=mean(rent$log_price_m2), sd=sd(rent$log_price_m2))
dystrybuanta_teoretyczna_kupno<-pnorm(z, mean=mean(apartments$log_price_m2), sd=sd(apartments$log_price_m2))
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))
lillie.test(rent$log_price_m2)
lillie.test(apartments$log_price_m2)
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")
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)")
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)")
model_rent <- lm(log_price_m2 ~ month_num + centreDistance +
squareMeters +
age +
floor_ratio +
city +
hasElevator +
hasBalcony +
hasSecurity +
condition,
data = rent, na.action = na.exclude)
par(mfrow = c(2,2))
plot(model_rent)
summary(model_rent)
numeric_df <- rent %>%
select(price_m2, squareMeters, centreDistance, age, hasElevator,
hasBalcony,
hasSecurity)
corr <- cor(numeric_df, use = "complete.obs")
melted <- melt(corr)
ggplot(melted, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
theme_minimal()
model_apartments <- lm(log_price_m2 ~ month_num + centreDistance +
squareMeters +
age +
floor_ratio +
city +
hasElevator +
hasBalcony +
hasSecurity +
condition,
data = apartments, na.action = na.exclude)
par(mfrow = c(2,2))
plot(model_rent)
summary(model_apartments)
numeric_df <- apartments %>%
select(price_m2, squareMeters, centreDistance, age, hasElevator,
hasBalcony,
hasSecurity)
corr <- cor(numeric_df, use = "complete.obs")
melted <- melt(corr)
ggplot(melted, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_viridis_c(option = "plasma") +
theme_minimal()
rent_pred <- cbind(
rent,
predict(model_rent, interval = "confidence") %>%
as.data.frame()
)
apartments_pred <- cbind(
apartments,
predict(model_apartments, interval = "confidence") %>%
as.data.frame()
)
rent_plot <- rent_pred %>%
group_by(month_num) %>%
summarise(
fit = mean(fit, na.rm = TRUE),
lwr = mean(lwr, na.rm = TRUE),
upr = mean(upr, na.rm = TRUE),
.groups = "drop"
)
apartments_plot <- apartments_pred %>%
group_by(month_num) %>%
summarise(
fit = mean(fit, na.rm = TRUE),
lwr = mean(lwr, na.rm = TRUE),
upr = mean(upr, na.rm = TRUE),
.groups = "drop"
)
ggplot(rent_plot,
aes(x = month_num, y = fit)) +
geom_line(color = "darkgreen", linewidth = 1.3) +
geom_point(color = "darkgreen", size = 2) +
geom_ribbon(aes(ymin = lwr, ymax = upr),
alpha = 0.2,
fill = "green") +
theme_minimal() +
labs(
title = "Trend przewidywanych cen – wynajem",
x = "Miesiąc",
y = "log(price/m²)"
)
ggplot(apartments_plot,
aes(x = month_num, y = fit)) +
geom_line(color = "purple", linewidth = 1.3) +
geom_point(color = "purple", size = 2) +
geom_ribbon(aes(ymin = lwr, ymax = upr),
alpha = 0.2,
fill = "purple") +
theme_minimal() +
labs(
title = "Trend przewidywanych cen – sprzedaż mieszkań",
x = "Miesiąc",
y = "log(price/m²)"
)
var(rent$price)
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))
trend_model <- lm(divergence ~ as.numeric(month_id), data = wide)
summary(trend_model)
var(rent$price)
var(apartments$price)
+670 -110
View File
@@ -1,176 +1,736 @@
---
title: "Projekt Zaliczeniowy 1"
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
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(ggplot2)
library(patchwork)
library(dplyr)
library(ggplot2)
library(nortest)
fig.width = 8
fig.height = 5
library(tidyr)
library(purrr)
library(broom)
library(tibble)
library(binom)
library(reshape2)
library(forcats)
library(stringr)
fig.width = 10
fig.height = 10
```
# Import data
The dataset contains apartment sales and rent offers from the 15 largest cities in Poland (Warsaw, Lodz, Krakow, Wroclaw, Poznan, Gdansk, Szczecin, Bydgoszcz, Lublin, Katowice, Bialystok, Czestochowa). The data comes from local websites with apartments for sale. To fully capture the neighborhood of each apartment better, each offer was extended by data from the Open Street Map with distances to points of interest (POI). The data is collected monthly and covers a timespan between August 2023 and June 2024
## Files
- apartments_pl_YYYY_MM.csv: sale price
- apartments_rent_pl_YYYY_MM.csv: monthly rent
## Data Fields
1. ```city``` - the name of the city where the property is located
1. ```type``` - type of the building
1. ```squareMeters``` - the size of the apartment in square meters
1. ```rooms``` - number of rooms in the apartment
1. ```floor``` / floorCount - the floor where the apartment is located and the total number of floors in the building
1. ```buildYear``` - the year when the building was built
1. ```latitude```, ```longitude``` - geo coordinate of the property
1. ```centreDistance``` - distance from the city centre in km
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. ```ownership``` - the type of property ownership
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``` - offer price in Polish Zloty
# 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
2. `squareMeters` - powierzchnia mieszkania w metrach kwadratowych
3. `rooms` - liczba pomieszczeń w mieszkaniu
4. `floor`,`floorCount` - piętro, na którym znajduje się mieszkanie oraz całkowita liczba pięter w mieszkaniu
5. `buildYear` - rok wybudowania
6. `latitude`, `longitude` - wysokość oraz szerokość geograficzna
7. `centreDistance` - odległość od centrum mierzona w kilometrach
8. `poiCount` - liczba POI w promieniu 500 metrów od mieszkania (szkoły, kliniki, poczty, przedszkola, restauracje, wydziały, apteki)
9. `[poiName]Distance` - odległość od najblższego zadanego POI (szkoły, kliniki, poczty, przedszkola, restauracje, wydziały, apteki)
10. `condition` - stan mieszkania
11. `has[features]` - kluczowe cechy takie jak , balkon, winda, ochrona, schowek
12. `price` - cena oferty w złotówkach
```{r import data}
files <- list.files("rent", pattern = "\\.csv$", full.names = TRUE)
files <- sort(files)
df_list <- lapply(seq_along(files), function(i) {
d <- read.csv(files[i])
d$month_id <- i
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
})
rent <- do.call(rbind, df_list)
apartments_list <- lapply(seq_along(files_apartments), function(i) {
d <- read.csv(files_apartments[i])
d$offer_type = "apartment"
d$month_id = i
d
})
# price/m^2
rent$price_m2 <- rent$price / rent$squareMeters
df <- do.call(rbind, c(rent_list, apartments_list))
has_cols <- grep("^has", names(rent), value = TRUE)
df$price_m2 <- df$price / df$squareMeters
df$log_price <- log10(df$price)
df$log_price_m2 <- log10(df$price_m2)
df$floor_ratio <- df$floor / df$floorCount
df$age <- 2024 - df$buildYear
rent[has_cols] <- lapply(rent[has_cols], function(x) {
df$city <- as.factor(df$city)
df$offer_type <- as.factor(df$offer_type)
has_cols <- grep("^has", names(df), value = TRUE)
df[has_cols] <- lapply(df[has_cols], function(x) {
tolower(x) == "yes"
})
rent$poi_group <- cut(rent$poiCount,
breaks = c(-1, 2, 5, 10, Inf),
labels = c("0–2", "3–5", "6–10", "11+"))
rent_norm <- rent %>%
df <- df %>%
group_by(city) %>%
mutate(
latitude_n = as.numeric(scale(latitude)),
longitude_n = as.numeric(scale(longitude)),
price_n = (price - min(price)) / (max(price) - min(price))
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))
) %>%
mutate() %>%
ungroup()
rent_wo_outliers <- rent %>%
group_by(city) %>%
filter(log(price_m2) > 3) %>%
ungroup()
cities <- unique(rent$city)
df <- df %>%
mutate(city = str_to_title(city))
rent <- df[df$offer_type == "rent",]
rent_warsaw <- rent[rent$city == "Warszawa",]
apartments <- df[df$offer_type == "apartment",]
apartments_warsaw <- apartments[apartments$city == "Warszawa",]
```
# Wykresy
## Wstępna analiza
```{r heatmaps}
ggplot(rent, aes(x = city, y = price_m2)) +
geom_violin(fill = "lightgray", alpha = 0.3) +
```{r first overview}
summary(rent)
summary(apartments)
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),
se = sd(price_m2, na.rm = TRUE) / sqrt(sum(!is.na(price_m2))),
lwr = mean_price_m2 - qt(0.975, df = sum(!is.na(price_m2)) - 1) * se,
upr = mean_price_m2 + qt(0.975, df = sum(!is.na(price_m2)) - 1) * se,
.groups = "drop"
)
apartments_city <- apartments %>%
group_by(city) %>%
summarise(
mean_price_m2 = mean(price_m2, na.rm = TRUE),
se = sd(price_m2, na.rm = TRUE) / sqrt(sum(!is.na(price_m2))),
lwr = mean_price_m2 - qt(0.975, df = sum(!is.na(price_m2)) - 1) * se,
upr = mean_price_m2 + qt(0.975, df = sum(!is.na(price_m2)) - 1) * se,
.groups = "drop"
)
df_city_pie <- df_city %>%
mutate(
city = fct_lump(city, n = 7, w = perc),
city = fct_recode(city, "Inne" = "Other")
) %>%
group_by(city) %>%
summarise(
perc = sum(perc),
.groups = "drop"
) %>%
mutate(city = fct_reorder(city, city == "Inne", .fun = max)) %>%
arrange(desc(city == "Inne"), desc(perc)) %>%
mutate(
city = factor(city, levels = city),
legend_label = paste0(city, " (", round(perc, 1), "%)")
)
ggplot(df_city_pie,
aes(x = "", y = perc, fill = city)) +
geom_col(width = 1, color = "white") +
coord_polar(theta = "y", start = pi/2) +
scale_fill_discrete(
labels = df_city_pie$legend_label
) +
labs(
title = "Procent ofert według miasta",
fill = "Miasto"
) +
theme_void() +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "right"
)
ggplot(apartments_city, aes(x = reorder(city, mean_price_m2),
y = mean_price_m2)) +
geom_col(fill = "purple") +
geom_errorbar(aes(ymin = lwr, ymax = upr), width = 0.2) +
coord_flip() +
labs(
title = "Średnia cena/m² wg miasta (kupno)",
x = "Miasto",
y = "Średnia cena w PLN/m²"
) +
theme_minimal()
ggplot(rent_city, aes(x = reorder(city, mean_price_m2),
y = mean_price_m2)) +
geom_col(fill = "seagreen") +
geom_errorbar(aes(ymin = lwr, ymax = upr), width = 0.2) +
coord_flip() +
labs(
title = "Średnia cena/m² wg miasta (wynajem)",
x = "Miasto",
y = "Średnia cena w PLN/m²"
) +
theme_minimal()
features <- c("hasBalcony", "hasElevator", "hasParkingSpace", "hasSecurity", "hasStorageRoom")
wilson_ci <- map_dfr(features, function(f) {
x <- sum(apartments[[f]] == TRUE, na.rm = TRUE)
n <- sum(!is.na(apartments[[f]]))
res <- binom.confint(x, n, methods = "wilson")
tibble(
feature = f,
x = x,
n = n,
prop = x / n,
lower = res$lower,
upper = res$upper
)
})
ggplot(wilson_ci, aes(x = reorder(feature, prop), y = prop)) +
geom_col(fill = "steelblue") +
geom_errorbar(
aes(ymin = lower, ymax = upper),
width = 0.2
) +
coord_flip() +
theme_minimal() +
labs(
title = "Udział cech w populacji mieszkań (+ przedziały Wilsona)",
x = "Cecha",
y = "Proporcja mieszkań"
)
```
# Analiza cen
## Ceny/m\^2 w miastach
```{r violiny}
ggplot(apartments, aes(x = city, y = price_m2)) +
geom_violin(fill = "purple", alpha = 0.3) +
geom_boxplot(width = 0.1, outlier.alpha = 0.01) +
stat_summary(fun = mean,
geom = "point",
shape = 21,
size = 0.1,
fill = "red") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Rozkład ceny za m² w miastach",
labs(title = "Zależność ceny za m² w od miasta (kupno)",
x = "Miasto",
y = "Cena za m² (PLN)")
ggplot(rent, aes(x=centreDistance, y=price_m2, col = buildYear)) +
geom_point(, alpha=0.1, size=0.5) +
geom_smooth(method = "lm", se = TRUE, color = "red") +
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)")
```
## Zgodność rozkładów pomiędzy miesiącami
```{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(option = "plasma") +
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(option = "plasma") +
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"
)
```
## Czy ceny są log-normalne?
### Porównanie dystrybuant empirycznych z dystrybuantami estymowanych rozkładów normalnych
```{r dystrybuanty}
z <- seq(min(rent$log_price_m2,apartments$log_price_m2), max(rent$log_price_m2,apartments$log_price_m2), length.out = 300000)
dystrybuanta_empiryczna_wynajem<-ecdf(rent$log_price_m2)
dystrybuanta_empiryczna_kupno<-ecdf(apartments$log_price_m2)
dystrybuanta_teoretyczna_wynajem<-pnorm(z, mean=mean(rent$log_price_m2), sd=sd(rent$log_price_m2))
dystrybuanta_teoretyczna_kupno<-pnorm(z, mean=mean(apartments$log_price_m2), sd=sd(apartments$log_price_m2))
df$colour = lapply(df$offer_type, function (t) if(t == "rent") "seagreen" else "purple")
ggplot(df, aes(x = log_price_m2_n, fill = colour)) +
geom_histogram(aes(y = after_stat(density)),
bins = 50,
color = "white") +
facet_wrap(~ offer_type) +
labs(
title = "Rozkład Log ceny na m²",
x = "Log ceny na m²",
y = "Gestosc"
) +
scale_fill_identity()
qqnorm(apartments$log_price_m2_n)
qqline(apartments$log_price_m2_n, col = "red")
qqnorm(rent$log_price_m2_n)
qqline(rent$log_price_m2_n, col = "red")
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))
lillie.test(apartments$log_price_m2)
lillie.test(rent$log_price_m2)
var.test(rent$log_price_m2, apartments$log_price_m2)
```
## Cena a dokładna lokalizacja
```{r heatmapy}
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 = "długość geograficzna",
y = "szerokość geograficzna")
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 = "długość geograficzna",
y = "szerokość geograficzna")
```
### Warszawa
```{r heatmapy Warszawa}
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 = "długość geograficzna",
y = "szerokość geograficzna")
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 = "długość geograficzna",
y = "szerokość geograficzna")
```
# Regresja
```{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 = "Cena za m² vs odległość od centrum",
title = "Zależność ceny za m² od odległości od centrum (kupno)",
x = "Odległość od centrum (km)",
y = "Cena za m² (PLN)")
ggplot(rent_norm, aes(x=longitude_n, y=latitude_n, col=price_m2)) +
geom_point(alpha=0.01, size=0.2) +
```
```{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) +
scale_color_viridis_c() +
theme_minimal()
ggplot(rent[rent$city=="warszawa", ], aes(x=longitude, y=latitude, col=log(price))) +
geom_point(alpha=0.1, size=0.2) +
scale_color_viridis_c() +
theme_minimal()
ggplot(rent[rent$city=="warszawa", ], aes(x=longitude, y=latitude, col=price_m2)) +
geom_point(alpha=0.1, size=0.2) +
scale_color_viridis_c() +
theme_minimal()
ggplot(rent, aes(x = poi_group, y = price_m2)) +
geom_boxplot(fill = "lightgray") +
stat_summary(fun = mean, geom = "point", shape = 21, size = 3, fill = "red") +
theme_minimal() +
labs(
title = "Cena za m² w zależności od dostępności POI",
x = "POI (grupy)",
y = "Cena za m² (PLN)"
title = "Zależność ceny za m² od odległości od centrum (wynajem)",
x = "Odległość od centrum (km)",
y = "Cena za m² (PLN)")
```
## Model liniowy
```{r model liniowy kupna}
model_apartments <- lm(log_price_m2 ~ month_id + centreDistance +
squareMeters +
age +
floor_ratio +
city +
hasElevator +
hasBalcony +
hasSecurity +
hasParkingSpace +
hasStorageRoom +
poiCount +
condition,
data = apartments, na.action = na.exclude)
coef_apartments <- tidy(model_apartments, conf.int = TRUE) %>%
filter(term != "(Intercept)") %>%
arrange(estimate)
summary(model_apartments)
ggplot(coef_apartments, aes(x = reorder(term, estimate), y = estimate)) +
geom_col(fill = "purple") +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
width = 0.2) +
coord_flip() +
theme_minimal() +
labs(
title = "Wpływ zmiennych na log(cena/m²) – kupno",
x = "Zmienna",
y = "Współczynnik (β)"
)
par(mfrow = c(2,2))
plot(model_apartments)
numeric_df <- apartments %>%
select(price_m2, month_id,
centreDistance,
squareMeters,
age,
floor_ratio,
hasElevator,
hasBalcony,
hasSecurity,
hasParkingSpace,
hasStorageRoom,
poiCount)
corr <- cor(numeric_df, use = "complete.obs")
melted <- melt(corr)
ggplot(melted, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_viridis_c(option = "plasma") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
```
# Czy ceny normalne?
```{r model liniowy wynajmu}
```{r lillieford}
log_rent <- log(rent$price_m2)
summary(log_rent)
prices_overall <- log_rent
lillie.test(prices_overall)
model_rent <- lm(log_price_m2 ~ month_id + centreDistance +
squareMeters +
age +
floor_ratio +
city +
hasElevator +
hasBalcony +
hasSecurity +
hasParkingSpace +
hasStorageRoom +
poiCount +
condition,
data = rent, na.action = na.exclude)
df_plot <- data.frame(log_price = log_rent)
ggplot(df_plot, aes(x = log_price)) +
geom_histogram(aes(y = after_stat(density)),
bins = 50,
fill = "lightblue",
color = "white")
coef_rent <- tidy(model_rent, conf.int = TRUE) %>%
filter(term != "(Intercept)") %>%
arrange(estimate)
results <- rent %>%
group_by(city) %>%
summarise(p_value = lillie.test(log(price_m2))$p.value, mean = mean(price_m2), var = var(price_m2))
summary(model_rent)
show(results)
ggplot(coef_rent, aes(x = reorder(term, estimate), y = estimate)) +
geom_col(fill = "seagreen") +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
width = 0.2) +
coord_flip() +
theme_minimal() +
labs(
title = "Wpływ zmiennych na log(cena/m²) – wynajem",
x = "Zmienna",
y = "Współczynnik (β)"
)
qqnorm(log_rent)
qqline(log_rent, col = "red")
par(mfrow = c(2,2))
plot(model_rent)
qqnorm(log(rent_wo_outliers$price_m2))
qqline(log(rent_wo_outliers$price_m2), col = "red")
numeric_df <- rent %>%
select(price_m2, month_id,
centreDistance,
squareMeters,
age,
floor_ratio,
hasElevator,
hasBalcony,
hasSecurity,
hasParkingSpace,
hasStorageRoom,
poiCount)
corr <- cor(numeric_df, use = "complete.obs")
melted <- melt(corr)
ggplot(melted, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
```
# Wiek vs Cena
## Przewidywania rynku najmu/sprzedaży
```{r buildYear}
ggplot(rent) + geom_jitter(aes(x=buildYear, y=price, col=centreDistance), alpha=0.2, size=0.01) + scale_color_viridis_c()
```{r predictions}
rent_pred <- cbind(
rent,
predict(model_rent, interval = "confidence") %>%
as.data.frame()
)
apartments_pred <- cbind(
apartments,
predict(model_apartments, interval = "confidence") %>%
as.data.frame()
)
rent_plot <- rent_pred %>%
group_by(month_id) %>%
summarise(
fit = mean(fit, na.rm = TRUE),
lwr = mean(lwr, na.rm = TRUE),
upr = mean(upr, na.rm = TRUE),
.groups = "drop"
)
apartments_plot <- apartments_pred %>%
group_by(month_id) %>%
summarise(
fit = mean(fit, na.rm = TRUE),
lwr = mean(lwr, na.rm = TRUE),
upr = mean(upr, na.rm = TRUE),
.groups = "drop"
)
ggplot(apartments_plot,
aes(x = month_id, y = fit)) +
geom_line(color = "purple", linewidth = 1.3) +
geom_point(color = "purple", size = 2) +
geom_ribbon(aes(ymin = lwr, ymax = upr),
alpha = 0.2,
fill = "purple") +
theme_minimal() +
labs(
title = "Trend przewidywanych cen (kupno)",
x = "Miesiąc",
y = "log(cena/m²)"
)
ggplot(rent_plot,
aes(x = month_id, y = fit)) +
geom_line(color = "darkgreen", linewidth = 1.3) +
geom_point(color = "darkgreen", size = 2) +
geom_ribbon(aes(ymin = lwr, ymax = upr),
alpha = 0.2,
fill = "green") +
theme_minimal() +
labs(
title = "Trend przewidywanych cen (wynajem)",
x = "Miesiąc",
y = "log(cena/m²)"
)
```
## Porównanie rynków
```{r}
var(rent$price)
var(apartments$price)
```
```{r dywergencja rynków}
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ń: kupno vs wynajem",
subtitle = "Różnica średniej log-ceny m² (kupno - wynajem)",
x = "Miesiąc",
y = "Dywergencja (log price m²)"
) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
+122
View File
@@ -0,0 +1,122 @@
---
title: "Projekt Zaliczeniowy 1"
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(dplyr)
library(ggplot2)
library(nortest)
fig.width = 8
fig.height = 5
```
### Data Fields
1. ```city``` - the name of the city where the property is located
1. ```type``` - type of the building
1. ```squareMeters``` - the size of the apartment in square meters
1. ```rooms``` - number of rooms in the apartment
1. ```floor``` / floorCount - the floor where the apartment is located and the total number of floors in the building
1. ```buildYear``` - the year when the building was built
1. ```latitude```, ```longitude``` - geo coordinate of the property
1. ```centreDistance``` - distance from the city centre in km
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. ```ownership``` - the type of property ownership
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``` - offer price in Polish Zloty
apartments_pl_YYYY_MM.csv: sale price
apartments_rent_pl_YYYY_MM.csv: monthly rent
```{r import data}
files <- list.files("rent", pattern = "\\.csv$", full.names = TRUE)
files <- sort(files)
df_list <- lapply(seq_along(files), function(i) {
d <- read.csv(files[i])
d$month_id <- i
d
})
rent <- do.call(rbind, df_list)
has_cols <- grep("^has", names(rent), value = TRUE)
rent[has_cols] <- lapply(rent[has_cols], function(x) {
tolower(x) == "yes"
})
rent_norm <- rent %>%
group_by(city) %>%
mutate(
latitude_n = as.numeric(scale(latitude)),
longitude_n = as.numeric(scale(longitude)),
price_n = (price - min(price)) / (max(price) - min(price))
) %>%
mutate() %>%
ungroup()
```
# Ceny, heatmapy
```{r heatmaps}
ggplot(rent) + geom_point(aes(x=city, y=price, col=centreDistance), alpha=0.1, size=0.5) + scale_color_viridis_c()
ggplot(rent_norm) + geom_point(aes(x=longitude_n, y=latitude_n, col=log(price)), alpha=0.01, size=0.2) + facet_wrap(~ city) + scale_color_viridis_c()
ggplot(rent[rent$city=="warszawa", ]) + geom_point(aes(x=longitude, y=latitude, col=log(price)), alpha=0.1, size=0.1) + scale_color_viridis_c()
```
# Czy ceny normalne?
```{r isnormal}
prices_overall <- rent$price
lillie.test(prices_overall)
prices_warsaw <- rent[rent$city=="bialystok", ]$price
lillie.test(prices_warsaw)
prices_without_warsaw <- rent[rent$city!="warszawa", ]$price
lillie.test(prices_without_warsaw)
```
```{r dyst empi vs dyst teor}
install.packages("ggplot2")
library(ggplot2)
z <- seq(min(x), max(x), length.out = 20000)
x<-log(rent$price_m2)
dystrybuanta_empiryczna<-ecdf(x)
dystrybuanta_teoretyczna<-pnorm(z, mean=mean(x), sd=sd(x))
ggplot() +
# Dystrybuanta empiryczna
geom_step(
aes(x = z, y = dystrybuanta_empiryczna(z), color = "Empiryczna"),
linewidth = 1,
linetype = "solid"
) +
# Dystrybuanta teoretyczna
geom_line(
aes(x = z, y = dystrybuanta_teoretyczna, color = "Teoretyczna"),
linewidth = 1,
linetype = "dashed"
) +
# Dodanie legendy i dostosowanie kolorów
scale_color_manual(
values = c("Empiryczna" = "blue", "Teoretyczna" = "red"),
labels = c("Empiryczna", "Teoretyczna")
) +
# Etykiety i tytuł
labs(
title = "Porównanie dystrybuanty empirycznej i teoretycznej",
x = "Wartość",
y = "Prawdopodobieństwo skumulowane",
color = "Typ dystrybuanty" # Tytuł legendy
) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5))
```