Files
sad/projekt.rmd
T

187 lines
5.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
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 rent 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_rent_pl_YYYY_MM.csv: monthly rent
## 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 <- list.files("rent", pattern = "\\.csv$", full.names = TRUE)
files <- sort(files)
df_list <- lapply(seq_along(files), function(i) {
d <- read.csv(files[i])
d$month_id <- i
d
})
rent <- do.call(rbind, df_list)
# price/m^2
rent$price_m2 <- rent$price / rent$squareMeters
rent$log_price <- log(rent$price_m2)
rent$floor_ratio <- rent$floor / rent$floorCount
rent$age <- 2024 - rent$buildYear
has_cols <- grep("^has", names(rent), value = TRUE)
rent[has_cols] <- lapply(rent[has_cols], function(x) {
tolower(x) == "yes"
})
rent$poi_group <- cut(rent$poiCount,
breaks = c(-1, 2, 5, 10, Inf),
labels = c("0–2", "3–5", "6–10", "11+"))
rent_norm <- rent %>%
group_by(city) %>%
mutate(
latitude_n = as.numeric(scale(latitude)),
longitude_n = as.numeric(scale(longitude)),
price_n = (price - min(price)) / (max(price) - min(price))
) %>%
mutate() %>%
ungroup()
rent_wo_outliers <- rent %>%
group_by(city) %>%
filter(floor >= 7) %>%
ungroup()
cities <- unique(rent$city)
```
# 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) +
stat_summary(fun = mean,
geom = "point",
shape = 21,
size = 0.1,
fill = "red") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Rozkład ceny 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",
x = "Odległość od centrum (km)",
y = "Cena za m² (PLN)")
ggplot(rent_norm, aes(x=longitude_n, y=latitude_n, col=price_m2)) +
geom_point(alpha=0.01, size=0.2) +
facet_wrap(~ city) +
scale_color_viridis_c() +
theme_minimal()
ggplot(rent[rent$city=="warszawa", ], aes(x=longitude, y=latitude, col=log(price))) +
geom_point(alpha=0.1, size=0.2) +
scale_color_viridis_c() +
theme_minimal()
ggplot(rent[rent$city=="warszawa", ], aes(x=longitude, y=latitude, col=price_m2)) +
geom_point(alpha=0.1, size=0.2) +
scale_color_viridis_c() +
theme_minimal()
ggplot(rent, aes(x = poi_group, y = price_m2)) +
geom_boxplot(fill = "lightgray") +
stat_summary(fun = mean, geom = "point", shape = 21, size = 3, fill = "red") +
theme_minimal() +
labs(
title = "Cena za m² w zależności od dostępności POI",
x = "POI (grupy)",
y = "Cena za m² (PLN)"
)
```
# Czy ceny normalne?
```{r lillieford}
log_rent <- log(rent$price_m2)
prices_overall <- log_rent
lillie.test(log(rent_wo_outliers[rent_wo_outliers$city == "warszawa",]$price_m2))
df_plot <- data.frame(log_price = log_rent)
ggplot(df_plot, aes(x = log_price)) +
geom_histogram(aes(y = after_stat(density)),
bins = 50,
fill = "lightblue",
color = "white")
qqnorm(log_rent)
qqline(log_rent, col = "red")
qqnorm(log(rent_wo_outliers$price_m2))
qqline(log(rent_wo_outliers$price_m2), col = "red")
```
# Model liniowy
```{r linear model}
model <- lm(log_price ~ centreDistance +
squareMeters +
age +
floor_ratio +
city +
hasElevator +
hasBalcony + hasSecurity +
condition,
data = rent)
summary(model)
par(mfrow = c(2,2))
plot(model)
```