# Getting Started: Plant Growth Case Study

```{block, type="learningObj"}

Our learning objectives in this session...

- Import data
- Performing descriptive and inferential statistics
- Plotting functions

```

## Set up your environment

Using the environment you created in the last chapter, continue you script by following along with the following commands. First, import the packages used in this case study. Remember, you'll need to install them using the terminal first.

```{python echo = TRUE, message=FALSE, warning=FALSE}
# import an entire library
import math # Functions beyond the basic maths

# Import an entire library and give it an alias
import pandas as pd # For DataFrame and handling
import numpy as np # Array and numerical processing
import matplotlib.pyplot as plt # Low level plotting
import seaborn as sns # High level Plotting
import statsmodels.api as sm # Modeling, e.g. ANOVA

# Import only specific modules from a library
# we'll use this for the t-test function
from scipy import stats

# Import only specific functions from a library 
# ols is for ordinary least squares
from statsmodels.formula.api import ols

```

## load dataset

Read in the data set using:

```{python}
plant_growth = pd.read_csv('data/plant_growth.csv')
```

```{python eval = FALSE, echo = FALSE}
plant_growth = pd.read_csv('data/Plant Growth.txt', delimiter="\t")
```

## Examine the data 

It's always good practice to look at our data before we start working on it.

```{python}
# The object
plant_growth.info()
plant_growth.shape
plant_growth.columns
```

```{python}
# Summaries
plant_growth.describe()
plant_growth['group'].value_counts()
```

```{python}
# explore first dataset rows
plant_growth.head()
```

## Descriptive statistics

Let's take a look at some first steps in descriptive statistics.

```{python}
# count group members
plant_growth['group'].value_counts()

# get average weight
np.mean(plant_growth['weight'])

# summary statistics
plant_growth.groupby(['group']).describe()
```

There are always many ways to get to the same solution, but we'd tend to prefer the simplest.

```{python}
df1 = plant_growth.groupby(['group']).mean()
df2 = plant_growth.groupby(['group']).std()
final_df = pd.concat([df1, df2], axis=1)
```

Can you tell what the difference is between the following two commands? Why is this important?

```{python}
# Produces Pandas Series
plant_growth.groupby('group')['weight'].mean() 
```

```{python}
# Produces Pandas DataFrame
plant_growth.groupby('group')[['weight']].mean()
```

This is one easy and specific way:

```{python}
plant_growth.groupby(['group']).agg({'weight':['mean','std']})
```

## Plotting

Alright, let's take a look at some data visualization of weight described by group. Here, we have a box plot:

```{python eval = FALSE}
sns.boxplot(x='group', y='weight', data=plant_growth)
```

```{python, echo = FALSE, eval = TRUE}
sns.boxplot(x='group', y='weight', data=plant_growth)
plt.show()
```

Just the points:

```{python eval = FALSE}
sns.catplot(x="group", y="weight", data=plant_growth)
```

```{python echo = FALSE, message = FALSE, warning = FALSE}
sns.catplot(x="group", y="weight", data=plant_growth)
plt.show()
```

and just the means with their standard deviations:

```{python eval = FALSE}
sns.pointplot(x="group", y="weight", data=plant_growth, join=False)
# alternatively:
# sns.catplot(x="group", y="weight", data=plant_growth, kind="point")
```


```{python echo = FALSE}
sns.pointplot(x="group", y="weight", data=plant_growth, join=False)
plt.show()
```

## Inferential Statistics

We discussed a couple different tests in inferential statistics that we'd like to perform on our data. Let's take a look.

### T-test

There is a specific t-test function, which we'll explore later on, but in this case, we have a specific set-up in that we have three groups and we're interested in two specific two-group comparisons. We can accomplish this by establishing a linear model.

```{python}
# fit a linear model
# specify model
model = ols("weight ~ group", plant_growth)

# fit model
results = model.fit()
```

We can get the coefficients of the model directly:

```{python}
# extract coefficients
results.params.Intercept
results.params["group[T.trt1]"]
results.params["group[T.trt2]"]
```

Can you imagine what these number refer to?

Finally, let's take a look at a summary of our model:

```{python}
# Explore model results
results.summary()
```

### ANOVA

Alright, let's wrap this up with our one-way ANOVA. Notice that we're using our model, `results`, that we fitted above.

```{python}
# ANOVA
# compute anova
aov_table = sm.stats.anova_lm(results, typ=2)

# explore anova results
aov_table
```

```{python echo = FALSE}
print(aov_table)
```

What conclusions can you draw from you analysis? Did using different treatment affect the growth of our plants?

### Exercises

```{exercise}
In the data folder you'll find another file called "Chick Weights.txt". 

Here you'll find the results from an experiment in which newly hatched chicks were randomly allocated into six groups. Each group was given a different feed supplement. After six weeks, their weights in grams were measured.

Using the commands we've learned so far complete the following tasks:

- Calculate the mean and standard deviation for each group.
- Calculate the number of chicks in each group.
- Calculate a within-group z-score.
- Produce a strip chart showing each chick as an individual data point
- Calculate a 1-way ANOVA.
- Calculate Tukey's post-hoc test (i.e. p-values for _all_ pair-wise t-tests)

```

```{python echo = FALSE, eval = FALSE}
sns.stripplot(x='group', y='weight', data=plant_growth, jitter=0.2)
```

You'll have to find the function to perform Tukey's Honestly Significant differences (hsd)  post-how test. The results will be as follows:

```{python, echo = FALSE}
# t-test
chick_weights = pd.read_csv('data/Chick Weights.txt', delimiter="\t")

from statsmodels.stats.multicomp import pairwise_tukeyhsd
print(pairwise_tukeyhsd(chick_weights['weight'], chick_weights['feed']))
```

## Wrap-up

```{block, type="chapRecap"}
Congratulations! So far in our journey we've seen a complete walk-through of two case studies. We use fairly small data sets and saw the process from importing data all the way to making some meaningful conclusions. Now that we have an overview, we're going to zoom in on the details of what we've been doing in the next chapter.
```
