on the finish line

This commit is contained in:
2026-05-05 20:12:33 +02:00
parent 85e9cb1e9a
commit 2c259e4f1a
+116 -21
View File
@@ -20,11 +20,15 @@ library(broom)
library(tibble)
library(binom)
library(reshape2)
library(forcats)
fig.width = 10
fig.height = 10
Sys.setenv(LANG="pl")
```
# 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).
@@ -81,9 +85,9 @@ 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_id <- as.factor(df$month_id)
df$month_num <- as.numeric(as.character(df$month_id))
#df$month_num <- as.numeric(as.character(df$month_id))
has_cols <- grep("^has", names(df), value = TRUE)
@@ -128,27 +132,65 @@ df_city <- df %>%
rent_city <- rent %>%
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 %>%
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") +
geom_text(aes(label = paste0(round(perc, 1), "%")),
vjust = -0.3, size = 3) +
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"
) %>%
# make sure "Inne" is first
mutate(city = fct_reorder(city, city == "inne", .fun = max)) %>%
# then sort remaining by size
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",
x = "Miasto",
y = "Procent"
fill = "Miasto"
) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
theme_void() +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "right"
)
ggplot(rent_city, aes(x = reorder(city, mean_price_m2),
y = mean_price_m2)) +
geom_col(fill = "green") +
geom_col(fill = "seagreen") +
geom_errorbar(aes(ymin = lwr, ymax = upr), width = 0.2) +
coord_flip() +
labs(
title = "Średnia cena/m² wg miasta (wynajem)",
@@ -160,6 +202,7 @@ ggplot(rent_city, aes(x = reorder(city, mean_price_m2),
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)",
@@ -428,7 +471,7 @@ ggplot(apartments, aes(x=centreDistance, y=price_m2)) +
```{r model liniowy wynajmu}
model_rent <- lm(log_price_m2 ~ month_num + centreDistance +
model_rent <- lm(log_price_m2 ~ month_id + centreDistance +
squareMeters +
age +
floor_ratio +
@@ -444,6 +487,22 @@ plot(model_rent)
summary(model_rent)
coef_rent <- tidy(model_rent, conf.int = TRUE) %>%
filter(term != "(Intercept)") %>%
arrange(estimate)
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 (β)"
)
numeric_df <- rent %>%
select(price_m2, squareMeters, centreDistance, age, hasElevator,
hasBalcony,
@@ -460,7 +519,7 @@ ggplot(melted, aes(Var1, Var2, fill = value)) +
```
```{r model liniowy kupna}
model_apartments <- lm(log_price_m2 ~ month_num + centreDistance +
model_apartments <- lm(log_price_m2 ~ month_id + centreDistance +
squareMeters +
age +
floor_ratio +
@@ -474,7 +533,21 @@ model_apartments <- lm(log_price_m2 ~ month_num + centreDistance +
par(mfrow = c(2,2))
plot(model_rent)
summary(model_apartments)
coef_apartments <- tidy(model_apartments, conf.int = TRUE) %>%
filter(term != "(Intercept)") %>%
arrange(estimate)
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 (β)"
)
numeric_df <- apartments %>%
select(price_m2, squareMeters, centreDistance, age, hasElevator,
@@ -507,7 +580,7 @@ apartments_pred <- cbind(
)
rent_plot <- rent_pred %>%
group_by(month_num) %>%
group_by(month_id) %>%
summarise(
fit = mean(fit, na.rm = TRUE),
lwr = mean(lwr, na.rm = TRUE),
@@ -516,7 +589,7 @@ rent_plot <- rent_pred %>%
)
apartments_plot <- apartments_pred %>%
group_by(month_num) %>%
group_by(month_id) %>%
summarise(
fit = mean(fit, na.rm = TRUE),
lwr = mean(lwr, na.rm = TRUE),
@@ -525,7 +598,7 @@ apartments_plot <- apartments_pred %>%
)
ggplot(rent_plot,
aes(x = month_num, y = fit)) +
aes(x = month_id, y = fit)) +
geom_line(color = "darkgreen", linewidth = 1.3) +
geom_point(color = "darkgreen", size = 2) +
@@ -543,7 +616,7 @@ ggplot(rent_plot,
)
ggplot(apartments_plot,
aes(x = month_num, y = fit)) +
aes(x = month_id, y = fit)) +
geom_line(color = "purple", linewidth = 1.3) +
geom_point(color = "purple", size = 2) +
@@ -591,5 +664,27 @@ ggplot(wide, aes(x = month_id, y = divergence, group = 1)) +
trend_model <- lm(divergence ~ as.numeric(month_id), data = wide)
summary(trend_model)
new_data <- data.frame(
month_id = wide$month_id
)
pred <- predict(
trend_model,
newdata = data.frame(month_id = as.numeric(wide$month_id)),
interval = "confidence",
level = 0.95
)
wide_pred <- cbind(wide, pred)
ggplot(wide_pred, aes(x = as.numeric(month_id))) +
geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = 0.2) +
geom_line(aes(y = fit), linewidth = 1.2, color = "blue") +
geom_point(aes(y = divergence)) +
theme_minimal() +
labs(
title = "Trend dywergencji z przedziałami ufności",
x = "Miesiąc",
y = "Dywergencja"
)
```