1 |
x1 |
|
y1 |
|
x2 |
|
y2 |
|
pts |
|
regfun |
|
fr1 |
|
fr2 |
|
alpha |
|
plotit |
|
xout |
|
SCAT |
|
outfun |
|
BLO |
|
nboot |
|
SEED |
|
xlab |
|
ylab |
|
pr |
|
eout |
|
... |
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 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (x1, y1, x2, y2, pts = NULL, regfun = tsreg, fr1 = 1,
fr2 = 1, alpha = 0.05, plotit = TRUE, xout = FALSE, SCAT = TRUE,
outfun = outpro, BLO = FALSE, nboot = 500, SEED = TRUE, xlab = "X",
ylab = "Y", pr = TRUE, eout = FALSE, ...)
{
x1 = as.matrix(x1)
x2 = as.matrix(x2)
if (ncol(x1) != ncol(x2))
stop("x1 and x2 have different number of columns")
if (SEED)
set.seed(2)
FLAG = pts
X = elimna(cbind(x1, y1, x2, y2))
if (ncol(X) > 4)
stop("Only one covariate is allowed")
x1 = as.matrix(x1)
x2 = as.matrix(x2)
p = ncol(x1)
p1 = p + 1
p2 = p + 2
p3 = p1 + p
p4 = p3 + 1
x1 = X[, 1:p]
y1 = X[, p1]
x2 = X[, p2:p3]
y2 = X[, p4]
if (xout) {
flag1 = outfun(x1)$out.id
flag2 = outfun(x2)$out.id
flag = unique(c(flag1, flag2))
if (length(flag) > 0)
X = X[-flag, ]
x1 = X[, 1:p]
y1 = X[, p1]
x2 = X[, p2:p3]
y2 = X[, p4]
}
flagF = identical(regfun, tsreg)
if (identical(regfun, tshdreg))
flagF = FALSE
if (flagF) {
if (pr) {
if (sum(duplicated(y1) > 0))
print("Duplicate values detected; tshdreg might have more power than tsreg")
pr = FALSE
}
if (pr) {
if (sum(duplicated(y2) > 0))
print("Duplicate values detected; tshdreg might have more power than tsreg")
}
}
if (is.null(pts[1])) {
npt <- 5
isub <- c(1:5)
test <- c(1:5)
xorder <- order(x1)
y1 <- y1[xorder]
x1 <- x1[xorder]
xorder <- order(x2)
y2 <- y2[xorder]
x2 <- x2[xorder]
n1 <- 1
n2 <- 1
vecn <- 1
for (i in 1:length(x1)) n1[i] <- length(y1[near(x1, x1[i],
fr1)])
for (i in 1:length(x1)) n2[i] <- length(y2[near(x2, x1[i],
fr2)])
for (i in 1:length(x1)) vecn[i] <- min(n1[i], n2[i])
sub <- c(1:length(x1))
isub[1] <- min(sub[vecn >= 12])
isub[5] <- max(sub[vecn >= 12])
isub[3] <- floor((isub[1] + isub[5])/2)
isub[2] <- floor((isub[1] + isub[3])/2)
isub[4] <- floor((isub[3] + isub[5])/2)
pts = x1[isub]
}
for (i in 1:length(pts)) {
n1 <- 1
n2 <- 1
n1[i] <- length(y1[near(x1, pts[i], fr1)])
n2[i] <- length(y2[near(x2, pts[i], fr2)])
}
mat <- matrix(NA, length(pts), 7)
dimnames(mat) <- list(NULL, c("X", "Est1", "Est2", "DIF",
"ci.low", "ci.hi", "p.value"))
mat[, 1] = pts
n = length(y1)
x1 = as.matrix(x1)
x2 = as.matrix(x2)
data <- matrix(sample(length(y1), size = n * nboot, replace = TRUE),
nrow = nboot)
est1 = apply(data, 1, Danctspb.sub, x1, y1, xr = pts, regfun = regfun,
xout = FALSE, ...)
est2 = apply(data, 1, Danctspb.sub, x2, y2, xr = pts, regfun = regfun,
xout = FALSE, ...)
mat[, 2] = regYhat(x1, y1, xr = pts, regfun = regfun, ...)
mat[, 3] = regYhat(x2, y2, xr = pts, regfun = regfun, ...)
est = est1 - est2
if (!is.matrix(est))
est = matrix(est, nrow = 1)
mat[, 4] = mat[, 2] - mat[, 3]
pv1 = apply(est < 0, 1, mean, na.rm = T)
pv2 = apply(est == 0, 1, mean, na.rm = T)
pv = pv1 + 0.5 * pv2
pv1m = 1 - pv
pv = 2 * apply(cbind(pv, pv1m), 1, min)
mat[, 7] = pv
ilow <- round((alpha/2) * nboot)
ihi <- nboot - ilow
ilow <- ilow + 1
estsort = apply(est, 1, sort)
mat[, 5] = estsort[ilow, ]
mat[, 6] = estsort[ihi, ]
if (plotit) {
if (eout && xout)
stop("Cannot have both eout and xout = F")
if (eout) {
flag <- outfun(cbind(x1, y1), plotit = FALSE, ...)$keep
x1 <- x1[flag]
y1 <- y1[flag]
flag <- outfun(cbind(x2, y2), plotit = FALSE, ...)$keep
x2 <- x2[flag]
y2 <- y2[flag]
}
if (xout) {
flag <- outfun(x1, ...)$keep
x1 <- x1[flag]
y1 <- y1[flag]
flag <- outfun(x2, ...)$keep
x2 <- x2[flag]
y2 <- y2[flag]
}
plot(c(x1, x2), c(y1, y2), type = "n", xlab = xlab, ylab = ylab)
if (SCAT)
points(x1, y1, pch = "o")
if (SCAT)
points(x2, y2, pch = "+")
abline(regfun(x1, y1)$coef)
abline(regfun(x2, y2)$coef, lty = 2)
}
list(output = mat)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.