import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns from epimargin.utils import cwd import statsmodels.api as sm # load data data = cwd() / "example_data" df = pd.read_csv(data / "metro_state_policy_evaluation.csv").dropna() df["Rt"] = df["RR_pred"] df["Rt_binarized"] = (df["RR_pred"] >= 1).astype(int) drop_cols = [col for col in df.columns if col.startswith("RR_")] + [ "metro-state", "date", "state", "state_name", "start_stay_at_home", "cbsa_fips", "end_stay_at_home", "mask_mandate_all", "metro_outbreak_start", "threshold_ind", "new_cases_ts", "daily_confirmed_cases", "religious_exception_stay_home" ] df.drop(columns=drop_cols, inplace=True) df.rename(lambda x: x.replace(".0", ""), axis=1, inplace=True) # set up metro fixed effects (some metro cols are all 0) metros = [ col for col in df.columns if col.startswith("metro") if df[col].sum() > 0 ] # drop one metro to avoid multicollinearity reference_metro, *metros = metros drop_metro_cols = [ col for col in df.columns
if show: plt.show() return coef def notch_filter(ts): "Implement notch filter with notches at {1/7, 2/7}" fs, f0, Q = 1, 1/7, 1 b1, a1 = iirnotch(f0, Q, fs) b2, a2 = iirnotch(2*f0, 2*Q, fs) b = convolve(b1, b2) a = convolve(a1, a2) notched = pd.Series(filtfilt(b, a, ts)) notched.index = ts.index return notched root = cwd() data = mkdir(root/"data") figs = mkdir(root/"figs") ########################################################### # download latest case data # download_data(data, 'state_wise_daily.csv') # df = load_statewise_data(data/"state_wise_daily.csv") # ts = get_time_series(df, "state") ########################################################### # load delay data api_diff = pd.read_csv(data/"daily_diff.csv", parse_dates=["status_change_date", "report_date"], dayfirst=True) delay = api_diff[(api_diff.current_status == "Hospitalized") & (api_diff.report_date > "2020-08-02")].copy() delay = delay.drop(columns = [col for col in delay.columns if col.startswith("Unnamed")] + ["rowhash"]) delay["newhash"] = delay[["patient_number", "date_announced", "detected_district", "detected_state","current_status", "status_change_date", "num_cases"]].apply(lambda x: hash(tuple(x)), axis = 1)
simplefilter("ignore") def project(dates, R_values, smoothing, period=7 * days): julian_dates = [_.to_julian_date() for _ in dates[-smoothing // 2:None]] return OLS( RR_pred[-smoothing//2:None], add_constant(julian_dates) )\ .fit()\ .predict([1, julian_dates[-1] + period])[0] # set to cloud temp directory if not explicitly told to run locally root = cwd() if len(sys.argv) > 1 and sys.argv[1] == "--local" else Path( "/tmp") data = root / "data" # model details gamma = 0.2 smoothing = 10 CI = 0.95 download_data(data, 'state_wise_daily.csv') state_df = load_statewise_data(data / "state_wise_daily.csv") country_time_series = get_time_series(state_df) estimates = [] timeseries = []