Compare commits

..
2 Commits
Author SHA1 Message Date
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
+325 -187
View File
@@ -20,11 +20,14 @@ library(broom)
library(tibble) library(tibble)
library(binom) library(binom)
library(reshape2) library(reshape2)
library(forcats)
library(stringr)
fig.width = 10 fig.width = 10
fig.height = 10 fig.height = 10
``` ```
# O danych # 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). 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).
@@ -50,7 +53,6 @@ Dane są zbierane miesięcznie i obejmują okres od sierpnia 2023 roku do czerwc
```{r import data} ```{r import data}
# Wczytujemy pliki
files_rent <- list.files("rent", pattern = "\\.csv$", full.names = TRUE) files_rent <- list.files("rent", pattern = "\\.csv$", full.names = TRUE)
files_apartments <- list.files("apartments", pattern = "\\.csv$", full.names = TRUE) files_apartments <- list.files("apartments", pattern = "\\.csv$", full.names = TRUE)
files_rent <- sort(files_rent) files_rent <- sort(files_rent)
@@ -72,7 +74,6 @@ apartments_list <- lapply(seq_along(files_apartments), function(i) {
df <- do.call(rbind, c(rent_list, apartments_list)) df <- do.call(rbind, c(rent_list, apartments_list))
# Dodatkowe parametry
df$price_m2 <- df$price / df$squareMeters df$price_m2 <- df$price / df$squareMeters
df$log_price <- log10(df$price) df$log_price <- log10(df$price)
df$log_price_m2 <- log10(df$price_m2) df$log_price_m2 <- log10(df$price_m2)
@@ -81,9 +82,6 @@ df$age <- 2024 - df$buildYear
df$city <- as.factor(df$city) df$city <- as.factor(df$city)
df$offer_type <- as.factor(df$offer_type) 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) has_cols <- grep("^has", names(df), value = TRUE)
@@ -107,17 +105,24 @@ df <- df %>%
) %>% ) %>%
ungroup() ungroup()
# Podzbiory danych
df <- df %>%
mutate(city = str_to_title(city))
rent <- df[df$offer_type == "rent",] rent <- df[df$offer_type == "rent",]
rent_warsaw <- rent[rent$city == "warszawa",] rent_warsaw <- rent[rent$city == "Warszawa",]
apartments <- df[df$offer_type == "apartment",] apartments <- df[df$offer_type == "apartment",]
apartments_warsaw <- apartments[apartments$city == "warszawa",] apartments_warsaw <- apartments[apartments$city == "Warszawa",]
``` ```
## Wstępna analiza ## Wstępna analiza
```{r first overview} ```{r first overview}
summary(df) summary(rent)
summary(apartments)
df_city <- df %>% df_city <- df %>%
count(city) %>% count(city) %>%
@@ -128,61 +133,123 @@ df_city <- df %>%
rent_city <- rent %>% rent_city <- rent %>%
group_by(city) %>% group_by(city) %>%
summarise(mean_price_m2 = mean(price_m2, na.rm = TRUE)) 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 %>% apartments_city <- apartments %>%
group_by(city) %>% group_by(city) %>%
summarise(mean_price_m2 = mean(price_m2, na.rm = TRUE)) 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"
)
ggplot(df_city, aes(x = reorder(city, -perc), y = perc)) +
geom_col(fill = "steelblue") + df_city_pie <- df_city %>%
geom_text(aes(label = paste0(round(perc, 1), "%")), mutate(
vjust = -0.3, size = 3) + 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( labs(
title = "Procent ofert według miasta", title = "Procent ofert według miasta",
x = "Miasto", fill = "Miasto"
y = "Procent"
) + ) +
theme_minimal() + theme_void() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) theme(
plot.title = element_text(hjust = 0.5),
ggplot(rent_city, aes(x = reorder(city, mean_price_m2), legend.position = "right"
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), ggplot(apartments_city, aes(x = reorder(city, mean_price_m2),
y = mean_price_m2)) + y = mean_price_m2)) +
geom_col(fill = "purple") + geom_col(fill = "purple") +
geom_errorbar(aes(ymin = lwr, ymax = upr), width = 0.2) +
coord_flip() + coord_flip() +
labs( labs(
title = "Średnia cena/m² wg miasta (kupno)", title = "Średnia cena/m² wg miasta (kupno)",
x = "Miasto", x = "Miasto",
y = "Średnia cena/m²" y = "Średnia cena w PLN/m²"
) + ) +
theme_minimal() 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 # Analiza cen
## Ceny/m^2 w miastach ## Ceny/m^2 w miastach
```{r violiny} ```{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)) + ggplot(apartments, aes(x = city, y = price_m2)) +
geom_violin(fill = "purple", alpha = 0.3) + geom_violin(fill = "purple", alpha = 0.3) +
geom_boxplot(width = 0.1, outlier.alpha = 0.01) + geom_boxplot(width = 0.1, outlier.alpha = 0.01) +
@@ -191,6 +258,15 @@ ggplot(apartments, aes(x = city, y = price_m2)) +
labs(title = "Zależność ceny za m² w od miasta (kupno)", labs(title = "Zależność ceny za m² w od miasta (kupno)",
x = "Miasto", x = "Miasto",
y = "Cena za m² (PLN)") y = "Cena za m² (PLN)")
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 ## Zgodność rozkładów pomiędzy miesiącami
@@ -222,7 +298,7 @@ ks_long <- apartments %>%
ggplot(ks_long, aes(m1, m2, fill = ks)) + ggplot(ks_long, aes(m1, m2, fill = ks)) +
geom_tile() + geom_tile() +
facet_wrap(~city) + facet_wrap(~city) +
scale_fill_viridis_c() + scale_fill_viridis_c(option = "plasma") +
theme_minimal() + theme_minimal() +
labs( labs(
title = "Statystyka K-S D pomiędzy miesiącami (kupno)", title = "Statystyka K-S D pomiędzy miesiącami (kupno)",
@@ -232,7 +308,7 @@ ggplot(ks_long, aes(m1, m2, fill = ks)) +
ggplot(ks_long, aes(m1, m2, fill = p >= 0.05)) + ggplot(ks_long, aes(m1, m2, fill = p >= 0.05)) +
geom_tile() + geom_tile() +
facet_wrap(~city) + facet_wrap(~city) +
scale_fill_viridis_c() + scale_fill_viridis_c(option = "plasma") +
theme_minimal() + theme_minimal() +
labs( labs(
title = "Kiedy odrzucamy H_0 (dystrubuanty z tego samego rozkładu)", title = "Kiedy odrzucamy H_0 (dystrubuanty z tego samego rozkładu)",
@@ -267,7 +343,7 @@ ks_long <- rent %>%
ggplot(ks_long, aes(m1, m2, fill = ks)) + ggplot(ks_long, aes(m1, m2, fill = ks)) +
geom_tile() + geom_tile() +
facet_wrap(~city) + facet_wrap(~city) +
scale_fill_viridis_c(option = "plasma") + scale_fill_viridis_c() +
theme_minimal() + theme_minimal() +
labs( labs(
title = "Statystyka K-S D pomiędzy miesiącami (wynajem)", title = "Statystyka K-S D pomiędzy miesiącami (wynajem)",
@@ -277,7 +353,7 @@ ggplot(ks_long, aes(m1, m2, fill = ks)) +
ggplot(ks_long, aes(m1, m2, fill = p >= 0.05)) + ggplot(ks_long, aes(m1, m2, fill = p >= 0.05)) +
geom_tile() + geom_tile() +
facet_wrap(~city) + facet_wrap(~city) +
scale_fill_viridis_c(option = "plasma") + scale_fill_viridis_c() +
theme_minimal() + theme_minimal() +
labs( labs(
title = "Kiedy odrzucamy H_0 (dystrubuanty z tego samego rozkładu)", title = "Kiedy odrzucamy H_0 (dystrubuanty z tego samego rozkładu)",
@@ -285,55 +361,9 @@ ggplot(ks_long, aes(m1, m2, fill = p >= 0.05)) +
) )
``` ```
## Cena a dokładna lokalizacja ## Czy ceny są log-normalne?
```{r heatmapy} ### Porównanie dystrybuant empirycznych z dystrybuantami estymowanych rozkładów normalnych
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")
```
### Warszawa
```{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")
```
# Czy ceny są log-normalne?
## Porównanie dystrybuant empirycznych z dystrybuantami estymowanych rozkładów normalnych
```{r dystrybuanty} ```{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) z <- seq(min(rent$log_price_m2,apartments$log_price_m2), max(rent$log_price_m2,apartments$log_price_m2), length.out = 300000)
@@ -343,6 +373,27 @@ 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_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)) 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() + ggplot() +
geom_step( geom_step(
aes(x = z, y = dystrybuanta_empiryczna_wynajem(z), color = "Empiryczna wyn"), aes(x = z, y = dystrybuanta_empiryczna_wynajem(z), color = "Empiryczna wyn"),
@@ -377,26 +428,73 @@ ggplot() +
theme_minimal() + theme_minimal() +
theme(plot.title = element_text(hjust = 0.5)) theme(plot.title = element_text(hjust = 0.5))
lillie.test(rent$log_price_m2)
lillie.test(apartments$log_price_m2) lillie.test(apartments$log_price_m2)
lillie.test(rent$log_price_m2)
df_plot <- data.frame(log_price_m2 = df$log_price_m2_n) var.test(rent$log_price_m2, apartments$log_price_m2)
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) ## Cena a dokładna lokalizacja
qqline(rent$log_price_m2_n, col = "red")
qqnorm(apartments$log_price_m2_n) ```{r heatmapy}
qqline(apartments$log_price_m2_n, col = "red") 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 # 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 = "Zależność ceny za m² od odległości od centrum (kupno)",
x = "Odległość od centrum (km)",
y = "Cena za m² (PLN)")
```
```{r prosta regresja ceny od odległości od centrum dla wynajmu} ```{r prosta regresja ceny od odległości od centrum dla wynajmu}
ggplot(rent, aes(x=centreDistance, y=price_m2)) + ggplot(rent, aes(x=centreDistance, y=price_m2)) +
@@ -411,56 +509,10 @@ ggplot(rent, aes(x=centreDistance, y=price_m2)) +
``` ```
```{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)")
```
## Model liniowy ## Model liniowy
```{r model liniowy wynajmu}
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()
```
```{r model liniowy kupna} ```{r model liniowy kupna}
model_apartments <- lm(log_price_m2 ~ month_num + centreDistance + model_apartments <- lm(log_price_m2 ~ month_id + centreDistance +
squareMeters + squareMeters +
age + age +
floor_ratio + floor_ratio +
@@ -468,18 +520,46 @@ model_apartments <- lm(log_price_m2 ~ month_num + centreDistance +
hasElevator + hasElevator +
hasBalcony + hasBalcony +
hasSecurity + hasSecurity +
hasParkingSpace +
hasStorageRoom +
poiCount +
condition, condition,
data = apartments, na.action = na.exclude) data = apartments, na.action = na.exclude)
par(mfrow = c(2,2))
plot(model_rent) coef_apartments <- tidy(model_apartments, conf.int = TRUE) %>%
filter(term != "(Intercept)") %>%
arrange(estimate)
summary(model_apartments) 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 %>% numeric_df <- apartments %>%
select(price_m2, squareMeters, centreDistance, age, hasElevator, select(price_m2, month_id,
hasBalcony, centreDistance,
hasSecurity) squareMeters,
age,
floor_ratio,
hasElevator,
hasBalcony,
hasSecurity,
hasParkingSpace,
hasStorageRoom,
poiCount)
corr <- cor(numeric_df, use = "complete.obs") corr <- cor(numeric_df, use = "complete.obs")
@@ -488,7 +568,69 @@ melted <- melt(corr)
ggplot(melted, aes(Var1, Var2, fill = value)) + ggplot(melted, aes(Var1, Var2, fill = value)) +
geom_tile() + geom_tile() +
scale_fill_viridis_c(option = "plasma") + scale_fill_viridis_c(option = "plasma") +
theme_minimal() theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
```
```{r model liniowy wynajmu}
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)
coef_rent <- tidy(model_rent, conf.int = TRUE) %>%
filter(term != "(Intercept)") %>%
arrange(estimate)
summary(model_rent)
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 (β)"
)
par(mfrow = c(2,2))
plot(model_rent)
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))
``` ```
## Przewidywania rynku najmu/sprzedaży ## Przewidywania rynku najmu/sprzedaży
@@ -507,7 +649,7 @@ apartments_pred <- cbind(
) )
rent_plot <- rent_pred %>% rent_plot <- rent_pred %>%
group_by(month_num) %>% group_by(month_id) %>%
summarise( summarise(
fit = mean(fit, na.rm = TRUE), fit = mean(fit, na.rm = TRUE),
lwr = mean(lwr, na.rm = TRUE), lwr = mean(lwr, na.rm = TRUE),
@@ -516,7 +658,7 @@ rent_plot <- rent_pred %>%
) )
apartments_plot <- apartments_pred %>% apartments_plot <- apartments_pred %>%
group_by(month_num) %>% group_by(month_id) %>%
summarise( summarise(
fit = mean(fit, na.rm = TRUE), fit = mean(fit, na.rm = TRUE),
lwr = mean(lwr, na.rm = TRUE), lwr = mean(lwr, na.rm = TRUE),
@@ -524,26 +666,8 @@ apartments_plot <- apartments_pred %>%
.groups = "drop" .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, ggplot(apartments_plot,
aes(x = month_num, y = fit)) + aes(x = month_id, y = fit)) +
geom_line(color = "purple", linewidth = 1.3) + geom_line(color = "purple", linewidth = 1.3) +
geom_point(color = "purple", size = 2) + geom_point(color = "purple", size = 2) +
@@ -555,9 +679,27 @@ ggplot(apartments_plot,
theme_minimal() + theme_minimal() +
labs( labs(
title = "Trend przewidywanych cen – sprzedaż mieszkań", title = "Trend przewidywanych cen (kupno)",
x = "Miesiąc", x = "Miesiąc",
y = "log(price/m²)" 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²)"
) )
``` ```
@@ -582,14 +724,10 @@ ggplot(wide, aes(x = month_id, y = divergence, group = 1)) +
geom_point() + geom_point() +
theme_minimal() + theme_minimal() +
labs( labs(
title = "Dywergencja rynku mieszkań: sprzedaż vs wynajem", title = "Dywergencja rynku mieszkań: kupno vs wynajem",
subtitle = "Różnica średniej log-ceny m² (apartments - rent)", subtitle = "Różnica średniej log-ceny m² (kupno - wynajem)",
x = "Miesiąc", x = "Miesiąc",
y = "Dywergencja (log price m²)" y = "Dywergencja (log price m²)"
) + ) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) theme(axis.text.x = element_text(angle = 45, hjust = 1))
trend_model <- lm(divergence ~ as.numeric(month_id), data = wide)
summary(trend_model)
``` ```