breakpoint: Find One or Two Breakpoints

View source: R/breakpoint.R

breakpointR Documentation

Find One or Two Breakpoints

Description

Find one or two breakpoints.

Usage

breakpoint(
  object,
  pattern = c("111", "101", "10", "11", "01"),
  breakpoint.var = NULL,
  breakpoint.init = NULL,
  n.iter = 25,
  tol = 0.001,
  n.init = 5,
  n.points = 5,
  trace = 2,
  digits = -log10(tol)
)

Arguments

object

a lm object under the assumption of a linear relationship

pattern

[vector of character] the number and type of breakpoints to be search. 0 indicates a flat line.

breakpoint.var

[character] the variable regarding which the breakpoints should be found.

breakpoint.init

[list] a list containing initialization values for the breakpoints for each pattern. Initialisation values can be a vector or a matrix with the same number of rows as the number of breakpoints.

n.iter

[integer, >0] the maximum number of iterations used to estimates the breakpoints.

tol

[numeric, >0] the maximum accpetable difference between two consecutive estimates of the breakpoints. When reached, the estimation algorithm stops.

n.init

[integer, >0] the number of quantiles used to generate initialisation points. Only active when breakpoint.init is NULL.

n.points

[integer, >0] the number of points used to display the fit.

trace

[0,1,2] trace the execution of the function.

digits

[integer] how to round values that are displayed in the terminal.

Details

Argument pattern: 111 corresponding to three lines with different slopes while 101 corresponds to a three lines where the middle one has null slope.

References

Muggeo, V. M. R. Estimating regression models with unknown break-points. Statistics in medicine 2003; 22:3055-3071.

Examples

library(lava)
library(data.table)
library(ggplot2)

#### simulate data
m <- lvm(Y1[0:0.1] ~ X1,
         Y2[1:0.1] ~ 0,
         X2 ~ 1,
         Y3[3:0.1] ~ -1*X3)

distribution(m,~X1) <- uniform.lvm(0, 1)
distribution(m,~X2) <- uniform.lvm(1, 2)
distribution(m,~X3) <- uniform.lvm(2, 3)

set.seed(10)
dt <- as.data.table(lava::sim(m, n = 1e2))
dtL <- melt(dt,
     measure.vars = list(paste0("X",1:3), paste0("Y",1:3)),
     value.name = c("X","Y"))

gg <- ggplot(dtL, aes(x = X))
gg <- gg + geom_point(aes(y = Y, color = variable))
gg


#### fit breakpoint regression
e.lm <- lm(Y~X, data = dtL)
resBP <- breakpoint(e.lm)

BIC(resBP)
gg + geom_line(data = resBP$BP101$fit, aes(y = fit))

#### example from the package segmented
if(require(segmented)){
GS <- segmented(e.lm, psi = c(1,2))


cbind(value = resBP$BP111$breakpoint,
      se = resBP$BP111$breakpoint.se)
GS$psi
}

if(require(gridExtra)){
  autoplot(resBP)
}
 

bozenne/butils documentation built on July 3, 2024, 2:34 p.m.