1. Are annual earnings different for males and females in european countries?

The data for this research was collected from Eurostat database https://ec.europa.eu/eurostat/databrowser/view/ilc_di03__custom_9221149/default/table?lang=en https://ec.europa.eu/eurostat/databrowser/view/tps00175/default/table?lang=en

library(readxl)
## Warning: пакет 'readxl' был собран под R версии 4.3.2
library(dplyr)
## Warning: пакет 'dplyr' был собран под R версии 4.3.2
## 
## Присоединяю пакет: 'dplyr'
## Следующие объекты скрыты от 'package:stats':
## 
##     filter, lag
## Следующие объекты скрыты от 'package:base':
## 
##     intersect, setdiff, setequal, union
# file path
file_path <- "C:\\Users\\дом\\Desktop\\data1.xlsx"

# Read data from "Sheet 1"
raw_data <- read_excel(file_path, sheet = "Sheet 1")

selected_data <- raw_data %>%
  select(Country, `Annual earnings`, `Annual earnings males`, `Annual earnings females`,
         `Hours worked total`, `Hours worked males`, `Hours worked females`)

str(selected_data)
## tibble [35 × 7] (S3: tbl_df/tbl/data.frame)
##  $ Country                : chr [1:35] "Belgium" "Bulgaria" "Czechia" "Denmark" ...
##  $ Annual earnings        : chr [1:35] "27314" "5378" "12146" "33260" ...
##  $ Annual earnings males  : chr [1:35] "27729" "5664" "12585" "33752" ...
##  $ Annual earnings females: chr [1:35] "26826" "5117" "11788" "32760" ...
##  $ Hours worked total     : chr [1:35] "36.700000000000003" "40.200000000000003" "39.799999999999997" "34.4" ...
##  $ Hours worked males     : chr [1:35] "39.5" "40.4" "41" "36.5" ...
##  $ Hours worked females   : chr [1:35] "33.6" "40" "38.200000000000003" "32" ...
# we need to turn variable numeric
selected_data <- selected_data %>%
  mutate(across(starts_with("Annual earnings") | starts_with("Hours worked"), as.numeric))
## Warning: There were 6 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `across(...)`.
## Caused by warning:
## ! в результате преобразования созданы NA
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 5 remaining warnings.
str(selected_data)
## tibble [35 × 7] (S3: tbl_df/tbl/data.frame)
##  $ Country                : chr [1:35] "Belgium" "Bulgaria" "Czechia" "Denmark" ...
##  $ Annual earnings        : num [1:35] 27314 5378 12146 33260 25000 ...
##  $ Annual earnings males  : num [1:35] 27729 5664 12585 33752 25400 ...
##  $ Annual earnings females: num [1:35] 26826 5117 11788 32760 24578 ...
##  $ Hours worked total     : num [1:35] 36.7 40.2 39.8 34.4 35.2 37.8 36.3 41 37.7 37.2 ...
##  $ Hours worked males     : num [1:35] 39.5 40.4 41 36.5 38.8 39.1 39.6 43 39.9 39.5 ...
##  $ Hours worked females   : num [1:35] 33.6 40 38.2 32 31.1 36.6 32.5 38.2 35.2 34.9 ...

we need another variable for whether the country is in EU

#EU member countries
eu_member_countries <- c("Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czechia", "Denmark", "Estonia", 
                  "Finland", "France", "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", 
                  "Luxembourg", "Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", 
                  "Spain", "Sweden")

#  dummy variable for EU membership
selected_data <- selected_data %>%
  mutate(EU_member_dummy = ifelse(Country %in% eu_member_countries, 1, 0))

head(selected_data)
## # A tibble: 6 × 8
##   Country  `Annual earnings` `Annual earnings males` `Annual earnings females`
##   <chr>                <dbl>                   <dbl>                     <dbl>
## 1 Belgium              27314                   27729                     26826
## 2 Bulgaria              5378                    5664                      5117
## 3 Czechia              12146                   12585                     11788
## 4 Denmark              33260                   33752                     32760
## 5 Germany              25000                   25400                     24578
## 6 Estonia              14827                   15344                     14325
## # ℹ 4 more variables: `Hours worked total` <dbl>, `Hours worked males` <dbl>,
## #   `Hours worked females` <dbl>, EU_member_dummy <dbl>
# Remove rows with empty columns, because some countries don't have any data
selected_data <- selected_data %>%
  filter(!is.na(`Annual earnings`) & !is.na(`Hours worked total`))  

