pava: isotonic regression

Description Usage Arguments Value Author(s) References Examples

View source: R/pava.R

Description

This is an internal function not meant to be called directly. Wrapper for gpava in package isotone to apply the pava algorithm for isotonic regression

Usage

1
pava(explanatory, response, X_0 = NA, Y_0 = NA, w = NA)

Arguments

explanatory

Explanatory sample points

response

Observed responses at the explanatory sample points

X_0

can ignore

Y_0

can ignore

w

weights if given repeated observations at same explanatory point

Value

return(list(x = explanatory, y = response_fit, response_obs = response)) List with

x

Explanatory sample points

y

estimated isotonic regression values

response_obs

Observed responses at the explanatory sample points

Author(s)

Shawn Mankad

References

de Leeuw J, Hornik K, Mair P (2009). 'Isotone Optimization in R: Pool-Adjacent-Violators Algorithm (PAVA) and Active Set Methods.' Journal of Statistical Software, 32(5), 1-24. ISSN 1548-7660. URL http://www.jstatsoft.org/v32/i05.

Examples

 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
X=runif(25, 0,1)
Y=X^2+rnorm(n=length(X), sd=0.1)
pava(X, Y, 0.25, 0.5)

## The function is currently defined as
function (explanatory, response, X_0 = NA, Y_0 = NA, w = NA) 
{
    require(isotone)
    if (is.na(w)) 
        w = rep(1, length(explanatory))
    ind = order(explanatory, decreasing = FALSE)
    if (sum(diff(ind) < 0) != 0) {
        explanatory = explanatory[ind]
        response = response[ind]
    }
    if (is.na(X_0) && is.na(Y_0)) {
        fit = gpava(explanatory, response)
        response_fit = fit$x
    }
    else if (is.na(X_0) || is.na(Y_0)) {
        warning("Only X_0 or only Y_0 was supplied. Please check arguments.")
    }
    else {
        n = length(explanatory)
        if (sum(response < Y_0) == n && sum(explanatory < X_0) == 
            n) {
            warning("Warning: X_0 and Y_0 are outside observed region")
            fit = gpava(explanatory, response)
            response_fit = fit$x
        }
        else if (sum(response < Y_0) == n && sum(explanatory < 
            X_0) == 0) {
            warning("Warning: X_0 and Y_0 are outside observed region")
            return(list(x = explanatory, y = rep(Y_0, n), y_compressed = rep(Y_0, 
                n)))
        }
        else if (sum(response < Y_0) == n) {
            warning("Warning: Y_0 is outside observed region")
            n2 = n - sum(explanatory < X_0)
            y1 = response[explanatory < X_0]
            x1 = explanatory[explanatory < X_0]
            fit = gpava(x1, y1)
            response_fit = c(sapply(fit$x, min, Y_0), rep(Y_0, 
                n2))
        }
        else if (sum(response >= Y_0) == n && sum(explanatory < 
            X_0) == n) {
            warning("Warning: X_0 and Y_0 are outside observed region")
            return(list(x = explanatory, y = rep(Y_0, n), y_compressed = rep(Y_0, 
                n)))
        }
        else if (sum(response >= Y_0) == n && sum(explanatory < 
            X_0) == 0) {
            warning("Warning: X_0 and Y_0 are outside observed region")
            fit = gpava(explanatory, response)
            response_fit = fit$x
        }
        else if (sum(response >= Y_0) == n) {
            warning("Warning: Y_0 is outside observed region")
            n2 = n - sum(explanatory > X_0)
            y1 = response[explanatory > X_0]
            x1 = explanatory[explanatory > X_0]
            fit = gpava(x1, y1)
            response_fit = c(rep(Y_0, n2), sapply(fit$x, max, 
                Y_0))
        }
        else if (sum(explanatory < X_0) == n) {
            warning("Warning: X_0 is outside observed region")
            fit = gpava(explanatory, response)
            response_fit = sapply(fit$x, min, Y_0)
        }
        else if (sum(explanatory < X_0) == 0) {
            warning("Warning: X_0 is outside observed region")
            fit = gpava(explanatory, response)
            response_fit = sapply(fit$x, max, Y_0)
        }
        else {
            y1 = response[explanatory < X_0]
            x1 = explanatory[explanatory < X_0]
            y2 = response[explanatory >= X_0]
            x2 = explanatory[explanatory >= X_0]
            fit1 = gpava(x1, y1)
            fit2 = gpava(x2, y2)
            response_fit = c(sapply(fit1$x, min, Y_0), sapply(fit2$x, 
                max, Y_0))
        }
    }
    return(list(x = explanatory, y = response_fit, response_obs = response))
  }

Example output

