Anda di halaman 1dari 8

require (forecast)

 Require function is used to load the namespace of the package with name package and attach it
on the search list. Require is designed for use inside other functions; it returns FALSE and gives a
warning if the package does not exist. In this require function is used to call the forecast
function.
 Forecast is a generic function for forecasting from time series or time series models. The
function invokes particular methods which depend on the class of the first argument.

data <- c(-16.01, -0.78, -1.4, 8.93, 12.52, 21.24, 25.07, 21.75, 14.01, 2.97, -1.32, -10.57,

-3.24, 3.01, 12.46, 20.51, 31.12, 32.18, 33.9, 32.48, 26.57, 19.45, 6.91, 12.18,

6.01, 16.13, 20.01, 35.75, 40.09, 43.55, 41.44, 44.81, 32.14, 24.99, 22.07, 23.31,

25.07, 29.13, 30.01, 49.83, 54.69, 52.67, 52.22, 56.67, 47.28, 48.22, 29.9, 35.45,

37.83, 36.92, 46.49, 59.86, 60.92, 65.24, 67.75, 63.65, 61.9, 42.83, 49.58, 43.44,

44.8, 51.46, 57.6, 64.89, 73.57, 76.69, 83.88, 80.61, 71.46, 55.33, 55.27, 58.8,

52.19, 57.19, 67.75, 72.16, 86.52, 97.44, 96.01, 96.18, 81.77, 78.53, 66.71, 73.01,

70.22, 80.39, 73.24, 87.83, 97.06, 98.4, 108.71, 98.65, 98.59, 82.25, 79.59, 76.79,

78.41, 80.78, 92.29, 99.15, 110.35, 121.04, 111.79, 116.9, 110.41, 105.29, 99.18,

98.3, 91.68, 98.62, 103.22, 108.61, 121.98, 123.29, 130.16, 126.84, 128.44, 113.76,

100.89, 100.21, 104.83, 113.69, 119.83, 123.34, 136.99, 137.27, 145.61, 134.64,

129.19, 125.83, 113.34, 116.76, 116.88, 117.75, 133.71, 132.49, 149.11, 156.46,

148.08, 147.85, 144.63, 139.41, 131.72, 120.22)

 In this step we are creating the vector with all the data points.

plot(data, pch = 4)

 plot function is used for plotting the objects in R. In this we are plotting all the data points
stored in variable data, against their indexes. We have used the pch options in order to specify
the symbols to data points.

plot(data, pch = 19, type = "b")

 We have plotted another graph the


 After plotting the graph with both points and lines, we can see that there is a sequential
structure to the data.

data.ts <- ts(data, start = 2000, frequency = 12)


ts.plot(data.ts)

data.dec <- decompose(data.ts, type="additive")

plot(data.dec)

data.dec

## Holt Winters

data.holt <- HoltWinters(data.ts, seasonal = "additive")

plot(data.holt)

data.holt.pred <- predict(data.holt, n.ahead = 4 * 12)

ts.plot(data.ts, data.holt.pred, lty=1:2)

## Time Series Regression

LM <- lm( data.ts ~ time(data.ts))

summary(LM)

plot(data.ts)

abline(LM$coefficients[1], b=LM$coefficients[2])

plot(data.ts)

points(time(data.ts), LM$fitted.values, pch = 4, col = "red")


## Dummy Variables

LM.dummy <- lm( data.ts ~ time(data.ts) + seasonaldummy(data.ts))

summary(LM.dummy)

plot(data.ts)

pred.ts <- ts(LM.dummy$fitted.values, start = 2000, frequency = 12)

points(pred.ts, col="red", pch=4)

plot.colors <- c("red", "blue", "green", "purple", "orange", "dark red", "dark green", "grey",

"brown", "deep pink", "gold")

plot(data.ts)

points(x = time(pred.ts), y = pred.ts, col=plot.colors, pch=4)

legend("topleft", months(as.Date("2000-01-01")+(1:12)*28), cex = 0.7, col=plot.colors, lty=1, pch=4 )

## Done by Hand Without ts

X <- as.numeric(time(data.ts))

Y <- data

Z <- factor(rep(month.name, 12))

data.DF <- data.frame(X, Y, Z)

plot(data.DF[,1:2], ylim=c(-5,160), xlim=c(2000, 2013))

## Regular Regression

LM.old <- lm( Y ~ X , data = data.DF)

summary(LM.old)
points(data.DF$X, LM.old$fitted.values, pch = 4, col = "red")

## Prediction with Regular Regression

new <- data.frame(X = seq(2012, 2013, by = 0.1))

new.fit <- predict( LM.old, new, interval = "predict")

points(x=new[,], y=new.fit[,1], col="red", pch = 19)

## Regression with Dummy Variables

LM.old.dummy <- lm( Y ~ X + Z , data = data.DF)

plot(data.DF[,1:2], ylim=c(-5,160), xlim=c(2000, 2013))

points(data.DF$X, LM.old.dummy$fitted.values, col="red", pch = 4)

## Prediction with Dummy Variables

plot(data.DF[,1:2], ylim=c(-5,160), xlim=c(2000, 2013))

points(data.DF$X, LM.old.dummy$fitted.values, col="red", pch = 4)

new <- data.frame(X = seq(2012, 2013, by = 0.1), Z = data.DF$Z[1:11])

new.fit <- predict( LM.old.dummy, new, interval = "predict")

points(x=new[,"X"], y=new.fit[,1], col="red")