head(selected_data)
## # A tibble: 6 × 8
##   Country  `Annual earnings` `Annual earnings males` `Annual earnings females`
##   <chr>                <dbl>                   <dbl>                     <dbl>
## 1 Belgium              27314                   27729                     26826
## 2 Bulgaria              5378                    5664                      5117
## 3 Czechia              12146                   12585                     11788
## 4 Denmark              33260                   33752                     32760
## 5 Germany              25000                   25400                     24578
## 6 Estonia              14827                   15344                     14325
## # ℹ 4 more variables: `Hours worked total` <dbl>, `Hours worked males` <dbl>,
## #   `Hours worked females` <dbl>, EU_member_dummy <dbl>
summary(selected_data)
##    Country          Annual earnings Annual earnings males
##  Length:30          Min.   : 4509   Min.   : 4551        
##  Class :character   1st Qu.: 9689   1st Qu.: 9917        
##  Mode  :character   Median :17335   Median :17745        
##                     Mean   :19435   Mean   :19868        
##                     3rd Qu.:27159   3rd Qu.:27676        
##                     Max.   :45405   Max.   :46282        
##  Annual earnings females Hours worked total Hours worked males
##  Min.   : 4457           Min.   :31.70      Min.   :35.60     
##  1st Qu.: 9490           1st Qu.:36.40      1st Qu.:39.52     
##  Median :16952           Median :38.65      Median :40.10     
##  Mean   :19024           Mean   :38.08      Mean   :39.91     
##  3rd Qu.:26605           3rd Qu.:39.60      3rd Qu.:40.50     
##  Max.   :44520           Max.   :43.20      Max.   :44.30     
##  Hours worked females EU_member_dummy
##  Min.   :27.20        Min.   :0.0    
##  1st Qu.:33.45        1st Qu.:1.0    
##  Median :36.85        Median :1.0    
##  Mean   :35.98        Mean   :0.9    
##  3rd Qu.:38.58        3rd Qu.:1.0    
##  Max.   :42.00        Max.   :1.0
library(ggplot2)
## Warning: пакет 'ggplot2' был собран под R версии 4.3.2
#box plots of annual earnings by EU membership
ggplot(selected_data, aes(x = factor(EU_member_dummy), y = `Annual earnings`, fill = factor(EU_member_dummy))) +
  geom_boxplot() +
  labs(title = "Box Plot of Annual Earnings by EU Membership",
       x = "EU Member (1 = Yes, 0 = No)",
       y = "Annual Earnings") +
  theme_minimal()

It seems very different, so let’s test the hypothesis

t_test_result <- t.test(`Annual earnings` ~ EU_member_dummy, data = selected_data)

print(t_test_result)
## 
##  Welch Two Sample t-test
## 
## data:  Annual earnings by EU_member_dummy
## t = 0.88675, df = 2.0931, p-value = 0.4652
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -41672.12  64498.64
## sample estimates:
## mean in group 0 mean in group 1 
##        29706.67        18293.41

We confirm H0, there are no significant differencies between groups. let’s try to see if there are gender differencies in income

#paired t-test
paired_t_test_result <- t.test(selected_data$`Annual earnings males`, selected_data$`Annual earnings females`, paired = TRUE, alternative = "greater")

print(paired_t_test_result)
## 
##  Paired t-test
## 
## data:  selected_data$`Annual earnings males` and selected_data$`Annual earnings females`
## t = 8.3138, df = 29, p-value = 1.822e-09
## alternative hypothesis: true mean difference is greater than 0
## 95 percent confidence interval:
##  671.2961      Inf
## sample estimates:
## mean difference 
##        843.7333

we decline the H0 on p < 0.001. There is a statistically significant difference between males and females in income, and males earn more.

library(ggplot2)

#box plot
ggplot(selected_data, aes(x = factor(1), y = `Annual earnings males`)) +
  geom_boxplot(fill = "skyblue", color = "black") +
  geom_jitter(aes(x = factor(1), y = `Annual earnings males`), color = "blue", alpha = 0.7, width = 0.2) +
  geom_boxplot(aes(x = factor(2), y = `Annual earnings females`), fill = "pink", color = "black") +
  geom_jitter(aes(x = factor(2), y = `Annual earnings females`), color = "red", alpha = 0.7, width = 0.2) +
  labs(title = "Distribution of Annual Earnings by Gender",
       x = "Gender",
       y = "Annual Earnings") +
  theme_minimal() +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) +
  stat_summary(fun = mean, geom = "point", shape = 18, size = 3, color = "black", position = position_dodge(width = 0.2))

