############################################### ## Author: Joshua M. Tebbs ## Date: 8 October 2018 ## Update: 9 November 2018 ## STAT 511 course notes: R Code ## Chapter 5 ############################################### # Figure 5.1 # Page 114 x <- seq(0,1,0.001) y1 <- x y2 <- rep(0,length(x)) plot(x,y1,type="l",bty="n",xlim=c(0,1.1),ylim=c(0,1.1),xlab=expression(y[1]),ylab=expression(y[2]),xaxt='n',yaxt='n',cex.lab=1.25) lines(c(0,1),c(0,0),lty=1) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,1/2,1,1.2),labels=c("0","1/2","1","")) axis(side=2,at=c(0,1/2,1,1.2),labels=c("0","1/2","1","")) # Figure 5.2 # Page 115 x <- seq(1/8,1,0.001) y1 <- x-1/8 y2 <- rep(0,length(x)) plot(x,y1,type="l",bty="n",xlim=c(0,1.1),ylim=c(0,1.1),xlab=expression(y[1]),ylab=expression(y[2]),xaxt='n',yaxt='n',cex.lab=1.25) lines(c(1/8,1),c(0,0),lty=1) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") lines(c(0,1/8),c(0,0),lty=1) lines(c(0,1),c(0,1),lty=1) lines(c(1,1),c(7/8,1),lty=1) axis(side=1,at=c(0,1/8,1,1.2),labels=c("0","1/8","1","")) axis(side=2,at=c(0,7/8,1,1.2),labels=c("0","7/8","1","")) # Figure 5.3 # Page 118 x <- seq(0,1,0.001) y1 <- rep(2,length(x)) y2 <- rep(0,length(x)) plot(x,y1,type="l",bty="n",xlim=c(0,2.1),ylim=c(0,2.1),xaxt='n',yaxt='n',xlab=expression(y[1]),ylab=expression(y[2]),cex.lab=1.25) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,1/2,1,2,2.2),labels=c("0","1/2","1","2","")) axis(side=2,at=c(0,1,2,2.2),labels=c("0","1","2","")) # Figure 5.4 # Page 119 # Left y1 = seq(0,1,0.001) pdf = y1+(1/2) plot(y1,pdf,type="l",xlab=expression(y[1]),ylab="PDF",xlim=c(0,1),ylim=c(0,1.5),cex.lab=1.25) abline(h=0) abline(v=0,lty=2) abline(v=1,lty=2) # Right y2 = seq(0,2,0.001) pdf = (3/4)-(y2/4) plot(y2,pdf,type="l",xlab=expression(y[2]),ylab="PDF",xlim=c(0,2),ylim=c(0,1),cex.lab=1.25) abline(h=0) abline(v=0,lty=2) abline(v=2,lty=2) # Figure 5.5 # Page 121 x <- seq(0,2,0.001) y1 <- rep(2,length(x)) y2 <- rep(0,length(x)) plot(x,y1,type="l",bty="n",xlim=c(0,2),ylim=c(0,2),xaxt='n',yaxt='n',xlab=expression(y[1]),ylab=expression(y[2]),cex.lab=1.25) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,2),labels=c("0","")) axis(side=2,at=c(0,2),labels=c("0","")) # Figure 5.6 # Page 124 x <- seq(0,1,0.001) y1 <- rep(1,length(x)) y2 <- rep(0,length(x)) plot(x,y1,type="l",bty="n",xlim=c(0,1.1),ylim=c(0,1.1),xaxt='n',yaxt='n',xlab=expression(y[1]),ylab=expression(y[2]),cex.lab=1.25) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,1,1.2),labels=c("0","1","")) axis(side=2,at=c(0,1,1.2),labels=c("0","1","")) # Figure 5.7 # Page 125 # Left y1 = seq(0,1,0.001) pdf = (6/11)*(y1+4*y1^2) plot(y1,pdf,type="l",xlab=expression(y[1]),ylab="PDF",xlim=c(0,1),ylim=c(0,3),cex.lab=1.25) abline(h=0) abline(v=0,lty=2) abline(v=1,lty=2) # Right y2 = seq(0,1,0.001) pdf = (2/11)*(4+3*y2) plot(y2,pdf,type="l",xlab=expression(y[2]),ylab="PDF",xlim=c(0,1),ylim=c(0,1.5),cex.lab=1.25) abline(h=0) abline(v=0,lty=2) abline(v=1,lty=2) # Figure 5.8 # Page 127 y1 = seq(0,1,0.001) cond.pdf = (12/5)*y1^2 + (2/5)*y1 plot(y1,cond.pdf,type="l",xlab=expression(y[1]),ylab="PDF",xlim=c(0,1),ylim=c(0,3),cex.lab=1.25) abline(h=0) abline(v=0,lty=2) abline(v=1,lty=2) x = seq(0.5,1,0.001) y = (12/5)*x^2 + (2/5)*x polygon(c(0.5,x,1),c(0,y,0),col="lightblue") points(x=0.5,y=0,pch=19,cex=1.5) # Figure 5.9 # Page 128 x <- seq(0,2,0.001) y1 <- x y2 <- rep(2,length(x)) plot(x,y1,type="l",bty="n",xlim=c(0,2),ylim=c(0,2),xaxt='n',yaxt='n',xlab=expression(y[1]),ylab=expression(y[2]),cex.lab=1.25) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,2),labels=c("0","")) axis(side=2,at=c(0,2),labels=c("0","")) # Figure 5.10 # Page 132 x <- seq(0,1,0.001) y1 <- rep(1,length(x)) y2 <- rep(0,length(x)) plot(x,y1,type="l",bty="n",xlim=c(0,1.1),ylim=c(0,1.1),xaxt='n',yaxt='n',xlab=expression(y[1]),ylab=expression(y[2]),cex.lab=1.25) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,1,1.2),labels=c("0","1","")) axis(side=2,at=c(0,1,1.2),labels=c("0","1","")) # Figure 5.11 # Page 134 x <- seq(2,4,0.001) y1 <- rep(4.5,length(x)) y2 <- rep(1,length(x)) plot(x,y1,type="l",bty="n",xlim=c(0,6),ylim=c(0,6),xaxt='n',yaxt='n',xlab=expression(y[1]),ylab=expression(y[2]),cex.lab=1.25) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,2,4,6.25),labels=c("0","a","b","")) axis(side=2,at=c(0,1,4.5,6.25),labels=c("0","c","d","")) # Figure 5.12 # Page 142 x <- seq(0,1,0.001) y1 <- x y2 <- 2-x plot(x,y1,type="l",bty="n",xlim=c(0,2.1),ylim=c(0,2.1),xaxt='n',yaxt='n',xlab=expression(y[1]),ylab=expression(y[2]),cex.lab=1.25) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,1,2,2.2),labels=c("0","1","2","")) axis(side=2,at=c(0,1,2,2.2),labels=c("0","1","2","")) # Figure 5.13 # Page 147 x <- seq(0,1,0.001) y1 <- x y2 <- rep(0,length(x)) plot(x,y1,type="l",bty="n",xlim=c(0,1.1),ylim=c(0,1.1),xlab=expression(y[1]),ylab=expression(y[2]),xaxt='n',yaxt='n',cex.lab=1.25) lines(c(0,1),c(0,0),lty=1) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,1/2,1,1.2),labels=c("0","1/2","1","")) axis(side=2,at=c(0,1/2,1,1.2),labels=c("0","1/2","1","")) # Figure 5.14 # Page 151 # Left library(mvtnorm) x.points = seq(-3,3,length.out=100) y.points = x.points z = matrix(0,nrow=100,ncol=100) mu = c(0,0) sigma = matrix(c(1,0.5,0.5,1),nrow=2) for (i in 1:100) { for (j in 1:100) { z[i,j] <- dmvnorm(c(x.points[i],y.points[j]), mean=mu,sigma=sigma) }} contour(x.points,y.points,z,xlab="x",ylab="y",xlim=c(-2.5,2.5),ylim=c(-2.5,2.5),cex.lab=1.25) # Right library(mvtnorm) x.points = seq(-3,3,length.out=100) y.points = x.points z = matrix(0,nrow=100,ncol=100) mu = c(0,0) sigma = matrix(c(1,-0.5,-0.5,1),nrow=2) for (i in 1:100) { for (j in 1:100) { z[i,j] <- dmvnorm(c(x.points[i],y.points[j]), mean=mu,sigma=sigma) }} contour(x.points,y.points,z,xlab="x",ylab="y",xlim=c(-2.5,2.5),ylim=c(-2.5,2.5),cex.lab=1.25) # Figure 5.15 # Page 152 library(mvtnorm) x.points = seq(-3,3,length.out=100) y.points = x.points z = matrix(0,nrow=100,ncol=100) mu = c(0,0) sigma = matrix(c(1,0,0,1),nrow=2) for (i in 1:100) { for (j in 1:100) { z[i,j] <- dmvnorm(c(x.points[i],y.points[j]), mean=mu,sigma=sigma) }} contour(x.points,y.points,z,xlab="x",ylab="y",xlim=c(-2.5,2.5),ylim=c(-2.5,2.5),cex.lab=1.25) # Figure 5.16 # Page 154 library(mvtnorm) x.points = seq(20,60,length.out=100) y.points = seq(15,45,length.out=100) z = matrix(0,nrow=100,ncol=100) mu = c(40,30) sigma = matrix(c(76,0.5*sqrt(76*32),0.5*sqrt(76*32),32),nrow=2) for (i in 1:100) { for (j in 1:100) { z[i,j] <- dmvnorm(c(x.points[i],y.points[j]), mean=mu,sigma=sigma) }} contour(x.points,y.points,z,xlab="x",ylab="y", xlim=c(20,60),ylim=c(15,45),cex.lab=1.25) points(x=40,y=30,pch=19,cex=1.5) # Figure 5.17 # Page 166 # Left y = seq(0.5,20,0.01) pdf = (1/3)*exp(-(y-0.5)/3) plot(y,pdf,type="l",xlab=expression(y[1]),ylab="PDF",xlim=c(0,20),cex.lab=1.25) abline(h=0) abline(v=0.5,lty=2) # Right y2 = seq(2.3,2.8,0.01) pdf = rep(2,length(y2)) plot(y2,pdf,type="l",xlab=expression(y[2]),ylab="PDF",xaxt='n',yaxt='n',xlim=c(2.1,2.9),ylim=c(0,4),cex.lab=1.25) axis(side=1,at=c(2.3,2.5,2.8),labels=expression(y[1]-0.2,y[1],y[1]+0.3)) axis(side=2,at=c(0,2),labels=c("0","2")) lines(c(2.1,2.3),c(0,0),lty=1) lines(c(2.8,2.9),c(0,0),lty=1) # Figure 5.18 # Page 167 x <- seq(0.5,2,0.01) y1 <- x-0.2 y2 <- x+0.3 plot(x,y1,type="l",bty="n",xlim=c(0,2.2),ylim=c(0,2.2),xlab=expression(y[1]),ylab=expression(y[2]),xaxt='n',yaxt='n',cex.lab=1.25) polygon(c(x,rev(x)),c(y2,rev(y1)),col="grey") axis(side=1,at=c(0,0.5,1,2.2),labels=c("0","0.5","1","")) axis(side=2,at=c(0,0.3,0.8,2.2),labels=c("0","0.3","0.8",""))