# 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.
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.
x
- horizontal spatial coordinatey
- vertical spatial coordinatestat
- point estimate for each $(x,y)$ locationThe remaining i = 1:99 data frames each represent the upper and lower surfaces of a confidence interval. They must include two variables:
lb
- lower bound for the i$\%$ CI.ub
- upper bound for the i$\%$ CIAnd each row must correspond to the same locations as in the first data frame.
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.
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.
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:
confidence_intervals
. 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.
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.
With your data in this structure, shinyHMCI()
will create an interactive HMCI.
shinyHMCI(ests_and_cis)
A future version of mapapp will offer a function similar to the following:
all_in_one <- get_CI(model, x, y, levels)
- a function that creates, given the output from a modelling function, model
, the properly formatted data structure, at the locations and levels the user specifies, so that shinyHMCI(all_in_one)
creates a interactive HMCI in Shiny.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.