# Read in the data
NBA = read.csv("C:/Users/iambi/OneDrive/Documents/NBA_train.csv")
str(NBA)
## 'data.frame':    835 obs. of  20 variables:
##  $ SeasonEnd: int  1980 1980 1980 1980 1980 1980 1980 1980 1980 1980 ...
##  $ Team     : chr  "Atlanta Hawks" "Boston Celtics" "Chicago Bulls" "Cleveland Cavaliers" ...
##  $ Playoffs : int  1 1 0 0 0 0 0 1 0 1 ...
##  $ W        : int  50 61 30 37 30 16 24 41 37 47 ...
##  $ PTS      : int  8573 9303 8813 9360 8878 8933 8493 9084 9119 8860 ...
##  $ oppPTS   : int  8334 8664 9035 9332 9240 9609 8853 9070 9176 8603 ...
##  $ FG       : int  3261 3617 3362 3811 3462 3643 3527 3599 3639 3582 ...
##  $ FGA      : int  7027 7387 6943 8041 7470 7596 7318 7496 7689 7489 ...
##  $ X2P      : int  3248 3455 3292 3775 3379 3586 3500 3495 3551 3557 ...
##  $ X2PA     : int  6952 6965 6668 7854 7215 7377 7197 7117 7375 7375 ...
##  $ X3P      : int  13 162 70 36 83 57 27 104 88 25 ...
##  $ X3PA     : int  75 422 275 187 255 219 121 379 314 114 ...
##  $ FT       : int  2038 1907 2019 1702 1871 1590 1412 1782 1753 1671 ...
##  $ FTA      : int  2645 2449 2592 2205 2539 2149 1914 2326 2333 2250 ...
##  $ ORB      : int  1369 1227 1115 1307 1311 1226 1155 1394 1398 1187 ...
##  $ DRB      : int  2406 2457 2465 2381 2524 2415 2437 2217 2326 2429 ...
##  $ AST      : int  1913 2198 2152 2108 2079 1950 2028 2149 2148 2123 ...
##  $ STL      : int  782 809 704 764 746 783 779 782 900 863 ...
##  $ BLK      : int  539 308 392 342 404 562 339 373 530 356 ...
##  $ TOV      : int  1495 1539 1684 1370 1533 1742 1492 1565 1517 1439 ...
# How many wins to make the playoffs?
table(NBA$W, NBA$Playoffs)
##     
##       0  1
##   11  2  0
##   12  2  0
##   13  2  0
##   14  2  0
##   15 10  0
##   16  2  0
##   17 11  0
##   18  5  0
##   19 10  0
##   20 10  0
##   21 12  0
##   22 11  0
##   23 11  0
##   24 18  0
##   25 11  0
##   26 17  0
##   27 10  0
##   28 18  0
##   29 12  0
##   30 19  1
##   31 15  1
##   32 12  0
##   33 17  0
##   34 16  0
##   35 13  3
##   36 17  4
##   37 15  4
##   38  8  7
##   39 10 10
##   40  9 13
##   41 11 26
##   42  8 29
##   43  2 18
##   44  2 27
##   45  3 22
##   46  1 15
##   47  0 28
##   48  1 14
##   49  0 17
##   50  0 32
##   51  0 12
##   52  0 20
##   53  0 17
##   54  0 18
##   55  0 24
##   56  0 16
##   57  0 23
##   58  0 13
##   59  0 14
##   60  0  8
##   61  0 10
##   62  0 13
##   63  0  7
##   64  0  3
##   65  0  3
##   66  0  2
##   67  0  4
##   69  0  1
##   72  0  1
# Compute Points Difference
NBA$PTSdiff = NBA$PTS - NBA$oppPTS
NBA$PTSdiff
##   [1]   239   639  -222    28  -362  -676  -360    14   -57   257   484   323
##  [13]   -96   -94   346   295   -67   -31  -340   382  -493  -209  -254   482
##  [25]   162  -398  -689   -39  -518   -97    27   115     1   315   596  -494
##  [37]   133   644   452    73   236  -130  -135  -483     1    44   523  -166
##  [49]  -698  -363    43   -75    85    -3  -153  -254   399   449    56  -219
##  [61]   470   283    49   189  -606   339  -464    72   -78   439  -401  -610
##  [73]   -34    51   -33  -297  -951  -480   119   455   361   227   201   629
##  [85]   415   175   300  -396   263  -344   -11  -106   538  -425  -349    35
##  [97]   -90   294  -279  -253  -395  -121   308   341    90   315   180    73
## [109]   291   -22  -267   -14    93  -237  -119   545   -69  -226   178   200
## [121]   204  -602   141  -509  -219  -368   603   562    19  -380   336  -173
## [133]   279    75  -446    -9   -22   194   772  -312  -235    90   107    96
## [145]  -283   214  -273  -568   635   741  -163  -460   193  -245    87  -252
## [157]  -152    -8   -30  -148   593   545    73  -313   517   -71   282  -192
## [169]    82   -53  -937   763   321  -414  -514   -16  -200   257  -264  -418
## [181]    38    33  -112   295   487   279    62   358   334   424  -682   114
## [193]   -65  -846   479    44  -665   -40  -118  -367   371  -472  -400   169
## [205]   302   -63   403    95  -699   118   623   -99   139   476   -25    78
## [217]  -342  -813   588  -921   296  -521   309   123   631   120  -455  -597
## [229]   238   412  -177    84   327  -641   267   -25     6   116   499  -257
## [241]   120    13  -278   556  -797   -64  -347  -645   116  -731   409   582
## [253]   518  -415   286    85   393  -177    63   477  -430   746  -202  -375
## [265]  -895   268   134   287   -32  -283   553  -491   203  -322  -370   -19
## [277]  -326   -15   537   712  -556   370   101   273  -408  -123   297  -320
## [289]   856   447  -627  -645   167   320  -141   155    88   -90  -345  -140
## [301]  -578  -139   319  -567  -104   487   596  -497   272   154   524  -366
## [313]   -71    73   -20   516   529 -1246  -143  -114   -81   347   139    29
## [325]  -104   -94  -306  -638   103   505   108  -473   546   255  -260   219
## [337]   580   178  -577   432  -351   -18   253   330  -713   122  -638   143
## [349]   354   283  -469  -352   219  -531  -568   180   573   319  -625   397
## [361]   216  -473   431   745   346  -605   105  -154   269   396    51  -238
## [373]    69  -598  -444   174   303  -751   -18  -134  -358  -738  -257   255
## [385]   579  -416   318   313   -82   489   671   655  -459   100  -279   -47
## [397]  1004   212  -402  -222   205  -119   143   266  -295   365   117  -435
## [409]  -439  -347   190   456  -820    27   193  -222   517   639  -616   540
## [421]  -818    87   446  -601   153   886   151  -521  -515   430  -386   367
## [433]    80  -193   350   450  -155  -121  -374   256   -29  -536    54   342
## [445]  -277  -646   630  -256   721  -839   133   288  -215   164   583   224
## [457]  -501  -966   129  -748   -62   499  -604   635   404  -157    58   129
## [469]   202   -88  -196   425   115  -458   327   588  -760   536  -599    48
## [481]  -441   -62   219  -771  -287   -47  -174   118  -678   -71   377  -945
## [493]   701   255    18   207   -85   120    56   110   428   525   239   487
## [505]    78   -13   366  -461  -269  -427  -175   172  -746  -348   351  -202
## [517]  -139  -742   188   -16  -237   277   188   318   111  -414   216    81
## [529]   351   181   344   477   636     2   185   385  -470  -547  -347   181
## [541]    79  -700  -273   349  -477   175  -443  -401    22   -38   584  -610
## [553]  -126   -18   277   341  -327   129   131   -56   245   624   509   248
## [565]   -36    73  -115  -292   -32  -421  -789   638  -679   302   -93   121
## [577]   286  -338   191  -265  -414    19   170   428   173  -111    11   189
## [589]    93   214   533   444   -10  -481   196   -83  -381  -119  -521  -215
## [601]   364    88   479   -60   142   472  -376   320   200    43    87   450
## [613]   205    -8  -121  -576  -204  -307  -104   411   592   -52  -247  -100
## [625]  -462  -796    71  -491    87    65   471   166   317  -177   331    62
## [637]   -63  -243   188   535  -245   119  -123  -580  -200  -184   -61   584
## [649]  -328   177   640   188  -133  -350   -27  -390  -126  -328    52   183
## [661]   498    19   547  -111  -130   155   129   205   303   317   -85  -154
## [673]   112  -231  -525   -88  -160   455  -775   126   559  -248  -245  -216
## [685]   152  -390  -280  -307   411   314   592   133   341   -28   398  -200
## [697]   -38    -6  -422   -75  -359  -301   -63  -129  -234    64  -248   599
## [709]  -352  -148   691  -237    81   235   -42  -149   841  -359  -253   -29
## [721]   372   304   606   181   384  -115  -596   595  -509  -709  -565  -556
## [733]  -415   433  -542   448    34   414   -80  -185   393  -718   238   564
## [745]   -27   129   616  -104   -23   732   162   280   -40  -307   328   -91
## [757]  -719   628  -448    21   -89  -403  -200   127  -214  -500   549     6
## [769]   158   438  -718   308  -231   217  -612   382   300   120  -134   535
## [781]   223   335  -419  -295   -30  -247  -521   387  -124   187   139  -787
## [793]  -748  -202  -313   286   614  -320   402   271  -358   417  -146   438
## [805]  -392   -67   440  -328   600  -739   347   390  -295  -191   179   -88
## [817]  -257   501   192   612   -69  -544  -512    73    64   311   448   123
## [829]   -73   125  -438   468  -515  -150  -607
# Check for linear relationship
plot(NBA$PTSdiff, NBA$W)

# Linear regression model for wins
WinsReg = lm(W ~ PTSdiff, data=NBA)
summary(WinsReg)
## 
## Call:
## lm(formula = W ~ PTSdiff, data = NBA)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.7393 -2.1018 -0.0672  2.0265 10.6026 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 4.100e+01  1.059e-01   387.0   <2e-16 ***
## PTSdiff     3.259e-02  2.793e-04   116.7   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.061 on 833 degrees of freedom
## Multiple R-squared:  0.9423, Adjusted R-squared:  0.9423 
## F-statistic: 1.361e+04 on 1 and 833 DF,  p-value: < 2.2e-16
#Wins = 41+0.03259*(PD)
# We created an object containing 49 wins minus the intercept estimate (41) divided by the PTSdiff coefficient estimate
PD = (49-41)/0.03259
PD
## [1] 245.4741

##Our data shows that a team with 49 wins has never missed the playoffs. What is the expected points difference for a team to make it to the postseason? Use the lecture solution file and more specifically the WingsReg model.

#A point difference of 245 will guarantee a presence in the playoffs