weighted_median_line | R Documentation |
Robust simple linear regression based on medians: two methods are available:
"slopes"
and "product"
.
weighted_median_line(x, y = NULL, w, type = "slopes", na.rm = FALSE)
x |
|
y |
|
w |
|
type |
|
na.rm |
|
Robust simple linear regression based on medians
Two methods/ types are available. Let m(x,w)
denote the weighted median of variable x
with weights
w
:
type = "slopes"
:The slope is computed as
m[(y - m[y, w]) / (x - m[x, w]), w].
type = "products"
:The slope is computed as
b1 = \frac{m\big([y - m(y,w)][x - m(x,w)], w\big)} {m\big([x - m(x,w)]^2, w\big)}.
m([y - m(y, w)][x - m(x, w)], w) / m([x - m(x, w)]^2, w).
A vector with two components: intercept and slope
Overview (of all implemented functions)
line
, weighted_line
and
weighted_median_ratio
x <- c(1, 2, 4, 5) y <- c(3, 2, 7, 4) weighted_line(y~x, w=rep(1, length(x))) weighted_median_line(y~x, w = rep(1, length(x))) m <- weighted_median_line(y~x, w = rep(1, length(x)), type = "prod") m coef(m) fitted(m) residuals(m) data(cars) with(cars, weighted_median_line(dist ~ speed, w = rep(1, length(dist)))) with(cars, weighted_median_line(dist ~ speed, w = rep(1, length(dist)), type = "prod")) # weighted w <- c(rep(1,20), rep(2,20), rep(5, 10)) with(cars, weighted_median_line(dist ~ speed, w = w)) with(cars, weighted_median_line(dist ~ speed, w = w, type = "prod")) # outlier in y cars$dist[49] <- 360 with(cars, weighted_median_line(dist ~ speed, w = w)) with(cars, weighted_median_line(dist ~ speed, w = w, type = "prod")) # outlier in x data(cars) cars$speed[49] <- 72 with(cars, weighted_median_line(dist ~ speed, w = w)) with(cars, weighted_median_line(dist ~ speed, w = w, type = "prod"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.