library(performance)
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(corrplot)
## corrplot 0.92 loaded
library(caret)
## 載入需要的套件:lattice
## 
## 載入套件:'caret'
## 
## 下列物件被遮斷自 'package:purrr':
## 
##     lift
library(lme4)
## 載入需要的套件:Matrix
## 
## 載入套件:'Matrix'
## 
## 下列物件被遮斷自 'package:tidyr':
## 
##     expand, pack, unpack
library(sjPlot)
library(patchwork)
#import data
data<-read.csv("player_data.csv")

#clean the data
str(data)
## 'data.frame':    660 obs. of  21 variables:
##  $ team_id                  : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ player_id                : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ team_victory             : int  12 12 12 12 12 12 12 12 12 12 ...
##  $ team_coach_experience    : num  11.7 11.7 11.7 11.7 11.7 ...
##  $ team_avg_age             : num  28.8 28.8 28.8 28.8 28.8 ...
##  $ team_budget              : num  104 104 104 104 104 ...
##  $ team_training_facilities : chr  "Medium" "Medium" "Medium" "Medium" ...
##  $ team_previous_season_rank: int  26 26 26 26 26 26 26 26 26 26 ...
##  $ team_sponsorship_deals   : int  4 4 4 4 4 4 4 4 4 4 ...
##  $ team_morale              : num  9.71 9.71 9.71 9.71 9.71 ...
##  $ team_fan_base_size       : num  606 606 606 606 606 ...
##  $ age                      : num  25.8 21.1 27.2 20.8 26.8 ...
##  $ experience               : num  3.66 3.09 14.02 2.01 11.46 ...
##  $ injury_history           : int  0 2 1 2 1 4 1 2 0 4 ...
##  $ games_played             : int  17 26 22 22 37 15 36 21 22 31 ...
##  $ goals_scored             : int  6 5 8 13 11 13 8 4 8 5 ...
##  $ assists_made             : int  6 2 7 1 4 2 2 1 2 4 ...
##  $ training_hours_weekly    : num  11.5 21.3 23.1 20.3 23.6 ...
##  $ previous_season_score    : num  52 58.1 48.1 56 51.8 ...
##  $ fitness_level            : num  4.09 10.57 6.39 10.44 5.19 ...
##  $ performance_score        : num  49.9 52.5 51.6 51 48.3 ...
colSums(is.na(data))
##                   team_id                 player_id              team_victory 
##                         0                         0                         0 
##     team_coach_experience              team_avg_age               team_budget 
##                         0                         0                         0 
##  team_training_facilities team_previous_season_rank    team_sponsorship_deals 
##                         0                         0                         0 
##               team_morale        team_fan_base_size                       age 
##                         0                         0                         0 
##                experience            injury_history              games_played 
##                         0                         0                         0 
##              goals_scored              assists_made     training_hours_weekly 
##                         0                         0                         0 
##     previous_season_score             fitness_level         performance_score 
##                         0                         0                         0
summary(data)
##     team_id       player_id     team_victory   team_coach_experience
##  Min.   : 1.0   Min.   : 1.0   Min.   : 8.00   Min.   : 5.354       
##  1st Qu.: 8.0   1st Qu.: 6.0   1st Qu.:10.00   1st Qu.: 8.600       
##  Median :15.5   Median :11.5   Median :14.00   Median : 9.782       
##  Mean   :15.5   Mean   :11.5   Mean   :14.07   Mean   : 9.969       
##  3rd Qu.:23.0   3rd Qu.:17.0   3rd Qu.:18.00   3rd Qu.:11.139       
##  Max.   :30.0   Max.   :22.0   Max.   :21.00   Max.   :16.507       
##   team_avg_age    team_budget     team_training_facilities
##  Min.   :17.76   Min.   : 26.64   Length:660              
##  1st Qu.:25.52   1st Qu.: 44.30   Class :character        
##  Median :27.84   Median : 53.96   Mode  :character        
##  Mean   :27.65   Mean   : 56.59                           
##  3rd Qu.:30.69   3rd Qu.: 65.14                           
##  Max.   :35.20   Max.   :103.75                           
##  team_previous_season_rank team_sponsorship_deals  team_morale   
##  Min.   : 1.0              Min.   : 1.000         Min.   :1.160  
##  1st Qu.: 8.0              1st Qu.: 3.000         1st Qu.:3.779  
##  Median :15.5              Median : 4.500         Median :5.222  
##  Mean   :15.5              Mean   : 5.067         Mean   :5.717  
##  3rd Qu.:23.0              3rd Qu.: 6.000         3rd Qu.:8.056  
##  Max.   :30.0              Max.   :14.000         Max.   :9.874  
##  team_fan_base_size      age          experience     injury_history 
##  Min.   :213.3      Min.   :11.76   Min.   : 0.000   Min.   :0.000  
##  1st Qu.:393.9      1st Qu.:22.13   1st Qu.: 3.659   1st Qu.:1.000  
##  Median :497.2      Median :24.90   Median : 6.858   Median :2.000  
##  Mean   :505.1      Mean   :24.78   Mean   : 6.858   Mean   :1.985  
##  3rd Qu.:606.3      3rd Qu.:27.58   3rd Qu.: 9.526   3rd Qu.:3.000  
##  Max.   :906.2      Max.   :38.22   Max.   :21.008   Max.   :8.000  
##   games_played    goals_scored     assists_made   training_hours_weekly
##  Min.   :11.00   Min.   : 0.000   Min.   :0.000   Min.   : 4.267       
##  1st Qu.:21.00   1st Qu.: 6.000   1st Qu.:2.000   1st Qu.:16.528       
##  Median :25.00   Median : 8.000   Median :3.000   Median :19.804       
##  Mean   :24.67   Mean   : 7.983   Mean   :3.038   Mean   :19.808       
##  3rd Qu.:28.00   3rd Qu.:10.000   3rd Qu.:4.000   3rd Qu.:23.239       
##  Max.   :41.00   Max.   :14.000   Max.   :9.000   Max.   :33.627       
##  previous_season_score fitness_level    performance_score
##  Min.   :39.40         Min.   : 1.271   Min.   :40.07    
##  1st Qu.:53.02         1st Qu.: 6.169   1st Qu.:49.28    
##  Median :57.51         Median : 7.798   Median :53.47    
##  Mean   :57.28         Mean   : 7.722   Mean   :53.43    
##  3rd Qu.:61.47         3rd Qu.: 9.435   3rd Qu.:57.45    
##  Max.   :73.21         Max.   :14.844   Max.   :67.78
#converting the ordinal variable
data$team_training_facilities<- as.numeric(factor(data$team_training_facilities,
                                                  levels = c("Low","Medium","High"),
                                                  labels = c(1,2,3)))
summary(data)
##     team_id       player_id     team_victory   team_coach_experience
##  Min.   : 1.0   Min.   : 1.0   Min.   : 8.00   Min.   : 5.354       
##  1st Qu.: 8.0   1st Qu.: 6.0   1st Qu.:10.00   1st Qu.: 8.600       
##  Median :15.5   Median :11.5   Median :14.00   Median : 9.782       
##  Mean   :15.5   Mean   :11.5   Mean   :14.07   Mean   : 9.969       
##  3rd Qu.:23.0   3rd Qu.:17.0   3rd Qu.:18.00   3rd Qu.:11.139       
##  Max.   :30.0   Max.   :22.0   Max.   :21.00   Max.   :16.507       
##   team_avg_age    team_budget     team_training_facilities
##  Min.   :17.76   Min.   : 26.64   Min.   :1.000           
##  1st Qu.:25.52   1st Qu.: 44.30   1st Qu.:1.000           
##  Median :27.84   Median : 53.96   Median :2.000           
##  Mean   :27.65   Mean   : 56.59   Mean   :1.933           
##  3rd Qu.:30.69   3rd Qu.: 65.14   3rd Qu.:3.000           
##  Max.   :35.20   Max.   :103.75   Max.   :3.000           
##  team_previous_season_rank team_sponsorship_deals  team_morale   
##  Min.   : 1.0              Min.   : 1.000         Min.   :1.160  
##  1st Qu.: 8.0              1st Qu.: 3.000         1st Qu.:3.779  
##  Median :15.5              Median : 4.500         Median :5.222  
##  Mean   :15.5              Mean   : 5.067         Mean   :5.717  
##  3rd Qu.:23.0              3rd Qu.: 6.000         3rd Qu.:8.056  
##  Max.   :30.0              Max.   :14.000         Max.   :9.874  
##  team_fan_base_size      age          experience     injury_history 
##  Min.   :213.3      Min.   :11.76   Min.   : 0.000   Min.   :0.000  
##  1st Qu.:393.9      1st Qu.:22.13   1st Qu.: 3.659   1st Qu.:1.000  
##  Median :497.2      Median :24.90   Median : 6.858   Median :2.000  
##  Mean   :505.1      Mean   :24.78   Mean   : 6.858   Mean   :1.985  
##  3rd Qu.:606.3      3rd Qu.:27.58   3rd Qu.: 9.526   3rd Qu.:3.000  
##  Max.   :906.2      Max.   :38.22   Max.   :21.008   Max.   :8.000  
##   games_played    goals_scored     assists_made   training_hours_weekly
##  Min.   :11.00   Min.   : 0.000   Min.   :0.000   Min.   : 4.267       
##  1st Qu.:21.00   1st Qu.: 6.000   1st Qu.:2.000   1st Qu.:16.528       
##  Median :25.00   Median : 8.000   Median :3.000   Median :19.804       
##  Mean   :24.67   Mean   : 7.983   Mean   :3.038   Mean   :19.808       
##  3rd Qu.:28.00   3rd Qu.:10.000   3rd Qu.:4.000   3rd Qu.:23.239       
##  Max.   :41.00   Max.   :14.000   Max.   :9.000   Max.   :33.627       
##  previous_season_score fitness_level    performance_score
##  Min.   :39.40         Min.   : 1.271   Min.   :40.07    
##  1st Qu.:53.02         1st Qu.: 6.169   1st Qu.:49.28    
##  Median :57.51         Median : 7.798   Median :53.47    
##  Mean   :57.28         Mean   : 7.722   Mean   :53.43    
##  3rd Qu.:61.47         3rd Qu.: 9.435   3rd Qu.:57.45    
##  Max.   :73.21         Max.   :14.844   Max.   :67.78
select_data<-subset(data,select = -c(team_id,player_id,performance_score))
#create histogrames
data_long<-select_data |> 
  pivot_longer(cols = everything(),
               names_to = "variable",
               values_to = "value")


