In [1]:
import pandas as pd
import seaborn as sns
import statsmodels.formula.api as smf
import statsmodels.api as sm2
import statsmodels.api as sm 
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn import metrics 
from sklearn.model_selection  import train_test_split
from statsmodels.graphics.gofplots import qqplot_2samples, qqplot
import numpy as np

1) PROJECT OBJECTIVES

1) We will try to identify what kind of strategy followed the most successful teams :

    - High offenses
    - Tight defenses
    - Balance of two

2) We will valid this conclusion across four periods :

    - Period 1 - before 1920
    - Period 2 - 1920 to 1960
    - Period 3 - 1960 to 1990
    - Period 4 - 1990 to 2010

2) DATA EXPLORATION

1.Perform an exploratory data analysis to understand the statistical properties and distribution shapes of 8 key variables of your choice. Perform this analysis for a single dataset that spans 1960 to 2010

Eight key variables

- Games Won
- Games Lost
- Runs Scored
- Home Runs
- Strike outs
- Shutouts
- Home Run Allowed
- Errors
In [2]:
baseball = pd.read_csv(r'C:\Users\jaime\Documents\DATA\BBA\DATA ANALYSING\Certificate Data Analysis\Statistics for Data Analytics\project\baseball_teams v2.csv')
baseball
Out[2]:
Year League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost ... Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
0 1871 NaN BS1 BNA NaN 3 31 NaN 20 10 ... 367 2 42 23 225 NaN 0.830 Boston Red Stockings South End Grounds I NaN
1 1871 NaN CH1 CNA NaN 2 28 NaN 19 9 ... 308 6 28 22 218 NaN 0.820 Chicago White Stockings Union Base-Ball Grounds NaN
2 1871 NaN CL1 CFC NaN 8 29 NaN 10 19 ... 346 13 53 34 223 NaN 0.810 Cleveland Forest Citys National Association Grounds NaN
3 1871 NaN FW1 KEK NaN 7 19 NaN 7 12 ... 261 5 21 17 163 NaN 0.800 Fort Wayne Kekiongas Hamilton Field NaN
4 1871 NaN NY2 NNA NaN 5 33 NaN 16 17 ... 373 7 42 22 227 NaN 0.830 New York Mutuals Union Grounds (Brooklyn) NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2800 2015 NL LAN LAD W 1 162 81.0 92 70 ... 1317 145 395 1396 75 133.0 0.988 Los Angeles Dodgers Dodger Stadium 3764815.0
2801 2015 NL SFN SFG W 2 162 81.0 84 78 ... 1344 155 431 1165 78 145.0 0.987 San Francisco Giants AT&T Park 3375882.0
2802 2015 NL ARI ARI W 3 162 81.0 79 83 ... 1450 182 500 1215 86 146.0 0.986 Arizona Diamondbacks Chase Field 2080145.0
2803 2015 NL SDN SDP W 4 162 81.0 74 88 ... 1371 171 516 1393 92 138.0 0.985 San Diego Padres Petco Park 2459742.0
2804 2015 NL COL COL W 5 162 81.0 68 94 ... 1579 183 579 1112 95 171.0 0.985 Colorado Rockies Coors Field 2506789.0

2805 rows × 43 columns

In [5]:
pd.set_option('display.max_columns', None)
baseball.head()
Out[5]:
Year League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 Unnamed: 11 League_Win World_Series Runs_Scored At_Bats Hits Doubles Triples Home_Runs Walks Strike_Outs Stolen_Bases Caught_Stealing Hit_By_Pitch Sacrifice_Fly Runs_Against Earned_Runs Earned_Run_Average Complete_Games Shutout Saves Infield_Put_Outs Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
0 1871 NaN BS1 BNA NaN 3 31 NaN 20 10 NaN NaN False NaN 401 1372 426 70 37 3 60 19.0 73.0 NaN NaN NaN 303 109 3.55 22 1 3 828 367 2 42 23 225 NaN 0.83 Boston Red Stockings South End Grounds I NaN
1 1871 NaN CH1 CNA NaN 2 28 NaN 19 9 NaN NaN False NaN 302 1196 323 52 21 10 60 22.0 69.0 NaN NaN NaN 241 77 2.76 25 0 1 753 308 6 28 22 218 NaN 0.82 Chicago White Stockings Union Base-Ball Grounds NaN
2 1871 NaN CL1 CFC NaN 8 29 NaN 10 19 NaN NaN False NaN 249 1186 328 35 40 7 26 25.0 18.0 NaN NaN NaN 341 116 4.11 23 0 0 762 346 13 53 34 223 NaN 0.81 Cleveland Forest Citys National Association Grounds NaN
3 1871 NaN FW1 KEK NaN 7 19 NaN 7 12 NaN NaN False NaN 137 746 178 19 8 2 33 9.0 16.0 NaN NaN NaN 243 97 5.17 19 1 0 507 261 5 21 17 163 NaN 0.80 Fort Wayne Kekiongas Hamilton Field NaN
4 1871 NaN NY2 NNA NaN 5 33 NaN 16 17 NaN NaN False NaN 302 1404 403 43 21 1 33 15.0 46.0 NaN NaN NaN 313 121 3.72 32 1 0 879 373 7 42 22 227 NaN 0.83 New York Mutuals Union Grounds (Brooklyn) NaN
In [19]:
list(baseball)
Out[19]:
['Year ',
 'League',
 'Team ',
 'Franchise ',
 'Division',
 'Final_Standing',
 'Games_Played',
 'Unnamed: 7',
 'Games_Won',
 'Games_Lost',
 'Unnamed: 10',
 'Unnamed: 11',
 'League_Win',
 'World_Series',
 'Runs_Scored',
 'At_Bats',
 'Hits',
 'Doubles',
 'Triples',
 'Home_Runs',
 'Walks',
 'Strike_Outs',
 'Stolen_Bases',
 'Caught_Stealing',
 'Hit_By_Pitch',
 'Sacrifice_Fly',
 'Runs_Against',
 'Earned_Runs',
 'Earned_Run_Average',
 'Complete_Games',
 'Shutout',
 'Saves',
 'Infield_Put_Outs',
 'Hits_Allowed',
 'Home_Run_Allowed',
 'Walks_Allowed',
 'Strikeouts_Allowed',
 'Errors',
 'Double_Plays',
 'Fielding_Percentage',
 'Team_Name',
 'Home_Ball_Park',
 'Attendance']
In [4]:
baseball=baseball.set_index('Year ')
baseball
Out[4]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 ... Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
1871 NaN BS1 BNA NaN 3 31 NaN 20 10 NaN ... 367 2 42 23 225 NaN 0.830 Boston Red Stockings South End Grounds I NaN
1871 NaN CH1 CNA NaN 2 28 NaN 19 9 NaN ... 308 6 28 22 218 NaN 0.820 Chicago White Stockings Union Base-Ball Grounds NaN
1871 NaN CL1 CFC NaN 8 29 NaN 10 19 NaN ... 346 13 53 34 223 NaN 0.810 Cleveland Forest Citys National Association Grounds NaN
1871 NaN FW1 KEK NaN 7 19 NaN 7 12 NaN ... 261 5 21 17 163 NaN 0.800 Fort Wayne Kekiongas Hamilton Field NaN
1871 NaN NY2 NNA NaN 5 33 NaN 16 17 NaN ... 373 7 42 22 227 NaN 0.830 New York Mutuals Union Grounds (Brooklyn) NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2015 NL LAN LAD W 1 162 81.0 92 70 True ... 1317 145 395 1396 75 133.0 0.988 Los Angeles Dodgers Dodger Stadium 3764815.0
2015 NL SFN SFG W 2 162 81.0 84 78 False ... 1344 155 431 1165 78 145.0 0.987 San Francisco Giants AT&T Park 3375882.0
2015 NL ARI ARI W 3 162 81.0 79 83 False ... 1450 182 500 1215 86 146.0 0.986 Arizona Diamondbacks Chase Field 2080145.0
2015 NL SDN SDP W 4 162 81.0 74 88 False ... 1371 171 516 1393 92 138.0 0.985 San Diego Padres Petco Park 2459742.0
2015 NL COL COL W 5 162 81.0 68 94 False ... 1579 183 579 1112 95 171.0 0.985 Colorado Rockies Coors Field 2506789.0

2805 rows × 42 columns

Slice data between 1960 to 2010

In [53]:
baseball_1960_2010=baseball.loc['1960':'2010']
baseball_1960_2010
Out[53]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 ... Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
1960 AL BAL BAL NaN 2 154 77.0 89 65 NaN ... 1222 117 552 785 107 172.0 0.980 Baltimore Orioles Memorial Stadium 1187849.0
1960 AL BOS BOS NaN 7 154 77.0 65 89 NaN ... 1440 127 580 767 140 156.0 0.970 Boston Red Sox Fenway Park II 1129866.0
1960 AL CHA CHW NaN 3 154 77.0 87 67 NaN ... 1338 127 533 695 109 175.0 0.980 Chicago White Sox Comiskey Park 1644460.0
1960 NL CHN CHC NaN 7 156 79.0 60 94 NaN ... 1393 152 565 805 143 133.0 0.970 Chicago Cubs Wrigley Field 809770.0
1960 NL CIN CIN NaN 6 154 77.0 67 87 NaN ... 1417 134 442 740 125 155.0 0.970 Cincinnati Reds Crosley Field 663486.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2010 NL SLN STL C 2 162 81.0 86 76 False ... 1412 133 477 1094 99 172.0 0.984 St. Louis Cardinals Busch Stadium III 3301218.0
2010 AL TBA TBD E 1 162 81.0 96 66 True ... 1347 175 478 1189 85 134.0 0.986 Tampa Bay Rays Tropicana Field 1843445.0
2010 AL TEX TEX W 1 162 81.0 90 72 True ... 1355 162 551 1181 105 133.0 0.982 Texas Rangers Rangers Ballpark in Arlington 2505171.0
2010 AL TOR TOR E 4 162 78.0 85 77 False ... 1407 150 539 1184 92 172.0 0.985 Toronto Blue Jays Rogers Centre 1625555.0
2010 NL WAS WSN E 5 162 81.0 69 93 False ... 1469 151 512 1068 127 148.0 0.979 Washington Nationals Nationals Park 1828066.0

1312 rows × 42 columns

a) Descriptive Statistics

In [28]:
baseball_1960_2010[['Games_Won','Games_Lost','Runs_Scored','Home_Runs','Strike_Outs','Shutout','Home_Run_Allowed','Errors']].describe()
Out[28]:
Games_Won Games_Lost Runs_Scored Home_Runs Strike_Outs Shutout Home_Run_Allowed Errors
count 1312.000000 1312.000000 1312.000000 1312.000000 1312.000000 1312.000000 1312.000000 1312.000000
mean 79.523628 79.523628 703.445884 142.350610 932.101372 9.805640 142.350610 123.248476
std 12.516572 12.464481 102.107114 40.518801 152.857630 4.490301 34.559522 24.774944
min 37.000000 40.000000 329.000000 32.000000 379.000000 0.000000 40.000000 57.000000
25% 71.000000 71.000000 641.000000 113.000000 838.750000 7.000000 118.000000 105.000000
50% 80.000000 79.000000 704.000000 139.000000 927.000000 9.000000 141.000000 123.000000
75% 89.000000 88.000000 771.000000 169.000000 1037.250000 12.000000 166.000000 139.250000
max 116.000000 120.000000 1009.000000 264.000000 1529.000000 30.000000 241.000000 210.000000

b) Show a histogram for each of the 8 variables

Games Won

