# 1
library(readr)
setwd("C:/Users/user/Downloads/week12")
 
MData <- read.csv("jordan_playoffs.csv") # Downloading M Data
# Lebron Regular Season Data
LData <- read.csv("lebron_playoffs.csv") # Downloading L Data

Merged = merge(x = MData,
             y = LData,
             by = c("trb"))
head(Merged)
##   trb game.x    date.x series.x series_game.x team.x opp.x result.x mp.x fg.x
## 1   1     21 6/14/1998      FIN             6    CHI   UTA   W (+1)   44   15
## 2   2      8 5/23/1993      ECF             1    CHI   NYK   L (-8)   43   10
## 3   2      3 4/30/1997      EC1             3    CHI   WSB   W (+1)   43   14
## 4   2     13  6/4/1993      ECF             6    CHI   NYK   W (+8)   45    8
## 5   2      8 5/14/1996      ECS             5    CHI   NYK  W (+13)   43   13
## 6   2     15 5/29/1989      ECF             4    CHI   DET   L (-6)   43    5
##   fga.x fgp.x three.x threeatt.x threep.x ft.x fta.x ftp.x orb.x drb.x ast.x
## 1    35 0.429       3          7    0.429   12    15 0.800     0     1     1
## 2    27 0.370       2          4    0.500    5     6 0.833     1     1     5
## 3    24 0.583       0          5    0.000    0     0    NA     1     1     6
## 4    24 0.333       1          4    0.250    8     9 0.889     0     2     9
## 5    29 0.448       1          2    0.500    8     9 0.889     1     1     5
## 6    15 0.333       1          3    0.333   12    17 0.706     0     2     4
##   stl.x blk.x tov.x pts.x game_score.x plus_minus.x game.y    date.y series.y
## 1     4     0     1    45         28.5           NA      6  5/8/2007      ECS
## 2     4     0     1    27         17.2           NA     14 5/28/2014      ECF
## 3     0     1     2    28         19.9           NA     14 5/28/2014      ECF
## 4     3     2     1    25         19.7           NA     14 5/28/2014      ECF
## 5     2     0     2    35         22.0           NA     14 5/28/2014      ECF
## 6     1     0     1    23         13.9           NA     14 5/28/2014      ECF
##   series_game.y team.y opp.y result.y mp.y fg.y fga.y fgp.y three.y threeatt.y
## 1             2    CLE   NJN  W (+10)   46   12    24   0.5       3          7
## 2             5    MIA   IND   L (-3)   24    2    10   0.2       1          3
## 3             5    MIA   IND   L (-3)   24    2    10   0.2       1          3
## 4             5    MIA   IND   L (-3)   24    2    10   0.2       1          3
## 5             5    MIA   IND   L (-3)   24    2    10   0.2       1          3
## 6             5    MIA   IND   L (-3)   24    2    10   0.2       1          3
##   threep.y ft.y fta.y ftp.y orb.y drb.y ast.y stl.y blk.y tov.y pts.y
## 1    0.429    9    13 0.692     0     1    12     3     0     2    36
## 2    0.333    2     3 0.667     1     1     4     0     1     3     7
## 3    0.333    2     3 0.667     1     1     4     0     1     3     7
## 4    0.333    2     3 0.667     1     1     4     0     1     3     7
## 5    0.333    2     3 0.667     1     1     4     0     1     3     7
## 6    0.333    2     3 0.667     1     1     4     0     1     3     7
##   game_score.y plus_minus.y
## 1         31.3            8
## 2         -0.1            1
## 3         -0.1            1
## 4         -0.1            1
## 5         -0.1            1
## 6         -0.1            1
tail(Merged)
##      trb game.x    date.x series.x series_game.x team.x opp.x result.x mp.x
## 3601  15      8 5/13/1989      ECS             3    CHI   NYK  W (+23)   39
## 3602  15      8 5/13/1989      ECS             3    CHI   NYK  W (+23)   39
## 3603  16      5  5/8/1997      ECS             2    CHI   ATL   L (-8)   45
## 3604  16      5  5/8/1997      ECS             2    CHI   ATL   L (-8)   45
## 3605  16      5  5/8/1997      ECS             2    CHI   ATL   L (-8)   45
## 3606  19      8 5/14/1991      ECS             5    CHI   PHI   W (+5)   42
##      fg.x fga.x fgp.x three.x threeatt.x threep.x ft.x fta.x ftp.x orb.x drb.x
## 3601   14    25 0.560       1          2      0.5   11    13 0.846     2    13
## 3602   14    25 0.560       1          2      0.5   11    13 0.846     2    13
## 3603   12    29 0.414       0          6      0.0    3     3 1.000     6    10
## 3604   12    29 0.414       0          6      0.0    3     3 1.000     6    10
## 3605   12    29 0.414       0          6      0.0    3     3 1.000     6    10
## 3606   14    31 0.452       0          2      0.0   10    11 0.909     4    15
##      ast.x stl.x blk.x tov.x pts.x game_score.x plus_minus.x game.y    date.y
## 3601     9     6     1     3    40         41.4           NA      3 4/21/2011
## 3602     9     6     1     3    40         41.4           NA     17  6/7/2012
## 3603     6     0     1     1    27         21.8           NA     19 6/13/2016
## 3604     6     0     1     1    27         21.8           NA     16  6/7/2015
## 3605     6     0     1     1    27         21.8           NA     15 9/26/2020
## 3606     7     0     1     4    38         29.2           NA     11 5/13/2010
##      series.y series_game.y team.y opp.y result.y mp.y fg.y fga.y fgp.y three.y
## 3601      EC1             3    MIA   PHI   W (+6)   44    8    15 0.533       1
## 3602      ECF             6    MIA   BOS  W (+19)   45   19    26 0.731       2
## 3603      FIN             5    CLE   GSW  W (+15)   43   16    30 0.533       4
## 3604      FIN             2    CLE   GSW   W (+2)   50   11    35 0.314       3
## 3605      WCF             5    LAL   DEN  W (+10)   40   15    25 0.600       1
## 3606      ECS             6    CLE   BOS   L (-9)   46    8    21 0.381       2
##      threeatt.y threep.y ft.y fta.y ftp.y orb.y drb.y ast.y stl.y blk.y tov.y
## 3601          4     0.25    7    10 0.700     1    14     6     1     0     1
## 3602          4     0.50    5     9 0.556     2    13     5     0     0     4
## 3603          8     0.50    5     8 0.625     4    12     7     3     3     2
## 3604          6     0.50   14    18 0.778     4    12    11     1     1     3
## 3605          4     0.25    7     8 0.875     3    13    10     1     0     2
## 3606          4     0.50    9    12 0.750     3    16    10     3     1     9
##      pts.y game_score.y plus_minus.y
## 3601    24         24.2            8
## 3602    45         36.4           22
## 3603    41         39.2           13
## 3604    39         28.9            0
## 3605    38         37.3           12
## 3606    27         22.1           -5
# my independent variable is series_game.x and dependent variable is    game_score.x

