diff --git a/projekt.rmd b/projekt.rmd index 3e5ab9b..070b903 100644 --- a/projekt.rmd +++ b/projekt.rmd @@ -8,7 +8,7 @@ output: --- ```{r setup, include=FALSE} -knitr::opts_chunk$set(echo = TRUE) +knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE) library(ggplot2) library(patchwork) library(dplyr) @@ -18,6 +18,8 @@ library(tidyr) library(purrr) library(broom) library(tibble) +library(binom) +library(reshape2) fig.width = 10 fig.height = 10 ``` @@ -46,6 +48,8 @@ Dane są zbierane miesięcznie i obejmują okres od sierpnia 2023 roku do czerwc ```{r import data} + +# Wczytujemy pliki 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) @@ -67,10 +71,10 @@ apartments_list <- lapply(seq_along(files_apartments), function(i) { df <- do.call(rbind, c(rent_list, apartments_list)) -# price/m^2 +# Dodatkowe parametry df$price_m2 <- df$price / df$squareMeters -df$log_price <- log(df$price) -df$log_price_m2 <- log(df$price_m2) +df$log_price <- log10(df$price) +df$log_price_m2 <- log10(df$price_m2) df$floor_ratio <- df$floor / df$floorCount df$age <- 2024 - df$buildYear @@ -78,6 +82,8 @@ df$city <- as.factor(df$city) 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) df[has_cols] <- lapply(df[has_cols], function(x) { @@ -100,10 +106,17 @@ df <- df %>% ) %>% ungroup() +# Podzbiory danych rent <- df[df$offer_type == "rent",] rent_warsaw <- rent[rent$city == "warszawa",] apartments <- df[df$offer_type == "apartment",] apartments_warsaw <- apartments[apartments$city == "warszawa",] +``` + +## Wstępna analiza + +```{r first overview} +summary(df) df_city <- df %>% count(city) %>% @@ -116,12 +129,16 @@ rent_city <- rent %>% group_by(city) %>% summarise(mean_price_m2 = mean(price_m2, na.rm = TRUE)) +apartments_city <- apartments %>% + 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", + title = "Procent ofert według miasta", x = "Miasto", y = "Procent" ) + @@ -130,18 +147,44 @@ ggplot(df_city, aes(x = reorder(city, -perc), y = perc)) + ggplot(rent_city, aes(x = reorder(city, mean_price_m2), y = mean_price_m2)) + - geom_col(fill = "steelblue") + + geom_col(fill = "green") + coord_flip() + labs( - title = "Średnia cena m² wg miasta", + title = "Średnia cena/m² wg miasta (wynajem)", x = "Miasto", - y = "Średnia cena m²" + y = "Średnia cena/m²" + ) + + theme_minimal() + +ggplot(apartments_city, aes(x = reorder(city, mean_price_m2), + y = mean_price_m2)) + + geom_col(fill = "purple") + + coord_flip() + + labs( + title = "Średnia cena/m² wg miasta (kupno)", + x = "Miasto", + y = "Średnia cena/m²" ) + theme_minimal() ``` -# Wykresy +```{r czy miasta się różnią} +kruskal.test(price_m2 ~ city, data = rent) + +kruskal.test(price_m2 ~ city, data = apartments) +``` + +```{r udział balkonów} +x <- sum(df$hasBalcony, na.rm = TRUE) +n <- sum(!is.na(df$hasBalcony)) + +binom.confint(x, n, methods = "wilson") +``` + +# Analiza cen + +## Ceny/m^2 w miastach ```{r violiny} ggplot(rent, aes(x = city, y = price_m2)) + @@ -152,6 +195,7 @@ ggplot(rent, aes(x = city, y = price_m2)) + 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)) + geom_violin(fill = "purple", alpha = 0.3) + geom_boxplot(width = 0.1, outlier.alpha = 0.01) + @@ -162,6 +206,8 @@ ggplot(apartments, aes(x = city, y = price_m2)) + y = "Cena za m² (PLN)") ``` +## Zgodność rozkładów pomiędzy miesiącami + ```{r zgodność rozkładów na przestrzeni miesięcy (kupno)} ks_long <- apartments %>% group_split(city) %>% @@ -234,7 +280,7 @@ ks_long <- rent %>% ggplot(ks_long, aes(m1, m2, fill = ks)) + geom_tile() + facet_wrap(~city) + - scale_fill_viridis_c() + + scale_fill_viridis_c(option = "plasma") + theme_minimal() + labs( title = "Statystyka K-S D pomiędzy miesiącami (wynajem)", @@ -244,13 +290,16 @@ ggplot(ks_long, aes(m1, m2, fill = ks)) + ggplot(ks_long, aes(m1, m2, fill = p >= 0.05)) + geom_tile() + facet_wrap(~city) + - scale_fill_viridis_c() + + scale_fill_viridis_c(option = "plasma") + theme_minimal() + labs( title = "Kiedy odrzucamy H_0 (dystrubuanty z tego samego rozkładu)", x = "Miesiąc", y = "Miesiąc", fill = "p >= 0.05" ) ``` + +## Cena a dokładna lokalizacja + ```{r heatmapy} ggplot(rent, aes(x=longitude_n, y=latitude_n, col=price_m2_n)) + geom_point(alpha=0.03, size=0.4) + @@ -272,6 +321,8 @@ ggplot(apartments, aes(x=longitude_n, y=latitude_n, col=price_m2_n)) + 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) + @@ -293,16 +344,18 @@ ggplot(apartments_warsaw, aes(x=longitude, y=latitude, col=log_price)) + ``` -#Porównanie dystrybuanty empirycznej z dystrybuantą rozkładu normalnego -#o nieobciążonych estymatorach +# Czy ceny normalne? + +## Porównanie dystrybuant empirycznych z dystrybuantami estymowanych rozkładów normalnych + ```{r dystrybuanty} -x<-log(rent$price_m2) -t<-log(apartments$price_m2) -z <- seq(min(x,t), max(x,t), length.out = 300000) -dystrybuanta_empiryczna_wynajem<-ecdf(x) -dystrybuanta_empiryczna_kupno<-ecdf(t) -dystrybuanta_teoretyczna_wynajem<-pnorm(z, mean=mean(x), sd=sd(x)) -dystrybuanta_teoretyczna_kupno<-pnorm(z, mean=mean(t), sd=sd(t)) +z <- seq(min(rent$log_price_m2,apartments$log_price_m2), max(rent$log_price_m2,apartments$log_price_m2), length.out = 300000) + +dystrybuanta_empiryczna_wynajem<-ecdf(rent$log_price_m2) +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_kupno<-pnorm(z, mean=mean(apartments$log_price_m2), sd=sd(apartments$log_price_m2)) + ggplot() + geom_step( aes(x = z, y = dystrybuanta_empiryczna_wynajem(z), color = "Empiryczna wyn"), @@ -336,8 +389,27 @@ ggplot() + ) + theme_minimal() + theme(plot.title = element_text(hjust = 0.5)) + +lillie.test(rent$log_price_m2) +lillie.test(apartments$log_price_m2) + +df_plot <- data.frame(log_price_m2 = df$log_price_m2_n) +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) +qqline(rent$log_price_m2_n, col = "red") + +qqnorm(apartments$log_price_m2_n) +qqline(apartments$log_price_m2_n, col = "red") ``` +# Regresja + ```{r prosta regresja ceny od odległości od centrum dla wynajmu} ggplot(rent, aes(x=centreDistance, y=price_m2)) + @@ -365,30 +437,11 @@ ggplot(apartments, aes(x=centreDistance, y=price_m2)) + ``` -# Czy ceny normalne? +## Model liniowy -```{r lillieford} -lillie.test(rent_warsaw$log_price_m2_n) -lillie.test(apartments_warsaw$log_price_m2_n) +```{r model liniowy wynajmu} -df_plot <- data.frame(log_price_m2 = df$log_price_m2_n) -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) -qqline(rent$log_price_m2_n, col = "red") - -qqnorm(apartments$log_price_m2_n) -qqline(apartments$log_price_m2_n, col = "red") -``` - -# Model liniowy -```{r linear model} -model_rent <- lm(log_price_m2 ~ centreDistance + +model_rent <- lm(log_price_m2 ~ month_num + centreDistance + squareMeters + age + floor_ratio + @@ -397,9 +450,30 @@ model_rent <- lm(log_price_m2 ~ centreDistance + hasBalcony + hasSecurity + condition, - data = rent) + data = rent, na.action = na.exclude) -model_buy <- lm(log_price_m2 ~ centreDistance + +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} +model_apartments <- lm(log_price_m2 ~ month_num + centreDistance + squareMeters + age + floor_ratio + @@ -408,40 +482,101 @@ model_buy <- lm(log_price_m2 ~ centreDistance + hasBalcony + hasSecurity + condition, - data = apartments) + data = apartments, na.action = na.exclude) -model_compare <- lm(log_price_m2 ~ offer_type * - (centreDistance + - squareMeters + - age + - city + - hasElevator + - hasBalcony + - hasSecurity + - condition) + month_id, - data = df, na.action = na.exclude) +par(mfrow = c(2,2)) +plot(model_rent) -df$pred <- predict(model_compare) -summary(df$pred) -sum(is.na(df$pred)) +summary(model_apartments) -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²)") +numeric_df <- apartments %>% + 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(option = "plasma") + + theme_minimal() ``` -```{r propozycje od chata} +## Przewidywania rynku najmu/sprzedaży + +```{r predictions} +rent_pred <- cbind( + rent, + predict(model_rent, interval = "confidence") %>% + as.data.frame() +) + +apartments_pred <- cbind( + apartments, + predict(model_apartments, interval = "confidence") %>% + as.data.frame() +) + +rent_plot <- rent_pred %>% + group_by(month_num) %>% + summarise( + fit = mean(fit, na.rm = TRUE), + lwr = mean(lwr, na.rm = TRUE), + upr = mean(upr, na.rm = TRUE), + .groups = "drop" + ) + +apartments_plot <- apartments_pred %>% + group_by(month_num) %>% + summarise( + fit = mean(fit, na.rm = TRUE), + lwr = mean(lwr, na.rm = TRUE), + upr = mean(upr, na.rm = TRUE), + .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, + aes(x = month_num, y = fit)) + + + geom_line(color = "purple", linewidth = 1.3) + + geom_point(color = "purple", size = 2) + + + geom_ribbon(aes(ymin = lwr, ymax = upr), + alpha = 0.2, + fill = "purple") + + + theme_minimal() + + + labs( + title = "Trend przewidywanych cen – sprzedaż mieszkań", + x = "Miesiąc", + y = "log(price/m²)" + ) +``` + +## Porównanie rynków + +```{r dywergencja rynków} monthly_means <- df %>% group_by(month_id, offer_type) %>% summarise( @@ -466,4 +601,8 @@ ggplot(wide, aes(x = month_id, y = divergence, group = 1)) + y = "Dywergencja (log price m²)" ) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + +trend_model <- lm(divergence ~ as.numeric(month_id), data = wide) + +summary(trend_model) ``` \ No newline at end of file