<jupyter_start><jupyter_text>Week 3 Assignment: Generating a Correlation CoefficientIn this [assignment](https://www.coursera.org/learn/data-analysis-tools/peer/te8hb/generating-a-correlation-coefficient) I've chosen the [Gapminder](./data/gapminder.csv) dataset. Looking through its [codebook](./data/GapMinderCodebook.pdf) 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 Indicatorshas 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.<jupyter_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()<jupyter_output>Populating the interactive namespace from numpy and matplotlib<jupyter_text>Pearson correlation $r$This is just straightfoward.<jupyter_code>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)))<jupyter_output>Percentage of variability in the reponse variable given by the explanatory variable is 36.18%<jupyter_text>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.<jupyter_code># Setting an apropriate size for the graph.
factor = 1.3
figsize(6*factor, 4*factor)

# Plot the graph.
sn.regplot(df.incomeperperson, df.lifeexpectancy);<jupyter_output>/Users/sergio/anaconda3/lib/python3.4/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == str('face'):
{'fraction_non_alphanumeric': 0.08839381961911606, 'fraction_numerical': 0.016169601149838304, 'mean_word_length': 5.790243902439024, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '558277', 'license': 'MIT', 'n_tokens_mistral': 847, 'n_tokens_neox': 785, 'n_words': 290, 'path': 'Week_3.ipynb', 'repo_name': 'srodriguex/coursera_data_analysis_tools', 'size': '2783'}