# $ Y = beta_0 + simX(i)\beta_1 + \epsilon for the formula, Y is equal to series.game.x and x is equal to game_score.x
#lm(formula = Units ~ game_score.x, data = Merged)
MJ.lm = lm(data = Merged, formula = series_game.x ~ game_score.x)
summary(MJ.lm)
## 
## Call:
## lm(formula = series_game.x ~ game_score.x, data = Merged)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.5459 -1.2046 -0.1146  1.1409  4.0487 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   3.600379   0.090817  39.644  < 2e-16 ***
## game_score.x -0.020940   0.003415  -6.132 9.64e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.641 on 3604 degrees of freedom
## Multiple R-squared:  0.01032,    Adjusted R-squared:  0.01005 
## F-statistic:  37.6 on 1 and 3604 DF,  p-value: 9.643e-10
# Interpret the slope and intercept parameters .

#The slope of the data set indicates that a one-unit increase in the value of series_game results in an increase of 3.600379 in the game_score. Meanwhile, the intercept reveals that when the value of series_game is 0, the value of game_score.x is equivalent to the intercept.

Slope <- cov(Merged$series_game.x, Merged$game_score.x)/var(Merged$game_score.x)
Slope
## [1] -0.02093955
Intercept1 <- mean(Merged$series_game.x) - Slope * mean(Merged$game_score.x)
Intercept1
## [1] 3.600379
lm(data = Merged, formula = series_game.x ~ game_score.x)
## 
## Call:
## lm(formula = series_game.x ~ game_score.x, data = Merged)
## 
## Coefficients:
##  (Intercept)  game_score.x  
##      3.60038      -0.02094