In [50]:
baseball_1960_2010['Games_Won'].hist(figsize=(10,5),bins = 10, color='cornflowerblue')
plt.title('GAMES WON - BASEBALL DATA')
plt.ylabel('Counts')
plt.xlabel('Games Won')
Out[50]:
Text(0.5, 0, 'Games Won')
In [39]:
plt.figure(figsize=(10,5))
plt.title('GAMES WON - BASEBALL DATA')
plt.ylabel('Density')
sns.distplot(baseball_1960_2010['Games_Won'], hist=True, kde=True, 
             bins=10, color = 'mediumseagreen', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
Out[39]:
<matplotlib.axes._subplots.AxesSubplot at 0xfe85160>

Games Lost

In [51]:
baseball_1960_2010['Games_Lost'].hist(figsize=(10,5),bins = 10, color='cornflowerblue')
plt.title('GAMES LOST - BASEBALL DATA')
plt.ylabel('Counts')
plt.xlabel('Games Lost')
Out[51]:
Text(0.5, 0, 'Games Lost')
In [41]:
plt.figure(figsize=(10,5))
plt.title('GAMES LOST - BASEBALL DATA')
plt.ylabel('Density')
sns.distplot(baseball_1960_2010['Games_Lost'], hist=True, kde=True, 
             bins=10, color = 'mediumseagreen', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
Out[41]:
<matplotlib.axes._subplots.AxesSubplot at 0x1189ad60>

Runs Scored

In [52]:
baseball_1960_2010['Runs_Scored'].hist(figsize=(10,5),bins = 10, color='cornflowerblue')
plt.title('RUNS SCORED - BASEBALL DATA')
plt.ylabel('Counts')
plt.xlabel('Runs Scored')
Out[52]:
Text(0.5, 0, 'Runs Scored')
In [42]:
plt.figure(figsize=(10,5))
plt.title('RUNS SCORED - BASEBALL DATA')
plt.ylabel('Density')
sns.distplot(baseball_1960_2010['Runs_Scored'], hist=True, kde=True, 
             bins=10, color = 'mediumseagreen', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
Out[42]:
<matplotlib.axes._subplots.AxesSubplot at 0x118f63a0>

Home Runs

In [53]:
baseball_1960_2010['Home_Runs'].hist(figsize=(10,5),bins = 10, color='cornflowerblue')
plt.title('HOME RUNS - BASEBALL DATA')
plt.ylabel('Counts')
plt.xlabel('Home Runs')
Out[53]:
Text(0.5, 0, 'Home Runs')
In [43]:
plt.figure(figsize=(10,5))
plt.title('HOME RUNS - BASEBALL DATA')
plt.ylabel('Density')
sns.distplot(baseball_1960_2010['Home_Runs'], hist=True, kde=True, 
             bins=10, color = 'mediumseagreen', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
Out[43]:
<matplotlib.axes._subplots.AxesSubplot at 0x11972130>

Strike Outs

In [54]:
baseball_1960_2010['Strike_Outs'].hist(figsize=(10,5),bins = 10, color='cornflowerblue')
plt.title('STRIKE OUTS - BASEBALL DATA')
plt.ylabel('Counts')
plt.xlabel('Strike Outs')
Out[54]:
Text(0.5, 0, 'Strike Outs')
In [44]:
plt.figure(figsize=(10,5))
plt.title('STRIKE OUTS - BASEBALL DATA')
plt.ylabel('Density')
sns.distplot(baseball_1960_2010['Strike_Outs'], hist=True, kde=True, 
             bins=10, color = 'mediumseagreen', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
Out[44]:
<matplotlib.axes._subplots.AxesSubplot at 0x119ba7c0>

Shut Out

In [55]:
baseball_1960_2010['Shutout'].hist(figsize=(10,5),bins = 10, color='cornflowerblue')
plt.title('SHUT OUTS - BASEBALL DATA')
plt.ylabel('Counts')
plt.xlabel('Shut outs')
Out[55]:
Text(0.5, 0, 'Shut outs')
In [45]:
plt.figure(figsize=(10,5))
plt.title('SHUT OUTS - BASEBALL DATA')
plt.ylabel('Density')
sns.distplot(baseball_1960_2010['Shutout'], hist=True, kde=True, 
             bins=10, color = 'mediumseagreen', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x11a40280>

Home Run Allowed

In [56]:
baseball_1960_2010['Home_Run_Allowed'].hist(figsize=(10,5),bins = 10, color='cornflowerblue')
plt.title('HOME RUN ALLOWED - BASEBALL DATA')
plt.ylabel('Counts')
plt.xlabel('Home Run Allowed')
Out[56]:
Text(0.5, 0, 'Home Run Allowed')
In [46]:
plt.figure(figsize=(10,5))
plt.title('HOME RUN ALLOWED - BASEBALL DATA')
plt.ylabel('Density')
sns.distplot(baseball_1960_2010['Home_Run_Allowed'], hist=True, kde=True, 
             bins=10, color = 'mediumseagreen', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
Out[46]:
<matplotlib.axes._subplots.AxesSubplot at 0x11a96df0>

Errors

In [57]:
baseball_1960_2010['Errors'].hist(figsize=(10,5),bins = 10, color='cornflowerblue')
plt.title('ERRORS - BASEBALL DATA')
plt.ylabel('Counts')
plt.xlabel('Errors')
Out[57]:
Text(0.5, 0, 'Errors')
In [47]:
plt.figure(figsize=(10,5))
plt.title('ERRORS - BASEBALL DATA')
plt.ylabel('Density')
sns.distplot(baseball_1960_2010['Errors'], hist=True, kde=True, 
             bins=10, color = 'mediumseagreen', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
Out[47]:
<matplotlib.axes._subplots.AxesSubplot at 0x11afefd0>

c) Show a boxplot for each of the 8 variables

Games Won

In [60]:
boxplot=baseball_1960_2010.boxplot(column=['Games_Won'],figsize=(16,6))
plt.title('GAMES WON BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('Number of Games won ')
Out[60]:
Text(0, 0.5, 'Number of Games won ')

Games Lost

In [61]:
boxplot=baseball_1960_2010.boxplot(column=['Games_Lost'],figsize=(16,6))
plt.title('GAMES LOST BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('Number of Games Lost ')
Out[61]:
Text(0, 0.5, 'Number of Games Lost ')

Run Scored

In [62]:
boxplot=baseball_1960_2010.boxplot(column=['Runs_Scored'],figsize=(16,6))
plt.title('RUNS SCORED BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('Number of Runs scored ')
Out[62]:
Text(0, 0.5, 'Number of Runs scored ')

Home Runs

In [63]:
boxplot=baseball_1960_2010.boxplot(column=['Home_Runs'],figsize=(16,6))
plt.title('HOME RUNS BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('Number of Home Runs ')
Out[63]:
Text(0, 0.5, 'Number of Home Runs ')

Strike Outs

In [64]:
boxplot=baseball_1960_2010.boxplot(column=['Strike_Outs'],figsize=(16,6))
plt.title('STRIKE OUTS BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('Number of Strike outs ')
Out[64]:
Text(0, 0.5, 'Number of Strike outs ')

Shut Outs

In [65]:
boxplot=baseball_1960_2010.boxplot(column=['Shutout'],figsize=(16,6))
plt.title('SHUT OUT BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('Number of Shut Outs ')
Out[65]:
Text(0, 0.5, 'Number of Shut Outs ')

Home Run Allowed

In [66]:
boxplot=baseball_1960_2010.boxplot(column=['Home_Run_Allowed'],figsize=(16,6))
plt.title('HOME RUN ALLOWED BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('Number of Home Run Allowed ')
Out[66]:
Text(0, 0.5, 'Number of Home Run Allowed ')

Errors

In [67]:
boxplot=baseball_1960_2010.boxplot(column=['Errors'],figsize=(16,6))
plt.title('ERRORS BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('Number of Errors ')
Out[67]:
Text(0, 0.5, 'Number of Errors ')

d) Comment on the visual shape of the distribution of each variable

Is it symmetric?

Is there a single mode?

Is it narrow or wide?

Are there many outliers?

1) Games Won

We can see a wide histogram that is left - skewed with a single mode where Mean<Median<Mode. There are teams that won less games than the average and they produce the outliers show at the boxplot

2) Games Lost

We can see a wide histogram that is in appereance symmetric with a single mode . There are outliers up and down giving a wide shape to the histogram.

3) Run Scored

We can see a wide histogram that looks like left - skewed with a single mode where Mean<Median<Mode. There are teams that Run Scored less than the average and they produce the outliers show at the boxplot

4) Home Runs

We can see a wide histogram that is likely right - skewed with a single mode where Mean>Median>Mode. There are teams that made more Home runs than the average and they produce the outliers show at the boxplot

5) Strike outs

We can see a wide histogram that is a little left - skewed with a single mode where Mean<Median<Mode. There are teams that make less Strike outs than the average and they produce the outliers show at the boxplot more below to the box at the boxplot

6) Shut Outs

We can see a wide histogram that is likely right - skewed with a single mode where Mean>Median>Mode. There are teams that made more Shut Outs than the average and they produce the outliers above as show at the boxplot

7) Home Run Allowed

We can see a wide histogram that is in appereance symmetric with a single mode . There are some outliers up and down giving a wide shape to the histogram.

8) Errors

We can see a wide histogram that looks like left - skewed with a single mode where Mean<Median<Mode. There are teams that made less Errors than the average and they produce the outliers show at the above of the boxplot

e) Comment on the validity of the assumption that each of the 8 variables is normally distributed.

Game Won

In [84]:
x = baseball_1960_2010['Games_Won']
y = np.random.normal(10, 1, size=10000)
pp_x = sm.ProbPlot(x)
pp_y = sm.ProbPlot(y)
qqplot_2samples(pp_x, pp_y)
plt.show()

We can see an almost straight line , but watching the histogram it is lightly left - skewed .

Game Lost

In [85]:
x = baseball_1960_2010['Games_Lost']
y = np.random.normal(10, 1, size=10000)
pp_x = sm.ProbPlot(x)
pp_y = sm.ProbPlot(y)
qqplot_2samples(pp_x, pp_y)
plt.show()

We can see an straight line with fat tails following a normal distribution.

Run Scored

In [76]:
x = baseball_1960_2010['Runs_Scored']
y = np.random.normal(10, 1, size=10000)
pp_x = sm.ProbPlot(x)
pp_y = sm.ProbPlot(y)
qqplot_2samples(pp_x, pp_y)
plt.show()

We can see an almost straight line , but watching the histogram it is lightly left - skewed .

Home Runs

In [77]:
x = baseball_1960_2010['Home_Runs']
y = np.random.normal(10, 1, size=10000)
pp_x = sm.ProbPlot(x)
pp_y = sm.ProbPlot(y)
qqplot_2samples(pp_x, pp_y)
plt.show()

We can see an almost straight line , but watching the histogram it is lightly right - skewed .

Strike Outs

In [78]:
x = baseball_1960_2010['Strike_Outs']
y = np.random.normal(10, 1, size=10000)
pp_x = sm.ProbPlot(x)
pp_y = sm.ProbPlot(y)
qqplot_2samples(pp_x, pp_y)
plt.show()

We can see an almost straight line , but watching the histogram it is lightly left - skewed .

Shut Outs

In [79]:
x = baseball_1960_2010['Shutout']
y = np.random.normal(10, 1, size=10000)
pp_x = sm.ProbPlot(x)
pp_y = sm.ProbPlot(y)
qqplot_2samples(pp_x, pp_y)
plt.show()

We can see a line going up and watching the histogram it is right - skewed .

Home Run Allowed

In [80]:
x = baseball_1960_2010['Home_Run_Allowed']
y = np.random.normal(10, 1, size=10000)
pp_x = sm.ProbPlot(x)
pp_y = sm.ProbPlot(y)
qqplot_2samples(pp_x, pp_y)
plt.show()

We can see an straight line with fat tails following a normal distribution

Errors

In [81]:
x = baseball_1960_2010['Errors']
y = np.random.normal(10, 1, size=10000)
pp_x = sm.ProbPlot(x)
pp_y = sm.ProbPlot(y)
qqplot_2samples(pp_x, pp_y)
plt.show()

We can see an almost straight line , but watching the histogram it is lightly left - skewed

3) CORRELATION ANALYSIS

Do a correlation analysis for the following pairs of fields for the complete data spanning 1960 to 2010

a) Games Won vs Runs Scored

In [13]:
baseball_1960_2010[['Games_Won','Runs_Scored']].corr(method = 'pearson')
Out[13]:
Games_Won Runs_Scored
Games_Won 1.000000 0.595828
Runs_Scored 0.595828 1.000000
In [5]:
baseball_1960_2010[['Games_Won','Runs_Scored']].corr(method = 'spearman')
Out[5]:
Games_Won Runs_Scored
Games_Won 1.000000 0.555276
Runs_Scored 0.555276 1.000000
In [31]:
sns.scatterplot('Games_Won','Runs_Scored',data=baseball_1960_2010)
plt.title('GAMES WON vs RUNS SCORED', fontsize=18)
plt.ylabel('Runs Scored', fontsize=16)
plt.xlabel('Games Won', fontsize=16)
Out[31]:
Text(0.5, 0, 'Games Won')

b) Games Won vs Runs Against

In [14]:
baseball_1960_2010[['Games_Won','Runs_Against']].corr(method = 'pearson')
Out[14]:
Games_Won Runs_Against
Games_Won 1.000000 -0.248158
Runs_Against -0.248158 1.000000
In [6]:
baseball_1960_2010[['Games_Won','Runs_Against']].corr(method = 'spearman')
Out[6]:
Games_Won Runs_Against
Games_Won 1.000000 -0.357522
Runs_Against -0.357522 1.000000
In [32]:
sns.scatterplot('Games_Won','Runs_Against',data=baseball_1960_2010)
plt.title('GAMES WON vs RUNS AGAINST', fontsize=18)
plt.ylabel('Runs Against', fontsize=16)
plt.xlabel('Games Won', fontsize=16)
Out[32]:
Text(0.5, 0, 'Games Won')

c) Games Won vs (Runs Scored minus Runs Against)

In [9]:
baseball_1960_2010['Score_against'] = baseball_1960_2010['Runs_Scored'] - baseball_1960_2010['Runs_Against']
baseball_1960_2010.head(10)
<ipython-input-9-16f93c81ec6b>:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  baseball_1960_2010['Score_against'] = baseball_1960_2010['Runs_Scored'] - baseball_1960_2010['Runs_Against']
Out[9]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 ... Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance Score_against
Year
1960 AL BAL BAL NaN 2 154 77.0 89 65 NaN ... 117 552 785 107 172.0 0.98 Baltimore Orioles Memorial Stadium 1187849.0 76
1960 AL BOS BOS NaN 7 154 77.0 65 89 NaN ... 127 580 767 140 156.0 0.97 Boston Red Sox Fenway Park II 1129866.0 -117
1960 AL CHA CHW NaN 3 154 77.0 87 67 NaN ... 127 533 695 109 175.0 0.98 Chicago White Sox Comiskey Park 1644460.0 124
1960 NL CHN CHC NaN 7 156 79.0 60 94 NaN ... 152 565 805 143 133.0 0.97 Chicago Cubs Wrigley Field 809770.0 -142
1960 NL CIN CIN NaN 6 154 77.0 67 87 NaN ... 134 442 740 125 155.0 0.97 Cincinnati Reds Crosley Field 663486.0 -52
1960 AL CLE CLE NaN 4 154 77.0 76 78 NaN ... 161 636 771 126 165.0 0.97 Cleveland Indians Cleveland Stadium 950985.0 -26
1960 AL DET DET NaN 6 154 77.0 71 83 NaN ... 141 474 824 137 138.0 0.97 Detroit Tigers Briggs Stadium 1167669.0 -11
1960 AL KC1 OAK NaN 8 155 78.0 58 96 NaN ... 160 525 664 127 149.0 0.97 Kansas City Athletics Municipal Stadium I 774944.0 -141
1960 NL LAN LAD NaN 4 154 77.0 82 72 NaN ... 154 564 1122 124 142.0 0.97 Los Angeles Dodgers Los Angeles Memorial Coliseum 2253887.0 69
1960 NL ML1 ATL NaN 2 154 77.0 88 66 NaN ... 130 518 807 139 137.0 0.97 Milwaukee Braves County Stadium 1497799.0 66

10 rows × 43 columns

In [15]:
baseball_1960_2010[['Games_Won','Score_against']].corr(method = 'pearson')
Out[15]:
Games_Won Score_against
Games_Won 1.000000 0.850411
Score_against 0.850411 1.000000
In [10]:
baseball_1960_2010[['Games_Won','Score_against']].corr(method = 'spearman')
Out[10]:
Games_Won Score_against
Games_Won 1.000000 0.873039
Score_against 0.873039 1.000000
In [35]:
sns.scatterplot('Games_Won','Score_against',data=baseball_1960_2010)
plt.title('GAMES WON vs SCORE - AGAINST', fontsize=18)
plt.ylabel('Score - Against', fontsize=16)
plt.xlabel('Games Won', fontsize=16)
Out[35]:
Text(0.5, 0, 'Games Won')

d) Games Lost vs Runs Scored