ggplot(data_long,aes(x=value))+
  geom_histogram(bins=15,color="white")+
  facet_wrap(~variable,scales = "free_x")

#find out correltions
data_ggpairs <- 
  data |> 
  select(performance_score, everything()) |> 
  select(-c(2,3))

ggpairs(data_ggpairs)

ggcorr(select_data,label = TRUE)

M <- data.matrix(select_data) 
corrM <- cor(M)
highlyCorrM <- findCorrelation(corrM, cutoff=0.3) 
names(select_data)[highlyCorrM]
## [1] "team_avg_age"              "team_training_facilities" 
## [3] "team_coach_experience"     "previous_season_score"    
## [5] "team_previous_season_rank" "age"
#model 1
model1<-lm(performance_score~
             team_training_facilities+
             goals_scored+
             fitness_level+
             previous_season_score+
             previous_season_score:fitness_level,
           data)

model2<-lm(performance_score~
             team_training_facilities+
             goals_scored+
             fitness_level+
             previous_season_score+
             previous_season_score:goals_scored,
           data)

model3<-lm(performance_score~
             fitness_level+
             previous_season_score+
             training_hours_weekly,
           data)

compare_performance(model1,model2,model3)
tab_model(model1,model2,model3)
  performance score performance score performance score
Predictors Estimates CI p Estimates CI p Estimates CI p
(Intercept) 24.35 14.74 – 33.95 <0.001 31.99 25.04 – 38.95 <0.001 18.05 14.93 – 21.16 <0.001
team training facilities 0.62 0.27 – 0.97 0.001 0.60 0.26 – 0.95 0.001
goals scored 0.17 0.08 – 0.26 <0.001 -1.61 -2.40 – -0.83 <0.001
fitness level -0.38 -1.58 – 0.83 0.538 0.52 0.38 – 0.65 <0.001 0.55 0.41 – 0.68 <0.001
previous season score 0.39 0.22 – 0.57 <0.001 0.25 0.12 – 0.38 <0.001 0.55 0.50 – 0.61 <0.001
fitness level × previous
season score
0.02 -0.01 – 0.04 0.146
goals scored × previous
season score
0.03 0.02 – 0.05 <0.001
training hours weekly -0.03 -0.09 – 0.03 0.320
Observations 660 660 660
R2 / R2 adjusted 0.527 / 0.523 0.539 / 0.536 0.505 / 0.503
#model 2 has the best AIC

