<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Week 3 Assignment
Step1:  Pearson correlation $r$
This is just straightfoward.
Step2:  As we can see above, $r = 0.60$ with $pvalue=1.06*10^{-18}$, shows a moderately strong correlation between life expectancy and income per person. Let's take a look at the scatter plot to see how this correlation is formed.
<ASSISTANT_TASK:>
Python Code:
# Import all ploting and scientific library,
# and embed figures in this file.
%pylab inline
# Package to manipulate dataframes.
import pandas as pd
# Nice looking plot functions.
import seaborn as sn
# The Pearson correlation function.
from scipy.stats import pearsonr
# Read the dataset.
df = pd.read_csv('data/gapminder.csv')
# Set the country name as the index of the dataframe.
df.index = df.country
# This column is no longer needed.
del df['country']
# Select only the variables we're interested.
df = df[['lifeexpectancy','incomeperperson']]
# Convert the types.
df.lifeexpectancy = pd.to_numeric(df.lifeexpectancy, errors='coerce')
df.incomeperperson = pd.to_numeric(df.incomeperperson, errors='coerce')
# Remove missing values.
df = df.dropna()
Explanation: Week 3 Assignment: Generating a Correlation Coefficient
In this assignment I've chosen the Gapminder dataset. Looking through its codebook we've decided to study two numeric variables, incomeperperson and lifeexpectancy relationship:
incomeperperson
2010 Gross Domestic Product per capita in constant 2000 US$. The World Bank Work Development inflation but not the differences in the cost of living between countries Indicators
has been taken into account.
lifeexpectancy
2011 life expectancy at birth (years). The average number of years a newborn child would live if current mortality patterns were to stay the same.
End of explanation
r = pearsonr(df.incomeperperson, df.lifeexpectancy)
print('Correlation between incomeperperson and lifeexpectancy: {}'.format(r))
print('Percentage of variability in the reponse variable given by the explanatory variable is {:2}%'.
      format(round(r[0]**2*100,2)))
Explanation: Pearson correlation $r$
This is just straightfoward.
End of explanation
# Setting an apropriate size for the graph.
factor = 1.3
figsize(6*factor, 4*factor)
# Plot the graph.
sn.regplot(df.incomeperperson, df.lifeexpectancy);
Explanation: As we can see above, $r = 0.60$ with $pvalue=1.06*10^{-18}$, shows a moderately strong correlation between life expectancy and income per person. Let's take a look at the scatter plot to see how this correlation is formed.
End of explanation
<END_TASK>