############################################### ## Author: Joshua M. Tebbs ## Date: 23 December 2018 ## Update: 1 January 2019 ## STAT 512 course notes: R Code ## Chapter 7 ############################################### # Figure 7.1 # Page 56 t = seq(-5,5,0.01) plot(t,dnorm(t,0,1),type="l",lty=1,xlab="",ylab="PDF",cex.lab=1.25) lines(t,dt(t,3),lty=4) lines(t,dt(t,10),lty=8) abline(h=0) legend(2,0.35,lty=c(1,4,8),c(expression(paste("N(0,1)")),expression(paste("t(3)")),expression(paste("t(10)")))) # Figure 7.2 # Page 60 u = seq(0,8,0.01) plot(u,df(u,1,10),type="l",lty=1,xlab="",ylab="PDF",cex.lab=1.25,ylim=c(0,1)) lines(u,df(u,5,50),lty=4) lines(u,df(u,10,100),lty=8) abline(h=0) legend(3.5,0.6,lty=c(1,4,8),c(expression(paste(nu[1]," = 1, ",nu[2]," = 10")),expression(paste(nu[1]," = 5, ",nu[2], " = 50")),expression(paste(nu[1]," = 10, ", nu[2]," = 100")))) # Figure 7.3 # Page 63 # Left y = seq(0,10,1) prob = dpois(y,2) plot(y,prob,type="h",xlab="y",ylab="PMF",ylim=c(0,max(prob)),cex.lab=1.25) abline(h=0) # Right y = seq(400,600,1) prob = dpois(y,500) plot(y,prob,type="h",xlab="t",ylab="Sampling distribution",ylim=c(0,max(prob)),cex.lab=1.25) abline(h=0) lines(y,dnorm(y,500,sqrt(500)),lty=1,col="red") points(x=c(475,550),y=c(0,0),pch=19,cex=1.5) # Figure 7.4 # Page 64 y = seq(0,120,0.1) pdf = dexp(y,1/20) plot(y,pdf,type="l",xlab="y",ylab="PDF",cex.lab=1.25) abline(h=0) abline(v=0,lty=2) # Figure 7.5 # Page 65 # Left y = seq(0,50,0.01) plot(y,dgamma(y,10,1/2),type="l",lty=1,xlab="Sample mean",ylab="Exact sampling distribution",cex.lab=1.25) abline(h=0) x = seq(30,50,0.1) y = dgamma(x,10,1/2) polygon(c(30,x,50),c(0,y,0),col="lightblue") points(x=30,y=0,pch=19,cex=1.5) # Right y = seq(-10,50,0.01) plot(y,dnorm(y,20,sqrt(40)),type="l",lty=1,xlab="Sample mean",ylab="Approximate sampling distribution",cex.lab=1.25) abline(h=0) x = seq(30,50,0.1) y = dnorm(x,20,sqrt(40)) polygon(c(30,x,50),c(0,y,0),col="lightblue") points(x=30,y=0,pch=19,cex=1.5) # Figure 7.6 # Page 66 y = seq(0,1,0.001) pdf = (3/2)*(1-y^2) plot(y,pdf,type="l",xlab="y",ylab="PDF",xlim=c(0,1),cex.lab=1.25) abline(h=0) abline(v=0,lty=2) # Figure 7.7 # Page 68 t = seq(10,30,0.01) plot(t,dnorm(t,19.5,sqrt(3.0875)),type="l",lty=1,xlab="t",ylab="Approximate sampling distribution",cex.lab=1.25) abline(h=0) x = seq(22.5,30,0.1) y = dnorm(x,19.5,sqrt(3.0875)) polygon(c(22.5,x,30),c(0,y,0),col="lightblue") points(x=22.5,y=0,pch=19,cex=1.5) # Figure 7.8 # Page 72 # Left t = seq(25,75,1) prob = dbinom(t,100,0.5) plot(t,prob,type="h",xlab="t",ylab="Sampling distribution",ylim=c(0,max(prob)),cex.lab=1.25) abline(h=0) t = seq(25,75,0.01) lines(t,dnorm(t,50,sqrt(25)),lty=1,col="red") # Right t = seq(0,15,1) prob = dbinom(t,40,0.1) plot(t,prob,type="h",xlab="t",ylab="Sampling distribution",ylim=c(0,max(prob)),cex.lab=1.25) abline(h=0) t = seq(0,15,0.01) lines(t,dnorm(t,4,sqrt(3.6)),lty=1,col="red")