#list the different level of matrics

#level 1(person)
#age,
#experience,
#injury_history,
##games_played,
#goals_scored,
#assists_made,
#training_hours_weekly,
#previous_season_score,
#fitness_level,
#performance_score

#level 2(team)
#team_victory,
#team_coach_experience,
#team_avg_age,
##team_budget,
#team_training_facilities,
#team_previous_season_rank,
#team_sponsorship_deals,
#team_morale,
#team_fan_base_size

#find out the different level relationship
ggplot(data,aes(performance_score))+
  geom_histogram(bins = 10,color="white")+
  facet_wrap(~team_id,scales = "free_x")

ggplot(data,aes(previous_season_score,performance_score))+
  geom_point()+
  geom_smooth(method='lm')+
  facet_wrap(~team_id,scales = "free_x")
## `geom_smooth()` using formula = 'y ~ x'

#Different level
data$team_id<-as.factor(data$team_id)
ml1<-lmer(performance_score~1+(1|team_id),data = data)
summary(ml1)
## Linear mixed model fit by REML ['lmerMod']
## Formula: performance_score ~ 1 + (1 | team_id)
##    Data: data
## 
## REML criterion at convergence: 3982.6
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.1426 -0.5593  0.0204  0.6055  3.1757 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  team_id  (Intercept)  8.316   2.884   
##  Residual             22.146   4.706   
## Number of obs: 660, groups:  team_id, 30
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  53.4325     0.5575   95.85
plot_model(ml1,type="re",show.values = T,value.offset = 0.4)

