Statistics and Data Visualizations in Climate Science

with R and Python

 

A Cambridge University Press Book by

SSP Shen and GR North

 

 

Version 1.0 released in July 2023 and coded by Dr. Samuel Shen, Distinguished Professor
San Diego State University, California, USA
https://shen.sdsu.edu
Email:

 

 

 

 

Chapter 7: Introduction to Time Series

R Code for Fig. 7.1: Mauna Loa CO2 March 1958-July 2020

#setwd("/Users/sshen/climstats")
co2m = read.table("data/co2m.txt", header = TRUE)
dim(co2m)
## [1] 749   7
# [1] 749   7
co2m[1:3,]
##   year mon     date average interpolated  trend days
## 1 1958   3 1958.208  315.71       315.71 314.62   -1
## 2 1958   4 1958.292  317.45       317.45 315.29   -1
## 3 1958   5 1958.375  317.50       317.50 314.71   -1
#year mon     date average interpolated  trend days
#1 1958   3 1958.208  315.71       315.71 314.62   -1
#2 1958   4 1958.292  317.45       317.45 315.29   -1
#3 1958   5 1958.375  317.50       317.50 314.71   -1
mon = co2m[,3]
co2 = co2m[,5]
#setEPS() # save the .eps figure file to the working directory
#postscript("fig0701.eps", height = 8, width = 10)
par(mar=c(2.2,4.5,2,0.5))
plot(mon, co2, type="l", col="red",
     main = 
       expression(paste("Monthly Atmospheric ", 
                        CO[2]," at Mauna Loa Observatory")),
     xlab ="", 
     ylab="Parts Per Million [ppm]",
     cex.axis =1.4, cex.lab=1.4, cex.main= 1.5)
text(1985, 410, "Scripps Institution of Oceanography", 
     cex=1.5)
text(1985, 400, "NOAA Global Monitoring Laboratory", 
     cex=1.5)
lines(mon, co2m[,6]) #plot the trend data

#dev.off()

 

R Plot Fig. 7.2: Time Series Decomposition

co2.ts = ts(co2, start=c(1958,3), end=c(2020,7), 
            frequency =12)