the difference is not that visible, so let’s try another plot

library(ggplot2)

#density plot
ggplot(selected_data, aes(x = `Annual earnings males`, fill = "Male")) +
  geom_density(alpha = 0.5) +
  geom_density(aes(x = `Annual earnings females`, fill = "Female"), alpha = 0.5) +
  labs(title = "Density Plot of Annual Earnings by Gender",
       x = "Annual Earnings",
       y = "Density") +
  theme_minimal()

for the second part, the regression, we’ll take another dataset. This data was collected by me manually from nepremicnice.net and contains the information about rented apartments in Ljubljana.

library(readxl)

#file path
file_path <- "C:/Users/дом/Documents/Книга1.xlsx"

# read the Excel file
data <- read_excel(file_path, sheet = "Лист1", col_types = c("numeric", "numeric", "numeric", "text", "numeric", "text"))

head(data)
## # A tibble: 6 × 6
##      ID Price `Sqare m` district rooms Furniture
##   <dbl> <dbl>     <dbl> <chr>    <dbl> <chr>    
## 1     1   700       70  bezigrad   2   yes      
## 2     2  1100      132. bezigrad   2.5 no       
## 3     8   500       37  bezigrad   1   yes      
## 4    16   350       16  bezigrad   1   yes      
## 5    17   450       20  bezigrad   1   yes      
## 6     3  5000      215. center     3   yes

price is in EUR per month

# Then we create dummy variables for districts
dummy_matrix <- model.matrix(~district - 1, data = data)

data_with_dummies <- cbind(data, dummy_matrix)

data_with_dummies <- data_with_dummies[, -which(names(data_with_dummies) == "district")]

head(data_with_dummies)
##   ID Price Sqare m rooms Furniture districtbezigrad districtcenter
## 1  1   700    70.0   2.0       yes                1              0
## 2  2  1100   132.5   2.5        no                1              0
## 3  8   500    37.0   1.0       yes                1              0
## 4 16   350    16.0   1.0       yes                1              0
## 5 17   450    20.0   1.0       yes                1              0
## 6  3  5000   215.1   3.0       yes                0              1
##   districtcrnuce districtmoste districtrudnik districtsiska districtsostro
## 1              0             0              0             0              0
## 2              0             0              0             0              0
## 3              0             0              0             0              0
## 4              0             0              0             0              0
## 5              0             0              0             0              0
## 6              0             0              0             0              0
#we need to turn furniture variable into a dummy
data_with_dummies$Furniture_numeric <- ifelse(data_with_dummies$Furniture == "yes", 1, 0)

head(data_with_dummies)
##   ID Price Sqare m rooms Furniture districtbezigrad districtcenter
## 1  1   700    70.0   2.0       yes                1              0
## 2  2  1100   132.5   2.5        no                1              0
## 3  8   500    37.0   1.0       yes                1              0
## 4 16   350    16.0   1.0       yes                1              0
## 5 17   450    20.0   1.0       yes                1              0
## 6  3  5000   215.1   3.0       yes                0              1
##   districtcrnuce districtmoste districtrudnik districtsiska districtsostro
## 1              0             0              0             0              0
## 2              0             0              0             0              0
## 3              0             0              0             0              0
## 4              0             0              0             0              0
## 5              0             0              0             0              0
## 6              0             0              0             0              0
##   Furniture_numeric
## 1                 1
## 2                 0
## 3                 1
## 4                 1
## 5                 1
## 6                 1
summary(data_with_dummies)
##        ID            Price         Sqare m           rooms      
##  Min.   : 1.00   Min.   : 250   Min.   : 16.00   Min.   : 1.00  
##  1st Qu.:13.25   1st Qu.: 750   1st Qu.: 40.50   1st Qu.: 1.00  
##  Median :25.50   Median :1050   Median : 67.15   Median : 2.00  
##  Mean   :25.50   Mean   :1197   Mean   : 79.25   Mean   : 2.35  
##  3rd Qu.:37.75   3rd Qu.:1322   3rd Qu.: 88.75   3rd Qu.: 3.00  
##  Max.   :50.00   Max.   :5000   Max.   :347.00   Max.   :11.00  
##   Furniture         districtbezigrad districtcenter districtcrnuce
##  Length:50          Min.   :0.0      Min.   :0.00   Min.   :0.00  
##  Class :character   1st Qu.:0.0      1st Qu.:0.00   1st Qu.:0.00  
##  Mode  :character   Median :0.0      Median :0.00   Median :0.00  
##                     Mean   :0.1      Mean   :0.12   Mean   :0.14  
##                     3rd Qu.:0.0      3rd Qu.:0.00   3rd Qu.:0.00  
##                     Max.   :1.0      Max.   :1.00   Max.   :1.00  
##  districtmoste districtrudnik districtsiska  districtsostro Furniture_numeric
##  Min.   :0.0   Min.   :0.00   Min.   :0.00   Min.   :0.00   Min.   :0.00     
##  1st Qu.:0.0   1st Qu.:0.00   1st Qu.:0.00   1st Qu.:0.00   1st Qu.:1.00     
##  Median :0.0   Median :0.00   Median :0.00   Median :0.00   Median :1.00     
##  Mean   :0.1   Mean   :0.08   Mean   :0.34   Mean   :0.12   Mean   :0.86     
##  3rd Qu.:0.0   3rd Qu.:0.00   3rd Qu.:1.00   3rd Qu.:0.00   3rd Qu.:1.00     
##  Max.   :1.0   Max.   :1.00   Max.   :1.00   Max.   :1.00   Max.   :1.00
formula <- "Price ~ `Sqare m` + `rooms` + `Furniture_numeric` + `districtbezigrad` + `districtsiska` + `districtcenter` + `districtrudnik` + `districtsostro` + `districtmoste`"

