In [1]:
import datetime as dt

import numpy as np
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
from rpy2.robjects import numpy2ri, pandas2ri

pandas2ri.activate()
numpy2ri.activate()

In [2]:
z = np.load('p013r030_r50_n423_b8.npz')
dates, Y = z['dates'], z['Y']

In [43]:
bfast = importr('bfast')
strucchange = importr('strucchange')
zoo = importr('zoo')

reform = robjects.r['reformulate']

In [4]:
bfast.bfastmonitor


Out[4]:
<DocumentedSTFunction - Python:0x7fb6beb46dd0 / R:0x5572cbd38048>

Transform to bfastts

  • dates must be a list of date strings (e.g., %Y-%m-%d), not ordinal dates.

In [9]:
dates_ts = np.array([dt.datetime.fromordinal(_d).strftime('%Y-%m-%d') for _d in dates])

In [10]:
ts = bfast.bfastts(Y[4, 0, 0], dates_ts, type='irregular')
ts_np = np.array(ts)

In [20]:
pp = bfast.bfastpp(ts, order = 2, stl = 'none')

In [44]:
form = reform(np.array(['trend', 'harmon']), response='response')
tsBreaks = strucchange.breakpoints(data=pp, formula=form, breaks=2)

In [65]:
p_tsBreaks = dict(zip(tsBreaks.names, tsBreaks))

In [68]:
p_tsBreaks['breakpoints']


Out[68]:
<FloatVector - Python:0x7fb6be8bf998 / R:0x5572ce09b3b8>
[69.000000]