<jupyter_start><jupyter_text>Model 3 - Linear Regression of Country-wise factors The goal of this third bundle of models is to examine the correlation between total number of medals won by a country and some its characteristics. In this analysis we will consider each country's gross domestic product (GDP), human development index (HDI) and total population for each year the country competed in the Olympic Games.To investigate this problem we will employ some linear regression models. Towards the end we will also implement a logistic regression model and compare its results with those of the corresponding linear regression.As usual, we start off by importing everything that will be needed going forward.<jupyter_code>import pandas as pd
import os
import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from scipy.special import expit<jupyter_output><empty_output><jupyter_text>Cleaning the dataset Before implementing the models, there is a lot of cleaning up we need to do. We start by importing our main dataset and visualising it.<jupyter_code>initial_df = pd.read_csv('data/athlete_events.csv', index_col='ID') 
initial_df.head()<jupyter_output><empty_output><jupyter_text>The team names in this dataset do not always correspond to the country name, as we can clearly see in row 4. For this reason we use a second dataframe, which has the NOC values and the corresponding country names. So we import this dataframe and merge the two together.<jupyter_code>regions_df = pd.read_csv('data/noc_regions.csv')
regions_df.columns = ['NOC','Country','notes']
# Replace NOC SGP with SIN (Singapore appears with SGP and SIN, but we consider them both to be the same region)
initial_df["NOC"].replace({"SGP": "SIN"}, inplace=True)
initial_df = initial_df.merge(regions_df[['NOC', 'Country']], left_on = 'NOC',
                       right_on = 'NOC', how = 'left')<jupyter_output><empty_output><jupyter_text>This dataset is focused on the athletes, whereas our analysis is focused on the countries.  Naturally, we will not be needing all of these columns for our analysis, since we will be focusing on the countries as a whole rather than the single athletes. Also some over specific information about the specific olympic games, such as the city in which they took place, are irrelevant for our analysis. For these reasons, we drop some columns from our dataframe.<jupyter_code>columns = ["Name","Sex","Age","Height","Weight","Games","City","Sport","Season","Team"]
initial_df.drop(columns, inplace=True, axis=1)<jupyter_output><empty_output><jupyter_text>For future merging (of the dictionary) we will need to add another column, which displays the combination of Country and Year.<jupyter_code>Country_Year = [(str(initial_df['Country'][i])+' '+str(initial_df['Year'][i])) for i,row in initial_df.iterrows()]
initial_df['Country_Year'] = Country_Year<jupyter_output><empty_output><jupyter_text>Since a lot of data are missing before 1960, we delete every row with Year<1960.<jupyter_code>initial_df = initial_df[initial_df['Year']>1959].reset_index()<jupyter_output><empty_output><jupyter_text>Our first goal for this part is to check whether the number of total medals won by a country is affected (?) by its GDP, population size and HDI. For this reason we only include in the dataframe countries that have won a medal in a given olympic game.<jupyter_code>medal = ["Gold","Silver","Bronze"]
initial_df = initial_df[initial_df['Medal'].isin(medal)]
initial_df = initial_df.dropna(subset=["NOC","Medal"])
initial_df.head(3)<jupyter_output><empty_output><jupyter_text>Next, we check the total number of medals won by each country per year. To do this we first create a dictionary with the needed information and then convert it into a dataframe. We also compile a second dictionary to keep track of those events which award more than one medal per winner, for example synchronised diving. This way, in the final dataframe we will only account for one medal per event per winning country, rather than multiple ones.<jupyter_code>medals_per_year = {}
avoid_group_sports = {}
n=0
for i,row in initial_df.iterrows():
    y = int(initial_df['Year'][i])
    s = str(initial_df['Country_Year'][i])
    e = str(initial_df['Event'][i]+' '+s)
    if e not in avoid_group_sports:
        avoid_group_sports[e] = 1
        if s not in medals_per_year:
            medals_per_year[s] = 1
        else:
            medals_per_year[s] +=1
    else:
        avoid_group_sports[e] += 1
