weighted_median_line {robsurvey}R Documentation

Robust Simple Linear Regression Based on Medians

Description

Robust simple linear regression based on medians: two methods are available: "slopes" and "product".

Usage

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) denote the weighted median of variable x with weights w:

type = "slopes":

The slope is computed as

b1 = m\left( \frac{y - m(y,w)}{x - m(x,w)}, w\right).

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

Value

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"))

[Package robsurvey version 0.6 Index]