Sterph
Sterph

Reputation: 1

R Segmented: Is there a way to force breakpoints?

I'm trying to do an annual percent change analysis via Segmented in R. I already know where I want my breakpoints to occur, but I can't seem to find a way to force Segmented to use these points.

I have weekly rate data over 166 weeks and am dividing them into 3 periods. There are two breakpoints that divide these three periods, between weeks 52 and 53 and weeks 115 and 116.

As far as I can tell, the only input for breakpoints is the "psi =" argument. However, this only lets me enter starting points from which Segmented will estimate breakpoints it finds to be most fitting.

Instead, I already know where I want my breakpoints to occur, and I'm looking to set these in the model.

weekly <- read.csv("data.csv")

lm <- lm(Rate ~ Week, data = weekly)
summary(lm)

segment <- segmented(lm,
                     seg.Z = ~ Week,
                     psi = list(Week = c(53, 116)))

fitted <- fitted(segment)
model <- data.frame(Week = weekly$Week, Rate = fitted)

This ends up giving me a model fitted to the estimates segmented found, rather than the intended breakpoints at 53 and 116.

Is there a way to force segmented to use certain breakpoints at each, or is there another package that can do this?

Upvotes: 0

Views: 804

Answers (2)

Jian Ge
Jian Ge

Reputation: 1

Answer from 2024: There is now a argument fixed.psi = c(53, 116) you can put your predefined breaking points in the segmented().

If you want to keep fixed all breakpoints (to be specified in psi), use it.max=0 in seg.control()

Upvotes: 0

williamg15
williamg15

Reputation: 77

Maybe you could try use:

weekly <- as.factor(cut(weekly$week, c(0, 53, 116))

levels(weekly$week) 
0-53, 53-116, 116+

lm <- lm(Rate ~ Week, data = weekly)

Now weekly$week is a factor and will have levels equal to 0-53, 53-116, 116+

You should then be able to add these levels into your lm.

Upvotes: 0

Related Questions