Robust Simple Linear Regression Based on Medians
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
: [numeric vector]
explanatory variable.
y
: [numeric vector]
response variable (default: NULL
).
w
: [numeric vector]
weights (same length as x
).
type
: [character]
"slopes"
or "products"
(default: "slopes"
).
na.rm
: [logical]
indicating whether NA
values should be removed before the computation proceeds (default: FALSE
).
Overview.: Robust simple linear regression based on medians
Type.: Two methods/ types are available. Let
denote the weighted median of variable `x` with weights `w`:
- **`type = "slopes"`:**: The slope is computed as
- **`type = "products"`:**: The slope is computed as
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) # cars data head(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"))
Useful links