Loading required package: isotone
$x
 [1] 0.09837523 0.11732857 0.16941527 0.26140676 0.31938000 0.33134820
 [7] 0.36670614 0.38522139 0.42364887 0.43459143 0.44945278 0.45937503
[13] 0.51292358 0.51798004 0.55556051 0.58622866 0.64202220 0.67656152
[19] 0.68063562 0.70394799 0.73561398 0.76137701 0.76450524 0.79436492
[25] 0.90731847

$y
 [1] 0.09180087 0.09180087 0.09180087 0.50000000 0.50000000 0.50000000
 [7] 0.50000000 0.50000000 0.50000000 0.50000000 0.50000000 0.50000000
[13] 0.50000000 0.50000000 0.50000000 0.50000000 0.50000000 0.50000000
[19] 0.50000000 0.50000000 0.50000000 0.50000000 0.62069256 0.68618993
[25] 0.86576155

$response_obs
 [1]  0.12483306  0.12689096  0.02367858  0.27737997  0.20951037  0.15251393
 [7]  0.06484818 -0.01302850  0.09690818  0.23783948  0.15379018  0.26563976
[13]  0.16622279  0.29769942  0.17718547  0.29525158  0.34381040  0.43833899
[19]  0.27000112  0.59416043  0.40025357  0.43564189  0.62069256  0.68618993
[25]  0.86576155

function (explanatory, response, X_0 = NA, Y_0 = NA, w = NA) 
{
    require(isotone)
    if (is.na(w)) 
        w = rep(1, length(explanatory))
    ind = order(explanatory, decreasing = FALSE)
    if (sum(diff(ind) < 0) != 0) {
        explanatory = explanatory[ind]
        response = response[ind]
    }
    if (is.na(X_0) && is.na(Y_0)) {
        fit = gpava(explanatory, response)
        response_fit = fit$x
    }
    else if (is.na(X_0) || is.na(Y_0)) {
        warning("Only X_0 or only Y_0 was supplied. Please check arguments.")
    }
    else {
        n = length(explanatory)
        if (sum(response < Y_0) == n && sum(explanatory < X_0) == 
            n) {
            warning("Warning: X_0 and Y_0 are outside observed region")
            fit = gpava(explanatory, response)
            response_fit = fit$x
        }
        else if (sum(response < Y_0) == n && sum(explanatory < 
            X_0) == 0) {
            warning("Warning: X_0 and Y_0 are outside observed region")
            return(list(x = explanatory, y = rep(Y_0, n), y_compressed = rep(Y_0, 
                n)))
        }
        else if (sum(response < Y_0) == n) {
            warning("Warning: Y_0 is outside observed region")
            n2 = n - sum(explanatory < X_0)
            y1 = response[explanatory < X_0]
            x1 = explanatory[explanatory < X_0]
            fit = gpava(x1, y1)
            response_fit = c(sapply(fit$x, min, Y_0), rep(Y_0, 
                n2))
        }
        else if (sum(response >= Y_0) == n && sum(explanatory < 
            X_0) == n) {
            warning("Warning: X_0 and Y_0 are outside observed region")
            return(list(x = explanatory, y = rep(Y_0, n), y_compressed = rep(Y_0, 
                n)))
        }
        else if (sum(response >= Y_0) == n && sum(explanatory < 
            X_0) == 0) {
            warning("Warning: X_0 and Y_0 are outside observed region")
            fit = gpava(explanatory, response)
            response_fit = fit$x
        }
        else if (sum(response >= Y_0) == n) {
            warning("Warning: Y_0 is outside observed region")
            n2 = n - sum(explanatory > X_0)
            y1 = response[explanatory > X_0]
            x1 = explanatory[explanatory > X_0]
            fit = gpava(x1, y1)
            response_fit = c(rep(Y_0, n2), sapply(fit$x, max, 
                Y_0))
        }
        else if (sum(explanatory < X_0) == n) {
            warning("Warning: X_0 is outside observed region")
            fit = gpava(explanatory, response)
            response_fit = sapply(fit$x, min, Y_0)
        }
        else if (sum(explanatory < X_0) == 0) {
            warning("Warning: X_0 is outside observed region")
            fit = gpava(explanatory, response)
            response_fit = sapply(fit$x, max, Y_0)
        }
        else {
            y1 = response[explanatory < X_0]
            x1 = explanatory[explanatory < X_0]
            y2 = response[explanatory >= X_0]
            x2 = explanatory[explanatory >= X_0]
            fit1 = gpava(x1, y1)
            fit2 = gpava(x2, y2)
            response_fit = c(sapply(fit1$x, min, Y_0), sapply(fit2$x, 
                max, Y_0))
        }
    }
    return(list(x = explanatory, y = response_fit, response_obs = response))
}

twostageTE documentation built on May 1, 2019, 9:18 p.m.