In [11]:
baseball_1960_2010[['Games_Lost','Runs_Scored']].corr(method = 'spearman')
Out[11]:
Games_Lost Runs_Scored
Games_Lost 1.000000 -0.326352
Runs_Scored -0.326352 1.000000
In [12]:
baseball_1960_2010[['Games_Lost','Runs_Scored']].corr(method = 'pearson')
Out[12]:
Games_Lost Runs_Scored
Games_Lost 1.000000 -0.226772
Runs_Scored -0.226772 1.000000
In [34]:
sns.scatterplot('Games_Lost','Runs_Scored',data=baseball_1960_2010)
plt.title('GAMES LOST vs RUNS SCORED', fontsize=18)
plt.ylabel('Runs Scored', fontsize=16)
plt.xlabel('Games Lost', fontsize=16)
Out[34]:
Text(0.5, 0, 'Games Lost')

e) Games Lost vs Runs Against

In [16]:
baseball_1960_2010[['Games_Lost','Runs_Against']].corr(method = 'pearson')
Out[16]:
Games_Lost Runs_Against
Games_Lost 1.000000 0.611177
Runs_Against 0.611177 1.000000
In [17]:
baseball_1960_2010[['Games_Lost','Runs_Against']].corr(method = 'spearman')
Out[17]:
Games_Lost Runs_Against
Games_Lost 1.000000 0.579854
Runs_Against 0.579854 1.000000
In [36]:
sns.scatterplot('Games_Lost','Runs_Against',data=baseball_1960_2010)
plt.title('GAMES LOST vs RUNS AGAINST', fontsize=18)
plt.ylabel('Runs Against', fontsize=16)
plt.xlabel('Games Lost', fontsize=16)
Out[36]:
Text(0.5, 0, 'Games Lost')

f) Games Lost vs (Runs Scored minus Runs Against

In [18]:
baseball_1960_2010[['Games_Lost','Score_against']].corr(method = 'pearson')
Out[18]:
Games_Lost Score_against
Games_Lost 1.000000 -0.849358
Score_against -0.849358 1.000000
In [19]:
baseball_1960_2010[['Games_Lost','Score_against']].corr(method = 'spearman')
Out[19]:
Games_Lost Score_against
Games_Lost 1.00000 -0.86804
Score_against -0.86804 1.00000
In [37]:
sns.scatterplot('Games_Lost','Score_against',data=baseball_1960_2010)
plt.title('GAMES LOST vs SCORE - AGAINST', fontsize=18)
plt.ylabel('Score - Against', fontsize=16)
plt.xlabel('Games Lost', fontsize=16)
Out[37]:
Text(0.5, 0, 'Games Lost')

g) Provide an interpretation and commentary for your analysis.

We can see that there are two pair of fields where the correlation is quite strong :

- Game Won vs (Runs score minus runs against)  = 0.85 pearson (positive)
                                               = 0.87 spearman(positive) 
- Game Lost vs (Runs score minus runs against) = -0.85 pearson(negative)
                                               = -0.87 spearman(negative)

There are other two variables that have relative impact on the game to lost or win.

 - Game Won vs Runs score = 0.59 pearson (positive)
                          = 0.55 spearman(positive)
 - Game Lost vs Runs against = 0.61 pearson (positive)
                             = 0.58 spearman (positive)

We can see that these two variables ( Runs score , Runs against) could have a determinant impact on the strategy of the game. The rest of the pairs of fields has a very low correlation so , the impact on the game is very low or null.

4) LINEAR REGRESSION MODELS

Create a multiple linear regression model for each of the 4 time periods noted above and select the independent variables that are good predictors of games won.

Keep the model as simple as possible

Try combinations of offensive and defensive fields as independent variables.

a)Period 1 : before 1920

In [54]:
baseball_before_1920=baseball.loc['1871':'1919']
baseball_before_1920
Out[54]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 ... Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
1871 NaN BS1 BNA NaN 3 31 NaN 20 10 NaN ... 367 2 42 23 225 NaN 0.83 Boston Red Stockings South End Grounds I NaN
1871 NaN CH1 CNA NaN 2 28 NaN 19 9 NaN ... 308 6 28 22 218 NaN 0.82 Chicago White Stockings Union Base-Ball Grounds NaN
1871 NaN CL1 CFC NaN 8 29 NaN 10 19 NaN ... 346 13 53 34 223 NaN 0.81 Cleveland Forest Citys National Association Grounds NaN
1871 NaN FW1 KEK NaN 7 19 NaN 7 12 NaN ... 261 5 21 17 163 NaN 0.80 Fort Wayne Kekiongas Hamilton Field NaN
1871 NaN NY2 NNA NaN 5 33 NaN 16 17 NaN ... 373 7 42 22 227 NaN 0.83 New York Mutuals Union Grounds (Brooklyn) NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1919 NL PHI PHI NaN 8 138 71.0 47 90 NaN ... 1391 40 408 397 219 112.0 0.96 Philadelphia Phillies Baker Bowl 240424.0
1919 NL PIT PIT NaN 4 139 70.0 71 68 NaN ... 1113 23 263 391 160 89.0 0.96 Pittsburgh Pirates Forbes Field 276810.0
1919 AL SLA BAL NaN 5 140 70.0 67 72 NaN ... 1255 35 421 415 214 98.0 0.96 St. Louis Browns Sportsman's Park IV 349350.0
1919 NL SLN STL NaN 7 138 69.0 54 83 NaN ... 1146 25 415 414 217 112.0 0.96 St. Louis Cardinals Robison Field 167059.0
1919 AL WS1 MIN NaN 7 142 72.0 56 84 NaN ... 1237 20 451 536 227 86.0 0.96 Washington Senators Griffith Stadium I 234096.0

703 rows × 42 columns

In [55]:
pd.set_option('display.max_columns', None)
baseball_before_1920.head()
Out[55]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 Unnamed: 11 League_Win World_Series Runs_Scored At_Bats Hits Doubles Triples Home_Runs Walks Strike_Outs Stolen_Bases Caught_Stealing Hit_By_Pitch Sacrifice_Fly Runs_Against Earned_Runs Earned_Run_Average Complete_Games Shutout Saves Infield_Put_Outs Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
1871 NaN BS1 BNA NaN 3 31 NaN 20 10 NaN NaN False NaN 401 1372 426 70 37 3 60 19.0 73.0 NaN NaN NaN 303 109 3.55 22 1 3 828 367 2 42 23 225 NaN 0.83 Boston Red Stockings South End Grounds I NaN
1871 NaN CH1 CNA NaN 2 28 NaN 19 9 NaN NaN False NaN 302 1196 323 52 21 10 60 22.0 69.0 NaN NaN NaN 241 77 2.76 25 0 1 753 308 6 28 22 218 NaN 0.82 Chicago White Stockings Union Base-Ball Grounds NaN
1871 NaN CL1 CFC NaN 8 29 NaN 10 19 NaN NaN False NaN 249 1186 328 35 40 7 26 25.0 18.0 NaN NaN NaN 341 116 4.11 23 0 0 762 346 13 53 34 223 NaN 0.81 Cleveland Forest Citys National Association Grounds NaN
1871 NaN FW1 KEK NaN 7 19 NaN 7 12 NaN NaN False NaN 137 746 178 19 8 2 33 9.0 16.0 NaN NaN NaN 243 97 5.17 19 1 0 507 261 5 21 17 163 NaN 0.80 Fort Wayne Kekiongas Hamilton Field NaN
1871 NaN NY2 NNA NaN 5 33 NaN 16 17 NaN NaN False NaN 302 1404 403 43 21 1 33 15.0 46.0 NaN NaN NaN 313 121 3.72 32 1 0 879 373 7 42 22 227 NaN 0.83 New York Mutuals Union Grounds (Brooklyn) NaN
In [56]:
period1 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Hits + Doubles + Triples + Home_Runs + Walks + Strike_Outs + Stolen_Bases + Runs_Against +Earned_Runs + Earned_Run_Average + Complete_Games + Shutout + Saves + Infield_Put_Outs + Hits_Allowed + Home_Run_Allowed + Strikeouts_Allowed + Errors', data=baseball_before_1920).fit()
period1.params
Out[56]:
Intercept            -7.409097
Runs_Scored           0.083708
At_Bats              -0.017414
Hits                  0.001749
Doubles               0.017280
Triples               0.008804
Home_Runs             0.004749
Walks                -0.000186
Strike_Outs          -0.013991
Stolen_Bases         -0.005491
Runs_Against         -0.060905
Earned_Runs          -0.016614
Earned_Run_Average    1.839158
Complete_Games        0.149988
Shutout               0.290882
Saves                 0.460201
Infield_Put_Outs      0.034669
Hits_Allowed         -0.003811
Home_Run_Allowed      0.019212
Strikeouts_Allowed    0.003395
Errors                0.000273
dtype: float64