reg_model <- lm(formula, data = data_with_dummies)

summary(reg_model)
## 
## Call:
## lm(formula = formula, data = data_with_dummies)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1126.19  -251.50   -36.26   189.52  1987.74 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        549.011    262.868   2.089   0.0432 *  
## `Sqare m`           10.814      2.033   5.319 4.26e-06 ***
## rooms              -21.628     81.453  -0.266   0.7920    
## Furniture_numeric -170.867    223.781  -0.764   0.4496    
## districtbezigrad  -355.700    301.364  -1.180   0.2449    
## districtsiska      -23.999    238.987  -0.100   0.9205    
## districtcenter     373.009    287.404   1.298   0.2018    
## districtrudnik     113.151    333.321   0.339   0.7360    
## districtsostro     -96.103    318.791  -0.301   0.7646    
## districtmoste      -97.620    313.950  -0.311   0.7575    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 513.4 on 40 degrees of freedom
## Multiple R-squared:  0.6657, Adjusted R-squared:  0.5905 
## F-statistic: 8.851 on 9 and 40 DF,  p-value: 3.541e-07

We need to look for outliers using Cooks distance

cooksd <- cooks.distance(reg_model)

hist(cooksd, main = "Cook's Distances", xlab = "Observation Number", ylab = "Cook's Distance")

we have at least two outliers, so we need to identify them

data_with_cooks <- cbind(data_with_dummies, CooksDistance = cooksd)

ordered_data <- data_with_cooks[order(-data_with_cooks$CooksDistance), ]

