SStrlin: self start for a trilinear Function

SStrlinR Documentation

self start for a trilinear Function

Description

Self starter for a tri-linear function with parameters a (intercept), b (first slope), xs1 (first break-point), c (second slope), xs2 (second break-point) and d (third slope)

Usage

trlin(x, a, b, xs1, c, xs2, d)

SStrlin(x, a, b, xs1, c, xs2, d)

Arguments

x

input vector

a

the intercept

b

the first-phase slope

xs1

first break-point of transition between first-phase linear and second-phase linear

c

the second-phase slope

xs2

second break-point of transition between second-phase linear and third-phase linear

d

the third-phase slope

Details

This is a special case with just three parts (and two break points) but a more general approach is to consider a segmented function with several breakpoints and linear segments. Splines would be even more general. Also this model assumes that there are two break-points that needs to be estimated. The guess for the initial values splits the dataset in half, so it this will work when one break-point is in the first half and the second is in the second half.

Value

a numeric vector of the same length as x containing parameter estimates for equation specified

trlin: vector of the same length as x using the tri-linear function

See Also

package segmented.

Examples


require(ggplot2)
set.seed(1234)
x <- 1:30
y <- trlin(x, 0.5, 2, 10, 0.1, 20, 1.75) + rnorm(30, 0, 0.5)
dat <- data.frame(x = x, y = y)
fit <- nls(y ~ SStrlin(x, a, b, xs1, c, xs2, d), data = dat)
## plot
ggplot(data = dat, aes(x = x, y = y)) + 
  geom_point() + 
  geom_line(aes(y = fitted(fit)))
## Minimal example
## This is probably about the smallest dataset you 
## should use with this function
dat2 <- data.frame(x = 1:8, y = c(1.1, 1.9, 3.1, 2.5, 1.4, 0.9, 2.2, 2.9))
fit2 <- nls(y ~ SStrlin(x, a, b, xs1, c, xs2, d), data = dat2)
## expangin for plotting
ndat <- data.frame(x = seq(1, 8, by = 0.1))
ndat$prd <- predict(fit2, newdata = ndat)
ggplot() + 
  geom_point(data = dat2, aes(x = x, y = y)) + 
  geom_line(data = ndat, aes(x = x, y = prd))



femiguez/nlraa documentation built on Jan. 26, 2024, 9:31 p.m.