b) Period 2: 1920 to 1959

In [57]:
baseball_1920_1959=baseball.loc['1920':'1959']
baseball_1920_1959.head()
Out[57]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 Unnamed: 11 League_Win World_Series Runs_Scored At_Bats Hits Doubles Triples Home_Runs Walks Strike_Outs Stolen_Bases Caught_Stealing Hit_By_Pitch Sacrifice_Fly Runs_Against Earned_Runs Earned_Run_Average Complete_Games Shutout Saves Infield_Put_Outs Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
1920 AL BOS BOS NaN 5 154 76.0 72 81 NaN NaN False False 650 5199 1397 216 71 22 533 429.0 98.0 111.0 NaN NaN 698 592 3.82 92 11 6 4185 1481 39 461 481 183 131.0 0.97 Boston Red Sox Fenway Park I 402445.0
1920 NL BRO LAD NaN 1 155 78.0 93 61 NaN NaN True False 660 5399 1493 205 99 28 359 391.0 70.0 80.0 NaN NaN 528 415 2.62 89 17 10 4281 1381 25 327 553 226 118.0 0.96 Brooklyn Robins Ebbets Field 808722.0
1920 NL BSN ATL NaN 7 153 74.0 62 90 NaN NaN False False 523 5218 1358 168 86 23 385 488.0 88.0 98.0 NaN NaN 670 545 3.54 93 14 6 4158 1464 39 415 368 239 125.0 0.96 Boston Braves Braves Field 162483.0
1920 AL CHA CHW NaN 2 154 77.0 96 58 NaN NaN False False 794 5328 1574 263 98 37 471 353.0 112.0 96.0 NaN NaN 665 553 3.59 109 9 10 4158 1467 45 405 438 194 142.0 0.96 Chicago White Sox Comiskey Park 833492.0
1920 NL CHN CHC NaN 5 154 77.0 75 79 NaN NaN False False 619 5117 1350 223 67 34 428 421.0 115.0 129.0 NaN NaN 635 504 3.27 95 13 9 4164 1459 37 382 508 225 112.0 0.96 Chicago Cubs Wrigley Field 480783.0
In [58]:
period2 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Hits + Doubles + Triples + Home_Runs + Walks + Strike_Outs + Stolen_Bases + Runs_Against +Earned_Runs + Earned_Run_Average + Complete_Games + Shutout + Saves + Infield_Put_Outs + Hits_Allowed + Home_Run_Allowed + Strikeouts_Allowed + Errors', data=baseball_1920_1959).fit()
period2.params
Out[58]:
Intercept            -151.627537
Runs_Scored             0.085199
At_Bats                -0.014181
Hits                    0.008801
Doubles                 0.011921
Triples                 0.016868
Home_Runs              -0.005235
Walks                  -0.004030
Strike_Outs            -0.001276
Stolen_Bases           -0.020502
Runs_Against           -0.078548
Earned_Runs            -0.234614
Earned_Run_Average     36.479257
Complete_Games          0.130029
Shutout                 0.057469
Saves                   0.308981
Infield_Put_Outs        0.066116
Hits_Allowed           -0.002234
Home_Run_Allowed        0.010757
Strikeouts_Allowed      0.001868
Errors                 -0.007678
dtype: float64

c) Period 3: 1960 to 1989

In [59]:
baseball_1960_1989=baseball.loc['1960':'1989']
baseball_1960_1989.head()
Out[59]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 Unnamed: 11 League_Win World_Series Runs_Scored At_Bats Hits Doubles Triples Home_Runs Walks Strike_Outs Stolen_Bases Caught_Stealing Hit_By_Pitch Sacrifice_Fly Runs_Against Earned_Runs Earned_Run_Average Complete_Games Shutout Saves Infield_Put_Outs Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
1960 AL BAL BAL NaN 2 154 77.0 89 65 NaN NaN False False 682 5170 1307 206 33 123 596 801.0 37.0 24.0 NaN NaN 606 538 3.52 48 11 22 4125 1222 117 552 785 107 172.0 0.98 Baltimore Orioles Memorial Stadium 1187849.0
1960 AL BOS BOS NaN 7 154 77.0 65 89 NaN NaN False False 658 5215 1359 234 32 124 570 798.0 34.0 28.0 NaN NaN 775 699 4.62 34 6 23 4083 1440 127 580 767 140 156.0 0.97 Boston Red Sox Fenway Park II 1129866.0
1960 AL CHA CHW NaN 3 154 77.0 87 67 NaN NaN False False 741 5191 1402 242 38 112 567 648.0 122.0 48.0 NaN NaN 617 552 3.60 42 11 26 4143 1338 127 533 695 109 175.0 0.98 Chicago White Sox Comiskey Park 1644460.0
1960 NL CHN CHC NaN 7 156 79.0 60 94 NaN NaN False False 634 5311 1293 213 48 119 531 897.0 51.0 34.0 NaN NaN 776 678 4.35 36 6 25 4206 1393 152 565 805 143 133.0 0.97 Chicago Cubs Wrigley Field 809770.0
1960 NL CIN CIN NaN 6 154 77.0 67 87 NaN NaN False False 640 5289 1324 230 40 140 512 858.0 73.0 37.0 NaN NaN 692 618 4.00 33 8 35 4170 1417 134 442 740 125 155.0 0.97 Cincinnati Reds Crosley Field 663486.0
In [60]:
period3 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Hits + Doubles + Triples + Home_Runs + Walks + Strike_Outs + Stolen_Bases + Runs_Against +Earned_Runs + Earned_Run_Average + Complete_Games + Shutout + Saves + Infield_Put_Outs + Hits_Allowed + Home_Run_Allowed + Strikeouts_Allowed + Errors', data=baseball_1960_1989).fit()
period3.params
Out[60]:
Intercept            -12.729140
Runs_Scored            0.084690
At_Bats               -0.035491
Hits                   0.028635
Doubles               -0.002220
Triples                0.010240
Home_Runs              0.010061
Walks                 -0.001679
Strike_Outs            0.000274
Stolen_Bases          -0.002866
Runs_Against          -0.050538
Earned_Runs           -0.039561
Earned_Run_Average     3.349776
Complete_Games         0.141008
Shutout                0.196018
Saves                  0.306784
Infield_Put_Outs       0.050438
Hits_Allowed           0.001209
Home_Run_Allowed       0.000010
Strikeouts_Allowed     0.001286
Errors                -0.024885
dtype: float64

d) Period 4 : 1990 to 2010

In [61]:
baseball_1990_2010=baseball.loc['1990':'2010']
baseball_1990_2010.head()
Out[61]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 Unnamed: 11 League_Win World_Series Runs_Scored At_Bats Hits Doubles Triples Home_Runs Walks Strike_Outs Stolen_Bases Caught_Stealing Hit_By_Pitch Sacrifice_Fly Runs_Against Earned_Runs Earned_Run_Average Complete_Games Shutout Saves Infield_Put_Outs Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
1990 NL ATL ATL W 6 162 81.0 65 97 False NaN False False 682 5504 1376 263 26 162 473 1010.0 92.0 55.0 NaN NaN 821 727 4.58 17 8 30 4287 1527 128 579 938 158 133.0 0.97 Atlanta Braves Atlanta-Fulton County Stadium 980129.0
1990 AL BAL BAL E 5 161 80.0 76 85 False NaN False False 669 5410 1328 234 22 132 660 962.0 94.0 52.0 NaN NaN 698 644 4.04 10 5 43 4305 1445 161 537 776 91 151.0 0.98 Baltimore Orioles Memorial Stadium 2415189.0
1990 AL BOS BOS E 1 162 81.0 88 74 True NaN False False 699 5516 1502 298 31 106 598 795.0 53.0 52.0 NaN NaN 664 596 3.72 15 13 44 4326 1439 92 519 997 123 154.0 0.98 Boston Red Sox Fenway Park II 2528986.0
1990 AL CAL ANA W 4 162 81.0 80 82 False NaN False False 690 5570 1448 237 27 147 566 1000.0 69.0 43.0 NaN NaN 706 612 3.79 21 13 42 4362 1482 106 544 944 140 186.0 0.97 California Angels Anaheim Stadium 2555688.0
1990 AL CHA CHW W 2 162 80.0 94 68 False NaN False False 682 5402 1393 251 44 106 478 903.0 140.0 90.0 NaN NaN 633 581 3.61 17 10 68 4347 1313 106 548 914 124 169.0 0.98 Chicago White Sox Comiskey Park 2002357.0
In [62]:
period4 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Hits + Doubles + Triples + Home_Runs + Walks + Strike_Outs + Stolen_Bases + Runs_Against +Earned_Runs + Earned_Run_Average + Complete_Games + Shutout + Saves + Infield_Put_Outs + Hits_Allowed + Home_Run_Allowed + Strikeouts_Allowed + Errors', data=baseball_1990_2010).fit()
period4.params
Out[62]:
Intercept            -14.316908
Runs_Scored            0.078149
At_Bats               -0.036174
Hits                   0.031622
Doubles               -0.002154
Triples               -0.034648
Home_Runs              0.013826
Walks                 -0.001524
Strike_Outs            0.001597
Stolen_Bases          -0.001389
Runs_Against          -0.030092
Earned_Runs           -0.053664
Earned_Run_Average     3.522177
Complete_Games         0.074009
Shutout                0.175764
Saves                  0.359500
Infield_Put_Outs       0.050227
Hits_Allowed           0.001531
Home_Run_Allowed      -0.009283
Strikeouts_Allowed     0.003374
Errors                -0.044771
dtype: float64

Justify your model in terms of the independent variables you selected

First of all we created a model with the variables that could have an impact on winning a game for the four periods. Next step we will analyse the Statistical Significance for the variables and choose which ones have a direct contribution to the model.

5) MODEL VALIDATION

Evaluate each of your 4 regression models

Statistical significance of the overall model

Statistical significance of each coefficient, including the intercept

Are your independent variables approximately normally distributed?

a) Period 1 : before 1920

Model 1

