Movatterモバイル変換


[0]ホーム

URL:


covidcast0.5.2

Get started with covidcast

Source:vignettes/covidcast.Rmd
covidcast.Rmd

This package provides access to data frames of values from theCOVIDcastendpoint of the Epidata API. Using thecovidcast_signal() function, you can fetch any data you maybe interested in analyzing, then useplot.covidcast_signal() to make plots and maps. Since thedata is provided as a simple data frame, you can also wrangle it intowhatever form you need to conduct your desired analyses using otherpackages and functions.

Installing

This package isavailable onCRAN, so the easiest way to install it is simply

install.packages("covidcast")

Basic examples

To obtain smoothed estimates of COVID-like illness from ourCOVID-19 Trends andImpact Survey for every county in the United States between2020-05-01 and 2020-05-07, we can usecovidcast_signal():

library(covidcast)library(dplyr)cli<-covidcast_signal(data_source="fb-survey", signal="smoothed_wcli",                        start_day="2020-05-01", end_day="2020-05-07",                        geo_type="county")knitr::kable(head(cli))
data_sourcesignalgeo_valuetime_valuesourcegeo_typetime_typeissuelagmissing_valuemissing_stderrmissing_sample_sizevaluestderrsample_size
fb-surveysmoothed_wcli010002020-05-01fb-surveycountyday2020-09-031250000.82606250.13413811676.2773
fb-surveysmoothed_wcli010012020-05-01fb-surveycountyday2020-09-031250001.07077900.8213119109.0866
fb-surveysmoothed_wcli010032020-05-01fb-surveycountyday2020-09-031250000.50816440.2800777572.3194
fb-surveysmoothed_wcli010152020-05-01fb-surveycountyday2020-09-031250000.52776090.5192431118.8275
fb-surveysmoothed_wcli010312020-05-01fb-surveycountyday2020-09-031250000.37338110.3367309112.2687
fb-surveysmoothed_wcli010452020-05-01fb-surveycountyday2020-09-031250001.23695420.6464530108.5803

covidcast_signal() returns a data frame. (Here we’reusingknitr::kable() to make it more readable.) Each rowrepresents one observation in one county on one day. The county FIPScode is given in thegeo_value column, the date in thetime_value column. Herevalue is the requestedsignal—in this case, the smoothed estimate of the percentage of peoplewith COVID-like illness, based on the symptom surveys, andstderr is its standard error. See thecovidcast_signal() documentation for details on thereturned data frame.

To get a basic summary of the returned data frame:

summary(cli)
A `covidcast_signal` dataframe with 7030 rows and 15 columns.data_source : fb-surveysignal      : smoothed_wcligeo_type    : countyfirst date                          : 2020-05-01last date                           : 2020-05-07median number of geo_values per day : 1015

The COVIDcast API makes estimates available at several differentgeographic levels, andcovidcast_signal() defaults torequesting county-level data. To request estimates for states instead ofcounties, we use thegeo_type argument:

cli<-covidcast_signal(data_source="fb-survey", signal="smoothed_wcli",                        start_day="2020-05-01", end_day="2020-05-07",                        geo_type="state")knitr::kable(head(cli))
data_sourcesignalgeo_valuetime_valuesourcegeo_typetime_typeissuelagmissing_valuemissing_stderrmissing_sample_sizevaluestderrsample_size
fb-surveysmoothed_wcliak2020-05-01fb-surveystateday2020-09-031250000.36619090.14699181560.000
fb-surveysmoothed_wclial2020-05-01fb-surveystateday2020-09-031250000.77640200.10109897360.237
fb-surveysmoothed_wcliar2020-05-01fb-surveystateday2020-09-031250000.70655840.10515844781.483
fb-surveysmoothed_wcliaz2020-05-01fb-surveystateday2020-09-031250000.60258530.072421410973.073
fb-surveysmoothed_wclica2020-05-01fb-surveystateday2020-09-031250000.41390450.030633650482.138
fb-surveysmoothed_wclico2020-05-01fb-surveystateday2020-09-031250000.59847940.07173959888.894

One can also select a specific geographic region by its ID. Forexample, this is the FIPS code for Allegheny County, Pennsylvania:

cli<-covidcast_signal(data_source="fb-survey", signal="smoothed_wcli",                        start_day="2020-05-01", end_day="2020-05-07",                        geo_type="county", geo_value="42003")knitr::kable(head(cli))
data_sourcesignalgeo_valuetime_valuesourcegeo_typetime_typeissuelagmissing_valuemissing_stderrmissing_sample_sizevaluestderrsample_size
fb-surveysmoothed_wcli420032020-05-01fb-surveycountyday2020-09-031250000.62705200.25113772554.564
fb-surveysmoothed_wcli420032020-05-02fb-surveycountyday2020-09-031240000.64534980.25990372509.176
fb-surveysmoothed_wcli420032020-05-03fb-surveycountyday2020-09-031230000.55230670.24976622473.456
fb-surveysmoothed_wcli420032020-05-04fb-surveycountyday2020-09-031220000.14307720.08046422493.730
fb-surveysmoothed_wcli420032020-05-05fb-surveycountyday2020-09-031210000.18618890.09609072415.204
fb-surveysmoothed_wcli420032020-05-06fb-surveycountyday2020-09-031200000.31241500.12181942465.422

API keys

By default, this package submits queries to the API anonymously. Allthe examples in the package documentation are compatible with anonymoususe of the API, butthereare some limits on anonymous queries, including rate limits on thenumber of queries that can be submitted per hour. To lift these limits,see the “API keys” section of thecovidcast_signal()documentation for information on how to register for and use an APIkey.

Plotting and mapping

This package provides convenient functions for plotting and mappingthese signals. For example, simple line charts are easy toconstruct:

plot(cli, plot_type="line",     title="Survey results in Allegheny County, PA")

For more details and examples, including choropleth and bubble maps,seevignette("plotting-signals").

Finding signals of interest

Above we used data fromDelphi’s symptomsurveys, but the COVIDcast API includes numerous data streams:medical claims data, cases and deaths, mobility, and many others; newsignals are added regularly. This can make it a challenge to find thedata stream that you are most interested in.

TheCOVIDcastData Sources and Signals documentation lists all data sources andsignals available through COVIDcast. When you find a signal of interest,get the data source name (such asjhu-csse orfb-survey) and the signal name (such asconfirmed_incidence_num orsmoothed_wcli).These are provided as arguments tocovidcast_signal() torequest the data you want.

Finding counties and metro areas

The COVIDcast API identifies counties by their 5-digit FIPS code andmetropolitan areas by their CBSA ID number. (See thegeographiccoding documentation for details.) This means that to query aspecific county or metropolitan area, we must have some way to quicklyfind its identifier.

This package includes several utilities intended to make the processeasier. For example, if we look at?county_census, we findthat the package provides census data (such as population) on everycounty in the United States, including its FIPS code. Similarly, bylooking at?msa_census we can find data about metropolitanstatistical areas, their corresponding CBSA IDs, and recent censusdata.

(Note: themsa_census data includes types of area beyondmetropolitan statistical areas, including micropolitan statisticalareas. TheLSAD column identifies the type of each area.The COVIDcast API only provides estimates for metropolitan statisticalareas, not for their divisions or for micropolitan areas.)

Building on these datasets, the convenience functionsname_to_fips() andname_to_cbsa() conductgrep()-based searching of county or metropolitan area namesto find FIPS or CBSA codes, respectively:

name_to_fips("Allegheny")
Allegheny County          "42003"
name_to_cbsa("Pittsburgh")
Pittsburgh, PA        "38300"

Since these functions return vectors of IDs, we can use them toconstruct thegeo_values argument tocovidcast_signal() to select specific regions to query.

You may also want to convert FIPS codes or CBSA IDs back towell-known names, for instance to report in tables or graphics. Thepackage provides inverse mappingscounty_fips_to_name() andcbsa_to_name() that work in the analogous way:

42003"Allegheny County"
cbsa_to_name("38300")
38300"Pittsburgh, PA"

See their documentation for more details (for example, the optionsfor handling matches when counties have the same name).

Signal metadata

If we are interested in exploring the available signals and theirmetadata, we can usecovidcast_meta() to fetch a data frameof the available signals:

meta<-covidcast_meta()knitr::kable(head(meta))
data_sourcesignaltime_typegeo_typemin_timemax_timenum_locationsmin_valuemax_valuemean_valuestdev_valuelast_updatemax_issuemin_lagmax_lag
chngsmoothed_adj_outpatient_clidaycounty2020-02-012023-02-1431180.000933199.920122.2278523.84357916835669792023-02-193674
chngsmoothed_adj_outpatient_clidayhhs2020-02-012023-06-14100.006195320.775772.5301572.53130616872315822023-06-195674
chngsmoothed_adj_outpatient_clidayhrr2020-02-012023-06-143060.001029250.815902.3503552.76344216872315822023-06-195674
chngsmoothed_adj_outpatient_clidaymsa2020-02-012023-06-143920.000766299.998982.1532043.00024816872315832023-06-195674
chngsmoothed_adj_outpatient_clidaynation2020-02-012023-06-1410.015463912.086972.7782602.34410716872315832023-06-195674
chngsmoothed_adj_outpatient_clidaystate2020-02-012023-06-14550.001334333.238592.2642072.56388016872315832023-06-195674

Thecovidcast_meta() documentation describes the columnsand their meanings. The metadata data frame can be filtered and slicedas desired to obtain information about signals of interest. To get abasic summary of the metadata:

summary(meta)

(We silenced the evaluation because the output ofsummary() here is still quite long.)

Tracking issues and updates

The COVIDcast API records not just each signal’s estimate for a givenlocation on a given day, but alsowhen that estimate was made,and all updates to that estimate.

For example, consider using ourdoctorvisits signal, which estimates the percentage of outpatient doctorvisits that are COVID-related, and consider a result row withtime_value 2020-05-01 forgeo_values = "pa".This is an estimate for the percentage in Pennsylvania on May 1, 2020.That estimate wasissued on May 5, 2020, the delay being due tothe aggregation of data by our source and the time taken by theCOVIDcast API to ingest the data provided. Later, the estimate for May1st could be updated, perhaps because additional visit data from May 1starrived at our source and was reported to us. This constitutes a newissue of the data.

Data known “as of” a specific date

By default,covidcast_signal() fetches the most recentissue available. This is the best option for users who simply want tograph the latest data or construct dashboards. But if we are interestedin knowingwhen data was reported, we can request specific dataversions using theas_of,issues, orlag arguments. (Note these are mutually exclusive; only onecan be specified at a time.)

First, we can request the data that was availableas of aspecific date, using theas_of argument:

covidcast_signal(data_source="doctor-visits", signal="smoothed_adj_cli",                 start_day="2020-05-01", end_day="2020-05-01",                 geo_type="state", geo_values="pa", as_of="2020-05-07")
A `covidcast_signal` dataframe with 1 rows and 15 columns.data_source : doctor-visitssignal      : smoothed_adj_cligeo_type    : state    data_source           signal geo_value time_value        source geo_type1 doctor-visits smoothed_adj_cli        pa 2020-05-01 doctor-visits    state  time_type      issue lag missing_value missing_stderr missing_sample_size1       day 2020-05-07   6             0              5                   5     value stderr sample_size1 2.581509     NA          NA

This shows that an estimate of about 2.3% was issued on May 7. If wedon’t specifyas_of, we get the most recent estimateavailable:

covidcast_signal(data_source="doctor-visits", signal="smoothed_adj_cli",                 start_day="2020-05-01", end_day="2020-05-01",                 geo_type="state", geo_values="pa")
A `covidcast_signal` dataframe with 1 rows and 15 columns.data_source : doctor-visitssignal      : smoothed_adj_cligeo_type    : state    data_source           signal geo_value time_value        source geo_type1 doctor-visits smoothed_adj_cli        pa 2020-05-01 doctor-visits    state  time_type      issue lag missing_value missing_stderr missing_sample_size1       day 2020-07-04  64             0              5                   5     value stderr sample_size1 5.973572     NA          NA

Note the substantial change in the estimate, to over 5%, reflectingnew data that became availableafter May 7 about visitsoccurring on May 1. This illustrates the importance of issue datetracking, particularly for forecasting tasks. To backtest a forecastingmodel on past data, it is important to use the data that would have beenavailableat the time, not data that arrived much later.

Multiple issues of observations

By using theissues argument, we can request all issuesin a certain time period:

covidcast_signal(data_source="doctor-visits", signal="smoothed_adj_cli",                 start_day="2020-05-01", end_day="2020-05-01",                 geo_type="state", geo_values="pa",                 issues=c("2020-05-01","2020-05-15"))%>%knitr::kable()
data_sourcesignalgeo_valuetime_valuesourcegeo_typetime_typeissuelagmissing_valuemissing_stderrmissing_sample_sizevaluestderrsample_size
doctor-visitssmoothed_adj_clipa2020-05-01doctor-visitsstateday2020-05-0760552.581509NANA
doctor-visitssmoothed_adj_clipa2020-05-01doctor-visitsstateday2020-05-0870553.278896NANA
doctor-visitssmoothed_adj_clipa2020-05-01doctor-visitsstateday2020-05-0980553.321781NANA
doctor-visitssmoothed_adj_clipa2020-05-01doctor-visitsstateday2020-05-12110553.588683NANA
doctor-visitssmoothed_adj_clipa2020-05-01doctor-visitsstateday2020-05-13120553.631978NANA
doctor-visitssmoothed_adj_clipa2020-05-01doctor-visitsstateday2020-05-14130553.658009NANA
doctor-visitssmoothed_adj_clipa2020-05-01doctor-visitsstateday2020-05-15140553.662286NANA

This estimate was clearly updated many times as new data for May 1starrived. Note that these results include only data issued or updatedbetween 2020-05-01 and 2020-05-15. If a value was first reported on2020-04-15, and never updated, a query for issues between 2020-05-01 and2020-05-15 will not include that value among its results.

After fetching multiple issues of data, we can use thelatest_issue() orearliest_issue() functionsto subset the data frame to view only the latest or earliest issue ofeach observation.

Observations issued with a specific lag

Finally, we can use thelag argument to request onlydata reported with a certain lag. For example, requesting a lag of 7days means to request only issues 7 days after the correspondingtime_value:

covidcast_signal(data_source="doctor-visits", signal="smoothed_adj_cli",                 start_day="2020-05-01", end_day="2020-05-07",                 geo_type="state", geo_values="pa", lag=7)%>%knitr::kable()
Warning: Data not fetched for the following days: 2020-05-01
data_sourcesignalgeo_valuetime_valuesourcegeo_typetime_typeissuelagmissing_valuemissing_stderrmissing_sample_sizevaluestderrsample_size
doctor-visitssmoothed_adj_clipa2020-05-01doctor-visitsstateday2020-05-0870553.278896NANA
doctor-visitssmoothed_adj_clipa2020-05-02doctor-visitsstateday2020-05-0970553.225292NANA
doctor-visitssmoothed_adj_clipa2020-05-05doctor-visitsstateday2020-05-1270552.779908NANA
doctor-visitssmoothed_adj_clipa2020-05-06doctor-visitsstateday2020-05-1370552.557698NANA
doctor-visitssmoothed_adj_clipa2020-05-07doctor-visitsstateday2020-05-1470552.191677NANA

Note that though this query requested all values between 2020-05-01and 2020-05-07, May 3rd and May 4th werenot included in theresults set. This is because the query will only include a result forMay 3rd if a value were issued on May 10th (a 7-day lag), but in factthe value was not updated on that day:

covidcast_signal(data_source="doctor-visits", signal="smoothed_adj_cli",                 start_day="2020-05-03", end_day="2020-05-03",                 geo_type="state", geo_values="pa",                 issues=c("2020-05-09","2020-05-15"))%>%knitr::kable()
data_sourcesignalgeo_valuetime_valuesourcegeo_typetime_typeissuelagmissing_valuemissing_stderrmissing_sample_sizevaluestderrsample_size
doctor-visitssmoothed_adj_clipa2020-05-03doctor-visitsstateday2020-05-0960552.788618NANA
doctor-visitssmoothed_adj_clipa2020-05-03doctor-visitsstateday2020-05-1290553.015368NANA
doctor-visitssmoothed_adj_clipa2020-05-03doctor-visitsstateday2020-05-13100553.039310NANA
doctor-visitssmoothed_adj_clipa2020-05-03doctor-visitsstateday2020-05-14110553.021245NANA
doctor-visitssmoothed_adj_clipa2020-05-03doctor-visitsstateday2020-05-15120553.048725NANA

[8]ページ先頭

©2009-2025 Movatter.jp