# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Bonus: Temperature Analysis I

import pandas as pd
from datetime import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')

# "tobs" is "temperature observations"
df = pd.read_csv('Resources/hawaii_measurements.csv')
df.head()

# Convert the date column format from string to datetime
df['date']=pd.to_datetime(df['date'])

df.info()

# Set the date column as the DataFrame index
df.set_index('date',inplace=True)

# Drop the date column
df

# ### Compare June and December data across all years 

from scipy import stats
import datetime as dt
from scipy.stats import ttest_rel


# Filter data for desired months
June_dec_df=df.loc[(df.index.month_name()=='June')|(df.index.month_name()=='December')]
June_dec_df['month']=pd.DatetimeIndex(June_dec_df.index).month_name()
June_dec_df


# Identify the average temperature for June and December
June_dec_df.groupby('month').describe()['tobs']

# Create collections of temperature data
june =June_dec_df[June_dec_df['month'] == "June"]['tobs']
Dec = June_dec_df[June_dec_df['month'] == "December"]['tobs']

# paired t-test has to be used because the two compared samples of temperature observations are related to the same location and represent a difference between June temperature and December temperature.

# +
# Run paired t-test


# Python paired sample t-test
try:
    t_stat, p_val=stats.ttest_rel(june, Dec, nan_policy ='omit')
except:
    print("There is unequal sample sizes and can't use the paired t_test")

# -

# The solution is making the equal size sample:

June_df=df.loc[df.index.month_name()=='June']
December_df=df.loc[df.index.month_name()=='December']
June_df.sort_index(inplace=True)
December_df.sort_index(inplace=True)

# Create collections of temperature data
#the year of 2017 from filtered to make the equal sample sizes 
june =June_df['tobs'][:1509]
Dec = December_df['tobs'][:1509]


# +
# Run paired t-test

# Python paired sample t-test

t_stat, p_val=stats.ttest_rel(june, Dec, nan_policy ='omit')
t_stat, p_val

# -

# The results of our paired t-test reveal that there is a statistically significant difference between June an December in popularity (p < 0.01), such that June temp (M = 74.944118) were more High compared to December temp (M = 71.041529).

# #### But:
# The paired sample t-test has four main assumptions:
#
# • The dependent variable must be continuous (interval/ratio).
#
# • The observations are independent of one another.
#
# • The dependent variable should be approximately normally distributed.
#
# • The dependent variable should not contain any outliers
#
# Is our data meet this assumtions?!

plt.figure(figsize=(15, 7))
flierprops = dict(marker='o', markerfacecolor="palevioletred", markersize=12,
                  markeredgecolor='black')
sns.boxplot(x='month',y='tobs',data=June_dec_df,color="lightseagreen",flierprops=flierprops)
plt.savefig('Images/june_dec_box.png')

june_m =June_dec_df[June_dec_df['month'] == "June"]
Dec_m = June_dec_df[June_dec_df['month'] == "December"]
plt.figure(figsize=(20,7))
june_m['tobs'].plot(kind='hist',label='June',alpha=.8,color='palevioletred')
Dec_m['tobs'].plot(kind='hist',label='December',alpha=.5,color='lightseagreen')
plt.legend()
plt.title('Distribution of the temperature of two months of June and December',fontsize= 15,pad=10,fontweight="bold")
plt.xlabel("Temperature")
plt.ylabel("Frequency")
plt.savefig('images/Distribution_june_dec.png')

# The Shapiro-Wilk test is a statistical test used to check if a continuous variable follows a normal distribution. The null hypothesis (H0) states that the variable is normally distributed, and the alternative hypothesis (H1) states that the variable is NOT normally distributed. So after running this test:
#
# If p ≤ 0.05: then the null hypothesis can be rejected (i.e. the variable is NOT normally distributed).
#
# If p > 0.05: then the null hypothesis cannot be rejected (i.e. the variable MAY BE normally distributed).

#Checking the Normality of Data by shapiro test
print(f"{stats.shapiro(june)} for June")
print(f"{stats.shapiro(Dec)} for Decamber")


# So we can say that the data from both groups is not normally distributed.

#Checking the Homogeneity of Variances Assumption by levene test
stats.levene(june, Dec)

# The null hypothesis for Levene's test is that the groups we're comparing all have equal population variances. The p_value is less than  0.05 so population variances are not equal.

# So we have a 2 unequal sample size that are not normally distributed and have different variances.
#
# We will perform the non-parametric version of the two samples t-test, Mann-Whitney U Test, to determine if the groups differ in popularity. This test will allow us to test our hypothesis with non-normally distributed groups.

#performing <NAME> (non-parametric equivalent of the t-test)
stats.mannwhitneyu(june, Dec, alternative='two-sided')

# The results of our Mann-Whitney U Test reveal that there is a statistically significant difference between June an December in popularity (p < 0.01), such that June temp (M = 74.944118) were more High compared to December temp (M = 71.041529).

# ### More Analysis

# Filter data for desired months
June_df=df.loc[df.index.month_name()=='June']
June_df

December_df=df.loc[df.index.month_name()=='December']
December_df

June_df['year']=pd.DatetimeIndex(June_df.index).year
December_df['year']=pd.DatetimeIndex(December_df.index).year


# #### Temperature average per year in June and December

# Create collections of temperature data
june_df_year=June_df.groupby('year').mean()['tobs']
june_df_year.to_frame()

Dec_df_year=December_df.groupby('year').mean()['tobs']
Dec_df_year.to_frame()

plt.figure(figsize=(15, 7))
flierprops = dict(marker='o', markerfacecolor="palevioletred", markersize=12,
                  markeredgecolor='black')
sns.boxplot(x='year',y='tobs',data=June_df,color="lightseagreen",flierprops=flierprops)
plt.title('temperature by year in June',fontsize= 15,pad=30,fontweight="bold")
plt.savefig('Images/june_box.png')

plt.figure(figsize=(15, 7))
flierprops = dict(marker='o', markerfacecolor="palevioletred", markersize=12,
                  markeredgecolor='black')
sns.boxplot(x='year',y='tobs',data=December_df,color="lightseagreen",flierprops=flierprops)
plt.title('temperature by year in December',fontsize= 15,pad=30,fontweight="bold")
plt.savefig('Images/dec_box.png')

StarcoderdataJupyterScriptsDedupFiltered