RMT R studio project - Matej Murn - 19228426 - Business Informatics - Study year: 2023/2024

Data Source: Eurostat data frame name: Average full time adjusted salary per employee link: https://ec.europa.eu/eurostat/databrowser/view/nama_10_fte/default/table?lang=en

data details: Data extracted on 30/12/2023 17:07:39 from [ESTAT]
Dataset: Average full time adjusted salary per employee
Last updated: 28/11/2023 23:00

Explanation of dataset The table shows average yearly salary per employee in 26 European countries Unit of observation: European citizen variable: salary adjusted per employee Time frequency - annual
Unit of measure - euro

!SPECIAL NOTE: The database is missing Netherlands from all EU countries!

library(readxl) #activating readxl package
salary <- read_excel("C:/Users/Uporabnik/Desktop/Preparation course/naloga RMT/salary.xlsx")
head(salary)#heading salary dataset
## # A tibble: 6 × 11
##   Country  `2013` `2014` `2015` `2016` `2017` `2018` `2019` `2020` `2021` `2022`
##   <chr>     <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
## 1 Europea…  28919  29193  29685  30157  30849  31514  32516  32278  33627  35329
## 2 Euro ar…  32647  33128  33629  34130  34741  35325  36339  35849  37296  39082
## 3 Belgium   43289  43736  43935  44769  45054  46124  47271  46063  48877  52466
## 4 Bulgaria   5704   6025   6374   6767   7418   8127   8677   9322  10345  11850
## 5 Czechia   12312  11965  12435  13062  14427  15987  17130  16826  18193  20434
## 6 Denmark   54538  56015  57017  57630  58736  59434  60208  61384  63304  65666

From the upper data, I will be analyzing, whether the average salary per employee of European Union citizen in 2022, differs to average salary of European Union citizen in 2021.

Since observed data are panel data, I will conduct a paired samples t-test and Wilcoxon Signed Rank Test. Based on the data, I will decide, which is better.

salary_trimmed <- salary[-c(1:2),]#removing first 2 rows, since they are not representing countries
salary_trimmed$salary_diff <- salary_trimmed$'2022' - salary_trimmed$'2021'#creating an object difference, fir further analysis

Since observed data already has some computation for EU region, I decided to remove first 2 rows and compute statistics on my own. I created a new object called salary_trimmed, which has the first 2 rows removed. Secondly, I also created a computational value salary_diff -> salary 2022 - salary 2021

library(psych)
describe(salary_trimmed[,c("2021","2022","salary_diff")])#presenting descriptive statistics for 2021 and 2022
##             vars  n     mean       sd median  trimmed      mad   min   max
## 2021           1 26 30575.88 16881.31  25482 28957.27 15086.94 10345 71548
## 2022           2 26 32213.96 17323.52  26686 30496.95 15227.78 11850 75409
## salary_diff    3 26  1638.08   940.29   1595  1612.86   793.93  -573  3861
##             range skew kurtosis      se
## 2021        61203 0.75    -0.57 3310.70
## 2022        63559 0.80    -0.44 3397.42
## salary_diff  4434 0.21     0.47  184.41

n -number of observations mean - from the table, we can observe that, based on our sample, 2022 had a higher mean (32213,96), compared to 2021(20575,88). The last row shows the values of the object that I created. It explains the differences between 2022 and 2021 - will be further tested with t-test.

The medians also report that there are some differences in data distribution location - will be further tested with nonparametric test.

Standard deviation is higher in 2022, which indicates that data from 2022 have higher variability around the mean, compared to data from 2021.

min and max share the minimal and maximal absolute values from the dataset, while range indicate difference between these 2 absolute values.

Skewness coefficent measures asymmetry of the distribution, while kurtosis measures the degree of peakedness or flatness of the data. Both samples have a slightly positive skew and are asymetrical to the right.

se represents standard error.

The last column salary_diff reports the calculated difference between 2 samples. Here we can observe the difference in all statistical parameters - mean,median,sd,etc,. Interestingly, We can also observe that one country had a negative change in average salaries from 2021 to 2021, as the minimal change from 2022 to 2021 is -573.

PARAMETRIC TEST

Research question: Does average salary in 2022 differ from average salary in 2021

This analysis will be based on dependent samples, because sample data are a panel.

Paired sample t-test hypothesis H0: mean(2021)=mean(2022) H1: one mean is different from the other

assumptions: 1) variable is numeric - variables are numeric, therefore i will not test this assumption 2) differences on the population are normally distributed - i will check with shapiro-wilk normality test

ASSUMPTION CHECK: Normality

library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
ggplot (salary_trimmed, aes(salary_diff)) + #plotting differences in histogram to assess normality 
  geom_histogram(binwidth = 4, color = "red") + 
  xlab("Differences")

