Home > other >  How to apply the known R code for data breakpoints
How to apply the known R code for data breakpoints

Time:09-15

Experiment to obtain a series of time series data (including missing value), through literature search to find a time series segmentation method to use on your own data, but because do not know how to do statistical foundation weak, the statistical small white, the great god, please help to look at, (also welcome enthusiastic great god contact: QQ: 1434493937, thank you very much)
References: https://www.sciencedirect.com/science/article/pii/S0169809519315972
Statistical methods R code:
N_break_point & lt; - the function (serie, n_max=1, n_period=10,
Seed=FALSE, auto_select=FALSE,
Alpha=NULL, method='SNHT, DSTR=' norm ') {
# the select method
If (the exists (x='. The Random. Seed ')) {
Old_random & lt; -. The Random. Seed
}
Set. The seed (9658)
if(! Is the logical (seed)) {
If (length (seed)!={n_max)
Stop (' The given seed is not supported. If seed is given must be of length n_max ')
}
}

{
If (method=='pettit') {
Fun & lt; - pettit
} else if (method=='student') {
Fun & lt; - stu
} else if (method=='the mann - Whitney) {
Fun & lt; - man. Whi
} else if (method=='buishand') {
Fun & lt; - the function (x, n_period) {
Return (Buishand_R (serie=x, n_period=n_period, DSTR=DSTR))
}
} else if (method=='SNHT) {
Fun & lt; - the function (x, n_period) {
Return (SNHT (serie=x, n_period=n_period, DSTR=DSTR))
}
} else {stop (' Not supported method ')}
}


Target & lt; - the as. The vector (serie)
N_targ & lt; - length (target)
Isna & lt; - the as. Numeric (is. Na (target))
N_period_2 & lt; - the as. The integer (n_period)
Ii & lt; - c (rep (0, n_period_2), rollapply (isna, width=n_period + 1, sum), rep (0, n_period_2))
Ii & lt; - which (ii & gt;=(n_period_2/2))
If (length (ii) & gt; 1) {
Na_break & lt; - c (ii, n_targ + 1)
Ii_aux & lt; - ii [2: length (ii)] - ii [1: length (ii) - 1)]
Ii_aux & lt; - ii_aux & lt; 10
Jump & lt; - c (ii [1] } else if (length (ii)==1) {
Na_break & lt; - c (ii, n_targ + 1)
Jump & lt; - c (ii<1 - ii< n_period_2, n_targ +; N_period_2)
} else {
Na_break & lt; - n_targ + 1
Jump & lt; - FALSE
}
New_target & lt; - target
New_n_targ & lt; - n_targ
The output & lt; - the list ()
N_max_new & lt; - n_max
Outputcont & lt; 0
For (new_serie in 1: length (na_break)) {
N_max & lt; - n_max_new
If (new_serie==1) {
If (jump [1]) {next}
Ini & lt; 0
Target & lt; - new_target [1: (na_break [new_serie] 1)]
} else {
If (jump [new_serie]) {next}
Ini & lt; - na_break] [new_serie - 1-1
Target & lt; - new_target [na_break [] new_serie - 1: (na_break [new_serie] 1)]
}
Outputcont & lt; - outputcont + 1

N_targ & lt; - length (target)
If ((n_max + 1) * n_period & gt; N_targ - 2) {
N_max & lt; -/n_targ % % n_period - 1
If (n_max & lt; 1) {
If (length (na_break) & gt; 1) {
Warning ((' Not possible to find their context in part of the serie, too short '))
Outputcont & lt; - outputcont - 1; Next
} else {
Stop (' Not possible to find their context, target serie too short ')
}
}
Warning (paste0 (' the given n is too big for the target and n_period length, ', n_max, 'will be use as maximal amount of breakpoints'))
}
Output_aux & lt; - the list (breaks=list (), p.value=https://bbs.csdn.net/topics/list (), n=list ())
For (n in 1: n_max) {
If (is the logical (seed)) {
Breaks & lt; - the as. The integer (1: n * (n_targ/(n + 1))) + 1
} else {
If (length (seed [[n]])==n) {
Breaks & lt; - seed [[n]]
} else {
Warning (paste (' The seed provided at ', n 'breaks differs in length equal space break seed will be home use', sep="))
Breaks & lt; - the as. The integer (1: n * (n_targ/(n + 1))) + 1
}
}
Breaks & lt; - sort (breaks, decreasing=F)
p <- rep (1, length (breaks))
Breaks_old & lt; - rep (0, length (breaks))
If (n==1) {
Ff & lt; - fun (target, n_period)
Breaks & lt; Ff $breaks
p } else {
No_problem & lt; T
-Iters & lt; 0
Breaks_old_old_old & lt; - breaks
Breaks_old_old & lt; - breaks
P_old_old & lt; -p
P_old & lt; -p
While (any) (breaks_old!=breaks) & amp; No_problem) {
Iters & lt; - iters + 1
Breaks_old_old_old & lt; - breaks_old_old
Breaks_old_old & lt; - breaks_old
Breaks_old & lt; - breaks
P_old_old & lt; - p_old
P_old & lt; -p
For (I in 1: n) {
If (I==1) {
Aux & lt; - the target (1: (breaks)] [2] - 1
Break_aux & lt; 0
{} else if (I==n)
Aux & lt; - target [breaks [n - 1] : n_targ]
Break_aux & lt; - breaks] [n - 1-1
} else {
Aux & lt; - target [breaks [I - 1] : [breaks [I + 1) - 1)]
Break_aux & lt; - breaks] [I - 1-1
}

Ff & lt; - fun (aux, n_period)
Breaks [I] P [I] }
If (iters & gt; 3) {
If (all (breaks==breaks_old_old)) {
No_problem & lt; The -f
Warning (paste0 (' several critical point found at n=', n))
Breaks & lt; - NULL
Next
} else if (all (breaks==breaks_old_old_old)) {
No_problem & lt; The -f
Warning (paste0 (' several critical point found at n=', n))
Breaks & lt; - NULL
Next
}
}
}
}
If (is null (breaks)) {
Output_aux $breaks [[n]]
  • Related