In [63]:
period1.summary()
Out[63]:
OLS Regression Results
Dep. Variable: Games_Won R-squared: 0.966
Model: OLS Adj. R-squared: 0.964
Method: Least Squares F-statistic: 593.3
Date: Wed, 15 Jul 2020 Prob (F-statistic): 2.73e-292
Time: 18:32:45 Log-Likelihood: -1215.9
No. Observations: 439 AIC: 2474.
Df Residuals: 418 BIC: 2559.
Df Model: 20
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -7.4091 3.336 -2.221 0.027 -13.967 -0.851
Runs_Scored 0.0837 0.005 15.271 0.000 0.073 0.094
At_Bats -0.0174 0.003 -5.209 0.000 -0.024 -0.011
Hits 0.0017 0.005 0.325 0.745 -0.009 0.012
Doubles 0.0173 0.009 1.862 0.063 -0.001 0.036
Triples 0.0088 0.013 0.679 0.498 -0.017 0.034
Home_Runs 0.0047 0.021 0.231 0.817 -0.036 0.045
Walks -0.0002 0.004 -0.048 0.962 -0.008 0.007
Strike_Outs -0.0140 0.003 -4.536 0.000 -0.020 -0.008
Stolen_Bases -0.0055 0.004 -1.538 0.125 -0.013 0.002
Runs_Against -0.0609 0.008 -7.579 0.000 -0.077 -0.045
Earned_Runs -0.0166 0.009 -1.751 0.081 -0.035 0.002
Earned_Run_Average 1.8392 0.651 2.824 0.005 0.559 3.119
Complete_Games 0.1500 0.019 7.973 0.000 0.113 0.187
Shutout 0.2909 0.059 4.906 0.000 0.174 0.407
Saves 0.4602 0.084 5.454 0.000 0.294 0.626
Infield_Put_Outs 0.0347 0.004 9.290 0.000 0.027 0.042
Hits_Allowed -0.0038 0.004 -0.859 0.391 -0.013 0.005
Home_Run_Allowed 0.0192 0.023 0.841 0.401 -0.026 0.064
Strikeouts_Allowed 0.0034 0.003 1.171 0.242 -0.002 0.009
Errors 0.0003 0.006 0.042 0.966 -0.012 0.013
Omnibus: 0.284 Durbin-Watson: 2.052
Prob(Omnibus): 0.867 Jarque-Bera (JB): 0.166
Skew: 0.036 Prob(JB): 0.921
Kurtosis: 3.061 Cond. No. 1.15e+05


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.15e+05. This might indicate that there are
strong multicollinearity or other numerical problems.

We can see the R2 and adj R2 are very good numbers, F statistics is far from zero, we can say that the model is in good shape. There are coefficients where the p-value is very high ( more than 0.05), we eliminate them and check for the next model.

Model 2

In [64]:
period12 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Strike_Outs + Runs_Against + Earned_Run_Average + Complete_Games + Shutout + Saves + Infield_Put_Outs ', data=baseball_before_1920).fit()
period12.params
Out[64]:
Intercept            -0.344138
Runs_Scored           0.086609
At_Bats              -0.016800
Strike_Outs          -0.007529
Runs_Against         -0.068260
Earned_Run_Average    0.665946
Complete_Games        0.140833
Shutout               0.319498
Saves                 0.400946
Infield_Put_Outs      0.031915
dtype: float64
In [65]:
period12.summary()
Out[65]:
OLS Regression Results
Dep. Variable: Games_Won R-squared: 0.973
Model: OLS Adj. R-squared: 0.972
Method: Least Squares F-statistic: 2261.
Date: Wed, 15 Jul 2020 Prob (F-statistic): 0.00
Time: 18:34:10 Log-Likelihood: -1615.5
No. Observations: 583 AIC: 3251.
Df Residuals: 573 BIC: 3295.
Df Model: 9
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -0.3441 1.263 -0.273 0.785 -2.824 2.136
Runs_Scored 0.0866 0.002 43.986 0.000 0.083 0.090
At_Bats -0.0168 0.002 -8.195 0.000 -0.021 -0.013
Strike_Outs -0.0075 0.002 -4.529 0.000 -0.011 -0.004
Runs_Against -0.0683 0.003 -24.472 0.000 -0.074 -0.063
Earned_Run_Average 0.6659 0.347 1.917 0.056 -0.016 1.348
Complete_Games 0.1408 0.015 9.272 0.000 0.111 0.171
Shutout 0.3195 0.052 6.149 0.000 0.217 0.422
Saves 0.4009 0.077 5.212 0.000 0.250 0.552
Infield_Put_Outs 0.0319 0.002 14.672 0.000 0.028 0.036
Omnibus: 0.708 Durbin-Watson: 2.051
Prob(Omnibus): 0.702 Jarque-Bera (JB): 0.653
Skew: -0.082 Prob(JB): 0.722
Kurtosis: 3.013 Cond. No. 4.51e+04


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 4.51e+04. This might indicate that there are
strong multicollinearity or other numerical problems.

We can see the R2 and adj R2 are very good numbers, F statistics is far from zero, we can say that the model is in good shape. There is one coefficient where the p-value is high ( more than 0.05), Earned_Run_Average. We eliminate and check for the next model.

Model 3

In [66]:
period13 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Strike_Outs + Runs_Against + Complete_Games + Shutout + Saves + Infield_Put_Outs ', data=baseball_before_1920).fit()
period13.params
Out[66]:
Intercept           1.648306
Runs_Scored         0.087227
At_Bats            -0.018195
Strike_Outs        -0.007932
Runs_Against       -0.064160
Complete_Games      0.136551
Shutout             0.319593
Saves               0.391681
Infield_Put_Outs    0.033100
dtype: float64
In [67]:
period13.summary()
Out[67]:
OLS Regression Results
Dep. Variable: Games_Won R-squared: 0.972
Model: OLS Adj. R-squared: 0.972
Method: Least Squares F-statistic: 2531.
Date: Wed, 15 Jul 2020 Prob (F-statistic): 0.00
Time: 18:34:33 Log-Likelihood: -1617.3
No. Observations: 583 AIC: 3253.
Df Residuals: 574 BIC: 3292.
Df Model: 8
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept 1.6483 0.719 2.293 0.022 0.236 3.060
Runs_Scored 0.0872 0.002 44.800 0.000 0.083 0.091
At_Bats -0.0182 0.002 -9.472 0.000 -0.022 -0.014
Strike_Outs -0.0079 0.002 -4.799 0.000 -0.011 -0.005
Runs_Against -0.0642 0.002 -35.742 0.000 -0.068 -0.061
Complete_Games 0.1366 0.015 9.068 0.000 0.107 0.166
Shutout 0.3196 0.052 6.137 0.000 0.217 0.422
Saves 0.3917 0.077 5.090 0.000 0.241 0.543
Infield_Put_Outs 0.0331 0.002 15.834 0.000 0.029 0.037
Omnibus: 0.595 Durbin-Watson: 2.058
Prob(Omnibus): 0.743 Jarque-Bera (JB): 0.561
Skew: -0.076 Prob(JB): 0.755
Kurtosis: 3.000 Cond. No. 2.50e+04


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 2.5e+04. This might indicate that there are
strong multicollinearity or other numerical problems.

Now we have a perfect model with good numbers for R and R square and the coefficients shows a p-value = 0 all of them. The contribution of them to the model has a high impact on the result, in this case is "win a game".

b) Period 2: 1920 to 1959

Model 1

In [68]:
period2.summary()
Out[68]:
OLS Regression Results
Dep. Variable: Games_Won R-squared: 0.935
Model: OLS Adj. R-squared: 0.933
Method: Least Squares F-statistic: 442.6
Date: Wed, 15 Jul 2020 Prob (F-statistic): 0.00
Time: 18:34:51 Log-Likelihood: -1738.8
No. Observations: 640 AIC: 3520.
Df Residuals: 619 BIC: 3613.
Df Model: 20
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -151.6275 58.096 -2.610 0.009 -265.716 -37.539
Runs_Scored 0.0852 0.006 14.242 0.000 0.073 0.097
At_Bats -0.0142 0.003 -4.718 0.000 -0.020 -0.008
Hits 0.0088 0.005 1.713 0.087 -0.001 0.019
Doubles 0.0119 0.007 1.818 0.070 -0.001 0.025
Triples 0.0169 0.013 1.337 0.182 -0.008 0.042
Home_Runs -0.0052 0.009 -0.563 0.574 -0.024 0.013
Walks -0.0040 0.003 -1.282 0.200 -0.010 0.002
Strike_Outs -0.0013 0.003 -0.499 0.618 -0.006 0.004
Stolen_Bases -0.0205 0.007 -2.980 0.003 -0.034 -0.007
Runs_Against -0.0785 0.011 -7.013 0.000 -0.101 -0.057
Earned_Runs -0.2346 0.097 -2.414 0.016 -0.426 -0.044
Earned_Run_Average 36.4793 14.630 2.493 0.013 7.748 65.210
Complete_Games 0.1300 0.019 6.899 0.000 0.093 0.167
Shutout 0.0575 0.051 1.116 0.265 -0.044 0.159
Saves 0.3090 0.037 8.393 0.000 0.237 0.381
Infield_Put_Outs 0.0661 0.015 4.550 0.000 0.038 0.095
Hits_Allowed -0.0022 0.004 -0.609 0.543 -0.009 0.005
Home_Run_Allowed 0.0108 0.009 1.202 0.230 -0.007 0.028
Strikeouts_Allowed 0.0019 0.002 0.824 0.410 -0.003 0.006
Errors -0.0077 0.010 -0.780 0.436 -0.027 0.012
Omnibus: 0.632 Durbin-Watson: 2.134
Prob(Omnibus): 0.729 Jarque-Bera (JB): 0.732
Skew: -0.045 Prob(JB): 0.693
Kurtosis: 2.860 Cond. No. 2.93e+06


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 2.93e+06. This might indicate that there are
strong multicollinearity or other numerical problems.

We can see the R2 and adj R2 are very good numbers, F statistics is far from zero, we can say that the model is in good shape. There are some coefficients where the p-value is high ( more than 0.05). We eliminate them and check for the next model

Model 2

In [69]:
period22 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Stolen_Bases + Runs_Against +Earned_Runs + Earned_Run_Average + Complete_Games + Saves + Infield_Put_Outs ', data=baseball_1920_1959).fit()
period22.params
Out[69]:
Intercept            -151.310079
Runs_Scored             0.089440
At_Bats                -0.006244
Stolen_Bases           -0.011774
Runs_Against           -0.082425
Earned_Runs            -0.223702
Earned_Run_Average     34.794949
Complete_Games          0.138805
Saves                   0.319269
Infield_Put_Outs        0.058204
dtype: float64
In [70]:
period22.summary()
Out[70]:
OLS Regression Results
Dep. Variable: Games_Won R-squared: 0.932
Model: OLS Adj. R-squared: 0.931
Method: Least Squares F-statistic: 955.8
Date: Wed, 15 Jul 2020 Prob (F-statistic): 0.00
Time: 18:39:23 Log-Likelihood: -1752.5
No. Observations: 640 AIC: 3525.
Df Residuals: 630 BIC: 3570.
Df Model: 9
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -151.3101 57.704 -2.622 0.009 -264.626 -37.994
Runs_Scored 0.0894 0.002 40.082 0.000 0.085 0.094
At_Bats -0.0062 0.002 -2.855 0.004 -0.011 -0.002
Stolen_Bases -0.0118 0.006 -1.967 0.050 -0.024 -1.8e-05
Runs_Against -0.0824 0.007 -12.296 0.000 -0.096 -0.069
Earned_Runs -0.2237 0.095 -2.351 0.019 -0.411 -0.037
Earned_Run_Average 34.7949 14.484 2.402 0.017 6.353 63.237
Complete_Games 0.1388 0.018 7.751 0.000 0.104 0.174
Saves 0.3193 0.037 8.700 0.000 0.247 0.391
Infield_Put_Outs 0.0582 0.014 4.081 0.000 0.030 0.086
Omnibus: 0.619 Durbin-Watson: 2.170
Prob(Omnibus): 0.734 Jarque-Bera (JB): 0.498
Skew: -0.060 Prob(JB): 0.779
Kurtosis: 3.067 Cond. No. 2.72e+06


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 2.72e+06. This might indicate that there are
strong multicollinearity or other numerical problems.

Now we have a perfect model with good numbers for R and R square and the coefficients shows a p-value = 0 all of them. The contribution of them to the model has a high impact on the result, in this case is "win a game".

Period 3: 1960 to 1989

Model 1