## Using forecast TSLM

LM.forecast <- tslm( data.ts ~ trend + season )

summary(LM.forecast)

plot(data.ts)
points(x = time(pred.ts), y = LM.forecast$fitted.values, col="red", pch=4)

plot(forecast(LM.forecast, h=20))

cycle(data.ts)

window(data.ts, start = c(2000, 1)) ## All Data

window(data.ts, start = c(2000, 1), end = c(2011,1), deltat=1) ## Just Jan

window(data.ts, start = c(2000, 2), end = c(2011,2), deltat=1) ## Just Feb

seasonplot(data.ts, col = plot.colors)

legend("topleft", months(as.Date("2000-01-01")+(1:12)*28), cex = 0.7, col=plot.colors, lty=1, pch=20 )

for(i in 1:12)

if(i==1){

plot(window(data.ts, start = c(2000, i), end = c(2011,i), deltat=1), col=plot.colors[i],


ylim=range(data.ts), ylab = "data.ts")

}else{

lines(window(data.ts, start = c(2000, i), end = c(2011,i), deltat=1), col=plot.colors[i])

}
}

legend("topleft", months(as.Date("2000-01-01")+(1:12)*28), cex = 0.7, col=plot.colors, lty=1)

data <- c(166.4, 174.71, 293.77, 357.84, 357.82, 425.87, 388.27, 346.21, 338.53, 290.11, 277.15,

211.24, 209.29, 279.27, 237.39, 359.69, 376.88, 494.76, 500.57, 384.9, 378.02, 380.47,

261.44, 260.83, 247.85, 233.73, 404.03, 341.44, 503.14, 585.68, 618.4, 364.22, 463.31,

331.69, 327.31, 289.41, 309.69, 345.95, 344.65, 451.89, 589.75, 588.58, 569.15, 498.73,

523.76, 481.18, 374.4, 308.25, 284.93, 413.98, 394.31, 555.39, 583.05, 693.09, 757.77,

641.02, 550.38, 449.45, 371.99, 405.51, 401.7, 417.63, 556.9, 589.97, 716.86, 779.64,

827.88, 622.32, 596.16, 539.67, 527.61, 447.53, 365.81, 448.26, 498.53, 639.58, 829.51,

847.85, 938.71, 644.13, 698.87, 562.74, 506.27, 497.06, 489.94, 565.89, 617.32, 818.43,

694.08, 1038.64, 977.44, 898.57, 821.92, 520.79, 538.97, 454.86, 493.02, 642.99, 617.44,

899.6, 838.48, 1203.36, 1196.74, 909.78, 925.14, 691.49, 657.77, 557.33, 714.66, 595.66,

836.99, 874.48, 1123.95, 987.75, 1422.83, 1168.57, 991.16, 1011.82, 645.89, 798.4, 694.36,

809.15, 892.91, 1130.83, 1454.34, 1547.41, 1406.15, 1374.14, 964.71, 809.46, 680.92,

845.87, 721.14, 822.52, 1042.68, 1402.96, 1305.86, 1847.34, 1485.7, 1521.19, 1188.25,

1056.92, 828.16, 759.55)

 In this step we have to create the vector with new data points.

data.ts <- ts(data, start = 2000, frequency = 12)

 By using ts function we will create an time series object.

plot(data.ts)

 Plotting the time series data

data.dec <- decompose(data.ts, type = "multiplicative")

 Decomposition of additive time series to estimate seasonal effect and plotting it

plot(data.dec)

 Plotting the decomposed data


data.dec

 To view the value of the decomposed data

## Holt Winters

data.holt <- HoltWinters(data.ts, seasonal = "multiplicative")

 holtwinters function is used to find the Unknown parameters which is determined by minimizing
the squared prediction error as data here exhibits Multiplicative seasonality.

plot(data.holt)

data.holt.pred <- predict(data.holt, n.ahead = 4 * 12)

 prediction for 4 years ahead. till 2012 we have data and next 4 years prediction is done via
predict

ts.plot(data.ts, data.holt.pred, lty=1:2)

data.trans <- log(data.ts)

plot(data.trans)

summary(LM.forecast <- tslm( data.trans ~ trend + season ) )

plot(data.trans)

points(LM.forecast$fitted.values, pch = 4, col = "red")

plot(data.ts, type="b", pch=19, cex = 0.5)

points(time(data.ts), exp(LM.forecast$fitted.values), col="red", pch=4 )

## Now Forecast
forc.mult <- forecast(LM.forecast, h=20)

plot(forc.mult)

plot(data.ts, type="b", pch=19, cex = 0.5, xlim=c(2000, 2015), ylim=c(150, 2100))

points( 2012 + (1:20)/12 , exp(forc.mult$mean), col="red", pch = 19, type = "b")

## Cycles

seasonplot(data.ts, col = plot.colors)

legend("topleft", months(as.Date("2000-01-01")+(1:12)*28), cex = 0.7, col=plot.colors, lty=1, pch=20 )

for(i in 1:12)

if(i==1){

plot(window(data.ts, start = c(2000, i), end = c(2011,i), deltat=1), col=plot.colors[i],


ylim=range(data.ts), ylab = "data.ts")

}else{

lines(window(data.ts, start = c(2000, i), end = c(2011,i), deltat=1), col=plot.colors[i])

legend("topleft", months(as.Date("2000-01-01")+(1:12)*28), cex = 0.7, col=plot.colors, lty=1, pch=20 )

Anda mungkin juga menyukai