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 2: Elementary Probability and Statistics

R Plot Fig. 2.1

# Go to your working directory
# setwd("/Users/sshen/climstats") 
nycDat=read.csv("data/NYCDailyDatasummer2019.csv", header=T)
dim(nycDat)
## [1] 7555   13
#[1] 7555   13
dayNum=92
nycP=nycDat[1:dayNum, c(3,6)]

dw=c()
for (i in 1:dayNum){if (nycP[i,2] >= 0.2){dw[i] = 1} 
  else {dw[i]=0} }
dw
##  [1] 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 1
## [39] 1 0 0 1 0 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 0 0 1 0 0 1 1 0 1 1 1 1 0 0 0 1 0
## [77] 0 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0
n0=which(dw==0)
n1=which(dw==1)
m=dayNum - 1
par(mfrow=c(1,1))
par(mar=c(2.5,4.5,2.5,4))
plot(1:dayNum, nycP[,2], type="s",
     main="Daily Precipitation of New York City: 
      1 Jun 2019- 31 Aug 2019",
     ylab="Precipitation [mm/day]", 
     xaxt="n", xlab="",
     cex.lab=1.4, cex.axis=1.4)
axis(1, at=c(1, 30, 61, 92), 
     labels=c("1 June", "1 July", "1 Aug", "31 Aug"),
     cex.axis=1.4)
par(new=TRUE)
plot(n0+1.0, dw[n0]-0.2, cex.lab=1.4,
     ylim=c(0,15), pch=15, cex=0.4, col="red",
     axes=FALSE, xlab="", ylab="")
points(n1+1, dw[n1], pch=15, col="blue", cex=0.4)
axis(4, col="blue", col.axis="blue", 
     at=c(0,1), labels =c("D", "W"), 
     las=2, cex.axis=1)
mtext("Dry or Wet Days", 
      col="blue", side=4, line=1.5, cex=1.4)

 

R Plot Fig. 2.2

p=0.6471
n=1:12
pdfn= (1-p)*p^n
par(mar=c(4.5,4.5,3,0.5))
plot(n, pdfn, 
     ylim=c(0,0.25), type="h", lwd=15,
     cex.lab=1.5, cex.axis=1.5,
     xlab="Length of Dry Spell: n", ylab="Probability",
     main="Probability Distribution Function of Dry Spell")
text(1.5,0.25, "(a)",cex=1.5)

p=0.6471
N=1:12
cdfN= 1-p^N
par(mar=c(4.5,4.5,3,0.5))
plot(N, cdfN, 
     ylim=c(0,1), type="s", lwd=3,
     cex.lab=1.5, cex.axis=1.5,
     xlab="N: Dry Spell Not Longer Than N Days", 
     ylab="Cumulative Probability",
     main="Cumulative Distribution Function of Dry Spell")
text(1.5,0.95, "(b)",cex=1.5)

 

R Plot Fig. 2.3

n=20
x=0:n
pdfdx=dbinom(x, size=n, prob=0.3)
par(mar=c(4.5,4.5,2,4.5))
plot(x, pdfdx, type="h", lty=1, lwd=3,
     xlab="Number of successes: k", ylab="Probability", 
     main="Binomial Distribution: n=20, p=0.3",
     cex.lab=1.4, cex.axis=1.4)
text(11, 0.05, "PDF", cex=2)
par(new=TRUE)
cdfx=pbinom(x, size=n, prob=0.3)
plot(x, cdfx, col="blue",
     type="o", lty=2, axes=FALSE,
     xlab="", ylab="", cex.axis=1.4)
axis(4, col="blue", col.axis = "blue", cex.axis=1.4)
mtext("Cumulative Probability", col="blue",
      side=4, line=3, cex=1.4)
text(11, 0.9, "CDF", cex=2, col="blue")