Search code examples
rtimesegmenttrend

Error in time segmented analysis ( values too close each other. Please change (decreases number of) starting values)


I used time segmented analysis to get p value for the trend of stopped studies but it gives me an error

Used code

data<-read.table(text="
Year    Stopped
2015    973
2016    1025
2017    1151
2018    1384
2019    4507
2020    15557
", header=T, sep="")
library(segmented)
dput(names(data))
q.lm<-lm(Stopped ~ Year,data);summary (q.lm)

o<-segmented(q.lm,seg.Z=~Year,  psi = c(2019)); summary(o)
#Error: psi values too close each other. Please change (decreases number of) starting values
#Alternatively I tried this
o<-segmented(q.lm,seg.Z=~Year,  psi = NA); summary(o)
#Error: psi values too close each other. Please change (decreases number of) starting values
o$psi;slope(o);confint.segmented(o)
o.fitted<-fitted(o)
o.model<-data.frame(Year = data$Year, Percent =o.fitted)

Here is my plot that I need to get p value for its trend enter image description here

Any advice will be greatly appreciated.


Solution

  • Try fitting log(Stopped) by Year. The plot in the question suggests it might be worth a try. And it also suggests 2018 as the break point, not 2019.

    library(segmented)
    
    q.lm <- lm(log(Stopped) ~ Year, data)
    summary(q.lm)
    
    o <- segmented(q.lm, seg.Z = ~Year, psi = 2018)
    summary(o)
    
    plot(o)
    points(log(Stopped) ~ Year, data)
    

    enter image description here