signaturescience / fiphde

Forecasting Influenza in Support of Public Health Decision Making
https://signaturescience.github.io/fiphde/
GNU General Public License v3.0
2 stars 1 forks source link

get mobility data #34

Closed stephenturner closed 2 years ago

stephenturner commented 2 years ago

Get mobility data, possible predictor variable in regression model

vpnagraj commented 2 years ago

to consider ... what is the granularity of this data? state-level? national?

stephenturner commented 2 years ago

i got a start on this, pushing up some code soon that'll start as a jumping off point for our technical deep dive

stephenturner commented 2 years ago

tldr we can get at global/country data or down to the county

stephenturner commented 2 years ago

The code and the data

First cut in cec3b25. Let's talk through this next Thursday, how we might want to use something like this in a regression model. I'd worry about using this for COVID forecasting, because I think pretty much anyone would argue the change in mobility is causally downstream of covid ramping up or down. But the naive epidemiologist here thinks that mobility changes prompted by COVID ebbs and flows will have upstream causal effects on influenza transmission. But @vpnagraj is the card-carrying epidemiologist here 🤔

Apple

Some data from Apple maps (number of trips looked up)

https://covid19.apple.com/mobility

Daily requests for apple maps direction by type

image

Using slider::slide_dbl to move an average over a week smooths things out a bit

image

You can sum over all types

image

Or do it by state

image

Google

This is weekly average change compared to baseline. Different from raw number provided by apple maps.

US 7-day rolling average by type of trip. I think we might want to remove parks (open question?), and not sure what "residential" means here.

image

By state. The park increase is obvious in states with worse winter weather.

image

stephenturner commented 2 years ago

@vpnagraj I tried joining in the mobility data into the modeling in c1d8bbc but got hung up couldn't find the evaluate_models function. Any chance you had another lib loaded working on this?

vpnagraj commented 2 years ago

yeah thats coming from trendeval for me.

> evaluate_models
function (models, data, method = evaluate_resampling, ...) 
{
    ellipsis::check_dots_used()
    out <- lapply(models, function(model) safely(method)(model, 
        data, ...))
    out <- base_transpose(out)
    nms <- names(models)
    if (is.null(nms)) {
        out <- tibble::tibble(model = models, data = list(data), 
            result = out[[1]], warning = out[[2]], error = out[[3]])
    }
    else {
        out <- tibble::tibble(model_name = nms, model = models, 
            data = list(data), result = out[[1]], warning = out[[2]], 
            error = out[[3]])
    }
    result <- tidyr::unnest(out, "result", keep_empty = TRUE)
    result <- tidyr::pivot_wider(result, names_from = "metric", 
        values_from = "score")
    result$`NA` <- NULL
    result
}
<bytecode: 0x7fdce46b3e68>
<environment: namespace:trendeval>

i say "for me" because now im not seeing it in the docs:

https://rdrr.io/github/reconhub/trendeval/api/

anyways im using v0.0.1 trendeval and v0.0.3 for trending. both of those packages should probably be loaded at the top of the glm.R script.

stephenturner commented 2 years ago

yep, that did the trick. Added mobility data to all three of your models, still went with nb, no mobility data. changes to glm in 6eabfd491f122584247ea6d4065a241a678ae8b2

But that said, I'm not completely sure it's doing what I want it to do. might be helpful to me and chris to tear out what's being wrapped in these function calls in a "demo" above, something that "just fits the model" or something. tried doing this in 36a8a993c08e8304cd5e1ddbe5ca07414ea7466a but failed.

stephenturner commented 2 years ago

Yeah I'm not using that modeling api correctly whatsoever. Ignore the previous.

stephenturner commented 2 years ago

What I was trying to do in a4f4afd

# Test one model
tmp_weekly_flu_lags <-
  tmp_weekly_flu %>%
  mutate(lag_1 = lag(flu.admits, 1),
         lag_2 = lag(flu.admits, 2),
         lag_3 = lag(flu.admits, 3),
         lag_4 = lag(flu.admits, 4)) %>%
  filter(complete.cases(.))
mod <- trending::glm_nb_model(flu.admits ~ lag_1 + lag_2 + lag_3 + lag_4 + mobility)
fit <- fit(mod, data = tmp_weekly_flu_lags)
fit %>% get_model() %>% summary()

Result:

> fit %>% get_model() %>% summary()

Call:
MASS::glm.nb(formula = flu.admits ~ lag_1 + lag_2 + lag_3 + lag_4 + 
    mobility, data = data, init.theta = 221.7923432, link = log)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.5578  -0.8214  -0.1208   0.5020   1.9319  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)
(Intercept)  5.169e+00  3.690e-01  14.007  < 2e-16
lag_1        1.778e-03  5.610e-04   3.169  0.00153
lag_2        7.247e-04  6.033e-04   1.201  0.22968
lag_3       -4.432e-04  5.672e-04  -0.781  0.43457
lag_4       -4.090e-04  4.216e-04  -0.970  0.33203
mobility     4.295e-07  1.365e-06   0.315  0.75304

(Dispersion parameter for Negative Binomial(221.7923) family taken to be 1)

    Null deviance: 77.004  on 28  degrees of freedom
Residual deviance: 28.768  on 23  degrees of freedom
AIC: 290.17

Number of Fisher Scoring iterations: 1

              Theta:  221.8 
          Std. Err.:  96.9 

 2 x log-likelihood:  -276.169 

1 week lag looks to be useful but not much else. Certainly not the apple maps mobility data at least.

stephenturner commented 2 years ago

Maybe next week we can dive into looking at putting vax data into the model.

I'm also curious about whether there's any informative relationship between covid cases (lagged) and incident flu hosp.

stephenturner commented 2 years ago

closed via signaturescience/fluforce-init#6 but we could reopen as needed. one thing that wasn't clear was use restrictions. apple data could be used for covid public health, but made no mention of flu.

stephenturner commented 2 years ago

FWIW, I dug through documentation and clicked down a rabbit hole to find a contact form for Apple on their terms of service, privacy, etc., and send them a query about using this data for flu, not just C19.

If we ever hear anything back from them (or maybe even if we don't 🙈 ), there are some really useful functions in https://github.com/kjhealy/covmobility/blob/1b982c3e5615363f6492db05971d81cb9390f867/data-raw/DATASET.R#L63-L96 for scraping this data directly from apple. Full documentation on the data pkg at https://kjhealy.github.io/covmobility/

stephenturner commented 2 years ago

Transferred issue from fluforce-init for completeness here, but keeping closed for now.