segreg: Segmented Regression

Description Usage Arguments Details Value Examples

View source: R/segreg.R

Description

Fit a simple linear segmented regression (also known as changepoint, hockey stick, or broken line regression).

Usage

1
segreg(x, y, k = NULL)

Arguments

x

Numeric vector, the independent variable which will be "broken".

y

Numeric vector, the dependent variable which will be used to define the break point.

k

Numeric scalar, the location of the break point, if known. If NULL, the default, the optimal break point will be chosen from among all unique values of x except for the minimum and the maximum.

Details

The break point cuts the x vector into two groups, x <= k and x > k.

Value

List with three elements, fit: the resulting lm object, k, and pred: data frame with the unique values of x (and k) with predicted values.

Examples

1
2
3
4
5
6
indvar <- sample(1:50, 100, replace=TRUE)
depvar <- ifelse(indvar<32, indvar + 3, indvar/4 + 27) + rnorm(100, sd=2)
sr <- segreg(indvar, depvar)
plot(indvar, depvar)
lines(sr$pred)
abline(v=sr$k, lty=2)

JVAdams/jvamisc documentation built on Aug. 11, 2021, 6:43 a.m.