print(ordered_data)
##    ID Price Sqare m rooms Furniture districtbezigrad districtcenter
## 46  7  2800   347.0   5.0       yes                0              0
## 6   3  5000   215.1   3.0       yes                0              1
## 45  6  2800   282.0  11.0       yes                0              0
## 10 44   250    25.0   1.0        no                0              1
## 17 23  2500   114.8   4.0        no                0              0
## 2   2  1100   132.5   2.5        no                1              0
## 49 11  1725    69.0   3.0        no                0              0
## 26 26  2600   150.0   3.0       yes                0              0
## 24  5   800    85.0   2.5       yes                0              0
## 23 37  1100   140.0   3.5       yes                0              0
## 47  9  1525    61.0   2.0        no                0              0
## 27 27  1500    60.2   2.5       yes                0              0
## 25 25  1250   113.8   3.0       yes                0              0
## 19  4  1400    65.0   2.0       yes                0              0
## 8  42   500    21.0   1.0       yes                0              1
## 12 18   600    37.0   1.0        no                0              0
## 7  41   750    30.0   1.5       yes                0              1
## 48 10   960    48.0   2.0       yes                0              0
## 5  17   450    20.0   1.0       yes                1              0
## 16 22   790    65.3   2.0       yes                0              0
## 50 12  1250    77.7   2.0       yes                0              0
## 20 33  1300    82.3   3.0       yes                0              0
## 41 47  1800   103.9   3.0       yes                0              0
## 4  16   350    16.0   1.0       yes                1              0
## 11 45   800    25.0   1.0       yes                0              1
## 42 48  1480    77.0   2.0       yes                0              0
## 31 28   490    40.0   1.0       yes                0              0
## 32 29   490    40.0   1.0       yes                0              0
## 35 32  1000    90.0   3.5       yes                0              0
## 30 15   900    50.0   2.0        no                0              0
## 9  43   850    22.0   1.0       yes                0              1
## 3   8   500    37.0   1.0       yes                1              0
## 22 36  1200   100.0   3.0       yes                0              0
## 29 14  1280    74.0   3.0       yes                0              0
## 43 49  1250   105.9   3.5       yes                0              0
## 39 40  1330    77.0   2.0       yes                0              0
## 13 19   700    40.0   1.0       yes                0              0
## 38 39   800    60.0   2.0       yes                0              0
## 40 46  1500   100.6   3.5       yes                0              0
## 37 38   690    47.0   1.5       yes                0              0
## 21 35  1100    76.1   3.0       yes                0              0
## 34 31  1200    76.3   3.5       yes                0              0
## 36 34  1200    72.0   2.0       yes                0              0
## 14 20   750    42.0   1.0       yes                0              0
## 1   1   700    70.0   2.0       yes                1              0
## 15 21   850    50.0   1.5       yes                0              0
## 18 24   790    43.0   1.0       yes                0              0
## 33 30   950    64.0   2.0       yes                0              0
## 44 50  1200    83.0   3.0       yes                0              0
## 28 13   750    39.0   1.0       yes                0              0
##    districtcrnuce districtmoste districtrudnik districtsiska districtsostro
## 46              0             0              0             0              1
## 6               0             0              0             0              0
## 45              0             0              0             0              1
## 10              0             0              0             0              0
## 17              1             0              0             0              0
## 2               0             0              0             0              0
## 49              0             0              0             0              1
## 26              0             0              1             0              0
## 24              0             0              1             0              0
## 23              0             1              0             0              0
## 47              0             0              0             0              1
## 27              0             0              1             0              0
## 25              0             0              1             0              0
## 19              0             1              0             0              0
## 8               0             0              0             0              0
## 12              1             0              0             0              0
## 7               0             0              0             0              0
## 48              0             0              0             0              1
## 5               0             0              0             0              0
## 16              1             0              0             0              0
## 50              0             0              0             0              1
## 20              0             1              0             0              0
## 41              0             0              0             1              0
## 4               0             0              0             0              0
## 11              0             0              0             0              0
## 42              0             0              0             1              0
## 31              0             0              0             1              0
## 32              0             0              0             1              0
## 35              0             0              0             1              0
## 30              0             0              0             1              0
## 9               0             0              0             0              0
## 3               0             0              0             0              0
## 22              0             1              0             0              0
## 29              0             0              0             1              0
## 43              0             0              0             1              0
## 39              0             0              0             1              0
## 13              1             0              0             0              0
## 38              0             0              0             1              0
## 40              0             0              0             1              0
## 37              0             0              0             1              0
## 21              0             1              0             0              0
## 34              0             0              0             1              0
## 36              0             0              0             1              0
## 14              1             0              0             0              0
## 1               0             0              0             0              0
## 15              1             0              0             0              0
## 18              1             0              0             0              0
## 33              0             0              0             1              0
## 44              0             0              0             1              0
## 28              0             0              0             1              0
##    Furniture_numeric CooksDistance
## 46                 1  2.587481e+00
## 6                  1  1.537625e+00
## 45                 1  5.212509e-01
## 10                 0  1.954062e-01
## 17                 0  1.569378e-01
## 2                  0  8.643204e-02
## 49                 0  7.222893e-02
## 26                 1  6.082910e-02
## 24                 1  5.297160e-02
## 23                 1  5.160410e-02
## 47                 0  4.748848e-02
## 27                 1  3.248348e-02
## 25                 1  2.811302e-02
## 19                 1  2.680600e-02
## 8                  1  2.182647e-02
## 12                 0  1.712434e-02
## 7                  1  9.199090e-03
## 48                 1  7.696092e-03
## 5                  1  7.374817e-03
## 16                 1  5.392427e-03
## 50                 1  4.768666e-03
## 20                 1  4.580360e-03
## 41                 1  4.360788e-03
## 4                  1  4.311588e-03
## 11                 1  4.088334e-03
## 42                 1  3.165995e-03
## 31                 1  2.733186e-03
## 32                 1  2.733186e-03
## 35                 1  2.228616e-03
## 30                 0  2.129234e-03
## 9                  1  1.434799e-03
## 3                  1  1.265611e-03
## 22                 1  1.118939e-03
## 29                 1  1.098972e-03
## 43                 1  9.941725e-04
## 39                 1  9.723793e-04
## 13                 1  7.052838e-04
## 38                 1  6.668371e-04
## 40                 1  5.947143e-04
## 37                 1  5.747666e-04
## 21                 1  4.686365e-04
## 34                 1  3.773788e-04
## 36                 1  3.294877e-04
## 14                 1  3.276616e-04
## 1                  1  1.664905e-04
## 15                 1  1.129327e-04
## 18                 1  8.833345e-05
## 33                 1  7.339745e-05
## 44                 1  4.968727e-06
## 28                 1  6.483376e-07

