Академический Документы
Профессиональный Документы
Культура Документы
12/9/2019
library(Kendall)
library(quantmod)
##
## Attaching package: 'zoo'
library(readxl)
y = ts(ipc,frequency=12,start=c(2012,1))
MannKendall(ipc)
np = length(ipc)
T = length(ipc)
ipci = ts(ipc[1:(T-12)],frequency=12,start=c(2012,1))
ipcf = ts(ipc[(T-12+1):T],frequency=12,start=c(2017,7))
t = seq(1,(T-12),1)
t2 = t*t
t3 = t2*t
summary(m.cd)
##
## Call:
## lm(formula = ipci ~ t + t2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.2918 -0.7188 -0.0147 0.5371 3.0900
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.098e+02 4.817e-01 228.034 < 2e-16 ***
## t 1.147e-01 3.045e-02 3.767 0.000344 ***
## t2 4.637e-03 4.043e-04 11.471 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.325 on 69 degrees of freedom
## Multiple R-squared: 0.982, Adjusted R-squared: 0.9815
## F-statistic: 1886 on 2 and 69 DF, p-value: < 2.2e-16
summary(m.cu)
##
## Call:
## lm(formula = ipci ~ t + t2 + t3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.93317 -0.77148 0.03668 0.84054 2.18523
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.121e+02 5.253e-01 213.445 < 2e-16 ***
## t -2.472e-01 6.189e-02 -3.995 0.000162 ***
## t2 1.695e-02 1.963e-03 8.635 1.54e-12 ***
## t3 -1.124e-04 1.768e-05 -6.359 1.99e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.057 on 68 degrees of freedom
## Multiple R-squared: 0.9887, Adjusted R-squared: 0.9882
## F-statistic: 1989 on 3 and 68 DF, p-value: < 2.2e-16
r2 = m.cd$residuals
r3 = m.cu$residuals
f2 = m.cd$fitted.values
f3 = m.cu$fitted.values
np = length(ipci)
fecha = seq(as.Date("2012/01/01"), as.Date("2017-12-01"), by="months")
fechas = strptime(as.character(fecha), "%Y-%m-%d")
par(mfrow=c(1,1))
plot(fechas,ipci, xaxt="n",panel.first = grid(),type='l',ipclab='')
(c(AIC(m.cd),AIC(m.cu)))
par(mfrow=c(2,1))
plot(density(r2))
plot(density(r3))
Las siguientes graficas nos muestran como es el comportamiento de los modelos
estimados siguen casi de forma perfecta una normal lo cual es muy bueno para el
modelo.
qqnorm(r2)
qqline(r2)
qqnorm(r3)
q
qline(r3)
En la siguiente grafica vemos como los errores se ajustan con la distribución
normal requerida, aunque algunos datos no ajustan tanto se pueden ajustar de
mejor forma:
tt = seq((T-12+1),T,1)
tt2 = tt*tt
tt3 = tt2*tt
pr2 = predict(m.cd,
data.frame(t=tt,t2=tt2))
pr3 = predict(m.cu,
data.frame(t=tt,t2=tt2,t3=tt3))
par(mfrow=c(1,1))
plot(tt,ipcf,type='o',ipclim=c(min(ipcf),max(pr3)))
lines(tt,pr2,col='red')
lines(tt,pr3,col='blue')
pr = predict(m.cu,
data.frame(t=tp,t2=tp2,t3=tp3))
(c(tail(y),pr))
## 1
2
## 142.0984 142.2686 142.5033 142.6748 142.8420 143.2668 144.5141
144.6994
## 3 4 5 6
## 144.8605 144.9969 145.1078 145.1925
attach(B)
ipcw=loess(ipc ~ time(ipc))
np = length(ipc)
fecha = seq(as.Date("2012/01/01"), as.Date("2018/12/01"),
by="months")
fechas = strptime(as.character(fecha), "%Y-%m-%d")
plot(fechas,ipc, xaxt="n",panel.first = grid(),type="l",ipclab="")
axis.POSIXct(1, at=seq(as.Date(fechas[1]),as.Date(fechas[np]),
"months"), format="%m/%y")
axis.POSIXct(1, at=seq(as.Date(fechas[1]),as.Date(fechas[np]),
"years"), labels = FALSE, tcl = -0.2)
lines(fechas,ipcw$fitted, xaxt="n", panel.first = grid(),
type="l",col= "red",lwd=2)
De acuerdo con el método de Loees que sirve para poder ver si nuestra serie
de tiempo tiene una regresión polinomica en forma de parabola o lineal, en
este caso es lineal (calculo de minimos cuadrados ponderados) y es la major
predicción:
Taller parte 2
Alexandra Diaz Angie Rodriguez David Zapata
15/9/2019
library (fpp2)
library(quantmod)
##
## Attaching package: 'zoo'
getSymbols("COLNGDPRPCH",src="FRED")
## [1] "COLNGDPRPCH"
c <- COLNGDPRPCH
z <- ma(COLNGDPRPCH, 5)
zz <-cbind(c,z)
autoplot(y, series="Data") +
autolayer(ma(y,5), series="5-ma") +
xlab("año") + ylab("PIB") +
ggtitle("PIBCOLD") +
scale_colour_manual(values=c("Data"="grey50","5-MA"="red"),
breaks=c("Data","5-ma"))
Corremos una media movil de orden 4 (media movil de 4 porque los datos son
trimestrales) y luego la media movil de la media movil:
xy <- cbind(y,ma4,ma2x4)
View(xy)
library(fTrading)
##
## Attaching package: 'timeSeries'
##
## Attaching package: 'fBasics'
##
## Attaching package: 'fTrading'
y = ts(COLNGDPRPCH)
plot(y)
lines(y, col="blue")
y = ts(COLNGDPRPCH, frequency=4, start=c(2012,1), end=c(2018,2))
library(stats)
plot(as.ts(decompose_y$random))
plot(decompose_y)
library(seasonal)
##
## Attaching package: 'seasonal'
autoplot(fit) +
ggtitle("X11 consumer price index")
autoplot(y, series="Data") +
autolayer(trendcycle(fit), series="Trend") +
autolayer(seasadj(fit), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("DomesticPib") +
scale_colour_manual(values=c("orange","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
library(seasonal)
y %>% seas() %>%
autoplot() +
ggtitle("DomesticPib")