Skip to main content

Change detection in prescribing data

Detects changes in time series with a python wrapper around the R package gets (https://cran.r-project.org/web/packages/gets/index.html). Uses a combination of Google BigQuery and Python to query data, which is then fed to the R change detection code. Outputs a table containing results.

Installation

pip install change_detection

Anaconda users may have to conda install rpy2 and conda install geopandas if not already installed.

Usage

See https://github.com/ebmdatalab/change_detection/blob/master/examples/examples.ipynb for examples of use.

Data flow

  1. Get data, by:
    • using a csv in data/<name>, which must have only the fields code, month, numerator and denominator
    • creating a BigQuery SQL query in the same folder as the notebook that you're using, query must produce a table with only the fields code, month, numerator and denominator
    • querying any number of the OpenPrescribing measures in BigQuery
  2. Reshapes data with Pandas
  3. Splits data into chunks and passes each chunk to the R change detection code
  4. The resulting output is then extracted with further R code
  5. The R outputs are then concatenated

Options

  • name specifies either the name of the custom SQL file, or the name of the BigQuery measure to be queried
  • verbose makes the R output more verbose to help with bug fixing default = False
  • sample for testing purposes, takes a random sample of 100 entities, to reduce processing time default = False
  • measure specifies that the name specified refers to a measure, rather than custom SQL default = False
  • direction specifies which direction to look for changes, may be 'up', 'down', or 'both', default = 'both'
  • use_cache passes the use_cache option to bq.cached_read default = True
  • csv_name to specify a .csv file to be used in the change detection, rather than getting the data from BigQuery
  • overwrite forces reprocessing of the change detection, default behaviour is to not re-run if the output files exist default = False
  • draw_figures draw an R plot for each of the time-series, along with plotting regression lines/breaks. These are stored in the figures folder. Options are 'no' or 'yes' default = 'no'

Output table

Timing Measures

is.tfirst First negative break is.tfirst.pknown First negative break after a known intervention date is.tfirst.pknown.offs First negative break after a known intervention date not offset by a XX% increase is.tfirst.offs First negative break not offset by a XX% increase is.tfirst.big Steepest break as identified by is.slope.ma

Slope Measures

is.slope.ma Average slope over steepest segment contributing at least XX% of total drop is.slope.ma.prop Average slope as proportion to prior level is.slope.ma.prop.lev Percentage of the total drop the segment used to evaluate the slope makes up

Level Measures

is.intlev.initlev Pre-drop level is.intlev.finallev End level is.intlev.levd Difference between pre and end level is.intlev.levdprop Proportion of drop

Requirements

Python with an associated install of R. Python dependencies should be dealt with on installation (though for my install, I had to install rpy2 separately. R packages should be installed with the package is first loaded.

Python installation requires:

R installation requires:

  • zoo
  • caTools
  • gets

Metadata

Release files for change_detection 0.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for change_detection 0.3.5
File Interpreter ABI Platform
change_detection-0.3.5-py2.py3-none-any.whl Python 3, Python 2 none any Details

Release files / change_detection-0.3.5-py2.py3-none-any.whl

Download URL change_detection-0.3.5-py2.py3-none-any.whl
Size 13.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e98002e2ea809993f607b338c73097d8495aa9ac03de4ff0a6c8b6d24936937d
BLAKE2b-256 checksum
How to use checksums
775896cfcc6f22266be5fd70d3116f5eb455bc49eed0900b28bc5fe3a2b76abf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.22.0

Release history Release notifications | RSS feed

This release

0.3.5 This release

1 release file

0.3.4

1 release file

0.3.3

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page