def convergentBreaks(testdata, controldata, datayears, model, mode="years", guide="Stability", pr=0.01, screenpr=0.05, trace=False, shallow=False):
  if not mode in controlmodes:
    raise CBException("keyword 'mode' should be in "+str(controlmodes))
  if guide in ["AIC", "Stability"]:
    if mode=="flat":
      ctrl=np.ones(np.shape(controldata))
      return convergentBreaks_Inner(testdata, controldata, datayears, ctrl, model, pr=pr, screenpr=screenpr, trace=trace, shallow=shallow)
    elif mode=="years":
      return convergentBreaks_Inner(testdata, controldata, datayears, datayears, model, pr=pr, screenpr=screenpr, trace=trace, shallow=shallow)
    elif mode=="control":
      return convergentBreaks_Inner(testdata, controldata, datayears, controldata, model, pr=pr, screenpr=screenpr, trace=trace, shallow=shallow)
    else:
      rstats = regress.analysed_regress(testdata, controldata)
      yh, r = regress.residuals(testdata, controldata, rstats)
      return convergentBreaks_Inner(testdata, controldata, datayears, yh, mode, pr=pr, screenpr=screenpr, trace=trace, shallow=shallow)
Exemple #2
0
def convergentBreaks(testdata, controldata, datayears, model, mode="years", guide="AIC"):
  if not mode in controlmodes:
    raise RCTestException("keyword 'mode' should be in "+str(controlmodes))
  if guide == "AIC":
    if mode=="flat":
      ctrl=np.ones(np.shape(controldata))
      return convergentBreaks_Inner(testdata, controldata, datayears, ctrl, model)
    elif mode=="years":
      return convergentBreaks_Inner(testdata, controldata, datayears, datayears, model)
    elif mode=="control":
      return convergentBreaks_Inner(testdata, controldata, datayears, controldata, model)
    else:
      stats = regress.analysed_regress(testdata, controldata)
      yh, r = regress.residuals(testdata, controldata, stats)
      return convergentBreaks_Inner(testdata, controldata, datayears, yh, model)
  def plot(self, x, y, years, title=0.0, save=True):
    ys = self.__ys
    xs = self.__xs
    print x, y, years
    #print self.__breakyears.keys()
    breaks=[years[0]]
    breaks.extend(np.sort(self.__breakyears.keys()))
    breaks.extend([years[-1]])
    #print breaks
    fig, ax = plt.subplots()
    if title != 0.0:
      ax.set_title(sys.argv[2]+ " "+title)
    pl = []
    #print "LOWESS",lowess(xs,ys)
    #sys.exit()
    #print "HERE"
    pl.append(ax.plot(years, y))

    for i in range(len(breaks)-1):
      try:
        pt = 0
        sYs = np.array(y[breaks[i]-breaks[0]:breaks[i+1]+1-breaks[0]])
        sXs = np.array(years[breaks[i]-breaks[0]:breaks[i+1]+1-breaks[0]])
        #print breaks[i]-breaks[0], breaks[i+1]+1-breaks[0], sXs, sYs
        #print "JIM", len(xs), len(ys)
        pt = 1
        stats=regress.analysed_regress(sYs,sXs)
        pt = 2
        yhat, resid=regress.residuals(sYs, sXs, stats)
        pt = 3
        pl.append(ax.plot(sXs, yhat, '-'))
      except Exception as e:
        print "Exception at "+str(e),pt
        
    if not save:
      plt.show()
    else:
      plt.savefig(sys.argv[2]+ " "+title+'.png')
    return breaks
        if byssummarycounts[k] == maxcount:
          maxlist.append(k)
        elif byssummarycounts[k] > maxcount:
          maxlist = [k]
          maxcount = byssummarycounts[k]
        print k, byssummarycounts[k]
        bysf.write("%s %s\n" % (str(k), str(byssummarycounts[k])))
    pl = []
    for breakk in maxlist:
      breaks=[int(b) for b in breakk[1:-1].split(',')]
      #print "BREAKS",breaks
      for i in range(1,len(breaks)):
        try:
          pt = 0
          #print "RANGE",years[0],breaks[i-1],years[0],breaks[i], range(years[0]-breaks[i-1],years[0]-breaks[i])
          sYs = np.array(rescaled[breaks[i-1]-years[0]:breaks[i] - years[0]])
          sXs = np.array(range(breaks[i-1],breaks[i]))
         # print "SXY",sYs, sXs
          #print breaks[i]-breaks[0], breaks[i+1]+1-breaks[0], sXs, sYs
          #print "JIM", len(xs), len(ys)
          pt = 1
          stats=regress.analysed_regress(sYs,sXs)
          pt = 2
          yhat, resid=regress.residuals(sYs, sXs, stats)
          pt = 3
          pl.append(plt.plot(sXs, yhat, '-'))
        except Exception as e:
          print bivariate.ExceptionInfo()
          print "Exception at ",str(e),pt
      
Exemple #5
0
def plotbreaks(x, y, breakpoints, title=None, subtitle="", breakyears=None, offset=0):
  fig = plt.figure()
  breaklist = []
  statslist = []
  for i in range(len(breakpoints)):
    if breakpoints[i] != 0:
      breaklist.append(i+offset+1)
  plt.plot(x, y, 'b-')
  lo = 0
  hi = len(y)
  #now perform and plot regressions
  for b in breaklist:
    print b
    stats = regress.analysed_regress(y[lo:b], x[lo:b])
    yh, r = regress.residuals(y[lo:b], x[lo:b], stats)
    plt.plot(x[lo:b], yh, 'r-')
    statslist.append(stats)
    lo = b
  stats = regress.analysed_regress(y[lo:hi], x[lo:hi])
  statslist.append(stats)
  yh, r = regress.residuals(y[lo:hi], x[lo:hi], stats)
  plt.plot(x[lo:hi], yh, 'r-')
  
  #plot the list of years also (this is probably from the strucchange analysis)
  if breakyears != None:
    lo = 0
    hi = len(y)
    breaklist2 = []
    statslist2 = []
    for yr in breakyears:
      #x is years
      breaklist2.append(list(x[:]).index(yr)+scoffset)
    #now perform and plot regressions
    for b in breaklist2:
      print title, "list 2", b
      stats = regress.analysed_regress(y[lo:b], x[lo:b])
      statslist2.append(stats)
      yh, r = regress.residuals(y[lo:b], x[lo:b], stats)
      plt.plot(x[lo:b], yh, 'g--')
      lo = b
    stats = regress.analysed_regress(y[lo:hi], x[lo:hi])
    statslist2.append(stats)
    yh, r = regress.residuals(y[lo:hi], x[lo:hi], stats)
    plt.plot(x[lo:hi], yh, 'g--')
#plot roger's breaklist as well
    
    
  breaklist3=[]
  statslist3=[]
  for b in rogerslist[title]:
    breaklist3.append(list(x[:]).index(b))
  lo = 0
  hi = len(y)
  for b in breaklist3:
    print title,"list 3",b
    stats = regress.analysed_regress(y[lo:b], x[lo:b])
    statslist3.append(stats)
    yh, r = regress.residuals(y[lo:b], x[lo:b], stats)
    plt.plot(x[lo:b], yh, 'r--')
    lo = b
  stats = regress.analysed_regress(y[lo:hi], x[lo:hi])
  statslist3.append(stats)
  yh, r = regress.residuals(y[lo:hi], x[lo:hi], stats)
  plt.plot(x[lo:hi], yh, 'r--')
    
  

  fig.suptitle(RunName+" "+title+"\n"+subtitle)
  fig.savefig(RunName+" "+title+'.png')  
  plt.show()
  return statslist, statslist2, statslist3
Exemple #6
0
def plotbreaksSummary(x, y, breaks, title=None, subtitle="", breakyears=None, breakyearsAve=None, offset=0):
  fig = plt.figure()
  breaklist = []
  statslist = []
  for yr in breaks:
    #x is years
    breaklist.append(list(x[:]).index(yr)+scoffset+1)
  #now perform and plot regressions

  plt.plot(x, y, 'b-')
  lo = 0
  hi = len(y)
  #now perform and plot regressions
  
  if breakyearsAve != None:
    lo = 0
    hi = len(y)
    breaklist4 = []
    statslist4 = []
    for yr in breakyearsAve:
      #x is years
      breaklist4.append(list(x[:]).index(yr)+scoffset)
    #now perform and plot regressions
    for b in breaklist4:
      print title, "list 2", b
      stats = regress.analysed_regress(y[lo:b], x[lo:b])
      statslist4.append(stats)
      yh, r = regress.residuals(y[lo:b], x[lo:b], stats)
      plt.plot(x[lo:b], yh, 'k--')
      lo = b
    stats = regress.analysed_regress(y[lo:hi], x[lo:hi])
    statslist4.append(stats)
    yh, r = regress.residuals(y[lo:hi], x[lo:hi], stats)
    plt.plot(x[lo:hi], yh, 'k--')
#plot roger's breaklist as well
    
  if breakyears != []:  
    breaklist3=[]
    statslist3=[]
    lo = 0
    hi = len(y)
    for yr in breakyears:
      #x is years
      breaklist3.append(list(x[:]).index(yr)+scoffset)
    if breaklist3 != []:
      for b in breaklist3:
        print title,"list 3",b
        stats = regress.analysed_regress(y[lo:b], x[lo:b])
        statslist3.append(stats)
        yh, r = regress.residuals(y[lo:b], x[lo:b], stats)
        plt.plot(x[lo:b], yh, 'r--')
        lo = b
      stats = regress.analysed_regress(y[lo:hi], x[lo:hi])
      statslist3.append(stats)
      yh, r = regress.residuals(y[lo:hi], x[lo:hi], stats)
      plt.plot(x[lo:hi], yh, 'r--')
    
  

  fig.suptitle(RunName+" "+title+"\n"+subtitle)
  fig.savefig(RunName+" "+title+'.png')  
  plt.show()
  return statslist, statslist3