1 |
x |
|
y |
|
tr |
|
h |
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 | ##---- 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 (x, y, tr = 0.2, h = NA)
{
if (is.na(h))
h <- length(x) - floor(tr * length(x))
ord <- order(x)
xs <- x[ord]
ys <- y[ord]
vec1 <- outer(ys, ys, "-")
vec2 <- outer(xs, xs, "-")
v1 <- vec1[vec2 > 0]
v2 <- vec2[vec2 > 0]
slope <- v1/v2
vec3 <- outer(ys, ys, "+")
vec4 <- outer(xs, xs, "+")
v3 <- vec3[vec2 > 0]
v4 <- vec4[vec2 > 0]
val <- NA
inter <- v3/2 - slope * v4/2
for (i in 1:length(slope)) {
risk <- (y - slope[i] * x - inter[i])^2
risk <- sort(risk)
val[i] <- sum(risk[1:h])
}
best <- min(val)
coef <- NA
coef[2] <- mean(slope[val == best])
coef[1] <- mean(inter[val == best])
res <- y - coef[2] * x - coef[1]
list(coef = coef, residuals = res)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.