253 lines
7.9 KiB
Plaintext
253 lines
7.9 KiB
Plaintext
---
|
|
title: "Projekt Zaliczeniowy 1"
|
|
author: "Michał Hiszpański, Nikola Kubiczek, Adrian Mirecki"
|
|
date: "2026-04-29"
|
|
output:
|
|
html_document: default
|
|
pdf_document: default
|
|
---
|
|
|
|
```{r setup, include=FALSE}
|
|
knitr::opts_chunk$set(echo = TRUE)
|
|
library(dplyr)
|
|
library(ggplot2)
|
|
library(nortest)
|
|
library(tidyr)
|
|
library(purrr)
|
|
library(broom)
|
|
fig.width = 8
|
|
fig.height = 5
|
|
```
|
|
|
|
# Import data
|
|
|
|
The dataset contains apartment sales and df offers from the 15 largest cities in Poland (Warsaw, Lodz, Krakow, Wroclaw, Poznan, Gdansk, Szczecin, Bydgoszcz, Lublin, Katowice, Bialystok, Czestochowa). The data comes from local websites with apartments for sale. To fully capture the neighborhood of each apartment better, each offer was extended by data from the Open Street Map with distances to points of interest (POI). The data is collected monthly and covers a timespan between August 2023 and June 2024
|
|
|
|
## Files
|
|
|
|
- apartments_pl_YYYY_MM.csv: sale price
|
|
- apartments_df_pl_YYYY_MM.csv: monthly df
|
|
|
|
## Data Fields
|
|
1. ```city``` - the name of the city where the property is located
|
|
1. ```type``` - type of the building
|
|
1. ```squareMeters``` - the size of the apartment in square meters
|
|
1. ```rooms``` - number of rooms in the apartment
|
|
1. ```floor``` / floorCount - the floor where the apartment is located and the total number of floors in the building
|
|
1. ```buildYear``` - the year when the building was built
|
|
1. ```latitude```, ```longitude``` - geo coordinate of the property
|
|
1. ```centreDistance``` - distance from the city centre in km
|
|
1. ```poiCount``` - number of points of interest in 500m range from the apartment (schools, clinics, post offices, kindergartens, restaurants, colleges, pharmacies)
|
|
1. ```[poiName]Distance``` - distance to the nearest point of interest (schools, clinics, post offices, kindergartens, restaurants, colleges, pharmacies)
|
|
1. ```ownership``` - the type of property ownership
|
|
1. ```condition``` - the condition of the apartment
|
|
1. ```has[features]``` - whether the property has key features such as assigned parking space, balcony, elevator, security, storage room
|
|
1. ```price``` - offer price in Polish Zloty
|
|
|
|
|
|
|
|
```{r import data}
|
|
files_rent <- list.files("rent", pattern = "\\.csv$", full.names = TRUE)
|
|
files_apartments <- list.files("apartments", pattern = "\\.csv$", full.names = TRUE)
|
|
|
|
rent_list <- lapply(seq_along(files_rent), function(i) {
|
|
d <- read.csv(files_rent[i])
|
|
d$type = "rent"
|
|
d
|
|
})
|
|
|
|
apartments_list <- lapply(seq_along(files_apartments), function(i) {
|
|
d <- read.csv(files_apartments[i])
|
|
d$type = "apartment"
|
|
d
|
|
})
|
|
|
|
df <- do.call(rbind, c(rent_list, apartments_list))
|
|
|
|
# price/m^2
|
|
df$price_m2 <- df$price / df$squareMeters
|
|
df$log_price <- log(df$price)
|
|
df$log_price_m2 <- log(df$price_m2)
|
|
df$floor_ratio <- df$floor / df$floorCount
|
|
df$age <- 2024 - df$buildYear
|
|
|
|
has_cols <- grep("^has", names(df), value = TRUE)
|
|
|
|
df[has_cols] <- lapply(df[has_cols], function(x) {
|
|
tolower(x) == "yes"
|
|
})
|
|
|
|
df <- df %>%
|
|
group_by(city) %>%
|
|
mutate(
|
|
latitude_n = as.numeric(scale(latitude)),
|
|
longitude_n = as.numeric(scale(longitude))
|
|
) %>%
|
|
ungroup() %>%
|
|
group_by(type) %>%
|
|
mutate(
|
|
price_n = as.numeric(scale(price)),
|
|
price_m2_n = as.numeric(scale(price_m2)),
|
|
log_price_n = as.numeric(scale(log_price)),
|
|
log_price_m2_n = as.numeric(scale(log_price_m2))
|
|
) %>%
|
|
ungroup()
|
|
|
|
rent <- df[df$type == "rent",]
|
|
rent_warsaw <- rent[rent$city == "warszawa",]
|
|
apartments <- df[df$type == "apartment",]
|
|
apartments_warsaw <- apartments[apartments$city == "warszawa",]
|
|
```
|
|
|
|
# Wykresy
|
|
|
|
```{r heatmaps}
|
|
ggplot(rent, aes(x = city, y = price_m2)) +
|
|
geom_violin(fill = "lightgray", alpha = 0.3) +
|
|
geom_boxplot(width = 0.1, outlier.alpha = 0.01) +
|
|
theme_minimal() +
|
|
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
|
|
labs(title = "Rozkład ceny wynajmu za m² w miastach",
|
|
x = "Miasto",
|
|
y = "Cena za m² (PLN)")
|
|
|
|
ggplot(apartments, aes(x = city, y = price_m2)) +
|
|
geom_violin(fill = "lightgray", alpha = 0.3) +
|
|
geom_boxplot(width = 0.1, outlier.alpha = 0.01) +
|
|
theme_minimal() +
|
|
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
|
|
labs(title = "Rozkład ceny kupna mieszkania za m² w miastach",
|
|
x = "Miasto",
|
|
y = "Cena za m² (PLN)")
|
|
|
|
ggplot(rent, aes(x=centreDistance, y=price_m2, col = buildYear)) +
|
|
geom_point(alpha=0.1, size=0.5) +
|
|
geom_smooth(method = "lm", se = TRUE, color = "red") +
|
|
facet_wrap(~ city) +
|
|
theme_minimal() +
|
|
labs(
|
|
title = "Cena za m² vs odległość od centrum (wynajem)",
|
|
x = "Odległość od centrum (km)",
|
|
y = "Cena za m² (PLN)")
|
|
|
|
ggplot(apartments, aes(x=centreDistance, y=price_m2, col = buildYear)) +
|
|
geom_point(alpha=0.1, size=0.5) +
|
|
geom_smooth(method = "lm", se = TRUE, color = "red") +
|
|
facet_wrap(~ city) +
|
|
theme_minimal() +
|
|
labs(
|
|
title = "Cena za m² vs odległość od centrum (kupno)",
|
|
x = "Odległość od centrum (km)",
|
|
y = "Cena za m² (PLN)")
|
|
|
|
ggplot(rent, aes(x=longitude_n, y=latitude_n, col=price_m2_n)) +
|
|
geom_point(alpha=0.01, size=0.2) +
|
|
facet_wrap(~ city) +
|
|
scale_color_viridis_c() +
|
|
theme_minimal()
|
|
|
|
ggplot(apartments, aes(x=longitude_n, y=latitude_n, col=price_m2_n)) +
|
|
geom_point(alpha=0.01, size=0.2) +
|
|
facet_wrap(~ city) +
|
|
scale_color_viridis_c() +
|
|
theme_minimal()
|
|
|
|
ggplot(rent_warsaw, aes(x=longitude, y=latitude, col=log_price)) +
|
|
geom_point(alpha=0.1, size=0.2) +
|
|
scale_color_viridis_c() +
|
|
theme_minimal()
|
|
|
|
ggplot(apartments_warsaw, aes(x=longitude, y=latitude, col=log_price)) +
|
|
geom_point(alpha=0.1, size=0.2) +
|
|
scale_color_viridis_c() +
|
|
theme_minimal()
|
|
|
|
|
|
```
|
|
|
|
# Czy ceny normalne?
|
|
|
|
```{r lillieford}
|
|
lillie.test(rent_warsaw$log_price_m2_n)
|
|
lillie.test(apartments_warsaw$log_price_m2_n)
|
|
|
|
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(~ 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_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_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)
|
|
```
|
|
|
|
#porównanie dystrybuanty empirycznej z teoretyczną
|
|
```{r dyst empi vs dyst teor}
|
|
install.packages("ggplot2")
|
|
library(ggplot2)
|
|
x<-log(rent$price_m2)
|
|
z <- seq(min(x), max(x), length.out = 20000)
|
|
dystrybuanta_empiryczna<-ecdf(x)
|
|
dystrybuanta_teoretyczna<-pnorm(z, mean=mean(x), sd=sd(x))
|
|
ggplot() +
|
|
# Dystrybuanta empiryczna
|
|
geom_step(
|
|
aes(x = z, y = dystrybuanta_empiryczna(z), color = "Empiryczna"),
|
|
linewidth = 1,
|
|
linetype = "solid"
|
|
) +
|
|
# Dystrybuanta teoretyczna
|
|
geom_line(
|
|
aes(x = z, y = dystrybuanta_teoretyczna, color = "Teoretyczna"),
|
|
linewidth = 1,
|
|
linetype = "dashed"
|
|
) +
|
|
# Dodanie legendy i dostosowanie kolorów
|
|
scale_color_manual(
|
|
values = c("Empiryczna" = "blue", "Teoretyczna" = "red"),
|
|
labels = c("Empiryczna", "Teoretyczna")
|
|
) +
|
|
# Etykiety i tytuł
|
|
labs(
|
|
title = "Porównanie dystrybuanty empirycznej i teoretycznej",
|
|
x = "Wartość",
|
|
y = "Prawdopodobieństwo skumulowane",
|
|
color = "Typ dystrybuanty" # Tytuł legendy
|
|
) +
|
|
theme_minimal() +
|
|
theme(plot.title = element_text(hjust = 0.5))
|
|
``` |