weighted_median_line: Robust simple linear regression based on medians

Description Usage Arguments Details Value See Also Examples

View source: R/robsurvey.R

Description

For type medslopes the median individual ratios response/explanatory is used as estimator of the slope. For version ratiomeds the ratio of the median crossproduct to the median of squares of the explanatory variable is used as the estimator of the slope. Survey weights may be used. Missing values are neglected.

Usage

1
weighted_median_line(x, y = NULL, w, type = "slopes", na.rm = FALSE)

Arguments

x

a numeric vector (explanatory variable)

y

a numeric vector (response variable)

w

a numeric vector of weights

type

either "slopes" (default) or "products"

na.rm

a logical value indicating whether rows with NA values should be stripped before the computation proceeds

Details

Uses weighted_median(). The median of slopes (type="slopes") uses b1=M((y-M(y,w))/(x-M(x,w)), w). The median of crossproducts by median of squares (type="products") uses b1=M((y-M(y,w))(x-M(x,w)), w )/ M((x-M(x,w )^2), w ), where M(x, w) is shorthand for the function weighted_median(x, w). The function allows weights and missing values.

Value

a vector with two components: intercept and slope

See Also

line, weighted_line, weighted_median_ratio

Examples

 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
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)))
weighted_median_line(y~x, w=rep(1, length(x)), type="prod")

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

martinSter/robsurvey documentation built on Oct. 11, 2019, 4:45 p.m.