#!/usr/bin/env python # coding: utf-8 # # Correlation and Experimental Design # > In this chapter, you'll learn how to quantify the strength of a linear relationship between two variables, and explore how confounding variables can affect the relationship between two other variables. You'll also see how a study’s design can influence its results, change how the data should be analyzed, and potentially affect the reliability of your conclusions. This is the Summary of lecture "Introduction to Statistics in Python", via datacamp. # # - toc: true # - badges: true # - comments: true # - author: Chanseok Kang # - categories: [Python, Datacamp, Statistics] # - image: images/lmplot_wh.png # In[1]: import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns # ## Correlation # - Correlation coefficient # - Quantifies the linear relationship between two variables # - Number between -1 and 1 # - Magnitude corresponds to strength of relationship # - Sign (+ or -) corresponds to direction of relationship # - Pearson product-moment correlation($r$) # - Most Common # - $\bar{x}$ = mean of $x$ # - $\sigma_x$ = standard deviation of $x$ # # $$ r = \sum_{i=1}^{n} \frac{(x_i - \bar{x})(y_i - \bar{y})}{\sigma_x \times \sigma_y} $$ # # - Variation # - Kendall's Tau # - Spearman's rho # ### Relationships between variables # In this chapter, you'll be working with a dataset `world_happiness` containing results from the [2019 World Happiness Report](https://worldhappiness.report/ed/2019/). The report scores various countries based on how happy people in that country are. It also ranks each country on various societal aspects such as social support, freedom, corruption, and others. The dataset also includes the GDP per capita and life expectancy for each country. # # In this exercise, you'll examine the relationship between a country's life expectancy (`life_exp`) and happiness score (`happiness_score`) both visually and quantitatively. # In[2]: world_happiness = pd.read_csv('./dataset/world_happiness.csv', index_col=0) world_happiness.head() # In[3]: # Create a scatterplot of happiness_score vs. life_exp and show sns.scatterplot(x='life_exp', y='happiness_score', data=world_happiness); # In[5]: # Create scatterplot of happiness_score vs. life_exp with trendline sns.lmplot(x='life_exp', y='happiness_score', data=world_happiness, ci=None); # In[6]: # Correlation between life_exp and happiness_score cor = world_happiness['life_exp'].corr(world_happiness['happiness_score']) print(cor) # ## Correlation caveats # - Correlation only accounts for linear relationships # - Transformation # - Certain statistical methods rely on variables having a linear relationship # - Correlation coefficient # - Linear regression # - Correlation does not imply causation # - $x$ is correlated with $y$ **does not mean** $x$ causes $y$ # ### What can't correlation measure? # While the correlation coefficient is a convenient way to quantify the strength of a relationship between two variables, it's far from perfect. In this exercise, you'll explore one of the caveats of the correlation coefficient by examining the relationship between a country's GDP per capita (`gdp_per_cap`) and happiness score. # # # In[7]: # Scatterplot of gdp_per_cap and life_exp sns.scatterplot(x='gdp_per_cap', y='life_exp', data=world_happiness); # Correlation between gdp_per_cap and life_exp cor = world_happiness['gdp_per_cap'].corr(world_happiness['life_exp']) print(cor) # ### Transforming variables # When variables have skewed distributions, they often require a transformation in order to form a linear relationship with another variable so that correlation can be computed. In this exercise, you'll perform a transformation yourself. # In[8]: # Scatterplot of happiness_score vs. gdp_per_cap sns.scatterplot(x='gdp_per_cap', y='happiness_score', data=world_happiness); # Calculate correlation cor = world_happiness['gdp_per_cap'].corr(world_happiness['happiness_score']) print(cor) # In[9]: # Create log_gdp_per_cap column world_happiness['log_gdp_per_cap'] = np.log(world_happiness['gdp_per_cap']) # Scatterplot of log_gdp_per_cap and happiness_score sns.scatterplot(x='log_gdp_per_cap', y='happiness_score', data=world_happiness); # Calculate correlation cor = world_happiness['log_gdp_per_cap'].corr(world_happiness['happiness_score']) print(cor) # ### Does sugar improve happiness? # A new column has been added to `world_happiness` called `grams_sugar_per_day`, which contains the average amount of sugar eaten per person per day in each country. In this exercise, you'll examine the effect of a country's average sugar consumption on its happiness score. # In[10]: world_happiness = pd.read_csv('./dataset/world_happiness_add_sugar.csv', index_col=0) world_happiness # In[11]: # Scatterplot of grams_sugar_per_day and happiness_score sns.scatterplot(x='grams_sugar_per_day', y='happiness_score', data=world_happiness); # Correlation between grams_sugar_per_day and happiness_score cor = world_happiness['grams_sugar_per_day'].corr(world_happiness['happiness_score']) print(cor) # ## Design of experiments # - Vocabulary # - Experiment aims to answer: What is the effect of the **treatment** on the **response**? # - Treatment: explanatory / independent variable # - Response: response / dependent variable # - E.g.: What is the effect of an advertisement on the number of products purchased? # - Treatment: advertisement # - Response: number of products purchased # - Controlled experiments # - Participants are assigned by researchers to either treatment group or control group # - Treatment group sees advertisement # - Control group does not # - Group should be comparable so that causation can be inferred # - If groups are not comparable, this could lead to confounding (bias) # - Gold standard of experiment # - Randomized controlled trial # - Participants are assigned to treatment/control randomly, not based on any other characteristics # - Choosing randomly helps ensure that groups are comparable # - Placebo # - Resembles treatement, but has no effect # - Participants will not know which group they're in # - Double-blind trial # - Person administering the treatment/running the study doesn't know whether the treatment is real or a placebo # - Prevents bias in the response and/or analysis of results # - Fewopportunities for bias = more reliable conclusion about causation # - Observational studies # - Participants are not assigned randomly to groups # - Participants assign themselves, usually based on pre-existing characteristics # - Many research questions are not conductive to a controlled experiment # - Cannot force someone to smoke or have a disease # - Establish association, not causation # - Effects can be confounded by factors that got certain people into the control or treatment group # - There are ways to control for confounders to get more reliable conclusions about association # - Longitudinal vs. cross-sectional studies # - Longitudinal study # - Participants are followed over a period of time to examine effect of treatment on response # - Effect of age on height is not confounded by generation # - More expensive, results take longer # - Cross-sectional study # - Data on participants is collected from a single snapshot in time # - Effect of age on height is confounded by generation # - Cheaper, fater, more convenient