print(len(medals_per_year),len(avoid_group_sports),len(initial_df))<jupyter_output>1062 12338 28400<jupyter_text>As can be seen from the print above, the lengths of the three datasets are very different:- 28,400 are the athletes that have compited in the olympics and won a medal since 1960- 12,338 are the events in which medals have been won throughout the years- 1,062 are the countries that have compited in the olympics, each country is counted multiple times depending on the year Finally, we initialise a new dataset, which keeps count of the amount of medals won by each country in a year. "mpy" stands for "medals per year".<jupyter_code>mpy = pd.DataFrame(medals_per_year.items())
mpy.columns = ['Country_Year','Medals_won']<jupyter_output><empty_output><jupyter_text>Lastly, to make it easier to match up the other datasets, we add two other columns to our dataset.<jupyter_code>countries = []
years = []
for i,row in mpy.iterrows():
    try:
        c,y = mpy['Country_Year'][i].split()
    except:
        try:
            c1,c2,y = mpy['Country_Year'][i].split()
            c=str(c1+' '+c2)
        except:
            c1,c2,c3,y = mpy['Country_Year'][i].split()
            c=str(c1+' '+c2+' '+c3)
    countries.append(c)
    years.append(y)
mpy['Country'] = countries
mpy['Year'] = years
mpy['Year'] = pd.to_numeric(mpy['Year'])
mpy.head(3)<jupyter_output><empty_output><jupyter_text>Next, we need to include some external dataframes into our main dataset. These correspond to the information we want to confront with the number of medals won - gdp, popsize and hdi.Firstly, we import the new datasets.<jupyter_code>GDP_df = pd.read_excel('data/w_gdp.xls')
pop_df = pd.read_excel('data/WorldPopulation.xlsx')
HDI_df = pd.read_excel('data/Human Development Index (HDI).xlsx')<jupyter_output><empty_output><jupyter_text>Adding GDP<jupyter_code>GDP_df.head()<jupyter_output><empty_output><jupyter_text>Remove unnecessary columns and melt down to dataframes.<jupyter_code>GDP_df = GDP_df.drop(['Indicator Name', 'Indicator Code','Country Code'], axis=1)
GDP_df = pd.melt(GDP_df, id_vars = ['Country Name'], var_name = 'Year', value_name = 'GDP')
GDP_df.columns = ['Country','Year','GDP']
GDP_df['Year'] = pd.to_numeric(GDP_df['Year'])
list(set(mpy['Country'].unique()) - set(GDP_df['Country'].unique()))<jupyter_output><empty_output><jupyter_text>Those are the countries that are in mpy but not in the GDP dataframe. This makes sense since "Individual Olympic Athletes" is not a country, and Taiwan is part of China. For HDI and population size there will also be similar problems, especially HDI which does not have a value for a lot of small countries.So lastly, we add a GDP column to our dataframe.<jupyter_code>complete_df = mpy.merge(GDP_df, on=['Year', 'Country'], how='left')
complete_df.head(3)<jupyter_output><empty_output><jupyter_text>These same steps are then repeated identically for the population size and HDI.  Adding population<jupyter_code>pop_df.head()
pop_df = pop_df.drop(['Indicator Name', 'Indicator Code','Country Code'], axis=1)
pop_df = pd.melt(pop_df, id_vars = ['Country'], var_name = 'Year', value_name = 'Population_Size')
pop_df = pop_df[:12369]
pop_df.columns = ['Country','Year','Population_Size']
pop_df['Year'] = pd.to_numeric(pop_df['Year'])
list(set(mpy['Country'].unique()) - set(pop_df['Country'].unique()))
complete_df = complete_df.merge(pop_df, on=['Year', 'Country'], how='left')
complete_df.head(3)<jupyter_output><empty_output><jupyter_text>Adding hdi<jupyter_code>HDI_df.head()
HDI_df = HDI_df.drop(['HDI Rank (2018)', 'HDI Rank (2018),Country,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018'], axis=1)
HDI_df = pd.melt(HDI_df, id_vars = ['Country'], var_name = 'Year', value_name = 'HDI')
HDI_df = HDI_df[:5957]
HDI_df['Year'] = pd.to_numeric(HDI_df['Year'])
HDI_df = HDI_df[HDI_df.HDI != ".."]
list(set(mpy['Country'].unique()) - set(HDI_df['Country'].unique()))
complete_df = complete_df.merge(HDI_df, on=['Year', 'Country'], how='left')
complete_df.head(3)<jupyter_output><empty_output><jupyter_text>Final touches to the dataset Finally, we create the final dataset, which only differs from the previous one for the type of some of its column, a change that was necessary to implement the linear regression later on.<jupyter_code>d = complete_df
d['Population_Size'] = pd.to_numeric(d['Population_Size'])
d['HDI'] = pd.to_numeric(d['HDI'])
d.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 1062 entries, 0 to 1061
Data columns (total 7 columns):
 #   Column           Non-Null Count  Dtype  
