viewof covid_tracking_states =
{
const rawest = aq.from(covid_tracking)
.select(["date", "state", "positiveIncrease", "deathIncrease", "hospitalizedCurrently"])
const dates = rawest.select("date").dedupe().reify()
const unique_states = rawest.select("state").dedupe().reify()
const all_combos = dates.join_full(unique_states, [[], []])
let filled_a_bit = rawest
.join_full(all_combos, [['date', 'state'], ['date', 'state']], [["positiveIncrease", "deathIncrease", "hospitalizedCurrently"], ['date', 'state']])
.derive({
"positive": d => d.positiveIncrease ? d.positiveIncrease : 0,
"rolling_hospitalized": d => d.hospitalizedCurrently ? d.hospitalizedCurrently : 0,
"deaths": d => d.deathIncrease ? d.deathIncrease : 0}
)
.join(states, [["state"], ["state_abbr"]], [["positive", "date", "state", "rolling_hospitalized", "deaths"], ["pop"]])
.groupby("state")
.orderby("date")
.params({lag: TIME_HORIZON})
.derive({"rolling_positive": aq.rolling((d, $) => op.sum(d.positive), [-TIME_HORIZON, 0]),
"rolling_deaths": aq.rolling((d, $) => op.sum(d.deaths), [-TIME_HORIZON, 0])
})
.derive(
{"change_positive": (d, $) => d.rolling_positive / op.lag(d.rolling_positive, $.lag),
"change_deaths": (d, $) => d.rolling_deaths / op.lag(d.rolling_deaths, $.lag),
"change_hospitalized": (d, $) => d.rolling_hospitalized / op.lag(d.rolling_hospitalized, $.lag)
})
.derive(
{"positive_per_capita": (d, $) => d.rolling_positive / d.pop,
"deaths_per_capita": (d, $) => d.rolling_deaths / d.pop,
"hospitalized_per_capita": (d, $) => d.rolling_hospitalized / d.pop
}).select(["state", "date",
"positive_per_capita", "deaths_per_capita", "hospitalized_per_capita",
"change_positive", "change_deaths", "change_hospitalized",
"rolling_positive", "rolling_deaths", "rolling_hospitalized"])
return filled_a_bit.view()
}