----------------- Scopo: vedere come dipende il trend di stl dalla frequency P <- 18 N <- P*5 T <- 1:N + 20 S <- 10*sin((1:N)*(2*pi)/P) R <- 3*rnorm(N) X <- T+S+R ts.plot(X) acf(X,40) ----------------- tentativi sbagliati X.ts <- ts(X,frequency=1) STL <- stl(X,10) STL <- stl(X.ts,10) ----------------- frequenza 5, 15, 25 X.ts.5 <- ts(X,frequency=5) STL.5 <- stl(X.ts.5,10) plot(STL.5) X.ts.15 <- ts(X,frequency=15) STL.15 <- stl(X.ts.15,10) plot(STL.15) X.ts.25 <- ts(X,frequency=25) STL.25 <- stl(X.ts.25,10) plot(STL.25) ---------------------- P <- 18 N <- P*5 T2 <- 0.05*((1:N)-0.5*N)^2 S2 <- 10*sin((1:N)*(2*pi)/P) R2 <- 3*rnorm(N) X2 <- T2+S2+R2 ts.plot(X2) X.ts2.5 <- ts(X2,frequency=5) STL2.5 <- stl(X.ts2.5,10) plot(STL2.5) X.ts2.25 <- ts(X2,frequency=25) STL2.25 <- stl(X.ts2.25,10) plot(STL2.25) ---------------------------- estrazione trend, detrendizzazione, scoperta della periodicità ?stl plot(STL.25$time.series[,1]) plot(STL.25$time.series[,2]) plot(STL.25$time.series[,3]) TREND <- STL.25$time.series[,2] X.detrend <- X-TREND plot(X.detrend) ts.plot(X) acf(X.detrend,40) ----------------------------------- STATISTICHE DEI RESIDUI Facciamo finta che X.detrend siano i nostri residui (ignoriamo la possibilità di eliminare la componente periodica) hist(X.detrend,20,FALSE) sembra bimodale (forse sintomo di struttura) m <- mean(X.detrend) sd <- sd(X.detrend) x <- (1:100)/100*40-20 y <- dnorm(x,m,sd) lines(x,y) valore minimo delle vendite al 90% (valore superato il 90% delle volte): qnorm(0.1,m,sd) [1] -9.92472 intervallo di confidenza al 90%: qnorm(0.05,m,sd); qnorm(0.95,m,sd) grande incertezza, perché non abbiamo catturato ulteriore struttura. --------------------------------------- ELIMINAZIONE COMPONENTE PERIODICA E RICALCOLO INCERTEZZA X.ts.true <- ts(X,frequency=18) STL.true <- stl(X.ts.true,10) RESID <- STL.true$time.series[,3] plot(RESID) hist(RESID,20,FALSE) m <- mean(RESID) sd <- sd(RESID) x <- (1:100)/100*14-7 y <- dnorm(x,m,sd) lines(x,y) intervallo di confidenza al 90%: qnorm(0.05,m,sd); qnorm(0.95,m,sd) ----------------------------------------- QQ PLOT qqnorm(RESID) qqnorm(X.detrend) -------------------------------------