---  ------           --------------  -----  
 0   Country_Year     1062 non-null   object 
 1   Medals_won       1062 non-null   int64  
 2   Country          1062 non-null   object 
 3   Year             1062 non-null   int64  
 4   GDP              957 non-null    float64
 5   Population_Size  1041 non-null   float64
 6   HDI              664 non-null    float64
dtypes: float64(3), int64(2), object(2)
memory usage: 58.1+ KB<jupyter_text>At this point, we also get rid of all na values that are present in the dataset and reset the index.<jupyter_code>d = d[d['GDP'].notna()]
d = d[d['Population_Size'].notna()]
d = d[d['HDI'].notna()]
d = d.reset_index()
d = d.drop(["index"], axis=1)
d<jupyter_output><empty_output><jupyter_text>This is our final dataset. We are now ready to work on the regression models.  Linear Regression Firstly, we try to apply the linear regression to the whole dataframe.  Medals won - GDP<jupyter_code>x = d['Medals_won']
p = d['GDP']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  # create object for the class
linear_regressor.fit(x, p)  # perform linear regression
Y_pred = linear_regressor.predict(x)  # make predictions
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.301')
plt.xlabel('Total number of medals won')
plt.ylabel('GDP')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[7.12126957e+10]]
the intercept is:  [3.77604283e+10]
the R^2 is: 0.30057360871339833<jupyter_text>Medals won - Population size<jupyter_code>x = d['Medals_won']
p = d['Population_Size']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p)  
Y_pred = linear_regressor.predict(x)  
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.075')
plt.xlabel('Total number of medals won')
plt.ylabel('Population size')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[3643630.60843405]]
the intercept is:  [33872774.28473325]
the R^2 is: 0.07520538315997538<jupyter_text>Medals won - HDI<jupyter_code>x = d['Medals_won']
p = d['HDI']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p)  
Y_pred = linear_regressor.predict(x)  
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.052')
plt.xlabel('Total number of medals won')
plt.ylabel('HDI')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[0.00183433]]
the intercept is:  [0.74691589]
the R^2 is: 0.05240689700548151<jupyter_text>Of course, this does not yield very interesting results. This happens because the entire dataframe includes the same countries multiple times, once for each time they participated in the olympic games.  Beijing 2008 For this reason, we decided to apply the linear regression to one olympic game at a time. We start by looking at the Beijing games of 2008.<jupyter_code>b = d[d['Year'] == 2008]
b<jupyter_output><empty_output><jupyter_text>Medals won - GDP<jupyter_code>x = b['Medals_won']
p = b['GDP']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p)  
Y_pred = linear_regressor.predict(x)  
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.625')
plt.xlabel('Total number of medals won')
plt.ylabel('GDP')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[8.52136376e+10]]
the intercept is:  [-1.83264464e+11]
the R^2 is: 0.6250596335038993<jupyter_text>Medals won - Population size<jupyter_code>x = b['Medals_won']
p = b['Population_Size']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p)  
Y_pred = linear_regressor.predict(x)  
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.204')
plt.xlabel('Total number of medals won')
plt.ylabel('Population size')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[5195169.04808747]]
the intercept is:  [10911962.66646458]
the R^2 is: 0.20382487429743723<jupyter_text>Medals won - HDI<jupyter_code>x = b['Medals_won']
p = b['HDI']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p)  
Y_pred = linear_regressor.predict(x)  
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.067')
plt.xlabel('Total number of medals won')
plt.ylabel('HDI')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[0.00201679]]
the intercept is:  [0.73518124]
the R^2 is: 0.0671544746373629<jupyter_text>Overall, this gave much better results. We can see how there is a pretty strong correlation between GDP and number of medals won, since R^2 is 0.625, while HDI and population size have much less of an impact, with $R^2$ < 0.2.We now analyse another Olympic Game, to see if we get similar results.  London 2012<jupyter_code>l = d[d['Year'] == 2012]
l<jupyter_output><empty_output><jupyter_text>Medals won - GDP<jupyter_code>x = l['Medals_won']
p = l['GDP']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p)  
Y_pred = linear_regressor.predict(x)  
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.671')
plt.xlabel('Total number of medals won')
plt.ylabel('GDP')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[1.01783561e+11]]
the intercept is:  [-2.69303123e+11]
the R^2 is: 0.6709874958827224<jupyter_text>Medals won - Population size<jupyter_code>x = l['Medals_won']
p = l['Population_Size']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p)  
Y_pred = linear_regressor.predict(x)  
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.169')
plt.xlabel('Total number of medals won')
plt.ylabel('Population size')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[4856114.53964468]]
the intercept is:  [13109991.18639519]
the R^2 is: 0.16928280295964604<jupyter_text>Medals won - HDI<jupyter_code>x = l['Medals_won']
p = l['HDI']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p)  
Y_pred = linear_regressor.predict(x)  
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.079')
plt.xlabel('Total number of medals won')
plt.ylabel('HDI')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[0.00175295]]
the intercept is:  [0.76524824]
the R^2 is: 0.07855380181417149<jupyter_text>In conclusion, we see that London 2012 has similar results to Beijing 2008. Since these are both summer games, we now analyse the situation for the winter games.  Vancouver 2010<jupyter_code>v = d[d['Year'] == 2010]
v.head()<jupyter_output><empty_output><jupyter_text>Medals won - GDP<jupyter_code>x = v['Medals_won']
p = v['GDP']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p) 
Y_pred = linear_regressor.predict(x)  
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.278')
plt.xlabel('Total number of medals won')
plt.ylabel('GDP')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[2.06703254e+11]]
the intercept is:  [-4.927075e+10]
the R^2 is: 0.2782329846662027<jupyter_text>Medals won - Population size<jupyter_code>x = v['Medals_won']
p = v['Population_Size']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()  
linear_regressor.fit(x, p)  
Y_pred = linear_regressor.predict(x)
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.019')
plt.xlabel('Total number of medals won')
plt.ylabel('Population size')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[4477881.18923623]]
the intercept is:  [53504473.53492143]
the R^2 is: 0.018814531085514452<jupyter_text>Medals won - HDI<jupyter_code>x = v['Medals_won']
p = v['HDI']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()
linear_regressor.fit(x, p)
Y_pred = linear_regressor.predict(x)
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.133')
plt.xlabel('Total number of medals won')
plt.ylabel('HDI')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[0.00266798]]
the intercept is:  [0.84043696]
the R^2 is: 0.1333013923658264<jupyter_text>For the winter games the result are much less satisfactory. Wee see that the correlation between GDP and number of medals won drops to a $R^2$ of less than 0.3 and population size to less than 0.1, while the impact of HDI remains about the same (but it was already pretty low for the summer games).We check these results by analysing another winter Olympic.  Sochi 2014<jupyter_code>s = d[d['Year'] == 2014]
s.head()<jupyter_output><empty_output><jupyter_text>Medals won - GDP<jupyter_code>x = s['Medals_won']
p = s['GDP']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()
linear_regressor.fit(x, p)
Y_pred = linear_regressor.predict(x)
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.150')
plt.xlabel('Total number of medals won')
plt.ylabel('GDP')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[1.99685955e+11]]
the intercept is:  [2.32593566e+11]
the R^2 is: 0.15022225161499114<jupyter_text>Medals won - Population size<jupyter_code>x = s['Medals_won']
p = s['Population_Size']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression() 
linear_regressor.fit(x, p)
Y_pred = linear_regressor.predict(x)
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.012')
plt.xlabel('Total number of medals won')
plt.ylabel('Population size')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[3993057.02799721]]
the intercept is:  [58550620.46048849]
the R^2 is: 0.012326548172627794<jupyter_text>Medals won - HDI<jupyter_code>x = s['Medals_won']
p = s['HDI']
x=x.to_numpy()
p=p.to_numpy()
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()
linear_regressor.fit(x, p)
Y_pred = linear_regressor.predict(x)
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('R^2 = 0.100')
plt.xlabel('Total number of medals won')
plt.ylabel('HDI')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))<jupyter_output>the coefficient is:  [[0.00248522]]
the intercept is:  [0.85104653]
the R^2 is: 0.09962955266465834<jupyter_text>Again, the final results are pretty similar. This difference in results between the summer and winter games makes sense considering that considering that countries with a colder climate tend to be better at winter sports, and those countries also happen to have pretty high GDPs and small populations, which in turn means there is not much of a correlation between those features and the number of medals won.  Comparison between Linear Regression and Logistic Regression In this last part, we compare the linear regression model with the logistic regression one, to see which one applies better to this particular problem. We chose the GDP and number of medals won in London 2012 as the field of comparison, since we already know it gives pretty good results for the linear regression ( $R^2$ = 0.671). We use the total sum of the residuals as a way of comparing the two regression models.We start by recalling the linear regression model we have previously implemented, just with standardised number of medals and GDPs. This is done because the standardisation will be necessary for the logistic model, so they both need to have it otherwise comparison will not be possible.<jupyter_code>stand_m =[]
for i,row in l.iterrows():
    x=int(l['Medals_won'][i])
    stand_m.append(x)