ml2<-lmer(performance_score~fitness_level+previous_season_score+team_budget+games_played+(previous_season_score|team_id),data=data)
## boundary (singular) fit: see help('isSingular')
summary(ml2)          
## Linear mixed model fit by REML ['lmerMod']
## Formula: 
## performance_score ~ fitness_level + previous_season_score + team_budget +  
##     games_played + (previous_season_score | team_id)
##    Data: data
## 
## REML criterion at convergence: 3608.4
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.8635 -0.6224 -0.0124  0.6335  4.2684 
## 
## Random effects:
##  Groups   Name                  Variance Std.Dev. Corr 
##  team_id  (Intercept)           120.0578 10.9571       
##           previous_season_score   0.0505  0.2247  -1.00
##  Residual                        12.2120  3.4946       
## Number of obs: 660, groups:  team_id, 30
## 
## Fixed effects:
##                       Estimate Std. Error t value
## (Intercept)           22.16943    2.65973   8.335
## fitness_level          0.46395    0.06479   7.160
## previous_season_score  0.46258    0.04957   9.331
## team_budget            0.03669    0.01184   3.100
## games_played          -0.05517    0.02888  -1.910
## 
## Correlation of Fixed Effects:
##             (Intr) ftnss_ prvs__ tm_bdg
## fitness_lvl  0.004                     
## prvs_ssn_sc -0.911 -0.182              
## team_budget -0.223 -0.017 -0.027       
## games_playd -0.272  0.035 -0.008  0.022
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
ml3<-lmer(performance_score~fitness_level+previous_season_score+goals_scored+goals_scored:previous_season_score+team_training_facilities+(previous_season_score|team_id),data=data)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
## Model failed to converge with max|grad| = 11.6719 (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
summary(ml3)
## Linear mixed model fit by REML ['lmerMod']
## Formula: 
## performance_score ~ fitness_level + previous_season_score + goals_scored +  
##     goals_scored:previous_season_score + team_training_facilities +  
##     (previous_season_score | team_id)
##    Data: data
## 
## REML criterion at convergence: 3588.2
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.1726 -0.6254 -0.0063  0.6132  3.7741 
## 
## Random effects:
##  Groups   Name                  Variance Std.Dev. Corr 
##  team_id  (Intercept)           34.55985 5.8788        
##           previous_season_score  0.01434 0.1198   -0.99
##  Residual                       12.15435 3.4863        
## Number of obs: 660, groups:  team_id, 30
## 
## Fixed effects:
##                                     Estimate Std. Error t value
## (Intercept)                        36.408353   3.590318  10.141
## fitness_level                       0.477713   0.064545   7.401
## previous_season_score               0.174901   0.067019   2.610
## goals_scored                       -1.728666   0.382907  -4.515
## team_training_facilities            0.569774   0.288014   1.978
## previous_season_score:goals_scored  0.034159   0.006908   4.945
## 
## Correlation of Fixed Effects:
##             (Intr) ftnss_ prvs__ gls_sc tm_tr_
## fitness_lvl  0.029                            
## prvs_ssn_sc -0.977 -0.149                     
## goals_scord -0.851 -0.030  0.832              
## tm_trnng_fc -0.064 -0.015 -0.077 -0.022       
## prvs_ssn_:_  0.856  0.022 -0.845 -0.994  0.016
## optimizer (nloptwrap) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 11.6719 (tol = 0.002, component 1)
## Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
ml4<-lmer(performance_score~fitness_level+previous_season_score+goals_scored+team_training_facilities+(previous_season_score|team_id),data=data)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model failed to converge with max|grad| = 3.62882 (tol = 0.002, component 1)

## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
summary(ml4)
## Linear mixed model fit by REML ['lmerMod']
## Formula: 
## performance_score ~ fitness_level + previous_season_score + goals_scored +  
##     team_training_facilities + (previous_season_score | team_id)
##    Data: data
## 
## REML criterion at convergence: 3599.6
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.9913 -0.6439 -0.0083  0.6252  4.0179 
## 
## Random effects:
##  Groups   Name                  Variance Std.Dev. Corr 
##  team_id  (Intercept)           58.69664 7.661         
##           previous_season_score  0.02402 0.155    -1.00
##  Residual                       12.44287 3.527         
## Number of obs: 660, groups:  team_id, 30
## 
## Fixed effects:
##                          Estimate Std. Error t value
## (Intercept)              21.32339    2.05886  10.357
## fitness_level             0.46759    0.06536   7.154
## previous_season_score     0.45265    0.04033  11.223
## goals_scored              0.15324    0.04250   3.606
## team_training_facilities  0.53088    0.27435   1.935
## 
## Correlation of Fixed Effects:
##             (Intr) ftnss_ prvs__ gls_sc
## fitness_lvl  0.020                     
## prvs_ssn_sc -0.937 -0.220              
## goals_scord -0.002 -0.074 -0.113       
## tm_trnng_fc -0.102 -0.014 -0.122 -0.061
## optimizer (nloptwrap) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 3.62882 (tol = 0.002, component 1)
## Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
ml5<-lmer(performance_score~fitness_level+previous_season_score+goals_scored+team_training_facilities+(1|team_id),data=data)
summary(ml5)
## Linear mixed model fit by REML ['lmerMod']
## Formula: 
## performance_score ~ fitness_level + previous_season_score + goals_scored +  
##     team_training_facilities + (1 | team_id)
##    Data: data
## 
## REML criterion at convergence: 3630.2
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.6759 -0.6335  0.0077  0.6489  3.1054 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  team_id  (Intercept)  1.092   1.045   
##  Residual             13.472   3.670   
## Number of obs: 660, groups:  team_id, 30
## 
## Fixed effects:
##                          Estimate Std. Error t value
## (Intercept)              19.82950    1.59528  12.430
## fitness_level             0.48822    0.06705   7.282
## previous_season_score     0.47590    0.02924  16.274
## goals_scored              0.15842    0.04421   3.584
## team_training_facilities  0.67740    0.28260   2.397
## 
## Correlation of Fixed Effects:
##             (Intr) ftnss_ prvs__ gls_sc
## fitness_lvl  0.035                     
## prvs_ssn_sc -0.874 -0.316              
## goals_scord  0.003 -0.075 -0.169       
## tm_trnng_fc -0.209 -0.033 -0.104 -0.063
ml6<-lmer(performance_score~fitness_level+previous_season_score+(previous_season_score|team_id),data=data)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model failed to converge with max|grad| = 1.6411 (tol = 0.002, component 1)

## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
summary(ml6)
## Linear mixed model fit by REML ['lmerMod']
## Formula: 
## performance_score ~ fitness_level + previous_season_score + (previous_season_score |  
##     team_id)
##    Data: data
## 
## REML criterion at convergence: 3614.4
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.0246 -0.6098 -0.0197  0.6373  3.9882 
## 
## Random effects:
##  Groups   Name                  Variance Std.Dev. Corr 
##  team_id  (Intercept)           36.96648 6.0800        
##           previous_season_score  0.01705 0.1306   -1.00
##  Residual                       12.79664 3.5772        
## Number of obs: 660, groups:  team_id, 30
## 
## Fixed effects:
##                       Estimate Std. Error t value
## (Intercept)           22.29029    1.89209  11.781
## fitness_level          0.48196    0.06607   7.295
## previous_season_score  0.47345    0.03713  12.752
## 
## Correlation of Fixed Effects:
##             (Intr) ftnss_
## fitness_lvl  0.013       
## prvs_ssn_sc -0.963 -0.250
## optimizer (nloptwrap) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 1.6411 (tol = 0.002, component 1)
## Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
tab_model(ml1,ml2,ml3,ml4,ml5,ml6)
  performance score performance score performance score performance score performance score performance score
Predictors Estimates CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p
(Intercept) 53.43 52.34 – 54.53 <0.001 22.17 16.95 – 27.39 <0.001 36.41 29.36 – 43.46 <0.001 21.32 17.28 – 25.37 <0.001 19.83 16.70 – 22.96 <0.001 22.29 18.57 – 26.01 <0.001
fitness level 0.46 0.34 – 0.59 <0.001 0.48 0.35 – 0.60 <0.001 0.47 0.34 – 0.60 <0.001 0.49 0.36 – 0.62 <0.001 0.48 0.35 – 0.61 <0.001
previous season score 0.46 0.37 – 0.56 <0.001 0.17 0.04 – 0.31 0.009 0.45 0.37 – 0.53 <0.001 0.48 0.42 – 0.53 <0.001 0.47 0.40 – 0.55 <0.001
team budget 0.04 0.01 – 0.06 0.002
games played -0.06 -0.11 – 0.00 0.057
goals scored -1.73 -2.48 – -0.98 <0.001 0.15 0.07 – 0.24 <0.001 0.16 0.07 – 0.25 <0.001
team training facilities 0.57 0.00 – 1.14 0.048 0.53 -0.01 – 1.07 0.053 0.68 0.12 – 1.23 0.017
previous season score ×
goals scored
0.03 0.02 – 0.05 <0.001
Random Effects
σ2 22.15 12.21 12.15 12.44 13.47 12.80
τ00 8.32 team_id 120.06 team_id 34.56 team_id 58.70 team_id 1.09 team_id 36.97 team_id
τ11   0.05 team_id.previous_season_score 0.01 team_id.previous_season_score 0.02 team_id.previous_season_score   0.02 team_id.previous_season_score
ρ01   -1.00 team_id -0.99 team_id -1.00 team_id   -1.00 team_id
ICC 0.27 0.31 0.15 0.17 0.07 0.18
N 30 team_id 30 team_id 30 team_id 30 team_id 30 team_id 30 team_id
Observations 660 660 660 660 660 660
Marginal R2 / Conditional R2 0.000 / 0.273 0.396 / 0.580 0.483 / 0.559 0.454 / 0.550 0.492 / 0.530 0.421 / 0.525
compare_performance(model2,ml2,ml3,ml4,ml5,ml6)
test_performance(model2,ml3)
tab_model(model2,ml3,
          show.aic = TRUE,
          p.style = "stars",
          dv.labels = c("Linear regression","GLMM"))
  Linear regression GLMM
