An R interface to the PredictIt API

The rpredictit package provides an interface to the PredictIt public API.

In addition to providing a wrapper to retrieve market data, this package includes visualization functions for plotting historical price data and exploring available markets. The package also comes with a demo shiny application for illustrating example use cases.

rpredictit is not affiliated with any predictive markets and is presented for informational purposes only. Always confirm with your own research before making an investment. License to use data made available via the API is for non-commercial use and PredictIt is the sole source of such data.

Installation

Once released, you may install the stable version from CRAN, or the development version using devtools:

# development version, via devtools
devtools::install_github('danielkovtun/rpredictit')

Usage

Demo Shiny Application

To start off, try running a demo Shiny application included with the package by running:

library(rpredictit)
rpredictit::runExample('demo')  
All Markets

Try rpredictit::all_markets() to return a tibble containing bid and ask data for all PredictIt markets:

rpredictit::all_markets()
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> Warning: `as.tbl()` was deprecated in dplyr 1.0.0.
#> Please use `tibble::as_tibble()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
#> # A tibble: 664 × 20
#>       id name       shortName image url   timeStamp           status contract_id
#>    <int> <chr>      <chr>     <chr> <chr> <dttm>              <chr>        <int>
#>  1  6867 Which par… Which pa… http… http… 2022-07-18 07:00:04 Open         23546
#>  2  6867 Which par… Which pa… http… http… 2022-07-18 07:00:04 Open         23547
#>  3  6867 Which par… Which pa… http… http… 2022-07-18 07:00:04 Open         23545
#>  4  6867 Which par… Which pa… http… http… 2022-07-18 07:00:04 Open         23548
#>  5  6874 Which par… Who will… http… http… 2022-07-18 07:00:04 Open         23587
#>  6  6874 Which par… Who will… http… http… 2022-07-18 07:00:04 Open         23586
#>  7  6892 Which par… Which pa… http… http… 2022-07-18 07:00:04 Open         23690
#>  8  6892 Which par… Which pa… http… http… 2022-07-18 07:00:04 Open         23689
#>  9  6953 Will Kama… Harris f… http… http… 2022-07-18 07:00:04 Open         24205
#> 10  6975 Will Mike… Pence fi… http… http… 2022-07-18 07:00:04 Open         24361
#> # … with 654 more rows, and 12 more variables: dateEnd <chr>,
#> #   contract_image <chr>, contract_name <chr>, contract_shortName <chr>,
#> #   contract_status <chr>, lastTradePrice <dbl>, bestBuyYesCost <dbl>,
#> #   bestBuyNoCost <dbl>, bestSellYesCost <dbl>, bestSellNoCost <dbl>,
#> #   lastClosePrice <dbl>, displayOrder <int>
Interactive Table

Alternatively, to return an interactive htmlwidget (DT::datatable) table containing HTML formatted market data, pass the returned bid/ask data to rpredictit::markets_table():

data <- rpredictit::all_markets()
#> Warning in rpredictit::all_markets(): Error fetching PredictIt market data 
#> Check your internet connection and try again 
#> 
#> Note: do not abuse the API by requesting data more than once every 60 seconds
rpredictit::markets_table(data)
Interactive Plot

To plot historical prices, download a ‘csv’ file for a specific contract from PredictIt’s website and pass the file path to rpredictit::parse_historical_ohlcv(). Then, pass in the returned contract data object to rpredictit::historical_plot():

filename <- "What_will_be_the_balance_of_power_in_Congress_after_the_2020_election.csv"
csv_path <- system.file("extdata", filename, package = "rpredictit")
contract_data <- rpredictit::parse_historical_csv(csv_path)
rpredictit::historical_plot(contract_data)
Individual Market

To return data for a specific market, use rpredictit::single_market(id), where id refers to the numerical code pertaining to the market of interest. You can find a market’s numerical code by consulting its URL or by first calling the all markets API (all_markets())

markets <- rpredictit::all_markets()
id <- markets$id[1]
rpredictit::single_market(id)

See the full documentation at https://danielkovtun.github.io/rpredictit/.