-
Notifications
You must be signed in to change notification settings - Fork 0
/
percent2probit.R
149 lines (134 loc) · 3.65 KB
/
percent2probit.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#' Probit transformation
#'
#' These functions transform data between percentage, probit and Normal
#' Equivalent Deviate (NED)
#' \insertCite{bliss_method_1934,finney_probit_1952}{viabilitymetrics}.
#' \loadmathjax
#'
#' Probit transformation can be used to transform a sigmoid curve of percentage
#' data to a linear one. The probit transformation is defined as
#' \mjseqn{\textrm{NED} + 5}. However the two terms probit and NED are used
#' interchangeably in literature.
#'
#' NED function (\mjseqn{\Phi^{-1}}) is is the inverse of the cumulative
#' distribution function (\mjseqn{\Phi}) of the standard normal distribution
#' (\mjseqn{z\sim N(0,1)}) or the quantile function associated with the standard
#' normal distribution.
#'
#' For percentage \mjseqn{p},
#'
#' \mjsdeqn{\textrm{NED}(p)=\Phi^{-1}(p)= \sqrt{2}~\textrm{erf}^{-1}(2p-1)}
#'
#' and
#'
#' \mjsdeqn{\textrm{probit}(p)=\textrm{NED}(p)+5}
#'
#' The \code{PercentAdjust} function adjusts the percentage values of 0 and 100
#' to \mjseqn{100\times \dfrac{0.25}{n}} and \mjseqn{100\times
#' \dfrac{n-0.25}{n}} respectively, according to the sample size \mjseqn{n} to
#' avoid infinity values during probit transformation
#' \insertCite{miller_estimation_1944}{viabilitymetrics}.
#'
#' @param percentage The percentage value.
#' @param probit The probit value
#' @param NED The NED value.
#' @param n Sample size for estimation of percentage.
#'
#' @return The transformed value.
#' @export PercentAdjust
#' @export Percent2NED
#' @export Percent2Probit
#' @export Probit2NED
#' @export Probit2Percent
#' @export NED2Probit
#' @export NED2Percent
#' @importFrom stats pnorm qnorm
#'
#' @references \insertRef{bliss_method_1934}{viabilitymetrics}
#'
#' \insertRef{miller_estimation_1944}{viabilitymetrics}
#'
#' \insertRef{finney_probit_1952}{viabilitymetrics}
#'
#' @examples
#' Percent2NED(0:100)
#' Percent2Probit(0:100)
#'
#' Percent2NED(25)
#' Percent2Probit(25)
#' Percent2NED(25) +5
#' NED2Probit(-0.6744898)
#'
#' # Percentage adjustment for 0 and 100
#' Percent2Probit(100)
#' Percent2Probit(0)
#' n = 50
#' Percent2Probit(PercentAdjust(100, n))
#' Percent2Probit(PercentAdjust(0, n))
#'
#' @name Percent2Probit
#' @rdname Percent2Probit
#' @export
PercentAdjust <- function(percentage, n) {
if (!is.numeric(percentage)) {
stop('"percentage" is not numeric.')
}
if (!is.numeric(n)) {
stop('"n" is not numeric.')
}
# check for n
AdjPercent <- ifelse(test = percentage == 100,
yes = (100 * (n - 0.25)) / n,
no = ifelse(test = percentage == 0,
yes = 100 * (0.25 / n),
no = percentage))
return(AdjPercent)
}
#' @rdname Percent2Probit
#' @export
Percent2NED <- function(percentage) {
if (!is.numeric(percentage)) {
stop('"percentage" is not numeric.')
}
qnorm(percentage / 100, 0, 1)
}
#' @rdname Percent2Probit
#' @export
Percent2Probit <- function(percentage) {
if (!is.numeric(percentage)) {
stop('"percentage" is not numeric.')
}
qnorm(percentage / 100, 5, 1)
}
#' @rdname Percent2Probit
#' @export
Probit2NED <- function(probit) {
if (!is.numeric(probit)) {
stop('"probit" is not numeric.')
}
probit - 5
}
#' @rdname Percent2Probit
#' @export
NED2Probit <- function(NED) {
if (!is.numeric(NED)) {
stop('"NED" is not numeric.')
}
NED + 5
}
#' @rdname Percent2Probit
#' @export
NED2Percent <- function(NED) {
if (!is.numeric(NED)) {
stop('"NED" is not numeric.')
}
pnorm(NED, 0, 1) * 100
}
#' @rdname Percent2Probit
#' @export
Probit2Percent <- function(probit) {
if (!is.numeric(probit)) {
stop('"probit" is not numeric.')
}
pnorm(probit, 5, 1) * 100
}