forked from amypitts01/REU2018
-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
512 lines (512 loc) · 15.6 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
return(prod(product_runs))
}
old_new_product_death = function(k){
for (j in k){
product_runs = append(product_runs,(n-3*ma-(q1-1)*(2*ma)+j))
}
return(prod(product_runs))
}
prior = NA
birth_old_to_new_ratio = (factorial(num_of_bkpts+1)*q2(dpois(k_ends_new,lambda)))/(old_new_product_birth(num_of_bkpts)*prior)
birth_new_to_old_ratio = (factorial(num_of_bkpts)*q1(dpois(k_ends,lambda)))/(product_death(num_of_bkpts)*prior)
death_new_to_old_ratio = (factorial(num_of_bkpts)*q2*(dpois(k_ends,lambda)))/(product_death(num_of_bkpts)*prior)
death_old_to_new_ratio = (factorial(num_of_bkpts-1)*make_k*(dpois(k_ends_new,lambda)))/(old_new_product_death(num_of_bkpts)*prior)
#
#end of birth and death ratios
delta_bic = (-2*new_loglik + log(n)*(length(k_ends_new)-1)*(3+ma)) - (-2*old_loglik + log(n)*(length(k_ends)-1)*(3+ma))
ratio = (-1*delta_bic/2) + (log(q1*dnorm(length(k_ends_new)-2,epsilon_list[ma],init_sigma2)) - log(q2*dnorm(length(k_ends)-2,epsilon_list[ma],init_sigma2))) #both dnorms are the same, make sure that this is correct
u_ratio = log(runif(1)) #random number from 0 to 1 taken from a uniform distribution and then log transformed
if(abs(delta_bic) == Inf){ #safe guard against random models creating infinite ratios
k_ends <<- k_ends #old
} else if(ratio > u_ratio) {
k_ends <<- k_ends_new #new
} else {
k_ends <<- k_ends #old
}
if(progress == TRUE){
setTxtProgressBar(burn_progress, i)
}
}
#initializing matrices/storage objects for final Metropolis-Hasting
all_k_best = data.frame(matrix(ncol=(length(k_ends)-2),nrow=0))
if(fit_storage == TRUE){
bar_v = 0
bar_beta = 0
fit = 0
all_fits = data.frame()
all_MSE = data.frame()
}
all_BIC = data.frame()
accept_count = 0
#setting up counters for final Metropolis-Hasting
type = "0"
a.count <<- 0
s.count <<- 0
m.count <<- 0
j.count <<- 0
add.accept.count <<- 0
sub.accept.count <<- 0
move.accept.count <<- 0
jiggle.accept.count <<- 0
#getting constants for qs for final Metropolis-Hasting
starting_bkpts = length(k_ends) - 1 #most probable number of breakpoints based on starting info
starting_nfree = length(freeObservations(k_ends,ma))
burn_in = 50
make_murder_p = 0.5
percent = 0.02
lambda = 1
tao = abs(1)
bs(1), alpha = 0, mu = 1.5, jump_p = 0.25, ma = 1, progress = TRUE, fit_storage = TRUE)
jump_p = 0.25
ma = 1
mu = 1.5,
mu = 1.5
alpha = 0
fit_storage = TRUE
progress = TRUE
burn_in = 50
make_murder_p = 0.5
lambda = 1
percent = 0.02
y = rnorm(1,0,1)
mu = 1
alpha = .5
epsilon = c(rnorm(1,0,1))
epsilon = function(t){ # t MUST be greater than or equal to 2
for (i in 2:t){
epsilon[i-1] = rnorm(1,0,1)
}
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon(2) # + .............
}
y[i]= mu + epsilon[1] + alpha*epsilon(2) # + .............
epsilon[1]
epsilon = function(t){ # t MUST be greater than or equal to 2
epsilon_t = c(epsilon)
for (i in 2:t){
epsilon_t[i-1] = rnorm(1,0,1)
}
}
y[i]= mu + epsilon + alpha*epsilon(2) # + .............
epsilon = rnorm(1,0,1)
y[i]= mu + epsilon + alpha*epsilon(2)
epsilon = function(t){ # t MUST be greater than or equal to 2
epsilon_t = c(epsilon)
for (i in 2:t){
epsilon_t[i-1] = rnorm(1,0,1)
}
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon(2) # + .............
}
epsilon = rnorm(1,0,1)
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon(2) # + .............
}
epsilon = function(t){ # t MUST be greater than or equal to 2
epsilon_t = c(epsilon)
for (i in 2:t){
epsilon_t[i-1] = rnorm(1,0,1)
}
}
mu = 1
y = rnorm(1,0,1)
alpha = .5
epsilon = rnorm(1,0,1)
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon(2) # + .............
}
mu = 1
y = rnorm(1,0,1)
alpha = .5
epsilon = rnorm(1,0,1)
epsilon_t = function(t){ # t MUST be greater than or equal to 2
epsilon_t = c(epsilon)
for (i in 2:t){
epsilon_t[i-1] = rnorm(1,0,1)
}
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
mu = 1
y = rnorm(1,0,1)
alpha = .5
epsilon = rnorm(1,0,1)
epsilon_t = function(t){ # t MUST be greater than or equal to 2
epsilon_t = c(epsilon)
for (i in 1:t){
epsilon_t[i] = rnorm(1,0,1)
}
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
mu = 1
y = rnorm(1,0,1)
alpha = .5
epsilon = rnorm(1,0,1)
for (i in 1:t){
epsilon_t[i] = rnorm(1,0,1)
}
epsilon_t = function(t){ # t MUST be greater than or equal to 2
epsilon_t = c(epsilon)
for (i in 1:t){
epsilon_t[i] = rnorm(1,0,1)
}
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
epsilon_t(2)
epsilon_t(2)
mu
epsilon
alpha
epsilon_t = function(t){ # t MUST be greater than or equal to 2
epsilon_t = c(epsilon)
for (i in 1:t){
epsilon_t[i] = rnorm(1,0,1)
}
return(epsilon_t[t])
}
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
epsilon_t[i] = rnorm(1,0,1)
epsilon_t = function(t){ # t MUST be greater than or equal to 2
epsilon_t = c(epsilon)
for (i in 1:t){
epsilon_t[i] = rnorm(1,0,1)
}
return(epsilon_t[t])
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
beta=-1
y=rnorm(1,0,1)
for(i in 2:32){y[i]=beta*y[i-1]+rnorm(1,0,1)}
for(i in 33:90){y[i]=beta*y[i-1]+rnorm(1,0,3)}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
for (i in 1:90){
y[i]= mu + epsilon + alpha*epsilon_t(2) # + .............
}
new_baar_fit = function(current_data,breakpoints){
new_fits = c() #list of new fitted values
spliced_fitted_arima = ts() #time series
new_time = c() #list of time vectors for each bkpt (for spliced_data)
new_data = c() #list of data_45 vectors for each bkpt (for spliced_data)
for (i in breakpoints){
splice_data = function(current_data){ #Why does this function need to be called for each bkpt?
for (j in 1:ncol(current_data)){
if (j == 1){ #if j is time
append(new_time,c(current_data[,j][1:i]))
} else if (j==2) {
append(new_data,c(current_data[,j][1:i]))
}
}
new_start = data.frame(new_time,new_data) #data frame of time and data_45 vectors for each bkpt
return(new_start)
}
spliced_data = splice_data(current_data)
#append to list of arima'ed time series
spliced_fitted_arima = arima(new_start$new_data,order = c(2,0,0)) #Something wrong here
new_fits = fitted(spliced_fitted_arima)
}
return(new_fits)
}
#WORK IN PROGRESS ---------------------------------------------------
fits = new_baar_fit(current_data,A3)
library(devtools)
#install_version("FitAR", version = "1.94", repos = "http://cran.us.r-project.org")
#library(FitAR)
library(forecast)
library(strucchange)
library(stats)
library(MASS)
par(mar=c(1,1,1,1))
setwd("/Users/mellm/github/REU2022/test_Cases")
suicide<-read.csv("suicideRatesData.csv")
attach(suicide)
setwd("/Users/mellm/github/REU2022/")
source("ar_bai_perron.r")
source("data_for_trials.r")
source("Variations_of_bars/fixedbaar1.r")
#fit data with ARIMA
single_model <- arima(suicide$X15.24.years, order=c(2,0,0))
single_fitted <- fitted(single_model)
test_data_45 = function(){
data_45=suicide$X15.24.years
time = c(1:37)
test_data_45 = data.frame(time, data_45)
return(test_data_45)
}
iterations=1000
runs=10
L=matrix(NA,nrow=iterations,ncol=runs)
A=NA
M=NA
B=NA
for(i in 1:runs){
current_data = test_data_45()
break_p = breakpoints(current_data[,2] ~ current_data[,1], breaks = 3, h = 0.1)
starting_breakpoints = break_p$breakpoints
test1=baar(starting_breakpoints,1:37,current_data[,2],iterations, 15, jump=0.25, ar=3)
print(i)
L[,i]=test1$NumBkpts
M[i]=length(starting_breakpoints[!is.na(starting_breakpoints)])
A=c(A,(unlist(test1$Breakpoints))) #Baar location of breakpoints
B=c(B,(starting_breakpoints)) #B-P
}
A2=A[!is.na(A)]
B2=B[!is.na(B)]
mean(L)
sd(L)
mean(M)
sd(M)
current_data = ts(current_data)
A3 = sort(unique(A2))
#WORK IN PROGRESS ---------------------------------------------------
fits = new_baar_fit(current_data,A3)
new_start = data.frame(new_time,new_data)
new_baar_fit = function(current_data,breakpoints){
new_fits = c() #list of new fitted values
spliced_fitted_arima = ts() #time series
new_time = c() #list of time vectors for each bkpt (for spliced_data)
new_data = c() #list of data_45 vectors for each bkpt (for spliced_data)
for (i in breakpoints){
splice_data = function(current_data){ #Why does this function need to be called for each bkpt?
for (j in 1:ncol(current_data)){
if (j == 1){ #if j is time
append(new_time,c(current_data[,j][1:i]))
} else if (j==2) {
append(new_data,c(current_data[,j][1:i]))
}
}
new_start = data.frame(new_time,new_data) #data frame of time and data_45 vectors for each bkpt
return(new_start)
}
spliced_data = splice_data(current_data)
#append to list of arima'ed time series
spliced_fitted_arima = arima(new_start$new_data,order = c(2,0,0)) #Something wrong here
new_fits = fitted(spliced_fitted_arima)
}
return(new_fits)
}
#WORK IN PROGRESS ---------------------------------------------------
fits = new_baar_fit(current_data,A3)
current_data
current_data = current_data
current_data = test_data_45()
current_data = current_data
as.data.frame(current_data)
is.data.frame(current_data)
is.data.frame(single_model)
is.ts(single_model)
typeof(single_model)
current_data = as.list(current_data)
current_data
current_data[,1]
current_data[1]
current_data[1][1:5]
current_data[1][2]
current_data[1][[2]]
current_data[[1]]
current_data[[1]][1:5]
is.list(current_data[[1]])
typeof(current_data[[1]])
c(current_data[[1]][1:5])
typeof(current_data[[1]][1:5])
typeof(c(current_data[[1]][1:5]))
as.list(current_data[[1]][1:5])
append(current_data[[1]][1:5], 3)
new_start = list(new_time,new_data) #data frame of time and data_45 vectors for each bkpt
new_time = c() #list of time vectors for each bkpt (for spliced_data)
new_data = c() #list of data_45 vectors for each bkpt (for spliced_data)
new_start = list(new_time,new_data) #data frame of time and data_45 vectors for each bkpt
new_baar_fit = function(current_data,breakpoints){
new_fits = c() #list of new fitted values
spliced_fitted_arima = list() #list
new_time = c() #list of time vectors for each bkpt (for spliced_data)
new_data = c() #list of data_45 vectors for each bkpt (for spliced_data)
for (i in breakpoints){
for (j in 1:ncol(current_data)){
if (j == 1){ #if j is time
append(new_time,current_data[[j]][1:i])
} else if (j==2) {
append(new_data,current_data[[j]][1:i])
}
}
}
new_start = list(new_time,new_data) #data frame of time and data_45 vectors for each bkpt
spliced_fitted_arima = arima(new_start$new_data,order = c(2,0,0)) #Something wrong here
new_fits = fitted(spliced_fitted_arima)
}
return(new_fits)
new_fits = c() #list of new fitted values
new_baar_fit = function(current_data,breakpoints){
new_fits = c() #list of new fitted values
spliced_fitted_arima = list() #list
new_time = c() #list of time vectors for each bkpt (for spliced_data)
new_data = c() #list of data_45 vectors for each bkpt (for spliced_data)
for (i in breakpoints){
for (j in 1:ncol(current_data)){
if (j == 1){ #if j is time
append(new_time,current_data[[j]][1:i])
} else if (j==2) {
append(new_data,current_data[[j]][1:i])
}
}
}
new_start = list(new_time,new_data) #data frame of time and data_45 vectors for each bkpt
spliced_fitted_arima = arima(new_start$new_data,order = c(2,0,0)) #Something wrong here
new_fits = fitted(spliced_fitted_arima)
}
new_baar_fit = function(current_data,breakpoints){
new_fits = c() #list of new fitted values
spliced_fitted_arima = list() #list
new_time = c() #list of time vectors for each bkpt (for spliced_data)
new_data = c() #list of data_45 vectors for each bkpt (for spliced_data)
for (i in breakpoints){
for (j in 1:ncol(current_data)){
if (j == 1){ #if j is time
append(new_time,current_data[[j]][1:i])
} else if (j==2) {
append(new_data,current_data[[j]][1:i])
}
}
}
new_start = list(new_time,new_data) #data frame of time and data_45 vectors for each bkpt
spliced_fitted_arima = arima(new_start$new_data,order = c(2,0,0)) #Something wrong here
new_fits = fitted(spliced_fitted_arima)
return(new_fits)
}
#WORK IN PROGRESS ---------------------------------------------------
fits = new_baar_fit(current_data,A3)
#WORK IN PROGRESS ---------------------------------------------------
fits = new_baar_fit(current_data,A3)
ncol(current_data)
length(current_data)
new_baar_fit = function(current_data,breakpoints){
new_fits = c() #list of new fitted values
spliced_fitted_arima = list() #list
new_time = c() #list of time vectors for each bkpt (for spliced_data)
new_data = c() #list of data_45 vectors for each bkpt (for spliced_data)
for (i in breakpoints){
for (j in 1:length(current_data)){
if (j == 1){ #if j is time
append(new_time,current_data[[j]][1:i])
} else if (j==2) {
append(new_data,current_data[[j]][1:i])
}
}
}
new_start = list(new_time,new_data) #data frame of time and data_45 vectors for each bkpt
spliced_fitted_arima = arima(new_start$new_data,order = c(2,0,0)) #Something wrong here
new_fits = fitted(spliced_fitted_arima)
return(new_fits)
}
#WORK IN PROGRESS ---------------------------------------------------
fits = new_baar_fit(current_data,A3)
new_start = cbind(c(1:length(as.numeric(new_time))), as.numeric(new_data)) #combining time and data inputs
View(new_start)
new_time = c(1,2,4) #list of time vectors for each bkpt (for spliced_data)
new_data = c(1,2,3) #list of data_45 vectors for each bkpt (for spliced_data)
new_start = cbind(c(1:length(as.numeric(new_time))), as.numeric(new_data)) #combining time and data inputs
new_start = cbind(c(as.numeric(new_time)), as.numeric(new_data)) #combining time and data inputs
new_start = ts(c(as.numeric(new_time)), as.numeric(new_data)) #combining time and data inputs
spliced_fitted_arima = arima(new_start$new_data,order = c(2,0,0)) #Something wrong here