# pif.heatmap: Graphical Sensitivity Analysis of Potential Impact Fraction's... In pifpaf: Potential Impact Fraction and Population Attributable Fraction for Cross-Sectional Data

## Description

Provides a graphical sensitivity analysis for `pif` by varying the parameters of a bivariate counterfactual function `cft`. By default it evaluates the counterfactual:

cft(X) = aX + b.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10``` ```pif.heatmap(X, thetahat, rr, cft = function(X, a, b) { a * X + b }, method = "empirical", weights = rep(1/nrow(as.matrix(X)), nrow(as.matrix(X))), Xvar = var(X), deriv.method.args = list(), deriv.method = "Richardson", adjust = 1, n = 512, ktype = "gaussian", bw = "SJ", legendtitle = "PIF", mina = 0, maxa = 1, minb = -1, maxb = 0, nmesh = 10, title = paste0("Potential Impact Fraction (PIF) with counterfactual", "\nf(X)= aX+b"), xlab = "a", ylab = "b", colors = rev(heat.colors(nmesh)), check_exposure = TRUE, check_rr = TRUE, check_integrals = TRUE) ```

## Arguments

 `X` Random sample (`data.frame`) which includes exposure and covariates or sample `mean` if `"approximate"` method is selected. `thetahat` Asymptotically consistent or Fisher consistent estimator (`vector`) of `theta` for the Relative Risk function. `rr` `function` for Relative Risk which uses parameter `theta`. The order of the parameters shound be `rr(X, theta)`. **Optional** `cft` `function` `cft(X, a, b)` for counterfactual dependent on one dimensional parameters `a` and `b`. Default counterfactual is affine: `aX + b`. `method` Either `"empirical"` (default), `"kernel"` or `"approximate"`. For details on estimation methods see `pif`. `weights` Normalized survey `weights` for the sample `X`. `Xvar` Variance of exposure levels (for `"approximate"` method). `deriv.method.args` `method.args` for `hessian` (for `"approximate"` method). `deriv.method` `method` for `hessian`. Don't change this unless you know what you are doing (for `"approximate"` method). `adjust` Adjust bandwith parameter (for `"kernel"` method) from `density`. `n` Number of equally spaced points at which the density (for `"kernel"` method) is to be estimated (see `density`). `ktype` `kernel` type: `"gaussian"`, `"epanechnikov"`, `"rectangular"`, `"triangular"`, `"biweight"`, `"cosine"`, `"optcosine"` (for `"kernel"` method). Additional information on kernels in `density`. `bw` Smoothing bandwith parameter (for `"kernel"` method) from `density`. Default `"SJ"`. `legendtitle` `string` title for the legend of plot. `mina` Minimum for parameter `a` for the counterfactual (default `0`). `maxa` Maximum for parameter `a` for the counterfactual (default `1`). `minb` Minimum for parameter `b` for the counterfactual (default `-1`). `maxb` Maximum for parameter `b` for the counterfactual (default `0`). `nmesh` Number of tiles in plot (default `10`). `title` `string` title for the plot. `xlab` `string` label for the X-axis of the plot (corresponding to "a"). `ylab` `string` label for the Y-axis of the plot (corresponding to "b"). `colors` `vector` of colours for the heatmap. `check_exposure` `boolean` Check that exposure `X` is positive and numeric. `check_rr` `boolean` Check that Relative Risk function `rr` equals `1` when evaluated at `0`. `check_integrals` `boolean` Check that counterfactual `cft` and relative risk's `rr` expected values are well defined for this scenario.

## Value

plotpif `ggplot` object plotting a heatmap with sensitivity analysis of the counterfactual.

## Author(s)

Rodrigo Zepeda-Tello rzepeda17@gmail.com

See `pif` for Potential Impact Fraction estimation, `pif.sensitivity` for sensitivity analysis of the convergence process, `pif.plot` for a plot of Potential Impact Fraction as a function of the relative risk's parameter `theta`.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98``` ```## Not run: #Example 1 #------------------------------------------------------------------ X <- data.frame(rnorm(25,3)) #Sample rr <- function(X,theta){exp(X*theta)} #Relative risk theta <- 0.01 #Estimate of theta pif.heatmap(X, theta = theta, rr = rr) #Save file using ggplot2 #require(ggplot2) #ggsave("My Potential Impact Fraction Heatmap Analysis.pdf") #Change pif estimation method to kernel pif.heatmap(X, theta = theta, rr = rr, method = "kernel") #Example 2 #------------------------------------------------------------------ X <- data.frame(Exposure = rbeta(100, 1, 0.2)) theta <- c(0.12, 1) rr <- function(X,theta){X*theta[1] + theta[2]} cft <- function(X, a, b){sin(a*X)*b} pif.heatmap(X, theta = theta, rr = rr, cft = cft, nmesh = 15, colors = rainbow(30), method = "empirical", title = "PIF with counterfactual cft(X) = sin(a*X)*b") #Change estimation method to approximate Xmean <- data.frame(mean(X[,"Exposure"])) Xvar <- var(X) pif.heatmap(Xmean, Xvar = Xvar, theta = theta, rr = rr, cft = cft, nmesh = 15, colors = rainbow(30), method = "approximate", title = "PIF with counterfactual cft(X) = sin(a*X)*b") #Example 3: Plot univariate counterfactuals #------------------------------------------------------------------ X <- data.frame(rgamma(100, 1, 0.2)) theta <- c(0.12, 1) rr <- function(X,theta){X*theta[1] + theta[2]} cft <- function(X, a, b){sqrt(a*X)} #Leave two variables in it pifplot <- pif.heatmap(X, theta = theta, rr = rr, cft = cft, mina = 0, maxa = 1, minb = 0, maxb = 0, legendtitle = "Potential Impact Fraction", title ="Univariate counterfactual", ylab = "", colors = topo.colors(10)) pifplot #You can also add additional ggplot objects #require(ggplot2) #pifplot + annotate("text", x = 0.25, y = 0.4, label = "10yr scenario") + #geom_vline(aes(xintercept = 0.5), linetype = "dashed") + #geom_segment(aes(x = 0.25, y = 0.38, xend = 0.5, yend = 0.3), #arrow = arrow(length = unit(0.25, "cm"))) #Example 4: Plot counterfactual with categorical risks #------------------------------------------------------------------ set.seed(18427) X <- data.frame(Exposure = sample(c("Normal","Overweight","Obese"), 100, replace = TRUE, prob = c(0.4, 0.1, 0.5))) thetahat <- c(1, 1.7, 2) #Categorical relative risk function rr <- function(X, theta){ #Create return vector with default risk of 1 r_risk <- rep(1, nrow(X)) #Assign categorical relative risk r_risk[which(X[,"Exposure"] == "Normal")] <- thetahat[1] r_risk[which(X[,"Exposure"] == "Overweight")] <- thetahat[2] r_risk[which(X[,"Exposure"] == "Obese")] <- thetahat[3] return(r_risk) } #Counterfactual of reducing a certain percent of obesity and overweight cases #to normality cft <- function(X, per.over, per.obese){ #Find the overweight and obese individuals which_obese <- which(X[,"Exposure"] == "Obese") which_over <- which(X[,"Exposure"] == "Overweight") #Reduce per.over % of overweight and per.obese % of obese X[sample(which_obese, length(which_obese)*per.obese), "Exposure"] <- "Normal" X[sample(which_over, length(which_over)*per.over), "Exposure"] <- "Normal" return(X) } pif.heatmap(X, thetahat = thetahat, rr = rr, cft = cft, mina = 0, minb = 0, maxa = 1, maxb = 1, title = "PIF of excess-weight reduction", xlab = "% Overweight cases", ylab = "% Obese cases", check_exposure = FALSE, check_rr = FALSE) ## End(Not run) ```