 | :: Smoothing Time Series Example - Free Statistics Software (Calculator) :: | |
|
All rights reserved. The non-commercial (academic) use of this software is free of charge. The only thing that is asked in return is to cite this software when results are used in publications.
This R module demonstrates with real data that gas consumption as usually measured by drivers is quite variable, and with no apparent trend, until a nonparametric smoother reveals a very distinct seasonal pattern. Ask students if they see anything in the data. The first graph you see is the raw data, and in the next graph the smoothed fit is displayed. The point of the demo is that nonparametric smoothing is easy to understand and useful, and should be taught in intro stats courses. The program can also be used to demonstrate the trial-and-error approach to choosing the degree of smoothing, by varying the parameter 'span'.
Click here to edit the underlying code of this R Module.Click here to edit the underlying code of this R Module.
| Source code of R module | | 1 | x <- c(1999.38 | | 2 | ,1999.39 | | 3 | ,1999.41 | | 4 | ,1999.42 | | 5 | ,1999.44 | | 6 | ,1999.45 | | 7 | ,1999.46 | | 8 | ,1999.47 | | 9 | ,1999.49 | | 10 | ,1999.5 | | 11 | ,1999.51 | | 12 | ,1999.52 | | 13 | ,1999.53 | | 14 | ,1999.54 | | 15 | ,1999.54 | | 16 | ,1999.58 | | 17 | ,1999.6 | | 18 | ,1999.64 | | 19 | ,1999.66 | | 20 | ,1999.59 | | 21 | ,1999.67 | | 22 | ,1999.68 | | 23 | ,1999.69 | | 24 | ,1999.71 | | 25 | ,1999.72 | | 26 | ,1999.73 | | 27 | ,1999.74 | | 28 | ,1999.76 | | 29 | ,1999.77 | | 30 | ,1999.78 | | 31 | ,1999.79 | | 32 | ,1999.81 | | 33 | ,1999.82 | | 34 | ,1999.84 | | 35 | ,1999.84 | | 36 | ,1999.86 | | 37 | ,1999.88 | | 38 | ,1999.89 | | 39 | ,1999.91 | | 40 | ,1999.92 | | 41 | ,1999.94 | | 42 | ,1999.95 | | 43 | ,1999.96 | | 44 | ,1999.98 | | 45 | ,2000 | | 46 | ,2000.02 | | 47 | ,2000.03 | | 48 | ,2000.04 | | 49 | ,2000.05 | | 50 | ,2000.16 | | 51 | ,2000.17 | | 52 | ,2000.19 | | 53 | ,2000.21 | | 54 | ,2000.21 | | 55 | ,2000.24 | | 56 | ,2000.25 | | 57 | ,2000.26 | | 58 | ,2000.27 | | 59 | ,2000.28 | | 60 | ,2000.29 | | 61 | ,2000.31 | | 62 | ,2000.34 | | 63 | ,2000.36 | | 64 | ,2000.36 | | 65 | ,2000.39 | | 66 | ,2000.4 | | 67 | ,2000.42 | | 68 | ,2000.43 | | 69 | ,2000.45 | | 70 | ,2000.46 | | 71 | ,2000.47 | | 72 | ,2000.51 | | 73 | ,2000.54 | | 74 | ,2000.56 | | 75 | ,2000.57 | | 76 | ,2000.59 | | 77 | ,2000.62 | | 78 | ,2000.63 | | 79 | ,2000.67 | | 80 | ,2000.67 | | 81 | ,2000.68 | | 82 | ,2000.7 | | 83 | ,2000.71 | | 84 | ,2000.73 | | 85 | ,2000.74 | | 86 | ,2000.76 | | 87 | ,2000.77 | | 88 | ,2000.78 | | 89 | ,2000.8 | | 90 | ,2000.81 | | 91 | ,2000.83 | | 92 | ,2000.84 | | 93 | ,2000.86 | | 94 | ,2000.87 | | 95 | ,2000.88 | | 96 | ,2000.88 | | 97 | ,2000.9 | | 98 | ,2000.91 | | 99 | ,2000.92 | | 100 | ,2000.93 | | 101 | ,2000.94 | | 102 | ,2000.96 | | 103 | ,2000.97 | | 104 | ,2000.98 | | 105 | ,2001.01 | | 106 | ,2001.03 | | 107 | ,2001.04 | | 108 | ,2001.06 | | 109 | ,2001.07 | | 110 | ,2001.08 | | 111 | ,2001.09 | | 112 | ,2001.12 | | 113 | ,2001.13 | | 114 | ,2001.16 | | 115 | ,2001.19 | | 116 | ,2001.2 | | 117 | ,2001.21 | | 118 | ,2001.23 | | 119 | ,2001.24 | | 120 | ,2001.26 | | 121 | ,2001.26 | | 122 | ,2001.27 | | 123 | ,2001.3 | | 124 | ,2001.31 | | 125 | ,2001.33 | | 126 | ,2001.34 | | 127 | ,2001.36 | | 128 | ,2001.36 | | 129 | ,2001.38 | | 130 | ,2001.4 | | 131 | ,2001.41 | | 132 | ,2001.43 | | 133 | ,2001.45 | | 134 | ,2001.46 | | 135 | ,2001.48 | | 136 | ,2001.5 | | 137 | ,2001.51 | | 138 | ,2001.53 | | 139 | ,2001.55 | | 140 | ,2001.57 | | 141 | ,2001.58 | | 142 | ,2001.59 | | 143 | ,2001.61 | | 144 | ,2001.62 | | 145 | ,2001.67 | | 146 | ,2001.69 | | 147 | ,2001.72 | | 148 | ,2001.73 | | 149 | ,2001.74 | | 150 | ,2001.76 | | 151 | ,2001.77 | | 152 | ,2001.78 | | 153 | ,2001.8 | | 154 | ,2001.81 | | 155 | ,2001.82 | | 156 | ,2001.84 | | 157 | ,2001.89 | | 158 | ,2001.9 | | 159 | ,2001.91 | | 160 | ,2001.92 | | 161 | ,2001.94 | | 162 | ,2001.96 | | 163 | ,2001.98 | | 164 | ,2002.01 | | 165 | ,2002.02 | | 166 | ,2002.04 | | 167 | ,2002.06 | | 168 | ,2002.07 | | 169 | ,2002.09 | | 170 | ,2002.1 | | 171 | ,2002.12 | | 172 | ,2002.14 | | 173 | ,2002.15 | | 174 | ,2002.16 | | 175 | ,2002.17 | | 176 | ,2002.19 | | 177 | ,2002.2 | | 178 | ,2002.21 | | 179 | ,2002.24 | | 180 | ,2002.24 | | 181 | ,2002.27 | | 182 | ,2002.28 | | 183 | ,2002.29 | | 184 | ,2002.31 | | 185 | ,2002.35 | | 186 | ,2002.37 | | 187 | ,2002.4 | | 188 | ,2002.34 | | 189 | ,2002.44 | | 190 | ,2002.5 | | 191 | ,2002.56 | | 192 | ,2002.6 | | 193 | ,2002.64 | | 194 | ,2002.65 | | 195 | ,2002.67 | | 196 | ,2002.69 | | 197 | ,2002.7 | | 198 | ,2002.72 | | 199 | ,2002.72 | | 200 | ,2002.74 | | 201 | ,2002.76 | | 202 | ,2002.77 | | 203 | ,2002.78 | | 204 | ,2002.8 | | 205 | ,2002.82 | | 206 | ,2002.84 | | 207 | ,2002.84 | | 208 | ,2002.87 | | 209 | ,2002.89 | | 210 | ,2002.9 | | 211 | ,2002.91 | | 212 | ,2002.92 | | 213 | ,2002.93 | | 214 | ,2002.99 | | 215 | ,2003.01 | | 216 | ,2003.04 | | 217 | ,2003.05 | | 218 | ,2003.07 | | 219 | ,2003.08 | | 220 | ,2003.1 | | 221 | ,2003.13 | | 222 | ,2003.15 | | 223 | ,2003.18 | | 224 | ,2003.19 | | 225 | ,2003.2 | | 226 | ,2003.22 | | 227 | ,2003.23 | | 228 | ,2003.24 | | 229 | ,2003.25 | | 230 | ,2003.26 | | 231 | ,2003.27 | | 232 | ,2003.29 | | 233 | ,2003.3 | | 234 | ,2003.32 | | 235 | ,2003.33 | | 236 | ,2003.34 | | 237 | ,2003.36 | | 238 | ,2003.36 | | 239 | ,2003.37 | | 240 | ,2003.39 | | 241 | ,2003.41 | | 242 | ,2003.42 | | 243 | ,2003.43 | | 244 | ,2003.46 | | 245 | ,2003.47 | | 246 | ,2003.48 | | 247 | ,2003.49 | | 248 | ,2003.51 | | 249 | ,2003.54 | | 250 | ,2003.55 | | 251 | ,2003.56 | | 252 | ,2003.58 | | 253 | ,2003.61 | | 254 | ,2003.66 | | 255 | ,2003.68 | | 256 | ,2003.68 | | 257 | ,2003.7 | | 258 | ,2003.72 | | 259 | ,2003.73 | | 260 | ,2003.74 | | 261 | ,2003.76 | | 262 | ,2003.81 | | 263 | ,2003.82 | | 264 | ,2003.84 | | 265 | ,2003.85 | | 266 | ,2003.86 | | 267 | ,2003.88 | | 268 | ,2003.88 | | 269 | ,2003.89 | | 270 | ,2003.91 | | 271 | ,2003.92 | | 272 | ,2003.94 | | 273 | ,2003.95 | | 274 | ,2003.96 | | 275 | ,2003.99 | | 276 | ,2004.02 | | 277 | ,2004.05 | | 278 | ,2004.03 | | 279 | ,2004.07 | | 280 | ,2004.08 | | 281 | ,2004.09 | | 282 | ,2004.11 | | 283 | ,2004.15 | | 284 | ,2004.17 | | 285 | ,2004.19 | | 286 | ,2004.21 | | 287 | ,2004.24 | | 288 | ,2004.26 | | 289 | ,2004.27 | | 290 | ,2004.28 | | 291 | ,2004.3 | | 292 | ,2004.31 | | 293 | ,2004.33 | | 294 | ,2004.34 | | 295 | ,2004.36 | | 296 | ,2004.38 | | 297 | ,2004.39 | | 298 | ,2004.41 | | 299 | ,2004.43 | | 300 | ,2004.44 | | 301 | ,2004.46 | | 302 | ,2004.48) | | 303 | y <- c(10.69 | | 304 | ,9.09 | | 305 | ,11.29 | | 306 | ,10.59 | | 307 | ,9.97 | | 308 | ,9.65 | | 309 | ,9.18 | | 310 | ,10.49 | | 311 | ,9.27 | | 312 | ,10.4 | | 313 | ,8.9 | | 314 | ,9.17 | | 315 | ,10.4 | | 316 | ,9.53 | | 317 | ,8.19 | | 318 | ,10.04 | | 319 | ,9.37 | | 320 | ,10.46 | | 321 | ,10.09 | | 322 | ,7.38 | | 323 | ,10.56 | | 324 | ,9.83 | | 325 | ,9.57 | | 326 | ,10.46 | | 327 | ,12.2 | | 328 | ,7.64 | | 329 | ,10.26 | | 330 | ,9.43 | | 331 | ,9.94 | | 332 | ,11.19 | | 333 | ,10.17 | | 334 | ,9.61 | | 335 | ,10.28 | | 336 | ,12.06 | | 337 | ,9.73 | | 338 | ,10.1 | | 339 | ,10.63 | | 340 | ,10.64 | | 341 | ,11.24 | | 342 | ,9.4 | | 343 | ,11.78 | | 344 | ,9.75 | | 345 | ,12.3 | | 346 | ,11.35 | | 347 | ,10.97 | | 348 | ,11.77 | | 349 | ,11.09 | | 350 | ,11.43 | | 351 | ,10.92 | | 352 | ,11.91 | | 353 | ,11.17 | | 354 | ,10.04 | | 355 | ,10.32 | | 356 | ,9.6 | | 357 | ,9.56 | | 358 | ,10.27 | | 359 | ,8.69 | | 360 | ,10.91 | | 361 | ,9.64 | | 362 | ,9.09 | | 363 | ,10.39 | | 364 | ,10.15 | | 365 | ,10.2 | | 366 | ,9.28 | | 367 | ,10.66 | | 368 | ,9.37 | | 369 | ,10.14 | | 370 | ,10.43 | | 371 | ,10.24 | | 372 | ,9.28 | | 373 | ,10.38 | | 374 | ,9.85 | | 375 | ,10.81 | | 376 | ,12.2 | | 377 | ,9.47 | | 378 | ,9.49 | | 379 | ,8.9 | | 380 | ,9.76 | | 381 | ,9.62 | | 382 | ,9.6 | | 383 | ,9.99 | | 384 | ,10.48 | | 385 | ,9.56 | | 386 | ,9.19 | | 387 | ,9.94 | | 388 | ,9.57 | | 389 | ,10.4 | | 390 | ,9.26 | | 391 | ,10.69 | | 392 | ,10.35 | | 393 | ,10 | | 394 | ,9.34 | | 395 | ,10.45 | | 396 | ,10.76 | | 397 | ,9.74 | | 398 | ,7.34 | | 399 | ,11.21 | | 400 | ,10.67 | | 401 | ,9.22 | | 402 | ,10.34 | | 403 | ,10.66 | | 404 | ,9.96 | | 405 | ,12.11 | | 406 | ,10.71 | | 407 | ,10.09 | | 408 | ,10.25 | | 409 | ,9.96 | | 410 | ,11.73 | | 411 | ,9.71 | | 412 | ,10.1 | | 413 | ,10.29 | | 414 | ,11.05 | | 415 | ,10.35 | | 416 | ,12 | | 417 | ,10.56 | | 418 | ,10.43 | | 419 | ,8.19 | | 420 | ,11.38 | | 421 | ,9.95 | | 422 | ,10.51 | | 423 | ,8.61 | | 424 | ,10.29 | | 425 | ,10.11 | | 426 | ,10.12 | | 427 | ,9.84 | | 428 | ,10.05 | | 429 | ,10.13 | | 430 | ,9.97 | | 431 | ,10.13 | | 432 | ,9.87 | | 433 | ,9.92 | | 434 | ,9.44 | | 435 | ,9.71 | | 436 | ,9.77 | | 437 | ,9.82 | | 438 | ,10.31 | | 439 | ,8.82 | | 440 | ,9.62 | | 441 | ,9.64 | | 442 | ,10.69 | | 443 | ,9.08 | | 444 | ,9.59 | | 445 | ,9.57 | | 446 | ,8.17 | | 447 | ,9.99 | | 448 | ,9.09 | | 449 | ,9.66 | | 450 | ,9.21 | | 451 | ,9.44 | | 452 | ,10.04 | | 453 | ,9.3 | | 454 | ,10.24 | | 455 | ,10.15 | | 456 | ,10.2 | | 457 | ,11.36 | | 458 | ,9.5 | | 459 | ,9.96 | | 460 | ,10.15 | | 461 | ,11.02 | | 462 | ,10.05 | | 463 | ,10.65 | | 464 | ,10.31 | | 465 | ,11.42 | | 466 | ,10.08 | | 467 | ,10.44 | | 468 | ,10.39 | | 469 | ,10.74 | | 470 | ,10.99 | | 471 | ,11.02 | | 472 | ,10.55 | | 473 | ,10.72 | | 474 | ,10.54 | | 475 | ,10.67 | | 476 | ,10.35 | | 477 | ,9.68 | | 478 | ,10.78 | | 479 | ,9.8 | | 480 | ,11.09 | | 481 | ,10.45 | | 482 | ,10.66 | | 483 | ,10.65 | | 484 | ,9.9 | | 485 | ,9.55 | | 486 | ,9.91 | | 487 | ,10.26 | | 488 | ,10.14 | | 489 | ,9.82 | | 490 | ,10.27 | | 491 | ,10.12 | | 492 | ,9.52 | | 493 | ,9.05 | | 494 | ,10.26 | | 495 | ,9.4 | | 496 | ,8.99 | | 497 | ,8.98 | | 498 | ,9.49 | | 499 | ,10.42 | | 500 | ,9.31 | | 501 | ,9.13 | | 502 | ,9.71 | | 503 | ,10.3 | | 504 | ,9.75 | | 505 | ,9.05 | | 506 | ,10.91 | | 507 | ,10.09 | | 508 | ,11.15 | | 509 | ,10.69 | | 510 | ,10.36 | | 511 | ,10.56 | | 512 | ,10.52 | | 513 | ,10.32 | | 514 | ,9.78 | | 515 | ,12.11 | | 516 | ,10.75 | | 517 | ,11.12 | | 518 | ,10.32 | | 519 | ,12.33 | | 520 | ,10.03 | | 521 | ,11.4 | | 522 | ,12.47 | | 523 | ,13.57 | | 524 | ,10.2 | | 525 | ,10.2 | | 526 | ,10.84 | | 527 | ,9.61 | | 528 | ,10.35 | | 529 | ,10 | | 530 | ,9.76 | | 531 | ,10.3 | | 532 | ,9.86 | | 533 | ,10.52 | | 534 | ,10.49 | | 535 | ,10.14 | | 536 | ,10.11 | | 537 | ,6.86 | | 538 | ,9.58 | | 539 | ,10.17 | | 540 | ,8.9 | | 541 | ,10.54 | | 542 | ,9.49 | | 543 | ,10.23 | | 544 | ,9.04 | | 545 | ,10.47 | | 546 | ,11.88 | | 547 | ,10.55 | | 548 | ,8.86 | | 549 | ,11.71 | | 550 | ,9.88 | | 551 | ,9.07 | | 552 | ,9.63 | | 553 | ,10.5 | | 554 | ,10.16 | | 555 | ,7.65 | | 556 | ,8.56 | | 557 | ,10.51 | | 558 | ,8.9 | | 559 | ,9.7 | | 560 | ,9.83 | | 561 | ,9.73 | | 562 | ,9.03 | | 563 | ,10.18 | | 564 | ,9.45 | | 565 | ,11.27 | | 566 | ,10.35 | | 567 | ,10.76 | | 568 | ,11.2 | | 569 | ,11.08 | | 570 | ,9.17 | | 571 | ,12.67 | | 572 | ,11.09 | | 573 | ,10.32 | | 574 | ,10.71 | | 575 | ,10.88 | | 576 | ,8.44 | | 577 | ,10.15 | | 578 | ,10.06 | | 579 | ,9.76 | | 580 | ,12.45 | | 581 | ,10.02 | | 582 | ,8.89 | | 583 | ,10.66 | | 584 | ,10.71 | | 585 | ,10.52 | | 586 | ,10.58 | | 587 | ,10.15 | | 588 | ,11.1 | | 589 | ,9.96 | | 590 | ,11.31 | | 591 | ,9.51 | | 592 | ,6.98 | | 593 | ,10.55 | | 594 | ,7.22 | | 595 | ,10.16 | | 596 | ,8.03 | | 597 | ,9.77 | | 598 | ,10.36 | | 599 | ,9.63 | | 600 | ,10.6 | | 601 | ,9.6 | | 602 | ,8.75 | | 603 | ,10.32 | | 604 | ,10.23) | | 605 | merc.df <- as.data.frame(cbind(x,y)) | | 606 | colnames(merc.df) <- c("date1","lp100km") | | 607 | span <- as.numeric(par1) | | 608 | if (par2 == "TRUE") smooth <- TRUE else smooth <- FALSE | | 609 | x=merc.df$date1 | | 610 | y=merc.df$lp100km | | 611 | yl=loess(y~x,span=span) | | 612 | bitmap(file="noloessplot.png") | | 613 | plot(yl,col="darkgreen",type="p",main="5 years gas consumption, 1999-2004 (raw data)",xlab="decimal date",ylab="litres per 100 km") | | 614 | dev.off() | | 615 | if (smooth==TRUE) | | 616 | { | | 617 | bitmap(file="loessplot.png") | | 618 | plot(yl,col="darkgreen",type="p",main="5 years gas consumption, 1999-2004 (incl. LOESS)",xlab="decimal date",ylab="litres per 100 km") | | 619 | mn=mean(yl$y) | | 620 | lines(yl$x,yl$fitted,col="red",lwd=3) | | 621 | lines(yl$x,mn*rep(1,length(yl$x)),col="black",lwd=3) | | 622 | dev.off() | | 623 | bitmap(file="resplot.png") | | 624 | plot(yl$x,yl$residuals,col="darkgreen",ylab="residuals from | | 625 | smooth",xlab="decimal date",main="Residuals of (Data - Smooth)") | | 626 | lines(yl$x,0*rep(1,length(yl$x)),col="black",lwd=3) | | 627 | rl=loess(yl$residuals~yl$x,span=span) | | 628 | lines(rl$x,rl$fitted,col="red",lwd=3) | | 629 | dev.off() | | 630 | } |
|
| Cite this software as: | | Weldon Larry, (2008), Time Series Smoothing Example (v1.0.2) in Free Statistics Software (v1.1.23-r2), Office for Research Development and Education, URL http://www.wessa.net/rwasp_gas.wasp/ | | | The R code is based on : | | | Weldon Larry, Stat Ed Programs for Demos in R, Simon Fraser University, URL http://www.stat.sfu.ca/~weldon/title.doc | | | Weldon Larry, R Programs for Statistics Education, Simon Fraser University, URL http://www.stat.sfu.ca/~weldon/Programs
| | | Weldon Larry, Instructions for using R programs for Statistics Education, Simon Fraser University, URL http://www.stat.sfu.ca/~weldon/instructions.doc | | | | | | | | | |
|
To cite Wessa.net in publications use: Wessa, P. (2008), Free Statistics Software, Office for Research Development and Education, version 1.1.23-r2, URL http://www.wessa.net/
© Resa
R&D, and the 'Office for Research, Development, and Education' - All rights reserved. Academic license for non-commercial use only.
This website is an intellectual
and physical property of Resa R&D. This includes: html content,
graphical illustrations (gif files), computer software, online or electronic
documentation, associated media, and printed materials. The free use of the scientific content, services, and applications in this website is
granted for non commercial use only. In any case,
the source (url) should always be clearly displayed. Under no circumstances are
you allowed to reproduce, copy or redistribute the design, layout, or any
content of this website (for commercial use) including any materials contained
herein without the express written permission of Resa R&D or the Office for Research, Development, and Education.
Information provided
on this web site is provided "AS IS" without warranty of any kind, either
express or implied, including, without limitation, warranties of
merchantability, fitness for a particular purpose, and noninfringement. We use reasonable efforts to include accurate and timely information
and periodically update the information, and software without notice. However, Resa
R&D makes no warranties or representations
as to the accuracy or completeness of such information (or software), and it assumes no
liability or responsibility for errors or omissions in the content of this web
site, or any software bugs in online applications. Your use of this web site is AT YOUR OWN RISK. Under no circumstances and
under no legal theory shall Resa R&D be liable to you or any other
person for any direct, indirect, special, incidental, exemplary, or
consequential damages arising from your access to, or use of, this web site.
Software Version : 1.1.23-r2 Algorithms & Software : Prof. dr. P. Wessa Facilities : Resa R&D - Office for Research, Development, and Education Server : www.wessa.net
Comments, Feedback & Errors | Privacy Policy | Statistics Resources | Wessa.net Home
|
|
|