# Ścieżka dostępu do pliku .rds
setwd("C:/Users/Fbier/Documents/")

# Biblioteka ggplot2 będzie potrzebna do załadowania wykresu hat_values
library(ggplot2)
## Warning: pakiet 'ggplot2' został zbudowany w wersji R 4.3.2
# Załadownaie danych z .rds
your_data <- readRDS("papers.rds")

# Ścieżka dostępu do pliku .csv
df <- read.csv("C:/Users/Fbier/Downloads/Dobre_dane_Regresja_czyste_8_Wrzesnia__2015gdt.csv")
head(df)
##   PublicationYears BigSample Method AngloSaxon USA TimeSpan Sample
## 1               35         0      0          1   1        0    243
## 2               33         0      0          1   1        0    410
## 3               22         0      1          1   1        1     98
## 4               22         0      1          1   1        3    860
## 5               20         0      0          0   0        0    287
## 6               16         0      0          0   0        0   2428
##   BusinessSupport TC    TCYear EXCLUDED   PY Nr_Papieru
## 1               0  0 0.0000000        0 1980          2
## 2               0  4 0.1212121        0 1982          3
## 3               0 49 2.2272727        0 1993          5
## 4               0 35 1.5909091        0 1993          6
## 5               0  6 0.3000000        0 1995          7
## 6               0  7 0.4375000        0 1999          8
# Połączenie baz danych
merged_data <- merge(your_data, df)
# Model regresji 
regression_model <- lm(TCYear ~ PublicationYears + BigSample + Method + AngloSaxon + TimeSpan + Sample + BusinessSupport, data = df)
summary(regression_model)
## 
## Call:
## lm(formula = TCYear ~ PublicationYears + BigSample + Method + 
##     AngloSaxon + TimeSpan + Sample + BusinessSupport, data = df)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.6610 -1.4347 -0.4097  0.3778 13.8215 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)   
## (Intercept)      -1.085e+00  1.206e+00  -0.900   0.3718   
## PublicationYears  1.019e-01  5.645e-02   1.806   0.0760 . 
## BigSample         3.583e+00  1.129e+00   3.173   0.0024 **
## Method            6.020e-01  1.069e+00   0.563   0.5755   
## AngloSaxon        8.287e-01  1.027e+00   0.807   0.4231   
## TimeSpan          1.106e-01  6.237e-02   1.773   0.0815 . 
## Sample           -5.256e-05  7.557e-05  -0.696   0.4895   
## BusinessSupport   2.185e+00  2.184e+00   1.001   0.3212   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.633 on 59 degrees of freedom
## Multiple R-squared:  0.2427, Adjusted R-squared:  0.1529 
## F-statistic: 2.701 on 7 and 59 DF,  p-value: 0.01693
# Ekstrakcja zmiennych predykcyjnych
predictor_variables <- merged_data[c(
  "PublicationYears", "BigSample", "Method", "AngloSaxon",
  "TimeSpan", "Sample", "BusinessSupport"
)]
# Wykres dźwigni
hat_values <- hatvalues(regression_model)
ggplot(data.frame(Observation_Index = seq_along(hat_values), Hat_Values = hat_values), aes(x = Observation_Index, y = Hat_Values)) +
  geom_point(shape = 19) +
  geom_hline(yintercept = 2 * length(coefficients(regression_model)) / length(hat_values), col = "red") +
  labs(title = "Plot of Hat Values", x = "Observation Index", y = "Hat Values")

# Obserwacje opisane tym wzorem są (potencjalnie) istotne statystycznie. Na wykresie pojawi się czerwona linia, powyżej której znajdują się interesujące nas obserwacje. "significant_data" zawiera wyekstraktowane dane znajdujące się w przedziale istotności. 
average_leverage <- 2 * length(coefficients(regression_model)) / length(hat_values)
significant <- which(hat_values > average_leverage)
significant_data <- your_data[significant, ]
#Baza danych "significant_data" zawiera 7 obserwacji - dane nie są legalne do upublicznienia, więc nie przedstawiam ich w Rpubs, załączę listę do pliku z zadaniem. Wszystkie wylistowane za pomocą badania publikacje pochodzą z wydawnictwa "SAGE PUBLICATIONS LTD" i zostały wydane w języku angielskim.