Presented graph indicates that data are distributed normally, however based from the graph, we cannot confirm the assumption of normality.

To confirm normality, I will conduct a Shapiro - Wilk normality test: H0: data are normally distributed H1: data are not normally distributed

shapiro.test(salary_trimmed$salary_diff)#Shapiro normality test of differences
## 
##  Shapiro-Wilk normality test
## 
## data:  salary_trimmed$salary_diff
## W = 0.96896, p-value = 0.5966

since H0 cannot be rejected (at p=0,6), we can proceed to paired sample t-test.

t.test(salary_trimmed$"2021", salary_trimmed$"2022",#I conducted a paired sample t-test
       paired = TRUE,
       alternative = "two.sided")
## 
##  Paired t-test
## 
## data:  salary_trimmed$"2021" and salary_trimmed$"2022"
## t = -8.883, df = 25, p-value = 3.308e-09
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  -2017.867 -1258.287
## sample estimates:
## mean difference 
##       -1638.077

RESULT INTERPRETATION:

Based on our sample data we can conclude that in 26 European countries, the average salary adjusted per employee in 2022 was higher than the average salary adjusted per employee in 2021, at p<0,001. Based on our sample, the average salary adjusted per employee in 2022 way 1638,1 € higher than average salary adjusted per employee in 2021.

NON-PARAMETRIC TEST We will proceed compare distribution locations, using Wilcoxson Signed Rank Test.

wilcox.test(salary_trimmed$'2021',salary_trimmed$'2022',
            paired = TRUE,
            correct = FALSE,
            exact = FALSE,
            alternative = "two.sided")
## 
##  Wilcoxon signed rank test
## 
## data:  salary_trimmed$"2021" and salary_trimmed$"2022"
## V = 2, p-value = 1.049e-05
## alternative hypothesis: true location shift is not equal to 0

Based on our sample data, we can confirm that the true location shift is not equal to 0, at p<0,001. Based on our samples, we can observe that difference in average salary per employee in 26 european countries was positive in 2022, compared to 2021.This can be seen by comparing Medians, as median is rhe best representable statistics for distribution locations. Me(2022) = 26686 Me(2021) = 25482

To conclude my research hypothesis testing, we can observe from parametric and non-parametric test that changes did occur in 2022, compared to 2021. As no assumptions were broken, I would primarily base the result of my hypothesis research on paired samples t-test.

REGRESSION ANALYSIS

For my regression analysis, I have decided to conduct my analysis on dataset, already ingrained in R studio. It is dataset called state.x77.

state_analysis <- as.data.frame(state.x77)
print(state_analysis[,-c(2,4,6,7,8)]) #showing data, which will be used in regression analysis
##                Population Illiteracy Murder
## Alabama              3615        2.1   15.1
## Alaska                365        1.5   11.3
## Arizona              2212        1.8    7.8
## Arkansas             2110        1.9   10.1
## California          21198        1.1   10.3
## Colorado             2541        0.7    6.8
## Connecticut          3100        1.1    3.1
## Delaware              579        0.9    6.2
## Florida              8277        1.3   10.7
## Georgia              4931        2.0   13.9
## Hawaii                868        1.9    6.2
## Idaho                 813        0.6    5.3
## Illinois            11197        0.9   10.3
## Indiana              5313        0.7    7.1
## Iowa                 2861        0.5    2.3
## Kansas               2280        0.6    4.5
## Kentucky             3387        1.6   10.6
## Louisiana            3806        2.8   13.2
## Maine                1058        0.7    2.7
## Maryland             4122        0.9    8.5
## Massachusetts        5814        1.1    3.3
## Michigan             9111        0.9   11.1
## Minnesota            3921        0.6    2.3
## Mississippi          2341        2.4   12.5
## Missouri             4767        0.8    9.3
## Montana               746        0.6    5.0
## Nebraska             1544        0.6    2.9
## Nevada                590        0.5   11.5
## New Hampshire         812        0.7    3.3
## New Jersey           7333        1.1    5.2
## New Mexico           1144        2.2    9.7
## New York            18076        1.4   10.9
## North Carolina       5441        1.8   11.1
## North Dakota          637        0.8    1.4
## Ohio                10735        0.8    7.4
## Oklahoma             2715        1.1    6.4
## Oregon               2284        0.6    4.2
## Pennsylvania        11860        1.0    6.1
## Rhode Island          931        1.3    2.4
## South Carolina       2816        2.3   11.6
## South Dakota          681        0.5    1.7
## Tennessee            4173        1.7   11.0
## Texas               12237        2.2   12.2
## Utah                 1203        0.6    4.5
## Vermont               472        0.6    5.5
## Virginia             4981        1.4    9.5
## Washington           3559        0.6    4.3
## West Virginia        1799        1.4    6.7
## Wisconsin            4589        0.7    3.0
## Wyoming               376        0.6    6.9
library(psych)
describe(state_analysis[,-c(2,4,6,7,8)])#descriptve statistics for data
##            vars  n    mean      sd  median trimmed     mad   min     max
## Population    1 50 4246.42 4464.49 2838.50 3384.28 2890.33 365.0 21198.0
## Illiteracy    2 50    1.17    0.61    0.95    1.10    0.52   0.5     2.8
## Murder        3 50    7.38    3.69    6.85    7.30    5.19   1.4    15.1
##              range skew kurtosis     se
## Population 20833.0 1.92     3.75 631.37
## Illiteracy     2.3 0.82    -0.47   0.09
## Murder        13.7 0.13    -1.21   0.52

