# Examples in R

## Introduction

Here I show how to produce P-value, S-value, likelihood, and deviance functions with the concurve package using fake data and data from real studies. Simply put, these functions are rich sources of information for scientific inference and the image below, taken from Xie & Singh, 20131 displays why. For a more extensive discussion of these concepts, see the following references.113

To get started, we could generate some normal data and combine two vectors in a dataframe

library(concurve)
set.seed(1031)
GroupA <- rnorm(500)
GroupB <- rnorm(500)
RandomData <- data.frame(GroupA, GroupB)

and look at the differences between the two vectors. We’ll plug these vectors and the dataframe they’re in inside of the curve_mean() function. Here, the default method involves calculating CIs using the Wald method.

intervalsdf <- curve_mean(GroupA, GroupB,
data = RandomData, method = "default"
)

Each of the functions within concurve will generally produce a list with three items, and the first will usually contain the function of interest.

tibble::tibble(intervalsdf[])
#> # A tibble: 10,000 x 1
#>    intervalsdf[[1… $upper.limit$intrvl.width $intrvl.level$cdf $pvalue #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 -0.113 -0.113 0 0 0.5 1 #> 2 -0.113 -0.113 0.0000154 0.0001 0.500 1.000 #> 3 -0.113 -0.113 0.0000309 0.0002 0.500 1.000 #> 4 -0.113 -0.113 0.0000463 0.000300 0.500 1.000 #> 5 -0.113 -0.113 0.0000617 0.0004 0.500 1.000 #> 6 -0.113 -0.113 0.0000772 0.0005 0.500 1.000 #> 7 -0.113 -0.113 0.0000926 0.000600 0.500 0.999 #> 8 -0.113 -0.113 0.000108 0.0007 0.500 0.999 #> 9 -0.113 -0.112 0.000123 0.0008 0.500 0.999 #> 10 -0.113 -0.112 0.000139 0.0009 0.500 0.999 #> # … with 9,990 more rows, and 1 more variable:$svalue <dbl>

We can view the function using the ggcurve() function. The two basic arguments that must be provided are the data argument and the “type” argument. To plot a consonance function, we would write “c”.

(function1 <- ggcurve(data = intervalsdf[], type = "c")) We can see that the consonance “curve” is every interval estimate plotted, and provides the P-values, CIs, along with the median unbiased estimate It can be defined as such,

$C V_{n}(\theta)=1-2\left|H_{n}(\theta)-0.5\right|=2 \min \left\{H_{n}(\theta), 1-H_{n}(\theta)\right\}$

Its information counterpart, the surprisal function, can be constructed by taking the $$-log_{2}$$ of the P-value.3,14,15

To view the surprisal function, we simply change the type to “s”.

(function1 <- ggcurve(data = intervalsdf[], type = "s")) We can also view the consonance distribution by changing the type to “cdf”, which is a cumulative probability distribution. The point at which the curve reaches 50% is known as the “median unbiased estimate”. It is the same estimate that is typically at the peak of the P-value curve from above.

(function1s <- ggcurve(data = intervalsdf[], type = "cdf")) We can also get relevant statistics that show the range of values by using the curve_table() function. There are several formats that can be exported such as .docx, .ppt, and TeX.

(x <- curve_table(data = intervalsdf[], format = "image"))
 Lower Limit Upper Limit Interval Width Interval Level (%) CDF P-value S-value (bits) -0.132 -0.093 0.039 25.0 0.625 0.750 0.415 -0.154 -0.071 0.083 50.0 0.750 0.500 1.000 -0.183 -0.042 0.142 75.0 0.875 0.250 2.000 -0.192 -0.034 0.158 80.0 0.900 0.200 2.322 -0.201 -0.024 0.177 85.0 0.925 0.150 2.737 -0.214 -0.011 0.203 90.0 0.950 0.100 3.322 -0.233 0.008 0.242 95.0 0.975 0.050 4.322 -0.251 0.026 0.276 97.5 0.988 0.025 5.322 -0.271 0.046 0.318 99.0 0.995 0.010 6.644

# Comparing Functions

If we wanted to compare two studies to see the amount of “consonance”, we could use the curve_compare() function to get a numerical output.

First, we generate some more fake data

GroupA2 <- rnorm(500)
GroupB2 <- rnorm(500)
RandomData2 <- data.frame(GroupA2, GroupB2)
model <- lm(GroupA2 ~ GroupB2, data = RandomData2)
randomframe <- curve_gen(model, "GroupB2")

Once again, we’ll plot this data with ggcurve(). We can also indicate whether we want certain interval estimates to be plotted in the function with the “levels” argument. If we wanted to plot the 50%, 75%, and 95% intervals, we’d provide the argument this way:

(function2 <- ggcurve(type = "c", randomframe[], levels = c(0.50, 0.75, 0.95), nullvalue = TRUE)) Now that we have two datasets and two functions, we can compare them using the curve_compare() function.

(curve_compare(
data1 = intervalsdf[], data2 = randomframe[], type = "c",
plot = TRUE, measure = "default", nullvalue = TRUE
))
#>  "AUC = Area Under the Curve"
#> []
#>
#>
#>  AUC 1   AUC 2   Shared AUC   AUC Overlap (%)   Overlap:Non-Overlap AUC Ratio
#> ------  ------  -----------  ----------------  ------------------------------
#>  0.098   0.073        0.024             0.163                           0.195
#>
#> [] This function will provide us with the area that is shared between the curve, along with a ratio of overlap to non-overlap.

We can also do this for the surprisal function simply by changing type to “s”.

(curve_compare(
data1 = intervalsdf[], data2 = randomframe[], type = "s",
plot = TRUE, measure = "default", nullvalue = FALSE
))
#>  "AUC = Area Under the Curve"
#> []
#>
#>
#>  AUC 1   AUC 2   Shared AUC   AUC Overlap (%)   Overlap:Non-Overlap AUC Ratio
#> ------  ------  -----------  ----------------  ------------------------------
#>  3.947   1.531        1.531             0.388                           0.634
#>
#> [] It’s clear that the outputs have changed and indicate far more overlap than before.

# Constructing Functions From Single Intervals

We can also take a set of confidence limits and use them to construct a consonance, surprisal, likelihood or deviance function using the curve_rev() function.

Here, we’ll use two epidemiological studies16,17 that studied the impact of SSRI exposure in pregnant mothers, and the rate of autism in children.

Both of these studies suggested a null effect of SSRI exposure on autism rates in children.

curve1 <- curve_rev(point = 1.7, LL = 1.1, UL = 2.6, type = "c", measure = "ratio", steps = 10000)
(ggcurve(data = curve1[], type = "c", measure = "ratio", nullvalue = TRUE)) curve2 <- curve_rev(point = 1.61, LL = 0.997, UL = 2.59,type = "c", measure = "ratio", steps = 10000)
(ggcurve(data = curve2[], type = "c", measure = "ratio", nullvalue = TRUE)) The null value is shown via the red line and it’s clear that a large mass of the function is away from it.

We can also see this by plotting the likelihood functions via the curve_rev() function.

lik1 <- curve_rev(point = 1.7, LL = 1.1, UL = 2.6, type = "l", measure = "ratio", steps = 10000)
(ggcurve(data = lik1[], type = "l1", measure = "ratio", nullvalue = TRUE)) lik2 <- curve_rev(point = 1.61, LL = 0.997, UL = 2.59,type = "l", measure = "ratio", steps = 10000)
(ggcurve(data = lik2[], type = "l1", measure = "ratio", nullvalue = TRUE)) We can also view the amount of agreement between the likelihood functions of these two studies.

(plot_compare(
data1 = lik1[], data2 = lik2[], type = "l1", measure = "ratio", nullvalue = TRUE, title = "Brown et al. 2017. J Clin Psychiatry. vs. \nBrown et al. 2017. JAMA.",
subtitle = "J Clin Psychiatry: OR = 1.7, 1/6.83 LI: LL = 1.1, UL = 2.6 \nJAMA: HR = 1.61, 1/6.83 LI: LL = 0.997, UL = 2.59", xaxis = expression(Theta ~ "= Hazard Ratio / Odds Ratio")
)) and the consonance functions

(plot_compare(
data1 = curve1[], data2 = curve2[], type = "c", measure = "ratio", nullvalue = TRUE, title = "Brown et al. 2017. J Clin Psychiatry. vs. \nBrown et al. 2017. JAMA.",
subtitle = "J Clin Psychiatry: OR = 1.7, 1/6.83 LI: LL = 1.1, UL = 2.6 \nJAMA: HR = 1.61, 1/6.83 LI: LL = 0.997, UL = 2.59", xaxis = expression(Theta ~ "= Hazard Ratio / Odds Ratio")
)) # The Bootstrap and Consonance Functions

Some authors have shown that the bootstrap distribution is equal to the confidence distribution because it meets the definition of a consonance distribution.1,18,19 The bootstrap distribution and the asymptotic consonance distribution would be defined as:

$H_{n}(\theta)=1-P\left(\hat{\theta}-\hat{\theta}^{*} \leq \hat{\theta}-\theta | \mathbf{x}\right)=P\left(\hat{\theta}^{*} \leq \theta | \mathbf{x}\right)$

Certain bootstrap methods such as the BCa method and t-bootstrap method also yield second order accuracy of consonance distributions.

$H_{n}(\theta)=1-P\left(\frac{\hat{\theta}^{*}-\hat{\theta}}{\widehat{S E}^{*}\left(\hat{\theta}^{*}\right)} \leq \frac{\hat{\theta}-\theta}{\widehat{S E}(\hat{\theta})} | \mathbf{x}\right)$

Here, I demonstrate how to use these particular bootstrap methods to arrive at consonance curves and densities.

We’ll use the Iris dataset and construct a function that’ll yield a parameter of interest.

iris <- datasets::iris
foo <- function(data, indices) {
dt <- data[indices, ]
c(
cor(dt[, 1], dt[, 2], method = "p")
)
}

We can now use the curve_boot() method to construct a function. The default method used for this function is the “BCa” method provided by the bcaboot package.19

I will suppress the output of the function because it is unnecessarily long. But we’ve placed all the estimates into a list object called y.

The first item in the list will be the consonance distribution constructed by typical means, while the third item will be the bootstrap approximation to the consonance distribution.

ggcurve(data = y[])`