class: center, middle, inverse, title-slide # Statistics with R ## Statistical Modelling with R for Actuarial Students --- <style type="text/css"> pre { background: #ADD8E6; max-width: 100%; overflow-x: scroll; } </style> ***CS2B – Risk Modelling and Survival Analysis *** * The emphasis is placed on being able to apply statistical methods to actuarial problems using real data sets and the open-source software environment R. * Time Series. Probability Distributions, Survival Analysis --- ## Exercise 1 Monthly totals of airline passengers are given in time series data ‘***AirPassengers***’. Plot the data as time series with frequency =12. --- ### AirPassengers Monthly Airline Passenger Numbers 1949-1960 #### Description The classic Box & Jenkins airline data. Monthly totals of international airline passengers, 1949 to 1960. #### Usage ```r AirPassengers ``` #### Format A monthly time series, in thousands. #### Source Box, G. E. P., Jenkins, G. M. and Reinsel, G. C. (1976) Time Series Analysis, Forecasting and Control. Third Edition. Holden-Day. Series G. --- #### AirPassengers ```r AirPassengers ``` ``` ## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ## 1949 112 118 132 129 121 135 148 148 136 119 104 118 ## 1950 115 126 141 135 125 149 170 170 158 133 114 140 ## 1951 145 150 178 163 172 178 199 199 184 162 146 166 ## 1952 171 180 193 181 183 218 230 242 209 191 172 194 ## 1953 196 196 236 235 229 243 264 272 237 211 180 201 ## 1954 204 188 235 227 234 264 302 293 259 229 203 229 ## 1955 242 233 267 269 270 315 364 347 312 274 237 278 ## 1956 284 277 317 313 318 374 413 405 355 306 271 306 ## 1957 315 301 356 348 355 422 465 467 404 347 305 336 ## 1958 340 318 362 348 363 435 491 505 404 359 310 337 ## 1959 360 342 406 396 420 472 548 559 463 407 362 405 ## 1960 417 391 419 461 472 535 622 606 508 461 390 432 ``` --- ```r apts <- ts(AirPassengers, frequency=12) plot(apts) ``` --- <!-- --> --- ## Exercise 2 Plot the acf function. ```r acf(apts) ``` --- <!-- --> --- ## Exercise 3 Decompose the trend, seasonality and randomness for the data. (Hint: use ***decompose()*** function.) ```r f <- decompose(apts) plot(f) ``` --- <!-- --> --- ## Exercise 4 Fit Arima model. (Hint: order=c(1,0,0), list(order=c(2,1,0), period=12 as parameters.) ```r fit <- arima(AirPassengers, order=c(1,0,0), list(order=c(2,1,0), period=12)) fit ``` ``` ## ## Call: ## arima(x = AirPassengers, order = c(1, 0, 0), seasonal = list(order = c(2, 1, ## 0), period = 12)) ## ## Coefficients: ## ar1 sar1 sar2 ## 0.946 -0.133 0.082 ## s.e. 0.028 0.103 0.108 ## ## sigma^2 estimated as 143: log likelihood = -516, aic = 1040 ``` --- ```r class(fit) ``` ``` ## [1] "Arima" ``` ```r summary(fit) ``` ``` ## Length Class Mode ## coef 3 -none- numeric ## sigma2 1 -none- numeric ## var.coef 9 -none- numeric ## mask 3 -none- logical ## loglik 1 -none- numeric ## aic 1 -none- numeric ## arma 7 -none- numeric ## residuals 144 ts numeric ## call 4 -none- call ## series 1 -none- character ## code 1 -none- numeric ## n.cond 1 -none- numeric ## nobs 1 -none- numeric ## model 10 -none- list ``` --- ## Exercise 5 Predict the data for next 24 months. ```r # "fore" for forecast fore <- predict(fit, n.ahead=24) fore ``` ``` ## $pred ## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ## 1961 445 419 451 485 497 555 641 627 529 478 410 452 ## 1962 463 435 464 502 512 571 657 640 541 491 420 461 ## ## $se ## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ## 1961 12.0 16.5 19.6 22.1 24.1 25.7 27.1 28.3 29.3 30.2 31.0 31.6 ## 1962 35.7 38.9 41.7 43.9 45.9 47.5 49.0 50.2 51.3 52.3 53.2 53.9 ``` --- ## Exercise 6 Find upper and lower bound of fitted data and plot the same along with predicted data with legends. ```r U <- fore$pred + 2*fore$se L <- fore$pred - 2*fore$se c(U,L) ``` ``` ## [1] 469 452 491 529 545 607 695 684 587 539 472 516 535 513 547 590 604 666 755 ## [20] 741 643 596 526 569 421 386 412 441 449 504 587 571 470 418 348 389 392 358 ## [39] 380 414 420 476 559 540 438 386 313 354 ``` ```r ts.plot(AirPassengers, fore$pred, U, L, col=c(1,2,4,4), lty = c(1,1,2,2)) legend("topleft", c("Actual", "Forecast", "Error Bounds (95% Confidence)"), col=c(1,2,4), lty=c(1,1,2)) ``` --- <!-- --> ---