and now we delete it

ids_to_remove <- c(3, 7)

filtered_data <- subset(ordered_data, !(ID %in% ids_to_remove))

print(filtered_data)
##    ID Price Sqare m rooms Furniture districtbezigrad districtcenter
## 45  6  2800   282.0  11.0       yes                0              0
## 10 44   250    25.0   1.0        no                0              1
## 17 23  2500   114.8   4.0        no                0              0
## 2   2  1100   132.5   2.5        no                1              0
## 49 11  1725    69.0   3.0        no                0              0
## 26 26  2600   150.0   3.0       yes                0              0
## 24  5   800    85.0   2.5       yes                0              0
## 23 37  1100   140.0   3.5       yes                0              0
## 47  9  1525    61.0   2.0        no                0              0
## 27 27  1500    60.2   2.5       yes                0              0
## 25 25  1250   113.8   3.0       yes                0              0
## 19  4  1400    65.0   2.0       yes                0              0
## 8  42   500    21.0   1.0       yes                0              1
## 12 18   600    37.0   1.0        no                0              0
## 7  41   750    30.0   1.5       yes                0              1
## 48 10   960    48.0   2.0       yes                0              0
## 5  17   450    20.0   1.0       yes                1              0
## 16 22   790    65.3   2.0       yes                0              0
## 50 12  1250    77.7   2.0       yes                0              0
## 20 33  1300    82.3   3.0       yes                0              0
## 41 47  1800   103.9   3.0       yes                0              0
## 4  16   350    16.0   1.0       yes                1              0
## 11 45   800    25.0   1.0       yes                0              1
## 42 48  1480    77.0   2.0       yes                0              0
## 31 28   490    40.0   1.0       yes                0              0
## 32 29   490    40.0   1.0       yes                0              0
## 35 32  1000    90.0   3.5       yes                0              0
## 30 15   900    50.0   2.0        no                0              0
## 9  43   850    22.0   1.0       yes                0              1
## 3   8   500    37.0   1.0       yes                1              0
## 22 36  1200   100.0   3.0       yes                0              0
## 29 14  1280    74.0   3.0       yes                0              0
## 43 49  1250   105.9   3.5       yes                0              0
## 39 40  1330    77.0   2.0       yes                0              0
## 13 19   700    40.0   1.0       yes                0              0
## 38 39   800    60.0   2.0       yes                0              0
## 40 46  1500   100.6   3.5       yes                0              0
## 37 38   690    47.0   1.5       yes                0              0
## 21 35  1100    76.1   3.0       yes                0              0
## 34 31  1200    76.3   3.5       yes                0              0
## 36 34  1200    72.0   2.0       yes                0              0
## 14 20   750    42.0   1.0       yes                0              0
## 1   1   700    70.0   2.0       yes                1              0
## 15 21   850    50.0   1.5       yes                0              0
## 18 24   790    43.0   1.0       yes                0              0
## 33 30   950    64.0   2.0       yes                0              0
## 44 50  1200    83.0   3.0       yes                0              0
## 28 13   750    39.0   1.0       yes                0              0
##    districtcrnuce districtmoste districtrudnik districtsiska districtsostro
## 45              0             0              0             0              1
## 10              0             0              0             0              0
## 17              1             0              0             0              0
## 2               0             0              0             0              0
## 49              0             0              0             0              1
## 26              0             0              1             0              0
## 24              0             0              1             0              0
## 23              0             1              0             0              0
## 47              0             0              0             0              1
## 27              0             0              1             0              0
## 25              0             0              1             0              0
## 19              0             1              0             0              0
## 8               0             0              0             0              0
## 12              1             0              0             0              0
## 7               0             0              0             0              0
## 48              0             0              0             0              1
## 5               0             0              0             0              0
## 16              1             0              0             0              0
## 50              0             0              0             0              1
## 20              0             1              0             0              0
## 41              0             0              0             1              0
## 4               0             0              0             0              0
## 11              0             0              0             0              0
## 42              0             0              0             1              0
## 31              0             0              0             1              0
## 32              0             0              0             1              0
## 35              0             0              0             1              0
## 30              0             0              0             1              0
## 9               0             0              0             0              0
## 3               0             0              0             0              0
## 22              0             1              0             0              0
## 29              0             0              0             1              0
## 43              0             0              0             1              0
## 39              0             0              0             1              0
## 13              1             0              0             0              0
## 38              0             0              0             1              0
## 40              0             0              0             1              0
## 37              0             0              0             1              0
## 21              0             1              0             0              0
## 34              0             0              0             1              0
## 36              0             0              0             1              0
## 14              1             0              0             0              0
## 1               0             0              0             0              0
## 15              1             0              0             0              0
## 18              1             0              0             0              0
## 33              0             0              0             1              0
## 44              0             0              0             1              0
## 28              0             0              0             1              0
##    Furniture_numeric CooksDistance
## 45                 1  5.212509e-01
## 10                 0  1.954062e-01
## 17                 0  1.569378e-01
## 2                  0  8.643204e-02
## 49                 0  7.222893e-02
## 26                 1  6.082910e-02
## 24                 1  5.297160e-02
## 23                 1  5.160410e-02
## 47                 0  4.748848e-02
## 27                 1  3.248348e-02
## 25                 1  2.811302e-02
## 19                 1  2.680600e-02
## 8                  1  2.182647e-02
## 12                 0  1.712434e-02
## 7                  1  9.199090e-03
## 48                 1  7.696092e-03
## 5                  1  7.374817e-03
## 16                 1  5.392427e-03
## 50                 1  4.768666e-03
## 20                 1  4.580360e-03
## 41                 1  4.360788e-03
## 4                  1  4.311588e-03
## 11                 1  4.088334e-03
## 42                 1  3.165995e-03
## 31                 1  2.733186e-03
## 32                 1  2.733186e-03
## 35                 1  2.228616e-03
## 30                 0  2.129234e-03
## 9                  1  1.434799e-03
## 3                  1  1.265611e-03
## 22                 1  1.118939e-03
## 29                 1  1.098972e-03
## 43                 1  9.941725e-04
## 39                 1  9.723793e-04
## 13                 1  7.052838e-04
## 38                 1  6.668371e-04
## 40                 1  5.947143e-04
## 37                 1  5.747666e-04
## 21                 1  4.686365e-04
## 34                 1  3.773788e-04
## 36                 1  3.294877e-04
## 14                 1  3.276616e-04
## 1                  1  1.664905e-04
## 15                 1  1.129327e-04
## 18                 1  8.833345e-05
## 33                 1  7.339745e-05
## 44                 1  4.968727e-06
## 28                 1  6.483376e-07