In [71]:
period3.summary()
Out[71]:
OLS Regression Results
Dep. Variable: Games_Won R-squared: 0.934
Model: OLS Adj. R-squared: 0.932
Method: Least Squares F-statistic: 482.2
Date: Wed, 15 Jul 2020 Prob (F-statistic): 0.00
Time: 18:43:08 Log-Likelihood: -1833.0
No. Observations: 704 AIC: 3708.
Df Residuals: 683 BIC: 3804.
Df Model: 20
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -12.7291 19.417 -0.656 0.512 -50.853 25.394
Runs_Scored 0.0847 0.006 14.324 0.000 0.073 0.096
At_Bats -0.0355 0.005 -7.288 0.000 -0.045 -0.026
Hits 0.0286 0.006 4.823 0.000 0.017 0.040
Doubles -0.0022 0.006 -0.351 0.726 -0.015 0.010
Triples 0.0102 0.014 0.721 0.471 -0.018 0.038
Home_Runs 0.0101 0.008 1.285 0.199 -0.005 0.025
Walks -0.0017 0.003 -0.553 0.580 -0.008 0.004
Strike_Outs 0.0003 0.002 0.162 0.872 -0.003 0.004
Stolen_Bases -0.0029 0.004 -0.813 0.417 -0.010 0.004
Runs_Against -0.0505 0.013 -3.830 0.000 -0.076 -0.025
Earned_Runs -0.0396 0.036 -1.101 0.271 -0.110 0.031
Earned_Run_Average 3.3498 5.387 0.622 0.534 -7.227 13.927
Complete_Games 0.1410 0.017 8.535 0.000 0.109 0.173
Shutout 0.1960 0.042 4.716 0.000 0.114 0.278
Saves 0.3068 0.024 12.974 0.000 0.260 0.353
Infield_Put_Outs 0.0504 0.007 7.675 0.000 0.038 0.063
Hits_Allowed 0.0012 0.003 0.386 0.700 -0.005 0.007
Home_Run_Allowed 1.017e-05 0.008 0.001 0.999 -0.016 0.016
Strikeouts_Allowed 0.0013 0.001 0.917 0.360 -0.001 0.004
Errors -0.0249 0.010 -2.401 0.017 -0.045 -0.005
Omnibus: 1.503 Durbin-Watson: 1.917
Prob(Omnibus): 0.472 Jarque-Bera (JB): 1.542
Skew: -0.111 Prob(JB): 0.463
Kurtosis: 2.943 Cond. No. 1.19e+06


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.19e+06. This might indicate that there are
strong multicollinearity or other numerical problems.

We can see the R2 and adj R2 are very good numbers, F statistics is far from zero, we can say that the model is in good shape. There are some coefficients where the p-value is high ( more than 0.05). We eliminate them and check for the next model

Model 2

In [72]:
period32 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Hits + Runs_Against + Complete_Games + Shutout + Saves + Infield_Put_Outs + Errors', data=baseball_1960_1989).fit()
period32.params
Out[72]:
Intercept          -1.200281
Runs_Scored         0.088452
At_Bats            -0.029344
Hits                0.021558
Runs_Against       -0.067753
Complete_Games      0.148611
Shutout             0.205061
Saves               0.315405
Infield_Put_Outs    0.042081
Errors             -0.015785
dtype: float64
In [73]:
period32.summary()
Out[73]:
OLS Regression Results
Dep. Variable: Games_Won R-squared: 0.933
Model: OLS Adj. R-squared: 0.932
Method: Least Squares F-statistic: 1073.
Date: Wed, 15 Jul 2020 Prob (F-statistic): 0.00
Time: 18:47:38 Log-Likelihood: -1837.7
No. Observations: 704 AIC: 3695.
Df Residuals: 694 BIC: 3741.
Df Model: 9
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -1.2003 1.925 -0.624 0.533 -4.979 2.578
Runs_Scored 0.0885 0.003 28.923 0.000 0.082 0.094
At_Bats -0.0293 0.004 -7.701 0.000 -0.037 -0.022
Hits 0.0216 0.004 5.273 0.000 0.014 0.030
Runs_Against -0.0678 0.003 -24.115 0.000 -0.073 -0.062
Complete_Games 0.1486 0.015 9.809 0.000 0.119 0.178
Shutout 0.2051 0.041 5.044 0.000 0.125 0.285
Saves 0.3154 0.023 13.625 0.000 0.270 0.361
Infield_Put_Outs 0.0421 0.004 10.957 0.000 0.035 0.050
Errors -0.0158 0.007 -2.257 0.024 -0.030 -0.002
Omnibus: 1.574 Durbin-Watson: 1.938
Prob(Omnibus): 0.455 Jarque-Bera (JB): 1.640
Skew: -0.110 Prob(JB): 0.440
Kurtosis: 2.916 Cond. No. 1.10e+05


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.1e+05. This might indicate that there are
strong multicollinearity or other numerical problems.

Now we have a perfect model with good numbers for R and R square and the coefficients shows a p-value = 0 all of them. The contribution of them to the model has a high impact on the result, in this case is "win a game"

Now we have a perfect model with good numbers for R and R square and the coefficients shows a p-value = 0 all of them. The contribution of them to the model has a high impact on the result, in this case is "win a game".

Period 4 : 1990 to 2010

Model 1

In [74]:
period4 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Hits + Doubles + Triples + Home_Runs + Walks + Strike_Outs + Stolen_Bases + Runs_Against +Earned_Runs + Earned_Run_Average + Complete_Games + Shutout + Saves + Infield_Put_Outs + Hits_Allowed + Home_Run_Allowed + Strikeouts_Allowed + Errors', data=baseball_1990_2010).fit()
period4.params
Out[74]:
Intercept            -14.316908
Runs_Scored            0.078149
At_Bats               -0.036174
Hits                   0.031622
Doubles               -0.002154
Triples               -0.034648
Home_Runs              0.013826
Walks                 -0.001524
Strike_Outs            0.001597
Stolen_Bases          -0.001389
Runs_Against          -0.030092
Earned_Runs           -0.053664
Earned_Run_Average     3.522177
Complete_Games         0.074009
Shutout                0.175764
Saves                  0.359500
Infield_Put_Outs       0.050227
Hits_Allowed           0.001531
Home_Run_Allowed      -0.009283
Strikeouts_Allowed     0.003374
Errors                -0.044771
dtype: float64
In [75]:
period4.summary()
Out[75]:
OLS Regression Results
Dep. Variable: Games_Won R-squared: 0.946
Model: OLS Adj. R-squared: 0.944
Method: Least Squares F-statistic: 512.8
Date: Wed, 15 Jul 2020 Prob (F-statistic): 0.00
Time: 18:49:32 Log-Likelihood: -1500.7
No. Observations: 608 AIC: 3043.
Df Residuals: 587 BIC: 3136.
Df Model: 20
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -14.3169 14.275 -1.003 0.316 -42.354 13.720
Runs_Scored 0.0781 0.005 14.746 0.000 0.068 0.089
At_Bats -0.0362 0.004 -8.102 0.000 -0.045 -0.027
Hits 0.0316 0.005 5.756 0.000 0.021 0.042
Doubles -0.0022 0.005 -0.393 0.695 -0.013 0.009
Triples -0.0346 0.015 -2.290 0.022 -0.064 -0.005
Home_Runs 0.0138 0.007 1.989 0.047 0.000 0.027
Walks -0.0015 0.003 -0.564 0.573 -0.007 0.004
Strike_Outs 0.0016 0.002 1.064 0.288 -0.001 0.005
Stolen_Bases -0.0014 0.004 -0.353 0.724 -0.009 0.006
Runs_Against -0.0301 0.013 -2.320 0.021 -0.056 -0.005
Earned_Runs -0.0537 0.024 -2.280 0.023 -0.100 -0.007
Earned_Run_Average 3.5222 3.145 1.120 0.263 -2.654 9.698
Complete_Games 0.0740 0.028 2.655 0.008 0.019 0.129
Shutout 0.1758 0.045 3.890 0.000 0.087 0.264
Saves 0.3595 0.021 16.752 0.000 0.317 0.402
Infield_Put_Outs 0.0502 0.006 8.916 0.000 0.039 0.061
Hits_Allowed 0.0015 0.003 0.492 0.623 -0.005 0.008
Home_Run_Allowed -0.0093 0.007 -1.296 0.195 -0.023 0.005
Strikeouts_Allowed 0.0034 0.001 2.348 0.019 0.001 0.006
Errors -0.0448 0.011 -4.116 0.000 -0.066 -0.023
Omnibus: 2.686 Durbin-Watson: 2.072
Prob(Omnibus): 0.261 Jarque-Bera (JB): 2.682
Skew: 0.162 Prob(JB): 0.262
Kurtosis: 2.968 Cond. No. 9.30e+05


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 9.3e+05. This might indicate that there are
strong multicollinearity or other numerical problems.

We can see the R2 and adj R2 are very good numbers, F statistics is far from zero, we can say that the model is in good shape. There are some coefficients where the p-value is high ( more than 0.05). We eliminate them and check for the next model

Model 2

In [76]:
period42 = smf.ols(formula='Games_Won ~ Runs_Scored + At_Bats + Hits + Triples + Home_Runs + Runs_Against +Earned_Runs + Complete_Games + Shutout + Saves + Infield_Put_Outs + Strikeouts_Allowed + Errors', data=baseball_1990_2010).fit()
period42.params
Out[76]:
Intercept             1.603977
Runs_Scored           0.076472
At_Bats              -0.034784
Hits                  0.029940
Triples              -0.031800
Home_Runs             0.015560
Runs_Against         -0.028372
Earned_Runs          -0.033731
Complete_Games        0.069994
Shutout               0.188033
Saves                 0.358644
Infield_Put_Outs      0.045747
Strikeouts_Allowed    0.003186
Errors               -0.042976
dtype: float64
In [77]:
period42.summary()
Out[77]:
OLS Regression Results
Dep. Variable: Games_Won R-squared: 0.945
Model: OLS Adj. R-squared: 0.944
Method: Least Squares F-statistic: 791.7
Date: Wed, 15 Jul 2020 Prob (F-statistic): 0.00
Time: 18:52:47 Log-Likelihood: -1503.1
No. Observations: 608 AIC: 3034.
Df Residuals: 594 BIC: 3096.
Df Model: 13
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept 1.6040 1.872 0.857 0.392 -2.073 5.281
Runs_Scored 0.0765 0.004 20.071 0.000 0.069 0.084
At_Bats -0.0348 0.004 -8.389 0.000 -0.043 -0.027
Hits 0.0299 0.005 6.166 0.000 0.020 0.039
Triples -0.0318 0.014 -2.213 0.027 -0.060 -0.004
Home_Runs 0.0156 0.006 2.534 0.012 0.003 0.028
Runs_Against -0.0284 0.013 -2.226 0.026 -0.053 -0.003
Earned_Runs -0.0337 0.013 -2.601 0.010 -0.059 -0.008
Complete_Games 0.0700 0.026 2.707 0.007 0.019 0.121
Shutout 0.1880 0.044 4.237 0.000 0.101 0.275
Saves 0.3586 0.021 16.982 0.000 0.317 0.400
Infield_Put_Outs 0.0457 0.004 10.979 0.000 0.038 0.054
Strikeouts_Allowed 0.0032 0.001 2.559 0.011 0.001 0.006
Errors -0.0430 0.011 -4.059 0.000 -0.064 -0.022
Omnibus: 2.924 Durbin-Watson: 2.075
Prob(Omnibus): 0.232 Jarque-Bera (JB): 2.880
Skew: 0.169 Prob(JB): 0.237
Kurtosis: 2.995 Cond. No. 1.16e+05


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.16e+05. This might indicate that there are
strong multicollinearity or other numerical problems.

Now we have a perfect model with good numbers for R and R square and the coefficients shows a p-value = 0 all of them. The contribution of them to the model has a high impact on the result, in this case is "win a game"

6) PREDICTIONS

Use the 4th regression model from 1990 to 2010 and forecast the number of games won for the New York Yankees and the Toronto Blue Jays using values for the independent variables for 2012 and 2015.

i.) Discuss the error in your prediction by comparing the actual wins to predicted wins for the 2 teams in the 2 years.

ii.) Quantify your comments about the model error by calculating the Root Mean Square Error for your set of predictions.

a) NEW YORK YANKEES (2012 - 2015)

Prepare the Data for the Predictions

