I am new to R. I want to ask a question below:
Here is the data:
year <- c(2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022)
score <- c(85, 85, 88, 88, 94, 94, 94, 82, 82, 84, 84, 84, 84, 84, 84)
I want to set 2015 as the breakpoint to do the piecewise linear fitting (2008-2015 and 2015-2022). I have tried the following code, it get the results below. However, I think the result is not correct, especially for stage2, which shoube an increasing trend.
stage1 <- year - 2008
stage2 <- (year - 2015) * (year >= 2015)
fm <- lm(score~ stage1 + stage2)
summary(fm)
library(car)
linearHypothesis(fm, "stage1 + stage2", verbose = TRUE)
plot(score ~ year)
lines(fitted(fm) ~ year, col = "red")
abline(v = 2015, lty = 2)
The piecewise linaer fitting result