next step is the regression

formula <- "Price ~ `Sqare m` + `rooms` + `Furniture_numeric` + `districtbezigrad` + `districtsiska` + `districtcenter` + `districtrudnik` + `districtsostro` + `districtmoste`"

reg_model_filtered <- lm(formula, data = filtered_data)

summary(reg_model_filtered)
## 
## Call:
## lm(formula = formula, data = filtered_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -603.28 -178.84  -35.35  163.27  857.37 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        654.291    178.182   3.672 0.000737 ***
## `Sqare m`            7.191      3.085   2.331 0.025174 *  
## rooms               40.717     86.718   0.470 0.641371    
## Furniture_numeric -177.536    144.763  -1.226 0.227597    
## districtbezigrad  -349.535    191.864  -1.822 0.076366 .  
## districtsiska      -12.185    151.968  -0.080 0.936514    
## districtcenter    -103.937    199.498  -0.521 0.605393    
## districtrudnik     213.544    220.332   0.969 0.338578    
## districtsostro     168.095    211.289   0.796 0.431224    
## districtmoste      -41.250    200.966  -0.205 0.838464    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 326.4 on 38 degrees of freedom
## Multiple R-squared:  0.7086, Adjusted R-squared:  0.6396 
## F-statistic: 10.27 on 9 and 38 DF,  p-value: 8.171e-08

there are too many insignificant variables in the model. let’s get rid of all the districts except Bezigrad

formula <- "Price ~ `Sqare m` + `rooms` + `Furniture_numeric` + `districtbezigrad`"

