Skip to content

Instantly share code, notes, and snippets.

@BinarySpoon
Last active October 23, 2020 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinarySpoon/241c8ef3f08638ecfc14320e513452b5 to your computer and use it in GitHub Desktop.
Save BinarySpoon/241c8ef3f08638ecfc14320e513452b5 to your computer and use it in GitHub Desktop.
# Running ttest -->
def run_ttest():
#Get All relevant constraints -->
housing_data = convert_housing_data_to_quarters()
startQ = get_recession_start()
bottomQ = get_recession_bottom()
endQ = get_recession_end()
#Recession Timeframe -->
housing_data['Delta'] = housing_data[bottomQ]- housing_data[startQ]
#Regions with Universities in them -->
uni_regions = get_list_of_university_towns()
uni_regions['Uni_regions'] = True
#merge housing data with uni_town df -->
data2 = pd.merge(housing_data,uni_regions,how='right', left_index=True,right_on=['State', 'RegionName'])
uni_towns = data2[data2['Uni_regions']==True]['Delta']
#merge housing data with non-uni_town df -->
data3 = pd.merge(data,uni_regions,how='left',left_index=True,right_on=['State','RegionName'])
data3['Uni_regions'] = data3['Uni_regions'].replace({np.NaN: False})
non_uni_towns = data3[data3['Uni_regions'] == False]['Delta']
#Run ttest on both dataframes -->
st,p = stats.ttest_ind(uni_towns,non_uni_towns,nan_policy='omit')
#get different p-values (bar at 0.01) -->
different = False
if p < 0.01:
different = True # p<0.01 means lower than 1% of the thing happening by chance.
#Which place is better -->
better = ""
if uni_towns.mean() > non_uni_towns.mean():
better = "university town"
else:
better = "non-university towns"
return (different,p,better)
run_ttest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment