on the finish line
This commit is contained in:
+116
-21
@@ -20,11 +20,15 @@ library(broom)
|
|||||||
library(tibble)
|
library(tibble)
|
||||||
library(binom)
|
library(binom)
|
||||||
library(reshape2)
|
library(reshape2)
|
||||||
|
library(forcats)
|
||||||
fig.width = 10
|
fig.width = 10
|
||||||
fig.height = 10
|
fig.height = 10
|
||||||
|
Sys.setenv(LANG="pl")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 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).
|
||||||
@@ -81,9 +85,9 @@ 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_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)
|
has_cols <- grep("^has", names(df), value = TRUE)
|
||||||
|
|
||||||
@@ -128,27 +132,65 @@ 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"
|
||||||
|
) %>%
|
||||||
|
# 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(
|
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),
|
||||||
|
legend.position = "right"
|
||||||
|
)
|
||||||
|
|
||||||
ggplot(rent_city, aes(x = reorder(city, mean_price_m2),
|
ggplot(rent_city, aes(x = reorder(city, mean_price_m2),
|
||||||
y = 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() +
|
coord_flip() +
|
||||||
labs(
|
labs(
|
||||||
title = "Średnia cena/m² wg miasta (wynajem)",
|
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),
|
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)",
|
||||||
@@ -428,7 +471,7 @@ ggplot(apartments, aes(x=centreDistance, y=price_m2)) +
|
|||||||
|
|
||||||
```{r model liniowy wynajmu}
|
```{r model liniowy wynajmu}
|
||||||
|
|
||||||
model_rent <- lm(log_price_m2 ~ month_num + centreDistance +
|
model_rent <- lm(log_price_m2 ~ month_id + centreDistance +
|
||||||
squareMeters +
|
squareMeters +
|
||||||
age +
|
age +
|
||||||
floor_ratio +
|
floor_ratio +
|
||||||
@@ -444,6 +487,22 @@ plot(model_rent)
|
|||||||
|
|
||||||
summary(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 %>%
|
numeric_df <- rent %>%
|
||||||
select(price_m2, squareMeters, centreDistance, age, hasElevator,
|
select(price_m2, squareMeters, centreDistance, age, hasElevator,
|
||||||
hasBalcony,
|
hasBalcony,
|
||||||
@@ -460,7 +519,7 @@ ggplot(melted, aes(Var1, Var2, fill = value)) +
|
|||||||
```
|
```
|
||||||
|
|
||||||
```{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 +
|
||||||
@@ -474,7 +533,21 @@ model_apartments <- lm(log_price_m2 ~ month_num + centreDistance +
|
|||||||
par(mfrow = c(2,2))
|
par(mfrow = c(2,2))
|
||||||
plot(model_rent)
|
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 %>%
|
numeric_df <- apartments %>%
|
||||||
select(price_m2, squareMeters, centreDistance, age, hasElevator,
|
select(price_m2, squareMeters, centreDistance, age, hasElevator,
|
||||||
@@ -507,7 +580,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 +589,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),
|
||||||
@@ -525,7 +598,7 @@ apartments_plot <- apartments_pred %>%
|
|||||||
)
|
)
|
||||||
|
|
||||||
ggplot(rent_plot,
|
ggplot(rent_plot,
|
||||||
aes(x = month_num, y = fit)) +
|
aes(x = month_id, y = fit)) +
|
||||||
|
|
||||||
geom_line(color = "darkgreen", linewidth = 1.3) +
|
geom_line(color = "darkgreen", linewidth = 1.3) +
|
||||||
geom_point(color = "darkgreen", size = 2) +
|
geom_point(color = "darkgreen", size = 2) +
|
||||||
@@ -543,7 +616,7 @@ ggplot(rent_plot,
|
|||||||
)
|
)
|
||||||
|
|
||||||
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) +
|
||||||
@@ -591,5 +664,27 @@ ggplot(wide, aes(x = month_id, y = divergence, group = 1)) +
|
|||||||
|
|
||||||
trend_model <- lm(divergence ~ as.numeric(month_id), data = wide)
|
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"
|
||||||
|
)
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user