Segmented regression with quadratic polynomial and shock line

I am trying to implement segmented regression according to this example. Segmented regression, breakpoint analysis .

Now, how can I implement it in such a way that the second part will be a quadratic polynomial and the rest of other things.

I tried the same thing by changing Z= ~poly(DistanceMeters, 2) , however this did not work.

Also, how can I get equations like

 part 1: a1*x+b1 part 2: a2*x2**2 + b2*x + c1 part 3 :a3*x + b3 

There are similar questions like this one, however they do not explain the use of segmented function.

+6
source share
1 answer

I have two ideas, both with flaws. Perhaps you can customize one of them to suit your needs. Unfortunately, access to the Drive is currently not possible, so some artificial data is used.

1. Manually fit polynomial models

Here you can specify which models you like, some segments may be lm, some polynomials, etc.

the code:

 library(segmented) library(ggplot2) library(data.table) # Data set.seed(12) xx <- 1:100 yy <- 2 + 1.5 * pmax(xx-35, 0) - 1.5 * pmax(xx-70, 0) + 15 * pmax(runif(100) - 0.5, 0) + rnorm(100, 0, 2) dt <- data.table(x = xx, y = yy, type = 'act') dt_all <- copy(dt) # lm lm_lin <- lm(y ~ x, data = dt) summary(lm_lin) # Find segments lm_seg <- segmented( lm_lin, seg.Z = ~ x, psi = list(x = c(30, 80))) # "Manual" lm's breaks <- unname(lm_seg$psi[, 'Est.']) lm_poly1 <- lm(y ~ poly(x, 4), data = dt[x < breaks[1], ]) lm_2 <- lm(y ~ x, data = dt[x > breaks[1] & x < breaks[2], ]) lm_poly3 <- lm(y ~ poly(x, 4), data = dt[x > breaks[2], ]) dt_all <- rbind( dt_all, data.table(x = xx, y = c( predict(lm_poly1), predict(lm_2), predict(lm_poly3)), type = 'lm_poly' ) ) 

2. Install the model using breaks from segmented and some splines

Here you get a smooth transition between segments, but you have much less control over what is happening.

 # Using splines for smooth segments library(mgcv) spl <- gam(y ~ s(x, bs = "cc", k = 12), data = dt, knots = list(xx = breaks)) # Plot dt_all <- rbind(dt_all, data.table(x = xx, y = predict(spl), type = 'spl')) ggplot(dt_all, aes(x = x, y = y)) + geom_point(size = 1) + facet_grid(. ~ type) + theme_minimal() 

enter image description here

Both can be done using, for example, list() and lapply() to automate bits (for a different number of breaks, etc.).

Edit:

By changing the arguments poly and s , you can get some β€œbetter” fit models, but for the gam errors at the edges are pretty big, see degree = 6 and k = 30 :

enter image description here

+2
source

Source: https://habr.com/ru/post/1015546/


All Articles