unit of observation - American states Variables: Population - population estimate as of July 1, 1975 Illiteracy - illiteracy (1970, percent of population) Murder - murder and non-negligent manslaughter rate per 100,000 population (1976)

NOTE - descriptive statistics were already described above in HYPOTHESIS TESTING EXAMPLE

OLS REGRESSION ANALYSIS

explanatory variables - population + illiteracy dependent variable - murder rate

NOTE - I will create 3 models - two will involve just one explanatory variable, while the third will involve 2 explanatory variables. I could immediately do just the third, however for explanation purposes I decided to create 2 more. (see RESULTS)

Before proceeding with OLS regression analysis, we have to test the assumptions: 1) Linearity in parameters - will be checked with scatterplots, whether there is any linear relationship. 2) The expected value of errors equals 0 - since I don’t have any prior theory on this data and I am analyzing samples for exercise, I will assume assumption 2 is met. 3) Homoskedasticity - to check this assumption, I will conduct Breusch-Pagan test
4) Normal distribution of errors - will check this assumption with Shapiro-Wilk normality test 5) Errors are independent - we assume errors are independent, because we are not analyzing panel data

ASSUMPTION CHECK: LINEARITY

#install.packages(carData)
library(carData)
library(car)
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
scatterplot (state_analysis$Murder ~ state_analysis$Illiteracy,#creating a scatterplot to observe linearity
             smooth = FALSE, 
             boxplot = FALSE,
             ylab = "murder rate",
             xlab = "Illiteracy level")

scatterplot (state_analysis$Murder ~ state_analysis$Population, 
             smooth = FALSE, 
             boxplot = FALSE,
             ylab = "murder rate",
             xlab = "Population")

From graphs, we can observe that there is linear relationship between explanatory and dependent variables. The blue lines already depict regression lines.

ASSUMPTION CHECK: HOMOSKEDASTICITY

fit1 <- lm(state_analysis$Murder ~ state_analysis$Illiteracy,
           data = state_analysis) 
#I created a linear regression model - murder rate = dependent variable, illiteracy = explanatory variable
fit2 <- lm(state_analysis$Murder ~ state_analysis$Population,
           data = state_analysis)
#I created a linear regression model - murder rate = dependent variable, population = explanatory variable
fit3 <- lm(state_analysis$Murder ~ state_analysis$Illiteracy + state_analysis$Population,
           data = state_analysis)
#I created a linear regression model - murder rate = dependent variable ,illiteracy + population= explanatory variables

plot(fitted(fit1), residuals(fit1),
     xlab = "Fitted values",
     ylab = "Residuals",
     main = "Residuals vs Fitted Values for Homoscedasticity Check") # I created a graph which plots residuals and fitted values
abline(h=0, col = "red") #I added a red line at y=0 for better visibility

plot(fitted(fit2), residuals(fit2),
     xlab = "Fitted values",
     ylab = "Residuals",
     main = "Residuals vs Fitted Values for Homoscedasticity Check") # I created a graph which plots residuals and fitted values
abline(h=0, col = "red") #I added a red line at y=0 for better visibility

plot(fitted(fit3), residuals(fit3),
     xlab = "Fitted values",
     ylab = "Residuals",
     main = "Residuals vs Fitted Values for Homoscedasticity Check") # I created a graph which plots residuals and fitted values
abline(h=0, col = "red") #I added a red line at y=0 for better visibility

From the graph 1 and 3, we can observe there are no problems with heteroskedasticity. However, graph 2 might indicate some problems, therefore we will proceed with Brausch-Pagan tests.

BRAUSCH-PAGAN TEST HYPOTHESIS: h0: The variance is constant H1: Variance is not constant

