0
$\begingroup$

I am trying to do 2-time segmented analysis using the package segmented in R but I am encountering errors that I tried to figure it out using many links but I could not. Any advice will be greatly appreciated.

My dataframe

dataYear.retraxtionCount1980<-read.table(text= "Year    Number.of.publication   Count   retractionRate
1980    242166  12  0.004955279
1981    256127  2   0.000780863
1982    263374  3   0.001139065
1983    258677  10  0.003865825
1984    280389  6   0.002139884
1985    292607  9   0.003075798
1986    291861  11  0.003768917
1987    304377  11  0.003613939
1988    324732  4   0.001231785
1989    333832  10  0.002995519
1990    338945  10  0.002950331
1991    344601  12  0.003482288
1992    354939  12  0.003380863
1993    372882  14  0.003754539
1994    387403  12  0.00309755
1995    402846  16  0.003971741
1996    428639  8   0.001866372
1997    435888  16  0.003670668
1998    429134  36  0.008388988
1999    432389  18  0.004162918
2000    449256  15  0.003338854
2001    452956  13  0.002870036
2002    462908  21  0.004536539
2003    472556  25  0.005290378
2004    500073  41  0.008198803
2005    527682  45  0.008527863
2006    561239  85  0.015145063
2007    584953  88  0.015043944
2008    604574  139 0.022991396
2009    637235  190 0.029816316
2010    665883  173 0.02598054
2011    727619  307 0.042192411
2012    774961  378 0.048776648
2013    814599  395 0.048490116
2014    852142  305 0.035792157
2015    859926  465 0.05407442
2016    875640  592 0.067607693
2017    881086  488 0.055386194
2018    927343  344 0.037095228
2019    967337  548 0.056650371
2020    1048034 316 0.030151694
 ", sep="", header=T);dataYear.retraxtionCount1980

2002 in the following code is the year where I thought the breakpoint should be based on a bar plot. Here are some of the errors

P.for.trend<-lm(Year~Count,dataYear.retraxtionCount1980);summary (P.for.trend)

o <- segmented(P.for.trend, seg.Z = ~ Year, psi = 2002, control = seg.control(stop.if.error = F))
Error in lista[[2]] : subscript out of bounds
In addition: Warning messages:
1: In segmented.lm(P.for.trend, seg.Z = ~Year, psi = 2002, control = seg.control(stop.if.error = F)) :
   Argument 'stop.if.error' is working, but will be removed in the next releases. Please use 'fix.npsi' for the future..
2: In model.matrix.default(mt, mf, obj$contrasts) :
  the response appeared on the right-hand side and was dropped
3: In model.matrix.default(mt, mf, obj$contrasts) :
  problem with term 2 in model.matrix: no columns are assigned
#================================================================
o<-segmented(P.for.trend,
             seg.Z=~Year, 
             psi = c(Year = 2002)) #psi = c(2002)
Warning messages:
1: In model.matrix.default(mt, mf, obj$contrasts) :
  the response appeared on the right-hand side and was dropped
2: In model.matrix.default(mt, mf, obj$contrasts) :
  problem with term 2 in model.matrix: no columns are assigned
3: No breakpoint estimated 
#================================================================
> o<-segmented(P.for.trend,
              seg.Z=~Year, 
              psi = c(2002)) #psi = c(2002)
Warning messages:
1: In model.matrix.default(mt, mf, obj$contrasts) :
  the response appeared on the right-hand side and was dropped
2: In model.matrix.default(mt, mf, obj$contrasts) :
  problem with term 2 in model.matrix: no columns are assigned
3: No breakpoint estimated 

#================================================================
o<-segmented(P.for.trend,
             seg.Z=~Year,
             psi = NA) #, control = seg.control(K=1) ) #psi = list(Year=2002 )
Warning messages:
1: In model.matrix.default(mt, mf, obj$contrasts) :
  the response appeared on the right-hand side and was dropped
2: In model.matrix.default(mt, mf, obj$contrasts) :
  problem with term 2 in model.matrix: no columns are assigned
3: No breakpoint estimated 

$\endgroup$
  • 1
    $\begingroup$ Your formula is the wrong way around. It should be Count ~ Year. If you fix that, everything works. Formulas are always interpreted as y ~ X. $\endgroup$ – Roland Nov 30 '20 at 15:27
  • $\begingroup$ @Roland Thx a lot. That works. Greatly appreciated. Upvoted. $\endgroup$ – Mohamed Rahouma Nov 30 '20 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.