reg_model_filtered <- lm(formula, data = filtered_data)

summary(reg_model_filtered)
## 
## Call:
## lm(formula = formula, data = filtered_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -618.6 -197.4  -25.9  130.6  826.6 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        629.796    143.642   4.384 7.38e-05 ***
## `Sqare m`            8.316      2.637   3.153  0.00294 ** 
## rooms               30.951     75.653   0.409  0.68448    
## Furniture_numeric -196.587    130.398  -1.508  0.13897    
## districtbezigrad  -357.144    153.515  -2.326  0.02478 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 318.4 on 43 degrees of freedom
## Multiple R-squared:  0.6863, Adjusted R-squared:  0.6571 
## F-statistic: 23.52 on 4 and 43 DF,  p-value: 2.359e-10

rooms and furniture are still insignificant

formula <- "Price ~ `Sqare m` +  `districtbezigrad`"

reg_model_filtered <- lm(formula, data = filtered_data)

summary(reg_model_filtered)
## 
## Call:
## lm(formula = formula, data = filtered_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -664.73 -207.27  -46.53  111.16  969.71 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       462.302     90.345   5.117 6.22e-06 ***
## `Sqare m`           9.303      1.046   8.890 1.81e-11 ***
## districtbezigrad -354.899    152.245  -2.331   0.0243 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 319.9 on 45 degrees of freedom
## Multiple R-squared:  0.6686, Adjusted R-squared:  0.6539 
## F-statistic:  45.4 on 2 and 45 DF,  p-value: 1.61e-11

The constant represents the estimated value of the dependent variable (Price) when all predictor variables are zero. In this context, it doesn’t have a meaningful interpretation since having zero square meters is not realistic. For every increase in square meters by 1 m2, the estimated price for rented apartment increases by 9.303 eur per month

districtbezigrad coefficient is associated with the dummy variable for the Bezigrad district. The negative coefficient indicates that, compared to other districts, being in Bezigrad is associated with a decrease in the estimated price. F-statistics tests the overall significance of the model. The large F-statistic (p-value < 0.001) indicates that at least one of the predictor variables is significantly related to the response variable

library(olsrr)
## Warning: пакет 'olsrr' был собран под R версии 4.3.2
## 
## Присоединяю пакет: 'olsrr'
## Следующий объект скрыт от 'package:datasets':
## 
##     rivers
#Breusch-Pagan test
breusch_pagan_test <- ols_test_breusch_pagan(reg_model_filtered)

print(breusch_pagan_test)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##               Data                
##  ---------------------------------
##  Response : Price 
##  Variables: fitted values of Price 
## 
##          Test Summary          
##  ------------------------------
##  DF            =    1 
##  Chi2          =    9.117655 
##  Prob > Chi2   =    0.002531543

The p-value associated with the chi-squared test statistic is 0.0025, which is less than the common significance level of 0.05. We should would reject the null hypothesis (Ho) and conclude that there is heteroskedasticity in the model.

library(estimatr)
## Warning: пакет 'estimatr' был собран под R версии 4.3.2
robust_model <- lm_robust(Price ~ `Sqare m` +  `districtbezigrad`, data = filtered_data)

summary(robust_model)
## 
## Call:
## lm_robust(formula = Price ~ `Sqare m` + districtbezigrad, data = filtered_data)
## 
## Standard error type:  HC2 
## 
## Coefficients:
##                  Estimate Std. Error t value  Pr(>|t|) CI Lower CI Upper DF
## (Intercept)       462.302     88.512   5.223 4.363e-06  284.029   640.58 45
## `Sqare m`           9.303      1.381   6.737 2.507e-08    6.522    12.08 45
## districtbezigrad -354.899     86.433  -4.106 1.674e-04 -528.984  -180.81 45
## 
## Multiple R-squared:  0.6686 ,    Adjusted R-squared:  0.6539 
## F-statistic: 33.68 on 2 and 45 DF,  p-value: 1.145e-09

The constant represents the estimated value of the dependent variable (Price) when all predictor variables are zero. In this context, it doesn’t have a meaningful interpretation since having zero square meters is not realistic. For every increase in square meters by 1 m2, the estimated price for rented apartment increases by 9.303 eur per month The negative districtbezigrad coefficient indicates that, compared to other districts, being in Bezigrad is associated with a decrease in the estimated price. On average renting apartment in Bezigrad is 354.9 eur per month cheeper that in other districts. The F-statistic tests the overall significance of the model