m_std = (stand_m - np.mean(stand_m)) / np.std(stand_m)
stand_gdp =[]
for i,row in l.iterrows():
    x=int(l['GDP'][i])
    stand_gdp.append(x)
gdp_std = (stand_gdp - np.mean(stand_gdp)) / np.std(stand_gdp)
x = gdp_std
p = m_std
x=x.reshape(-1,1)
p=p.reshape(-1,1)
linear_regressor = LinearRegression()
linear_regressor.fit(x, p)
Y_pred = linear_regressor.predict(x) 
plt.scatter(x, p)
plt.plot(x, Y_pred, color='red')
plt.title('Linear Regression')
plt.xlabel('GDP')
plt.ylabel('Number of medals won')
plt.show()
print("the coefficient is: ",linear_regressor.coef_)
print("the intercept is: ",linear_regressor.intercept_)
print("the R^2 is:",linear_regressor.score(x,p))
tot_residuals = np.sum(np.power(p - Y_pred, 2))
tot_residuals<jupyter_output><empty_output><jupyter_text>Now we move on to the logistic regression, for which we will again need the standardised GDP values. Instead, for the medals we implement a new binary list, which has value 1 for those countries that have won at least 15 medals and 0 for those that did not.<jupyter_code>g=[]
for i,row in l.iterrows():
    x=int(l['GDP'][i])
    g.append(x)