In [78]:
baseball_2012_2015=baseball.loc['2012':'2015']
baseball_2012_2015
Out[78]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 Unnamed: 11 League_Win World_Series Runs_Scored At_Bats Hits Doubles Triples Home_Runs Walks Strike_Outs Stolen_Bases Caught_Stealing Hit_By_Pitch Sacrifice_Fly Runs_Against Earned_Runs Earned_Run_Average Complete_Games Shutout Saves Infield_Put_Outs Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
2012 NL ARI ARI W 3 162 81.0 81 81 False False False False 734 5462 1416 307 33 165 539 1266.0 93.0 51.0 41.0 45.0 688 626 3.93 4 9 39 4301 1432 155 417 1200 90 146.0 0.985 Arizona Diamondbacks Chase Field 2177617.0
2012 NL ATL ATL E 2 162 81.0 94 68 False True False False 700 5425 1341 263 30 149 567 1289.0 101.0 32.0 34.0 46.0 600 549 3.42 5 16 47 4336 1310 145 464 1232 86 147.0 0.986 Atlanta Braves Turner Field 2420171.0
2012 AL BAL BAL E 2 162 81.0 93 69 False True False False 712 5560 1375 270 16 214 480 1315.0 58.0 29.0 50.0 30.0 705 642 3.90 1 10 55 4449 1433 184 481 1177 106 151.0 0.983 Baltimore Orioles Oriole Park at Camden Yards 2102240.0
2012 AL BOS BOS E 5 162 81.0 69 93 False False False False 734 5604 1459 339 16 165 428 1197.0 97.0 31.0 45.0 55.0 806 754 4.70 6 4 35 4329 1449 190 529 1176 101 159.0 0.983 Boston Red Sox Fenway Park II 3043003.0
2012 AL CHA CHW C 2 162 81.0 85 77 False False False False 748 5518 1409 228 29 211 461 1203.0 109.0 43.0 65.0 36.0 676 646 4.02 6 11 37 4337 1365 186 503 1246 70 154.0 0.988 Chicago White Sox U.S. Cellular Field 1965955.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2015 NL LAN LAD W 1 162 81.0 92 70 True False False False 667 5385 1346 263 26 187 563 1258.0 59.0 34.0 60.0 30.0 595 553 3.44 6 21 47 4337 1317 145 395 1396 75 133.0 0.988 Los Angeles Dodgers Dodger Stadium 3764815.0
2015 NL SFN SFG W 2 162 81.0 84 78 False False False False 696 5565 1486 288 39 136 457 1159.0 93.0 36.0 49.0 37.0 627 597 3.72 7 18 41 4333 1344 155 431 1165 78 145.0 0.987 San Francisco Giants AT&T Park 3375882.0
2015 NL ARI ARI W 3 162 81.0 79 83 False False False False 720 5649 1494 289 48 154 490 1312.0 132.0 44.0 33.0 57.0 713 659 4.04 1 12 44 4400 1450 182 500 1215 86 146.0 0.986 Arizona Diamondbacks Chase Field 2080145.0
2015 NL SDN SDP W 4 162 81.0 74 88 False False False False 650 5457 1324 260 36 148 426 1327.0 82.0 29.0 40.0 42.0 731 655 4.09 1 6 41 4321 1371 171 516 1393 92 138.0 0.985 San Diego Padres Petco Park 2459742.0
2015 NL COL COL W 5 162 81.0 68 94 False False False False 737 5572 1479 274 49 186 388 1283.0 97.0 43.0 33.0 34.0 844 799 5.04 4 4 36 4279 1579 183 579 1112 95 171.0 0.985 Colorado Rockies Coors Field 2506789.0

120 rows × 42 columns

In [79]:
baseball_2012_2015['Team_Name'].unique()
Out[79]:
array(['Arizona Diamondbacks', 'Atlanta Braves', 'Baltimore Orioles',
       'Boston Red Sox', 'Chicago White Sox', 'Chicago Cubs',
       'Cincinnati Reds', 'Cleveland Indians', 'Colorado Rockies',
       'Detroit Tigers', 'Houston Astros', 'Kansas City Royals',
       'Los Angeles Angels of Anaheim', 'Los Angeles Dodgers',
       'Miami Marlins', 'Milwaukee Brewers', 'Minnesota Twins',
       'New York Yankees', 'New York Mets', 'Oakland Athletics',
       'Philadelphia Phillies', 'Pittsburgh Pirates', 'San Diego Padres',
       'Seattle Mariners', 'San Francisco Giants', 'St. Louis Cardinals',
       'Tampa Bay Rays', 'Texas Rangers', 'Toronto Blue Jays',
       'Washington Nationals'], dtype=object)
In [88]:
m1 = baseball_2012_2015['Team_Name'] == 'New York Yankees'
New_York_Yankees = baseball_2012_2015[m1]
New_York_Yankees
Out[88]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 Unnamed: 11 League_Win World_Series Runs_Scored At_Bats Hits Doubles Triples Home_Runs Walks Strike_Outs Stolen_Bases Caught_Stealing Hit_By_Pitch Sacrifice_Fly Runs_Against Earned_Runs Earned_Run_Average Complete_Games Shutout Saves Infield_Put_Outs Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
2012 AL NYA NYY E 1 162 81.0 95 67 True False False False 804 5524 1462 280 13 245 565 1176.0 93.0 27.0 62.0 49.0 668 617 3.84 6 9 51 4336 1401 190 431 1318 75 135.0 0.987 New York Yankees Yankee Stadium III 3542406.0
2013 AL NYA NYY E 4 162 81.0 85 77 False False False False 650 5449 1321 247 24 144 466 1214.0 115.0 31.0 57.0 36.0 671 633 3.94 7 10 49 4342 1452 171 437 1233 69 139.0 0.988 New York Yankees Yankee Stadium III 3279589.0
2014 AL NYA NYY E 2 162 81.0 84 78 False False False False 633 5497 1349 247 26 147 452 1133.0 112.0 26.0 56.0 47.0 664 605 3.75 5 10 48 4359 1392 164 398 1370 92 107.0 0.984 New York Yankees Yankee Stadium III 3401624.0
2015 AL NYA NYY E 2 162 81.0 87 75 False True False False 764 5567 1397 272 19 212 554 1227.0 63.0 25.0 63.0 54.0 698 652 4.03 3 4 48 4373 1416 182 474 1370 93 135.0 0.985 New York Yankees Yankee Stadium III 3193795.0
In [91]:
New_York_Yankess1=New_York_Yankees.drop(['League','Team ','Franchise ','Division','Final_Standing','Games_Played','Unnamed: 7','Games_Lost','Unnamed: 10','Unnamed: 11','League_Win','World_Series','Doubles','Walks','Strike_Outs','Stolen_Bases','Caught_Stealing','Hit_By_Pitch','Sacrifice_Fly','Earned_Run_Average','Hits_Allowed','Home_Run_Allowed','Walks_Allowed','Double_Plays','Fielding_Percentage','Team_Name','Home_Ball_Park','Attendance'],axis=1)
New_York_Yankess1
Out[91]:
Games_Won Runs_Scored At_Bats Hits Triples Home_Runs Runs_Against Earned_Runs Complete_Games Shutout Saves Infield_Put_Outs Strikeouts_Allowed Errors
Year
2012 95 804 5524 1462 13 245 668 617 6 9 51 4336 1318 75
2013 85 650 5449 1321 24 144 671 633 7 10 49 4342 1233 69
2014 84 633 5497 1349 26 147 664 605 5 10 48 4359 1370 92
2015 87 764 5567 1397 19 212 698 652 3 4 48 4373 1370 93
In [92]:
list(New_York_Yankess1)
Out[92]:
['Games_Won',
 'Runs_Scored',
 'At_Bats',
 'Hits',
 'Triples',
 'Home_Runs',
 'Runs_Against',
 'Earned_Runs',
 'Complete_Games',
 'Shutout',
 'Saves',
 'Infield_Put_Outs',
 'Strikeouts_Allowed',
 'Errors']
In [93]:
columns_reordered=['Runs_Scored','At_Bats',
 'Hits',
 'Triples',
 'Home_Runs',
 'Runs_Against',
 'Earned_Runs',
 'Complete_Games',
 'Shutout',
 'Saves',
 'Infield_Put_Outs',
 'Strikeouts_Allowed',
 'Errors','Games_Won']
New_York_Yankess1=New_York_Yankess1[columns_reordered]
In [94]:
New_York_Yankess1
Out[94]:
Runs_Scored At_Bats Hits Triples Home_Runs Runs_Against Earned_Runs Complete_Games Shutout Saves Infield_Put_Outs Strikeouts_Allowed Errors Games_Won
Year
2012 804 5524 1462 13 245 668 617 6 9 51 4336 1318 75 95
2013 650 5449 1321 24 144 671 633 7 10 49 4342 1233 69 85
2014 633 5497 1349 26 147 664 605 5 10 48 4359 1370 92 84
2015 764 5567 1397 19 212 698 652 3 4 48 4373 1370 93 87

Create a prediction for Games Won in 2012, 2013, 2014 and 2015 for New Yor Yankees using the values of those years for the independent variables.

In [96]:
X_predict = pd.DataFrame({'Runs_Scored': [804], 'At_Bats':[5524], 'Hits':[1462], 'Triples':[13], 'Home_Runs':[245], 'Runs_Against':[668], 'Earned_Runs':[617], 'Complete_Games':[6], 'Shutout':[9], 'Saves':[51], 'Infield_Put_Outs':[4336], 'Strikeouts_Allowed':[1318], 'Errors':[75]})
period42.predict(X_predict)
Out[96]:
0    98.082177
dtype: float64
In [97]:
def prediction(vec):
    var1 = vec[0]
    var2 = vec[1]
    var3 = vec[2]
    var4 = vec[3]
    var5 = vec[4]
    var6 = vec[5]
    var7 = vec[6]
    var8 = vec[7]
    var9 = vec[8]
    var10 = vec[9]
    var11 = vec[10]
    var12 = vec[11]
    var13 = vec[12]
    X = pd.DataFrame({'Runs_Scored': [var1], 'At_Bats':[var2], 'Hits':[var3], 'Triples':[var4], 'Home_Runs':[var5], 'Runs_Against':[var6], 'Earned_Runs':[var7], 'Complete_Games':[var8], 'Shutout':[var9], 'Saves':[var10], 'Infield_Put_Outs':[var11], 'Strikeouts_Allowed':[var12], 'Errors':[var13]})
    y = period42.predict(X)
    return y
Game_won_prediction =New_York_Yankess1[['Runs_Scored','At_Bats','Hits','Triples','Home_Runs','Runs_Against','Earned_Runs','Complete_Games','Shutout','Saves','Infield_Put_Outs','Strikeouts_Allowed','Errors']].apply(prediction, axis = 1)
In [100]:
Game_won_prediction=round(Game_won_prediction )
Game_won_prediction
Out[100]:
0
Year
2012 98.0
2013 82.0
2014 81.0
2015 88.0
In [101]:
New_York_Yankess1['Game_won_prediction']=Game_won_prediction
New_York_Yankess1
<ipython-input-101-747e916c819c>:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  New_York_Yankess1['Game_won_prediction']=Game_won_prediction
Out[101]:
Runs_Scored At_Bats Hits Triples Home_Runs Runs_Against Earned_Runs Complete_Games Shutout Saves Infield_Put_Outs Strikeouts_Allowed Errors Games_Won Game_won_prediction
Year
2012 804 5524 1462 13 245 668 617 6 9 51 4336 1318 75 95 98.0
2013 650 5449 1321 24 144 671 633 7 10 49 4342 1233 69 85 82.0
2014 633 5497 1349 26 147 664 605 5 10 48 4359 1370 92 84 81.0
2015 764 5567 1397 19 212 698 652 3 4 48 4373 1370 93 87 88.0
In [102]:
New_York_Yankess1[['Games_Won','Game_won_prediction']]
Out[102]:
Games_Won Game_won_prediction
Year
2012 95 98.0
2013 85 82.0
2014 84 81.0
2015 87 88.0

Plotting the Least Squares Line

