weighted_median_line function

Robust Simple Linear Regression Based on Medians

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)

Arguments

  • 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).

Details

  • Overview.: Robust simple linear regression based on medians

  • Type.: Two methods/ types are available. Let m(x,w)m(x,w)

     denote the weighted median of variable `x` with weights `w`:
     
     - **`type = "slopes"`:**: The slope is computed as 
    
b1=m(ym(y,w)xm(x,w),w).m[(ym[y,w])/(xm[x,w]),w]. b1 = m\left( \frac{y - m(y,w)}{x - m(x,w)}, w\right).m[(y - m[y, w]) / (x - m[x, w]), w].
   - **`type = "products"`:**: The slope is computed as 
b1=m([ym(y,w)][xm(x,w)],w)m([xm(x,w)]2,w). 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).

Returns

A vector with two components: intercept and slope

See Also

Overview (of all implemented functions)

line, weighted_line and weighted_median_ratio

Examples

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"))
  • Maintainer: Tobias Schoch
  • License: GPL (>= 2)
  • Last published: 2024-08-22