co2.ts #Display time series with month and year
##         Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct
## 1958               315.71 317.45 317.50 317.10 315.86 314.93 313.20 312.66
## 1959 315.62 316.38 316.71 317.72 318.29 318.15 316.54 314.80 313.84 313.26
## 1960 316.43 316.97 317.58 319.02 320.03 319.59 318.18 315.91 314.16 313.83
## 1961 316.93 317.70 318.54 319.48 320.58 319.77 318.57 316.79 314.80 315.38
## 1962 317.94 318.56 319.68 320.63 321.01 320.55 319.58 317.40 316.26 315.42
## 1963 318.74 319.08 319.86 321.39 322.25 321.47 319.74 317.77 316.21 315.99
## 1964 319.57 320.07 320.73 321.77 322.25 321.89 320.44 318.70 316.70 316.79
## 1965 319.44 320.44 320.89 322.13 322.16 321.87 321.39 318.81 317.81 317.30
## 1966 320.62 321.59 322.39 323.87 324.01 323.75 322.39 320.37 318.64 318.10
## 1967 322.07 322.50 323.04 324.42 325.00 324.09 322.55 320.92 319.31 319.31
## 1968 322.57 323.15 323.89 325.02 325.57 325.36 324.14 322.03 320.41 320.25
## 1969 324.00 324.42 325.64 326.66 327.34 326.76 325.88 323.67 322.38 321.78
## 1970 325.03 325.99 326.87 328.13 328.07 327.66 326.35 324.69 323.10 323.16
## 1971 326.17 326.68 327.18 327.78 328.92 328.57 327.34 325.46 323.36 323.57
## 1972 326.77 327.63 327.75 329.72 330.07 329.09 328.05 326.32 324.93 325.06
## 1973 328.54 329.56 330.30 331.50 332.48 332.07 330.87 329.31 327.51 327.18
## 1974 329.35 330.71 331.48 332.65 333.19 332.16 331.07 329.12 327.32 327.28
## 1975 330.73 331.46 331.90 333.17 333.94 333.45 331.97 329.95 328.50 328.34
## 1976 331.59 332.75 333.52 334.64 334.77 334.00 333.06 330.68 328.95 328.75
## 1977 332.66 333.13 334.95 336.13 336.93 336.17 334.88 332.56 331.29 331.27
## 1978 334.95 335.25 336.66 337.69 338.03 338.01 336.41 334.41 332.37 332.41
## 1979 336.14 336.69 338.27 338.95 339.21 339.26 337.54 335.75 333.98 334.19
## 1980 337.90 338.34 340.01 340.93 341.48 341.33 339.40 337.70 336.19 336.15
## 1981 339.29 340.55 341.61 342.53 343.03 342.54 340.78 338.44 336.95 337.08
## 1982 340.96 341.73 342.81 343.97 344.63 343.79 342.32 340.09 338.28 338.29
## 1983 341.68 342.90 343.33 345.25 346.03 345.63 344.19 342.27 340.35 340.38
## 1984 344.10 344.79 345.52 346.84 347.63 346.98 345.53 343.55 341.40 341.67
## 1985 345.21 346.16 347.74 348.34 349.06 348.38 346.71 345.02 343.27 343.13
## 1986 346.56 347.28 348.01 349.77 350.38 349.93 348.16 346.08 345.22 344.51
## 1987 348.52 348.73 349.73 351.31 352.09 351.53 350.11 348.08 346.52 346.59
## 1988 350.39 351.64 352.40 353.69 354.21 353.72 352.69 350.40 348.92 349.13
## 1989 352.91 353.27 353.96 355.64 355.86 355.37 353.99 351.81 350.05 350.25
## 1990 353.80 355.04 355.73 356.32 357.32 356.34 354.84 353.01 351.31 351.62
## 1991 354.84 355.73 357.23 358.66 359.13 358.13 356.19 353.85 352.25 352.35
## 1992 356.25 357.11 357.86 359.09 359.59 359.33 357.01 354.94 352.95 353.32
## 1993 357.00 357.31 358.47 359.27 360.19 359.52 357.33 355.64 354.03 354.12
## 1994 358.24 358.92 359.99 361.23 361.65 360.81 359.38 357.46 355.73 356.08
## 1995 359.92 360.86 361.83 363.30 363.69 363.19 361.64 359.12 358.17 357.99
## 1996 362.07 363.24 364.17 364.57 365.13 364.92 363.55 361.38 359.54 359.58
## 1997 363.09 364.03 364.51 366.35 366.64 365.59 364.31 362.25 360.29 360.82
## 1998 365.27 365.98 367.24 368.66 369.42 368.99 367.82 365.95 364.02 364.40
## 1999 368.18 369.07 369.68 370.99 370.96 370.30 369.45 366.90 364.81 365.37
## 2000 369.29 369.55 370.60 371.82 371.58 371.70 369.86 368.13 367.00 367.03
## 2001 370.59 371.51 372.43 373.37 373.85 373.22 371.50 369.61 368.18 368.45
## 2002 372.53 373.20 374.12 375.02 375.76 375.52 374.01 371.85 370.75 370.55
## 2003 374.88 375.64 376.45 377.73 378.60 378.28 376.70 374.38 373.17 373.15
## 2004 377.00 377.87 378.88 380.35 380.62 379.69 377.47 376.01 374.25 374.46
## 2005 378.46 379.73 380.77 382.29 382.45 382.21 380.74 378.74 376.70 377.00
## 2006 381.38 382.20 382.67 384.61 385.03 384.05 382.46 380.41 378.85 379.13
## 2007 382.89 383.90 384.58 386.50 386.56 386.10 384.50 381.99 380.96 381.12
## 2008 385.52 385.82 386.03 387.21 388.54 387.76 386.37 384.09 383.18 382.99
## 2009 386.94 387.48 388.82 389.55 390.14 389.48 388.03 386.11 384.74 384.43
## 2010 388.71 390.20 391.17 392.46 393.00 392.15 390.20 388.35 386.85 387.24
## 2011 391.33 391.86 392.60 393.25 394.19 393.74 392.51 390.13 389.08 389.00
## 2012 393.12 393.86 394.40 396.18 396.74 395.71 394.36 392.39 391.11 391.05
## 2013 395.55 396.80 397.43 398.41 399.78 398.60 397.32 395.20 393.45 393.70
## 2014 397.85 398.01 399.77 401.38 401.78 401.25 399.10 397.03 395.38 396.03
## 2015 399.98 400.28 401.54 403.28 403.96 402.80 401.31 398.93 397.63 398.29
## 2016 402.56 404.12 404.87 407.45 407.72 406.83 404.41 402.27 401.05 401.59
## 2017 406.17 406.46 407.22 409.04 409.69 408.88 407.12 405.13 403.37 403.63
## 2018 407.96 408.32 409.41 410.24 411.24 410.79 408.71 406.99 405.51 406.00
## 2019 410.83 411.75 411.97 413.33 414.64 413.93 411.74 409.95 408.54 408.52
## 2020 413.39 414.11 414.51 416.21 417.07 416.38 414.38                     
##         Nov    Dec
## 1958 313.33 314.67
## 1959 314.80 315.58
## 1960 315.00 316.19
## 1961 316.10 317.01
## 1962 316.69 317.69
## 1963 317.12 318.31
## 1964 317.79 318.71
## 1965 318.87 319.42
## 1966 319.79 321.08
## 1967 320.72 321.96
## 1968 321.31 322.84
## 1969 322.85 324.11
## 1970 323.98 325.13
## 1971 324.80 326.01
## 1972 326.50 327.55
## 1973 328.16 328.64
## 1974 328.30 329.58
## 1975 329.37 330.58
## 1976 330.15 331.62
## 1977 332.41 333.60
## 1978 333.75 334.90
## 1979 335.31 336.81
## 1980 337.27 338.32
## 1981 338.58 339.88
## 1982 339.60 340.90
## 1983 341.59 343.05
## 1984 343.10 344.70
## 1985 344.49 345.88
## 1986 345.93 347.22
## 1987 347.96 349.16
## 1988 350.20 351.41
## 1989 351.49 352.85
## 1990 353.07 354.33
## 1991 353.81 355.12
## 1992 354.32 355.57
## 1993 355.41 356.91
## 1994 357.53 358.98
## 1995 359.45 360.68
## 1996 360.89 362.24
## 1997 362.49 364.38
## 1998 365.52 367.13
## 1999 366.72 368.10
## 2000 368.37 369.67
## 2001 369.76 371.24
## 2002 372.25 373.79
## 2003 374.66 375.99
## 2004 376.16 377.51
## 2005 378.35 380.11
## 2006 380.15 381.82
## 2007 382.45 383.94
## 2008 384.19 385.56
## 2009 386.02 387.42
## 2010 388.67 389.79
## 2011 390.28 391.86
## 2012 392.98 394.34
## 2013 395.16 396.84
## 2014 397.28 398.91
## 2015 400.16 401.85
## 2016 403.55 404.45
## 2017 405.12 406.81
## 2018 408.02 409.07
## 2019 410.25 411.76
## 2020
#        Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    
#1958               315.71 317.45 317.50 317.10 315.86 314.93 
#1959 315.62 316.38 316.71 317.72 318.29 318.15 316.54 314.80 
#1960 316.43 316.97 317.58 319.02 320.03 319.59 318.18 315.91 

#Decompose a time series into components of trend, 
#seasonal cycle, and random residual
co2.decompose <- decompose(co2.ts)
#Plot the time series and its three components 
plot(co2.decompose, xlab ="")