CDFtoQuantiles
computes quantiles for a given CDF.
PDFtoQuantiles
computes the quantiles for given PDF values within
groups of other variables, if available.
Usage
PDFtoQuantiles(pdf_df, p = c(0.1, 0.3, 0.5, 0.7, 0.9), agg_over = NULL,
scaled = FALSE)
CDFtoQuantiles(cdf, x = NULL, p)
Arguments
- pdf_df
dataframe. Should have at least two columns:
rt
(for reaction times) orx
for the support values of the pdfdens
orpdf
for the pdf valuesAll other columns will be used as grouping factors, for which separate quantiles will be returned.
- p
numeric vector. Probabilities for returned quantiles. Default: c(.1, .3, .5, .7, .9).
- agg_over
character. Names of columns in
pdf_df
to aggregate over (using the mean of densities, which is valid only, if groups occur with equal probabilities) before computing the quantiles.- scaled
logical. Indicating whether the pdf values are from a proper probability distribution. Non-scaled pdfs will scaled to 1. If
scaled
is TRUE, this may cause problems with high probabilities. In any case we strongly recommend to cover the most probability mass with the values in the support vector.- cdf
numeric. A increasing vector of the same length as
x
giving the CDF for respective x-Values. Dataframe inputs are accepted. If a columnx
is available there, this will be used as support values.- x
numeric. A increasing vector of same length as
cdf
. Can also be specified as column ofcdf
.
Value
PDFtoQuantiles
returns a tibble
with columns p and q indicating
probabilities and respective quantiles. Furthermore, the output has grouping columns
identical to the additional columns in the input (without rt
/x
, dens
and densscaled
),
but without the ones in the agg_over
argument. CDFtoQuantiles
returns only a data.frame
with columns p
and q
.
Details
For a reasonable accuracy the number of steps in the support column (rt
/x
)
should be high, i.e. the distance between values small.
We recommend, to ensure that the support vector in the input to be equidistant,
i.e. the difference between consecutive support values should be constant, though
this is not required.
If both column names x
and rt
are present in pdf_df
, rt
will be preferred.
Attention should be given to the columns of pdf_df
other than rt
/x
and dens
/pdf
.
The column for the pdf may be scaled to integrate to 1 but do not have to.
Examples
## Demonstrate PDFtoQuantiles
pred <- expand.grid(model = c("dynWEV", "PCRMt"),
rt = seq(0, 15, length.out=1200),
condition = c(1,2,3),
rating = c(1,2))
pred$dens <- dchisq(pred$rt, 3) # pdf may also be used as column name
head(pred)
#> model rt condition rating dens
#> 1 dynWEV 0.00000000 1 1 0.00000000
#> 2 PCRMt 0.00000000 1 1 0.00000000
#> 3 dynWEV 0.01251043 1 1 0.04434345
#> 4 PCRMt 0.01251043 1 1 0.04434345
#> 5 dynWEV 0.02502085 1 1 0.06232006
#> 6 PCRMt 0.02502085 1 1 0.06232006
res <- PDFtoQuantiles(pred, p=c(0.3, 0.5, 0.7))
head(res)
#> # A tibble: 6 × 5
#> model condition rating p q
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 dynWEV 1 1 0.3 1.43
#> 2 dynWEV 1 1 0.5 2.36
#> 3 dynWEV 1 1 0.7 3.65
#> 4 dynWEV 1 2 0.3 1.43
#> 5 dynWEV 1 2 0.5 2.36
#> 6 dynWEV 1 2 0.7 3.65
nrow(res) #= 3(quantiles)*2(models)*3(conditions)*2(rating)
#> [1] 36
# Compare to true quantiles of Chi-square distribution
qchisq(p=c(0.3, 0.5, 0.7), 3)
#> [1] 1.423652 2.365974 3.664871
res$q[1:3]
#> [1] 1.426188 2.364470 3.653044
res2 <- PDFtoQuantiles(pred, p=c(0.3, 0.5, 0.7), agg_over = "model")
nrow(res2) #=18 because res aggregated over models
#> [1] 18
# \donttest{
pred$pdf <- dchisq(pred$rt, 3)
head(pred)
#> model rt condition rating dens pdf
#> 1 dynWEV 0.00000000 1 1 0.00000000 0.00000000
#> 2 PCRMt 0.00000000 1 1 0.00000000 0.00000000
#> 3 dynWEV 0.01251043 1 1 0.04434345 0.04434345
#> 4 PCRMt 0.01251043 1 1 0.04434345 0.04434345
#> 5 dynWEV 0.02502085 1 1 0.06232006 0.06232006
#> 6 PCRMt 0.02502085 1 1 0.06232006 0.06232006
# following call throws a warning, because both columns pdf and dens are present
PDFtoQuantiles(pred, p=c(0.3, 0.5, 0.7), agg_over = "model")
#> Warning: The column 'dens' is used as density values, not 'pdf'!
#> # A tibble: 18 × 4
#> condition rating p q
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 0.3 1.43
#> 2 1 1 0.5 2.36
#> 3 1 1 0.7 3.65
#> 4 1 2 0.3 1.43
#> 5 1 2 0.5 2.36
#> 6 1 2 0.7 3.65
#> 7 2 1 0.3 1.43
#> 8 2 1 0.5 2.36
#> 9 2 1 0.7 3.65
#> 10 2 2 0.3 1.43
#> 11 2 2 0.5 2.36
#> 12 2 2 0.7 3.65
#> 13 3 1 0.3 1.43
#> 14 3 1 0.5 2.36
#> 15 3 1 0.7 3.65
#> 16 3 2 0.3 1.43
#> 17 3 2 0.5 2.36
#> 18 3 2 0.7 3.65
# }
# \donttest{
pred2 <- data.frame(rt=seq(0, 7, length.out=100))
pred2$dens <- dchisq(pred2$rt, 5)
# following call throws a warning, because density is assumed to be scaled (scaled=TRUE), i.e.
# integrate to 1, but the .95 quantile is not reached in the rt column
PDFtoQuantiles(pred2, p=c(0.3, 0.5, 0.95), scaled=TRUE) # Gives a warning
#> Warning: There was 1 warning in `reframe()`.
#> ℹ In argument: `CDFtoQuantiles(.data$cdfscaled, .data$rt, p = p)`.
#> Caused by warning in `min()`:
#> ! no non-missing arguments to min; returning Inf
#> # A tibble: 3 × 2
#> p q
#> <dbl> <dbl>
#> 1 0.3 2.97
#> 2 0.5 4.38
#> 3 0.95 NA
# }
## Demonstrate CDFtoQuantiles
X <- seq(-2, 2, length.out=300)
pdf_values <- pnorm(X)
CDFtoQuantiles(pdf_values, X, p=c(0.2, 0.5, 0.8))
#> p q
#> 1 0.2 -0.836120401
#> 2 0.5 0.006688963
#> 3 0.8 0.849498328
qnorm(c(0.2, 0.5, 0.8))
#> [1] -0.8416212 0.0000000 0.8416212