#install.packages("lmtest")#installing package for breusch pagan test
library("lmtest")
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
bptest(fit1)#breusch pagan test
## 
##  studentized Breusch-Pagan test
## 
## data:  fit1
## BP = 0.73061, df = 1, p-value = 0.3927
bptest(fit2)
## 
##  studentized Breusch-Pagan test
## 
## data:  fit2
## BP = 1.4303, df = 1, p-value = 0.2317
bptest(fit3)
## 
##  studentized Breusch-Pagan test
## 
## data:  fit3
## BP = 1.856, df = 2, p-value = 0.3953

We cannot reject H0, which means assumption of homoskedasticity is not broken for any of 3 linear models.

ASSUMPTION CHECK 3: Normality of errors

Normality of errors will be checked with histogram of residuals and shapiro normality test of these residuals.

H0: The residuals are normally distributed H1: The residuals are not normally distributed

residuals_fit1 <- residuals(fit1)#creating objects to graph residuals
residuals_fit2 <- residuals(fit2)
residuals_fit3 <- residuals(fit3)
hist(residuals_fit1)

hist(residuals_fit2)

hist(residuals_fit3)

shapiro.test(residuals_fit1) #shapiro test of normality for residuals in all 3 linear models
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals_fit1
## W = 0.99075, p-value = 0.9621
shapiro.test(residuals_fit2)
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals_fit2
## W = 0.95662, p-value = 0.0642
shapiro.test(residuals_fit3)
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals_fit3
## W = 0.98134, p-value = 0.6098

We cannot reject H0 for any of the 3 models - assumption of normality of errors is not broken for any of the 3 linear models. Note: although the p-value for second model is very close to 0,05, it is still within 95% CI, therefore I continued with analysis.

summary(fit1) #summarizing all 3 linear models
## 
## Call:
## lm(formula = state_analysis$Murder ~ state_analysis$Illiteracy, 
##     data = state_analysis)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.5315 -2.0602 -0.2503  1.6916  6.9745 
## 
## Coefficients:
##                           Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                 2.3968     0.8184   2.928   0.0052 ** 
## state_analysis$Illiteracy   4.2575     0.6217   6.848 1.26e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.653 on 48 degrees of freedom
## Multiple R-squared:  0.4942, Adjusted R-squared:  0.4836 
## F-statistic: 46.89 on 1 and 48 DF,  p-value: 1.258e-08
summary(fit2)
## 
## Call:
## lm(formula = state_analysis$Murder ~ state_analysis$Population, 
##     data = state_analysis)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.9855 -3.0119 -0.3128  2.4986  7.9014 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               6.1713934  0.6869410   8.984 7.49e-12 ***
## state_analysis$Population 0.0002841  0.0001121   2.535   0.0146 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.503 on 48 degrees of freedom
## Multiple R-squared:  0.1181, Adjusted R-squared:  0.09972 
## F-statistic: 6.427 on 1 and 48 DF,  p-value: 0.01455
summary(fit3)
## 
## Call:
## lm(formula = state_analysis$Murder ~ state_analysis$Illiteracy + 
##     state_analysis$Population, data = state_analysis)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.7652 -1.6561 -0.0898  1.4570  7.6758 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               1.652e+00  8.101e-01   2.039  0.04713 *  
## state_analysis$Illiteracy 4.081e+00  5.848e-01   6.978 8.83e-09 ***
## state_analysis$Population 2.242e-04  7.984e-05   2.808  0.00724 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.481 on 47 degrees of freedom
## Multiple R-squared:  0.5668, Adjusted R-squared:  0.5484 
## F-statistic: 30.75 on 2 and 47 DF,  p-value: 2.893e-09

INTERPRETATION OF RESULTS:

Regression model fit3: The regression model fit 3 is my multiple linear regression model using OLS method. we can observe that both coefficients are statistically significant.

coefficient interpretation: Intercept - does not make sense to interpret this coefficient, because you cannot have an American state with 0 population

state_analysis$Illiteracy - If illiteracy level of a given American state increases by 1 unit, the murder rate in a given state increases by 4,08 units, assuming all other variables stay the same, at p<0,001

state_analysis$Population - If Population of a given American state increases by 1 unit, the murder rate in a given state increases by 0,0002242 units, assuming all other variables stay the same, at p<0,001

Multiple R squared (coefficient of determination)- this statistics shows us how much of variability of the data, we can explain with a given model. This statistics in used to compare models between each other. If we compare fit3 to models fit 1 and fit 2, we can see that multiple r squared is higher for fit3, than it is for fit 1 or fit2. Altough fit 1 and fit2 are both statistically significant, fit3 is the best model -> Multiple R squared interpretation: With a linear model fit3, we can explain 56,7% of variability of data with linear effect of illiteracy and population.

F statistics: F statistics checks the hypothesis H0: ρ² = 0 h1: ρ² is not equal to 0 based on our p value, which is much lower than 0,001, we can conclude that our regression linear model is statistically significant.