g_std = (g - np.mean(g)) / np.std(g)
m=[]
x = 15
for i,row in l.iterrows():
    if l['Medals_won'][i]>x:
        m.append(1)
    else:
        m.append(0)
X = np.asarray(g_std)
y = m
X=X.reshape(-1,1)
plt.scatter(X.ravel(), y,zorder=20)
X_test = np.linspace(min(X), max(X), 81)
clf = LinearRegression()
clf.fit(X,y)
plt.xlabel('GDP')
plt.ylabel('Won at least 15 medals or not')
loss = expit(X_test * clf.coef_ + clf.intercept_).ravel()
loss_y = expit(X * clf.coef_ + clf.intercept_).ravel()
plt.plot(X_test, loss, color='red', linewidth=3)
tot_residuals = np.sum(np.power(y - loss_y, 2))
print(tot_residuals)<jupyter_output>21.04827279675492
{'fraction_non_alphanumeric': 0.1342508130403744, 'fraction_numerical': 0.046006187038946614, 'mean_word_length': 5.789176090468498, 'pattern_counts': {'":': 1, '<': 142, '<?xml version=': 0, '>': 142, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '39', 'dir': 'jupyter-structured-clean-dedup', 'id': '201775', 'license': 'MIT', 'n_tokens_mistral': 10042, 'n_tokens_neox': 9032, 'n_words': 2437, 'path': 'Olympics - Final Project/Model3.ipynb', 'repo_name': 'tanselsimsek/Fundamental-of-Data-Science', 'size': '25214'}