In [104]:
sns.pairplot(New_York_Yankess1, x_vars=['Runs_Scored','At_Bats','Hits'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
Out[104]:
<seaborn.axisgrid.PairGrid at 0x12677790>
In [107]:
sns.pairplot(New_York_Yankess1, x_vars=['Triples','Home_Runs','Runs_Against'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
Out[107]:
<seaborn.axisgrid.PairGrid at 0x1566e0a0>
In [108]:
sns.pairplot(New_York_Yankess1, x_vars=['Earned_Runs','Complete_Games','Shutout'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
Out[108]:
<seaborn.axisgrid.PairGrid at 0x1607b940>
In [109]:
sns.pairplot(New_York_Yankess1, x_vars=['Saves','Infield_Put_Outs','Strikeouts_Allowed','Errors'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
Out[109]:
<seaborn.axisgrid.PairGrid at 0x165bb9d0>

Model Evaluation Metrics for Regression

In [110]:
a=np.array(Game_won_prediction)
a
Out[110]:
array([[98.],
       [82.],
       [81.],
       [88.]])
In [112]:
b=np.array(New_York_Yankess1['Games_Won'])
b
Out[112]:
array([95, 85, 84, 87], dtype=int64)
In [113]:
print ('Root Mean Square Error (RMSE): ',np.sqrt(metrics.mean_squared_error(b,a)))
Root Mean Square Error (RMSE):  2.6457513110645907

We can appreciate that the value for RMSE method is low , the differents between the real data and the prediction is small. In conclusion we can say that the model works very good with the selected variables

b) BLUE JAYS (2012 - 2015)

Prepare the Data for the Predictions

In [114]:
m2 = baseball_2012_2015['Team_Name'] == 'Toronto Blue Jays'
Toronto_Blue_Jays = baseball_2012_2015[m2]
Toronto_Blue_Jays
Out[114]:
League Team Franchise Division Final_Standing Games_Played Unnamed: 7 Games_Won Games_Lost Unnamed: 10 Unnamed: 11 League_Win World_Series Runs_Scored At_Bats Hits Doubles Triples Home_Runs Walks Strike_Outs Stolen_Bases Caught_Stealing Hit_By_Pitch Sacrifice_Fly Runs_Against Earned_Runs Earned_Run_Average Complete_Games Shutout Saves Infield_Put_Outs Hits_Allowed Home_Run_Allowed Walks_Allowed Strikeouts_Allowed Errors Double_Plays Fielding_Percentage Team_Name Home_Ball_Park Attendance
Year
2012 AL TOR TOR E 4 162 81.0 73 89 False False False False 716 5487 1346 247 22 198 473 1251.0 123.0 41.0 55.0 45.0 784 745 4.64 5 11 29 4331 1439 204 574 1142 101 167.0 0.984 Toronto Blue Jays Rogers Centre 2099663.0
2013 AL TOR TOR E 5 162 81.0 74 88 False False False False 712 5537 1398 273 24 185 510 1123.0 112.0 41.0 38.0 38.0 756 685 4.25 4 11 39 4356 1451 195 500 1208 111 145.0 0.982 Toronto Blue Jays Rogers Centre 2536562.0
2014 AL TOR TOR E 3 162 81.0 83 79 False False False False 723 5549 1435 282 24 177 502 1151.0 78.0 21.0 41.0 40.0 686 642 4.00 3 16 45 4329 1400 151 490 1199 87 130.0 0.985 Toronto Blue Jays Rogers Centre 2375525.0
2015 AL TOR TOR E 1 162 81.0 93 69 True False False False 891 5509 1480 308 17 232 570 1151.0 88.0 23.0 54.0 62.0 670 609 3.80 7 10 34 4323 1353 173 397 1117 88 145.0 0.985 Toronto Blue Jays Rogers Centre 2794891.0
In [117]:
Toronto_Blue_Jays1=Toronto_Blue_Jays.drop(['League','Team ','Franchise ','Division','Final_Standing','Games_Played','Unnamed: 7','Games_Lost','Unnamed: 10','Unnamed: 11','League_Win','World_Series','Doubles','Walks','Strike_Outs','Stolen_Bases','Caught_Stealing','Hit_By_Pitch','Sacrifice_Fly','Earned_Run_Average','Hits_Allowed','Home_Run_Allowed','Walks_Allowed','Double_Plays','Fielding_Percentage','Team_Name','Home_Ball_Park','Attendance'],axis=1)
Toronto_Blue_Jays1
Out[117]:
Games_Won Runs_Scored At_Bats Hits Triples Home_Runs Runs_Against Earned_Runs Complete_Games Shutout Saves Infield_Put_Outs Strikeouts_Allowed Errors
Year
2012 73 716 5487 1346 22 198 784 745 5 11 29 4331 1142 101
2013 74 712 5537 1398 24 185 756 685 4 11 39 4356 1208 111
2014 83 723 5549 1435 24 177 686 642 3 16 45 4329 1199 87
2015 93 891 5509 1480 17 232 670 609 7 10 34 4323 1117 88

Create a prediction for Games Won in 2012, 2013, 2014 and 2015 for Toronto Blue Jays using the values of those years for the independent variables

In [116]:
def prediction(vec):
    var1 = vec[0]
    var2 = vec[1]
    var3 = vec[2]
    var4 = vec[3]
    var5 = vec[4]
    var6 = vec[5]
    var7 = vec[6]
    var8 = vec[7]
    var9 = vec[8]
    var10 = vec[9]
    var11 = vec[10]
    var12 = vec[11]
    var13 = vec[12]
    X = pd.DataFrame({'Runs_Scored': [var1], 'At_Bats':[var2], 'Hits':[var3], 'Triples':[var4], 'Home_Runs':[var5], 'Runs_Against':[var6], 'Earned_Runs':[var7], 'Complete_Games':[var8], 'Shutout':[var9], 'Saves':[var10], 'Infield_Put_Outs':[var11], 'Strikeouts_Allowed':[var12], 'Errors':[var13]})
    y = period42.predict(X)
    return y
Game_won_prediction =Toronto_Blue_Jays1[['Runs_Scored','At_Bats','Hits','Triples','Home_Runs','Runs_Against','Earned_Runs','Complete_Games','Shutout','Saves','Infield_Put_Outs','Strikeouts_Allowed','Errors']].apply(prediction, axis = 1)
In [118]:
Game_won_prediction=round(Game_won_prediction )
Game_won_prediction
Out[118]:
0
Year
2012 71.0
2013 78.0
2014 85.0
2015 98.0
In [119]:
Toronto_Blue_Jays1['Game_won_prediction']=Game_won_prediction
Toronto_Blue_Jays1
Out[119]:
Games_Won Runs_Scored At_Bats Hits Triples Home_Runs Runs_Against Earned_Runs Complete_Games Shutout Saves Infield_Put_Outs Strikeouts_Allowed Errors Game_won_prediction
Year
2012 73 716 5487 1346 22 198 784 745 5 11 29 4331 1142 101 71.0
2013 74 712 5537 1398 24 185 756 685 4 11 39 4356 1208 111 78.0
2014 83 723 5549 1435 24 177 686 642 3 16 45 4329 1199 87 85.0
2015 93 891 5509 1480 17 232 670 609 7 10 34 4323 1117 88 98.0
In [121]:
Toronto_Blue_Jays1[['Games_Won','Game_won_prediction']]
Out[121]:
Games_Won Game_won_prediction
Year
2012 73 71.0
2013 74 78.0
2014 83 85.0
2015 93 98.0

Plotting the Least Squares Line

In [123]:
sns.pairplot(Toronto_Blue_Jays1, x_vars=['Runs_Scored','At_Bats','Hits'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
Out[123]:
<seaborn.axisgrid.PairGrid at 0x17b9a5b0>
In [124]:
sns.pairplot(Toronto_Blue_Jays1, x_vars=['Triples','Home_Runs','Runs_Against'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
Out[124]:
<seaborn.axisgrid.PairGrid at 0x18161bb0>
In [125]:
sns.pairplot(Toronto_Blue_Jays1, x_vars=['Earned_Runs','Complete_Games','Shutout'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
Out[125]:
<seaborn.axisgrid.PairGrid at 0x186c2a00>
In [126]:
sns.pairplot(Toronto_Blue_Jays1, x_vars=['Saves','Infield_Put_Outs','Strikeouts_Allowed','Errors'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
Out[126]:
<seaborn.axisgrid.PairGrid at 0x18bc3eb0>

Model Evaluation Metrics for Regression

In [127]:
a=np.array(Game_won_prediction)
a
Out[127]:
array([[71.],
       [78.],
       [85.],
       [98.]])
In [128]:
b=np.array(Toronto_Blue_Jays1['Games_Won'])
b
Out[128]:
array([73, 74, 83, 93], dtype=int64)
In [129]:
print ('Root Mean Square Error (RMSE): ',np.sqrt(metrics.mean_squared_error(b,a)))
Root Mean Square Error (RMSE):  3.5

We can appreciate that the value for RMSE method is quite low , the differents between the real data and predictions has a small difference, we should take into account that we have only four periods.

7) CONCLUSIONS

The Goal of this project is determine what kind of strategy followed the most successful teams :

- High offenses
- Tight defenses
- Balance of two

Firts of all, we can see on the following graph the most successfuls Teams that won the World Series Tournament.

In [5]:
World_series=baseball[baseball['World_Series'] == True]['Team_Name']
World_series
Out[5]:
Year 
1884        Providence Grays
1886        St. Louis Browns
1887      Detroit Wolverines
1888         New York Giants
1889         New York Giants
                ...         
2011     St. Louis Cardinals
2012    San Francisco Giants
2013          Boston Red Sox
2014    San Francisco Giants
2015      Kansas City Royals
Name: Team_Name, Length: 116, dtype: object
In [9]:
bb = World_series.value_counts()
bb
Out[9]:
New York Yankees          27
St. Louis Cardinals       11
New York Giants            7
Boston Red Sox             7
Pittsburgh Pirates         5
Cincinnati Reds            5
Philadelphia Athletics     5
Los Angeles Dodgers        5
Detroit Tigers             4
Oakland Athletics          4
San Francisco Giants       3
Chicago White Sox          3
Baltimore Orioles          3
Cleveland Indians          2
Toronto Blue Jays          2
Chicago Cubs               2
Florida Marlins            2
Minnesota Twins            2
New York Mets              2
Kansas City Royals         2
Philadelphia Phillies      2
Brooklyn Dodgers           1
Providence Grays           1
Detroit Wolverines         1
Atlanta Braves             1
Milwaukee Braves           1
Washington Senators        1
Anaheim Angels             1
Arizona Diamondbacks       1
Boston Braves              1
St. Louis Browns           1
Boston Americans           1
Name: Team_Name, dtype: int64
In [30]:
bb.plot(kind = 'bar', figsize=(10,5), color = 'pink')
plt.title('WORLD SERIES WINNERS')
plt.ylabel('Titles won')
plt.xlabel('Teams')
Out[30]:
Text(0.5, 0, 'Teams')

New York Yankees is for far the most successful Team in baseball history. Through our Regression Model we can figure out what strategy the Team used to apply to be the Best of the Best.

According to our Model developed in Period four, we choose thirteen variables where the p-value is less than 0.05, the probability that this coefficients drop on zero is very low so, their contribution to the model is important. Between these variables we can recognize some of them are offensive tecniques and others are defensive tecniques.

The first conclusion we can deduce from here is that The Team (New York Yankees) use a combination of both strategies to be sucessful, High offenses and tight defenses.

We can analyze more deeply this strategy figuring out which proportion New York Yankees use these techniques.

Offenses tecniques

-Runs Scored
-At bats
-Hits
-Triples
-Home runs

TOTAL = 5

Defenses techniques

-Runs against
-Earned runs
-Complete games
-Shut outs
-Saves
-Infield put outs
-Strike outs allowed
-Errors

TOTAL = 8

In [22]:
strate = {'Strategy': ['Offensives','Defensives'], 'Counts':[5,8]}
data = pd.DataFrame(strate)
data
Out[22]:
Strategy Counts
0 Offensives 5
1 Defensives 8
In [26]:
data = data.set_index('Strategy')
data
Out[26]:
Counts
Strategy
Offensives 5
Defensives 8
In [29]:
data.plot(y="Counts", kind='pie',autopct='%1.1f%%',figsize=(16,6))
plt.title('STRATEGIES')
plt.legend(loc= 'upper left')
Out[29]:
<matplotlib.legend.Legend at 0x1135c5e0>

We can see on the graph that New York Yankees use 62% defensive strategies and 38% Offensive strategies. We can conclude that a successful team like New Yor Yankees use a balance of defensive and offensive strategy with more proportion in defensives techniques.

In [ ]: