Compare commits

...
2 Commits
Author SHA1 Message Date
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
+216 -30
View File
@@ -17,6 +17,7 @@ library(nortest)
library(tidyr)
library(purrr)
library(broom)
library(tibble)
fig.width = 10
fig.height = 10
```
@@ -47,16 +48,20 @@ Dane są zbierane miesięcznie i obejmują okres od sierpnia 2023 roku do czerwc
```{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$type = "rent"
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$type = "apartment"
d$offer_type = "apartment"
d$month_id = i
d
})
@@ -69,6 +74,10 @@ 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) {
@@ -82,7 +91,7 @@ df <- df %>%
longitude_n = as.numeric(scale(longitude))
) %>%
ungroup() %>%
group_by(type) %>%
group_by(offer_type) %>%
mutate(
price_n = as.numeric(scale(price)),
price_m2_n = as.numeric(scale(price_m2)),
@@ -91,10 +100,45 @@ df <- df %>%
) %>%
ungroup()
rent <- df[df$type == "rent",]
rent <- df[df$offer_type == "rent",]
rent_warsaw <- rent[rent$city == "warszawa",]
apartments <- df[df$type == "apartment",]
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
@@ -116,7 +160,96 @@ ggplot(apartments, aes(x = city, y = price_m2)) +
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)) +
@@ -244,7 +377,7 @@ ggplot(df, aes(x = log_price_m2_n)) +
bins = 50,
fill = "lightblue",
color = "white") +
facet_wrap(~ type)
facet_wrap(~ offer_type)
qqnorm(rent$log_price_m2_n)
qqline(rent$log_price_m2_n, col = "red")
@@ -255,29 +388,82 @@ qqline(apartments$log_price_m2_n, col = "red")
# Model liniowy
```{r linear model}
model_rent <- lm(log_price_m2_n ~ centreDistance +
squareMeters +
age +
floor_ratio +
city +
hasElevator +
hasBalcony + hasSecurity +
condition,
data = rent)
summary(model_rent)
par(mfrow = c(2,2))
plot(model_rent)
model_rent <- lm(log_price_m2 ~ centreDistance +
squareMeters +
age +
floor_ratio +
city +
hasElevator +
hasBalcony +
hasSecurity +
condition,
data = rent)
model_apartments <- lm(log_price_m2_n ~ centreDistance +
squareMeters +
age +
floor_ratio +
city +
hasElevator +
hasBalcony + hasSecurity +
condition,
data = apartments)
summary(model_apartments)
par(mfrow = c(2,2))
plot(model_apartments)
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))
```