# library(shiny)
library(mapapp)
# library(ggplot2)
# library(dplyr)

Welcome to the mapapp vignette. If you don't have mapapp yet, you can download it from GitHub.

# install.packages("devtools")
devtools::install_github('cwcomiskey/mapapp')

This version of mapapp provides two functions: heatmapper() to create heat maps with the R package ggplot, and shinyHMCI() to create interactive heat map confidence intervals (HMCIs) with the R package Shiny. Take a look!

shinyHMCI(peralta)

This particular HCMI corresponds to estimates of success probability for baseball player Jhonny Peralta on swings at locations in vertical face of the hitting zone.

Interactive heat map confidence intervals allow you to move through the confidence levels to explore the estimated surface in a way you can't acheive with static graphics.

Shiny and ggplot2 create graphics with unlimited variety, but heatmapper() and shinyHMCI() offer an expedient shortcut if you want to quickly create heat maps and launch interactive HMCIs. The key to using mapapp is data formatting.

Formatting for shinyHMCI()

Heat maps work by mapping a statistic of interest to a color at a set of (x,y) domain locations; call the number of locations $n$. The mapapp dataset peralta uses $n =$ r nrow(peralta[[1]]) locations arranged in a evenly spaced grid. shinyHMCI() needs data for the point estimate map, and for every CI level---1\% through 99\%. Accordingly, shinyHMCI() requires a list with 100 elements, each a data frame with $n$ rows.

There are four primary steps to format your data for shinyHMCI(). You'll generally start with a fitted model that estimates a parameter of interest over a spatial domain. To explore the confidence interval surfaces in shinyHMCI() you'll need to construct the required point estimates and confidence intervals for a set of points (usually a grid) from this model and construct the list to hold them.

Step 1: Calculate Point Estimates

As an example I've included the data frame peralta_ests in mapapp:

head(peralta_ests)

In peralta_ests the columns px and pz are equispaced locations on a spatial grid (the vertical face of the hitting zone). phat represents a point estimate of the parameter of interest (the success probability of a swing at this location). This particular set of point estimates was constructed with a call to predict() from a model fit with glm().

This data frame of point estimates is ideal for the first element in the required list, but must have the required names x, y and stat.

point_estimates <- dplyr::select(peralta_ests, 
  x = px, y = pz, stat = phat)

Now we need to a data frame with the upper and lower bounds, for each of the 99 levels of confidence.

Step 2: Calculate Confidence Bounds

You can calculate the bounds however you wish, as long as each list element is a data frame of two columns named lb and ub and the row order matches that in point_estimates. I used a loop function to calculate my bounds, with the following basic structure:

  1. Define index i = 1:99
  2. For an i = 1\% CI, calculate the lower bounds (lb) and upper bounds (ub) for all $n$ points in your domain.
  3. Create $n \times 2$ data frame $[[\text{lb}, \text{ub}]]$, and save it as element i + 1 of confidence_intervals.
  4. Repeat for i = 2:99, until confidence_intervals is a 99 element list.

In code, this loop algorithm might look like this.

  for(i in 1:99){
    confidence_intervals[[i]] <- data.frame(
      lb = calculate_lower(i, other_arguments), 
      ub = calculate_upper(i, other_arguments)
      )
  }

calculate_lower and calculate_upper will be the method or function you use to calculate the lower and upper bounds for each confidence level.

As an example of the confidence_intervals object, take a look at the peralta_cis object provided with mapapp:

confidence_intervals <- peralta_cis
str(confidence_intervals, max.level = 1)

Now we just need to put them together.

Step 3: Construct list

To construct the list required for shinyHCMI() you simply combine the point estimates and list of confidence intervals:

ests_and_cis <- c(list(point_estimates), 
  confidence_intervals)

You might notice ests_and_cis is exactly the same as the object peralta from earlier.

Step 4: Create Interactive App

With your data in this structure, shinyHMCI() will create an interactive HMCI.

shinyHMCI(ests_and_cis)

Future Improvments

A future version of mapapp will offer a function similar to the following:



cwcomiskey/mapapp documentation built on May 9, 2019, 12:47 p.m.