Skip to content

Instantly share code, notes, and snippets.

@jcheong0428
Created November 15, 2021 04:52
Show Gist options
  • Save jcheong0428/ea2e1c169f87d8a934f6584e9703ae3f to your computer and use it in GitHub Desktop.
Save jcheong0428/ea2e1c169f87d8a934f6584e9703ae3f to your computer and use it in GitHub Desktop.
backpain_2.py
from scipy import stats
def create_future():
"""Code to create datetime index for the covid WFH period."""
future = list()
for y in [2020]:
for m in range(4, 13):
date = f'{y}-{m:02d}'
future.append([date])
for y in [2021]:
for m in range(1, 12):
date = f'{y}-{m:02d}'
future.append([date])
future = pd.DataFrame(future)
future.columns = ['ds']
future['ds'] = pd.to_datetime(future['ds'])
return future
for variableOfInterest in ['back pain', 'lower back pain', 'chiropractor near me']:
ts = pd.DataFrame({'ds':df.loc[:'2020-03-01'].index,'y':df.loc[:'2020-03-01'][variableOfInterest]})
prophet = Prophet(weekly_seasonality=False,
daily_seasonality=False,
changepoint_range=1,
changepoint_prior_scale=0.1,
)
prophet.fit(ts)
future = create_future()
forecast = prophet.predict(future)
f, ax =plt.subplots()
covid_df = df.loc['2020-04-01':][[variableOfInterest]]
covid_df.plot(ax=ax)
forecast_covid_df = forecast[['yhat']]
forecast_covid_df.index = covid_df.index
forecast_covid_df.plot(ax=ax, color='r')
ax.set_ylabel("searches (%)", rotation=0, labelpad=60)
ax.set(title=f'{variableOfInterest}')
plt.legend(['ground truth searches (y)', 'predicted searches (yhat)'], bbox_to_anchor=(1.05, .65), loc=2, borderaxespad=0.)
plt.show()
diff = covid_df.values - forecast_covid_df.values
print(stats.ttest_1samp(diff.ravel(), 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment