B <- read.table(file="bulgari.txt") lenB<-length(B[,1]) B<-B[1:lenB,1] ------------------------------ %% Lavorariamo solo su: lag <- 30 B0 <- B[1:(lenB-lag)] lenB0<-length(B0) ts.plot(B0) %%%%% C'è un TREND? x<-1:lenB0 reg <- lm(B0 ~ x) abline(reg$coefficient) %% però dipende dalla finestra temporale: x<-1:lenB0 reg <- lm(B0[230:lenB0] ~ x[230:lenB0]) abline(reg$coefficient) %% La seguente finestra sembra un buon compromesso: reg <- lm(B0[200:lenB0] ~ x[200:lenB0]) ts.plot(B0) abline(reg$coefficient) --------------------- %%% E se ipotizzassimo un trend paprabolico? reg2 <- lm(B0 ~ x+I(x^2)) summary(reg2) y<-reg2$coefficient[1]+reg2$coefficient[2]*x+reg2$coefficient[3]*x^2 B0<-as.ts(B0) y<-as.ts(y) ts.plot(B0,y) % non è plausibile (ma chi potrebbe escluderlo?) ------------------------------------------- reg <- lm(B0[200:lenB0] ~ x[200:lenB0]) y <- B0-(reg$coefficient[1]+reg$coefficient[2]*x) ts.plot(y) ts.plot(y[225:lenB0]) -------------------------------------------- %%%%% Sottraiamo il trend: ym <- B0/(reg$coefficient[1]+reg$coefficient[2]*x) ts.plot(ym) ts.plot(y[225:lenB0]) % era un trend azzeccato. ---------------------------------------- %%%%% Ora sembra ci sia una periodicità moltiplicativa. %%% Si potrebbe modellare direttamente. %%% Un altro modo è eliminare l'allargamento. %% valori estremi di y dopo il 230: 281, 0.65 355, -0.81 411, 0.84 494, -0.77 568, 1.12 640, -1.11 %% ribaltati e messi nel file estremi.txt: 281 0.65 355 0.81 411 0.84 494 0.77 568 1.12 640 1.11 est <- read.table(file="estremi.txt") xx<-est$V1 yy<-est$V2 reg.est <- lm(yy ~ xx) ts.plot(y) abline(reg.est$coefficient) ------------------------------ F <- y[225:lenB0]/(reg.est$coefficient[1]+reg.est$coefficient[2]*x[225:lenB0]) ts.plot(F) %%% Ora c'è una periodicità, senza allargamento. ------------------------------- acf(F,300) %% c'è un'elevata correlazione intorno a 140 circa. %% I seguenti comandi mostrano cosa si vede con differenze varie. ts.plot(diff(F,1)) ts.plot(diff(F,40)) ts.plot(diff(F,75)) ts.plot(diff(F,100)) ts.plot(diff(F,140)) acf(diff(F,140),200) acf(diff(F,80),200) acf(F,300) ------------------------------ length(F) F<-F[19:468] ts.plot(F) acf(F,300) per<-145 P1<-F[1:per] P2<-F[(per+1):(2*per)] P3<-F[(2*per+1):(3*per)] P1<-as.ts(P1) P2<-as.ts(P2) P3<-as.ts(P3) ts.plot(P1,P2,P3,lty=c(1,2,3)) -------------------------- Possibili idee facili: 1) una spezzata a triangolo che interpoli P1, P2, P3 (ottenuta con 2 regressioni) 2) la curva che media P1, P2, P3: Pmean <- (P1+P2+P3)/3 Pmean <-as.ts(Pmean) ts.plot(Pmean) ts.plot(Pmean,P1,P2,P3,lty=c(1,2,3,4)) ts.plot(Pmean) 3) una curva polinomiale che interpoli P1, P2, P3 con "Loess"; possiamo usare Pmean se lo abbiamo già calcolato: xxx<-1:length(Pmean) loe<-loess(Pmean~xxx) F.loe<-predict(loe, data.frame(time = seq(1, per, 1)), se = TRUE) ts.plot(F.loe$fit) ts.plot(F.loe$fit,P1,P2,P3,lty=c(1,2,3,4)) ----------------------------- ora bisogna ricostruire...