Predictors Estimates CI Estimates CI
(Intercept) 31.99 *** 25.04 – 38.95 36.41 *** 29.36 – 43.46
team training facilities 0.60 *** 0.26 – 0.95 0.57 * 0.00 – 1.14
goals scored -1.61 *** -2.40 – -0.83 -1.73 *** -2.48 – -0.98
fitness level 0.52 *** 0.38 – 0.65 0.48 *** 0.35 – 0.60
previous season score 0.25 *** 0.12 – 0.38 0.17 ** 0.04 – 0.31
goals scored × previous
season score
0.03 *** 0.02 – 0.05
previous season score ×
goals scored
0.03 *** 0.02 – 0.05
Random Effects
σ2   12.15
τ00   34.56 team_id
τ11   0.01 team_id.previous_season_score
ρ01   -0.99 team_id
ICC   0.15
N   30 team_id
Observations 660 660
R2 / R2 adjusted 0.539 / 0.536 0.483 / 0.559
AIC 3623.940 3608.205
  • p<0.05   ** p<0.01   *** p<0.001
as.formula( paste0("y ~ ", 
                   round(coefficients(model2)[1],2), 
                   " + ", 
                   paste(sprintf("%.2f * %s",coefficients(model2)[-1],
                                 names(coefficients(model2)[-1])),
                         collapse=" + ") ) )
## y ~ 31.99 + 0.6 * team_training_facilities + -1.61 * goals_scored + 
##     0.52 * fitness_level + 0.25 * previous_season_score + 0.03 * 
##     goals_scored:previous_season_score
#correlation plot between predictors and targets
p1<-data |> ggplot(aes(fitness_level,performance_score))+ 
  geom_point()+
  geom_smooth(method = "lm")


p2<-data |> 
  ggplot(aes(previous_season_score,performance_score))+
  geom_point()+
  geom_smooth(method = "lm")

p3<-data |> 
  ggplot(aes(goals_scored,performance_score))+
  geom_point()+
  geom_smooth(method = "lm")

p4<-data |> 
  ggplot(aes(as.factor(team_training_facilities),performance_score,colour = team_training_facilities))+
  geom_point()+
  geom_boxplot()+
  labs(x="team_training_facilities")+
  theme(legend.position="none")

p5<-data |> 
  ggplot(aes(as.factor(goals_scored),previous_season_score,colour = goals_scored))+
  geom_point()+
  geom_boxplot()+
  geom_smooth()+
  labs(x="goals_scored",
       title = "Correlation of goals_scored and previous_season_score")
  

data<-data |> 
  mutate(
    interaction=previous_season_score*goals_scored
  )
p6<-data |> 
  ggplot(aes(interaction,performance_score))+
  geom_point()+
  geom_smooth(method="lm")+
  labs(title = "Relationship of interaction and performance_score")
  
#plots of concluding the team factors
p7<-
  

random_effects <- ranef(ml3)$team_id
random_effects <- as.data.frame(random_effects)
names(random_effects) <- c("Intercept", "Slope")
p8<-random_effects |> 
  ggplot(aes(x = Intercept, y = Slope))+
  geom_point()+
  geom_smooth(method = "lm")+
  labs(title = "Random Intercepts vs Slopes",
       x = "Random Intercept (Team Performance_score)",
       y = "Random Slope (Effect of Previous_season_score)")

p9<-data |> 
  ggplot(aes(performance_score,previous_season_score,colour = team_id))+
  geom_point()+
  geom_boxplot(color="black")+
  geom_smooth(method = "lm")+
  facet_wrap(~team_id)+
  labs(title = "relationship between each team's random effect")+
  theme(legend.position = "none")
p7
p9+p8
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

p5+p6
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

p1+p2+p3+p4+plot_annotation(title = "corrlations of independent predictors with target variable")
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'