-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintervalprob.R
71 lines (53 loc) · 2.16 KB
/
intervalprob.R
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
#' Distribution of single-occasion drinking amount
#'
#' Computes the probability that each integer number of grams of ethanol per day
#' is consumed on a single drinking occasion.
#'
#' @param grams_ethanol Integer vector - the potential grams of ethanol drunk on a single occasion.
#' @param SODMean Numeric vector - the average amount that each individual is expected to
#' drink on a single drinking occasion.
#' @param SODSDV Numeric vector - the standard deviation of the amount that each individual is expected to
#' drink on a single drinking occasion.
#' @param SODFreq Numeric vector - the expected number of drinking occasions that
#' each individual has each week.
#' @param grams_ethanol_per_unit Numeric value giving the conversion factor for the number of grams of pure
#' ethanol in one UK standard unit of alcohol.
#'
#' @return Returns a numeric vector containing the probability that each amount of alcohol is consumed
#'
#' @importFrom data.table := setDT setnames
#'
#' @export
#'
#'
#'
#'
intervalprob <- function(
grams_ethanol = 1:540,
SODMean = NULL,
SODSDV = NULL,
SODFreq = NULL,
grams_ethanol_per_unit = 8
) {
kn <- max(grams_ethanol)
x <- t(vapply(X = grams_ethanol,
FUN = stats::pnorm,
FUN.VALUE = numeric(length(SODMean)),
mean = SODMean * grams_ethanol_per_unit, # mean
sd = SODSDV * grams_ethanol_per_unit # variance
))
# Convert from the cumulative distribution to the
# probability that each level of alcohol is consumed on a drinking occasion
#interval_prob <- x - c(0, x[1:(length(x) - 1)])
#interval_prob <- diff(x)
interval_prob <- apply(x, 2, diff)
#interval_prob <- interval_prob / sum(interval_prob)
# compare method against numeric integration
# The line below makes values sum to 1
# discussion with Alan has concluded that it is needed because
# the values are subsequently used in the computation of a weighted average
interval_prob <- interval_prob / matrix(colSums(interval_prob), nrow = kn - 1, ncol = ncol(interval_prob), byrow = T)
interval_prob[is.na(interval_prob)] <- 0
rm(kn, x)
return(interval_prob)
}