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) 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
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
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
pd.set_option('display.max_columns', None)
baseball.head()
list(baseball)
baseball=baseball.set_index('Year ')
baseball
Slice data between 1960 to 2010
baseball_1960_2010=baseball.loc['1960':'2010']
baseball_1960_2010
a) Descriptive Statistics
baseball_1960_2010[['Games_Won','Games_Lost','Runs_Scored','Home_Runs','Strike_Outs','Shutout','Home_Run_Allowed','Errors']].describe()
b) Show a histogram for each of the 8 variables
Games Won
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')
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})
Games Lost
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')
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})
Runs Scored
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')
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})
Home Runs
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')
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})
Strike Outs
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')
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})
Shut Out
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')
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})
Home Run Allowed
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')
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})
Errors
baseball_1960_2010['Errors'].hist(figsize=(10,5),bins = 10, color='cornflowerblue')
plt.title('ERRORS - BASEBALL DATA')
plt.ylabel('Counts')
plt.xlabel('Errors')
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})
c) Show a boxplot for each of the 8 variables
Games Won
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 ')
Games Lost
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 ')
Run Scored
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 ')
Home Runs
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 ')
Strike Outs
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 ')
Shut Outs
boxplot=baseball_1960_2010.boxplot(column=['Shutout'],figsize=(16,6))
plt.title('SHUT OUT BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('Number of Shut Outs ')
Home Run Allowed
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 ')
Errors
boxplot=baseball_1960_2010.boxplot(column=['Errors'],figsize=(16,6))
plt.title('ERRORS BOXPLOT GRAPH -BASEBALL DATA')
plt.ylabel('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
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
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
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
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
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
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
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
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
Do a correlation analysis for the following pairs of fields for the complete data spanning 1960 to 2010
a) Games Won vs Runs Scored
baseball_1960_2010[['Games_Won','Runs_Scored']].corr(method = 'pearson')
baseball_1960_2010[['Games_Won','Runs_Scored']].corr(method = 'spearman')
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)
b) Games Won vs Runs Against
baseball_1960_2010[['Games_Won','Runs_Against']].corr(method = 'pearson')
baseball_1960_2010[['Games_Won','Runs_Against']].corr(method = 'spearman')
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)
c) Games Won vs (Runs Scored minus Runs Against)
baseball_1960_2010['Score_against'] = baseball_1960_2010['Runs_Scored'] - baseball_1960_2010['Runs_Against']
baseball_1960_2010.head(10)
baseball_1960_2010[['Games_Won','Score_against']].corr(method = 'pearson')
baseball_1960_2010[['Games_Won','Score_against']].corr(method = 'spearman')
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)
d) Games Lost vs Runs Scored
baseball_1960_2010[['Games_Lost','Runs_Scored']].corr(method = 'spearman')
baseball_1960_2010[['Games_Lost','Runs_Scored']].corr(method = 'pearson')
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)
e) Games Lost vs Runs Against
baseball_1960_2010[['Games_Lost','Runs_Against']].corr(method = 'pearson')
baseball_1960_2010[['Games_Lost','Runs_Against']].corr(method = 'spearman')
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)
f) Games Lost vs (Runs Scored minus Runs Against
baseball_1960_2010[['Games_Lost','Score_against']].corr(method = 'pearson')
baseball_1960_2010[['Games_Lost','Score_against']].corr(method = 'spearman')
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)
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.
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
baseball_before_1920=baseball.loc['1871':'1919']
baseball_before_1920
pd.set_option('display.max_columns', None)
baseball_before_1920.head()
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
b) Period 2: 1920 to 1959
baseball_1920_1959=baseball.loc['1920':'1959']
baseball_1920_1959.head()
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
c) Period 3: 1960 to 1989
baseball_1960_1989=baseball.loc['1960':'1989']
baseball_1960_1989.head()
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
d) Period 4 : 1990 to 2010
baseball_1990_2010=baseball.loc['1990':'2010']
baseball_1990_2010.head()
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
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.
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
period1.summary()
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
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
period12.summary()
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
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
period13.summary()
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
period2.summary()
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
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
period22.summary()
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
period3.summary()
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
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
period32.summary()
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
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
period4.summary()
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
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
period42.summary()
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"
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
baseball_2012_2015=baseball.loc['2012':'2015']
baseball_2012_2015
baseball_2012_2015['Team_Name'].unique()
m1 = baseball_2012_2015['Team_Name'] == 'New York Yankees'
New_York_Yankees = baseball_2012_2015[m1]
New_York_Yankees
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
list(New_York_Yankess1)
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]
New_York_Yankess1
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.
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)
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)
Game_won_prediction=round(Game_won_prediction )
Game_won_prediction
New_York_Yankess1['Game_won_prediction']=Game_won_prediction
New_York_Yankess1
New_York_Yankess1[['Games_Won','Game_won_prediction']]
Plotting the Least Squares Line
sns.pairplot(New_York_Yankess1, x_vars=['Runs_Scored','At_Bats','Hits'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
sns.pairplot(New_York_Yankess1, x_vars=['Triples','Home_Runs','Runs_Against'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
sns.pairplot(New_York_Yankess1, x_vars=['Earned_Runs','Complete_Games','Shutout'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
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')
Model Evaluation Metrics for Regression
a=np.array(Game_won_prediction)
a
b=np.array(New_York_Yankess1['Games_Won'])
b
print ('Root Mean Square Error (RMSE): ',np.sqrt(metrics.mean_squared_error(b,a)))
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
m2 = baseball_2012_2015['Team_Name'] == 'Toronto Blue Jays'
Toronto_Blue_Jays = baseball_2012_2015[m2]
Toronto_Blue_Jays
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
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
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)
Game_won_prediction=round(Game_won_prediction )
Game_won_prediction
Toronto_Blue_Jays1['Game_won_prediction']=Game_won_prediction
Toronto_Blue_Jays1
Toronto_Blue_Jays1[['Games_Won','Game_won_prediction']]
Plotting the Least Squares Line
sns.pairplot(Toronto_Blue_Jays1, x_vars=['Runs_Scored','At_Bats','Hits'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
sns.pairplot(Toronto_Blue_Jays1, x_vars=['Triples','Home_Runs','Runs_Against'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
sns.pairplot(Toronto_Blue_Jays1, x_vars=['Earned_Runs','Complete_Games','Shutout'], y_vars='Games_Won', height=7, aspect=0.7, kind='reg')
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')
Model Evaluation Metrics for Regression
a=np.array(Game_won_prediction)
a
b=np.array(Toronto_Blue_Jays1['Games_Won'])
b
print ('Root Mean Square Error (RMSE): ',np.sqrt(metrics.mean_squared_error(b,a)))
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.
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.
World_series=baseball[baseball['World_Series'] == True]['Team_Name']
World_series
bb = World_series.value_counts()
bb
bb.plot(kind = 'bar', figsize=(10,5), color = 'pink')
plt.title('WORLD SERIES WINNERS')
plt.ylabel('Titles won')
plt.xlabel('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
strate = {'Strategy': ['Offensives','Defensives'], 'Counts':[5,8]}
data = pd.DataFrame(strate)
data
data = data.set_index('Strategy')
data
data.plot(y="Counts", kind='pie',autopct='%1.1f%%',figsize=(16,6))
plt.title('STRATEGIES')
plt.legend(loc= 'upper left')
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.