diff --git a/projekt.rmd b/projekt.rmd index 369d9b5..3e5ab9b 100644 --- a/projekt.rmd +++ b/projekt.rmd @@ -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$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 }) @@ -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) { @@ -151,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)) + @@ -290,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)) ``` \ No newline at end of file