#Foundation and Application of Econometrics, Spring 2015, Pepared by Dr. I-Ming Chiu #Accompany 368_Handout03.pdf #Set Operations (example from pp. 1) #Three commands: "union", "intersect", and "setdiff" rm(list=ls()) U = c(1,2,3,4,5,6) A = c(1,3,5) B = c(4,5,6) intersect(A,B) union(A,B) setdiff(U,A) setdiff(A,B) #also try setdiff(B, A) setdiff(union(A,B), intersect(A,B)) #Countig Rules (examples from pp. 3) rm(list=ls()) choose(10,3)*factorial(3) #becasue P(n, r) = C(n, r)*r! choose(10,3) choose(59, 5)*choose(35, 1) #all the possible combinations of powerball numbers #PMF & CDF of the RV (# of heads from tossing two coins, pp. 4 and 5) rm(list=ls()) x=c(0,1,2) y=c(0.25,0.5,0.25) plot(x,y,type="h",xlim=c(-1,3.5),ylim=c(0,1),lwd=2,col="blue",ylab="prob",xlab="RV(# of Heads)",main="PMF") points(x,y,pch=16,cex=1.2,col="dark red") plot(x,cumsum(y),type="s",ylim=c(0.2,1),col="blue",ylab="prob",xlab="RV(# of Heads)",main="CDF") #Bernoulli Distribution (pp. 6) rm(list=ls()) x=c(0,1) y=c(0.7,0.3) plot(x,y,type="h",xlim=c(0,1),ylim=c(0,1),lwd=2,col="blue",xlab = "X",ylab="prob",main="Bernoulli Dist") points(x,y,pch=16,cex=1.5,col="dark red") #Binomial Distribution (pp. 7) rm(list=ls()) n=10 p=0.2 x=0:10 p=dbinom(x,n,p) plot(x,p,type="h",xlim=c(-1,11),ylim=c(0,0.5),lwd=2,col="blue",xlab="Y",ylab="prob",main="Binomial Distribution") points(x,p,pch=16,cex=1.5,col="dark red") #Poisson Distribution (pp. 8) rm(list=ls()) x = 0:10 lambda1 = 2 lambda2 = 5 y = dpois(x,lambda1) y1 = dpois(x,lambda2) par(mfrow=c(2,1)) plot(x, y, type = "h", main="Poisson Dist (lambda = 2)",xlab="X",ylab="prob") plot(x, y1, type = "h", main="Poisson Dist (lambda = 5)",xlab="X",ylab="prob") #Standard Normal Distribution (pp. 9) rm(list=ls()) x = seq(-3,3,0.01) y = dnorm(x,0,1) #dnorm(x,3,2);mean=3, standard deviation=2 plot(x,y,type="l", main="Standard Normal Distribution") abline(v=-2) abline(v=2) text(0, 0.20,"95%",cex=1.5) #Chisquare Distribution (pp. 10) rm(list=ls()) x = seq(0,30,0.01) y = dchisq(x,df=2) y1 = dchisq(x,df=5) y2 = dchisq(x,df=20) plot(x,y,type="l",main="Chisquare Distribution") lines(x,y1,col="red") lines(x,y2,col="blue") text(3,0.3,"df=2") text(8,0.1,"df=5",col="red") text(18,0.1,"df=20",col="blue") text(20,0.4,"(d.f.:degree of freedom)") #t Distribution (pp. 11) rm(list=ls()) x = seq(-8,8,0.01) y = dt(x,df=1) y1 = dt(x,df=10) y2 = dnorm(x) plot(x,y,type="l", main="t Distribution",ylim=c(0,0.5),col="blue",lwd=1.5) lines(x,y1,col="blue",lwd=1.5) lines(x,y2,col="red",lty="dashed",lwd=2.5) text(-5,0.05,"df=1",col="blue") text(-3,0.3,"df=10",col="blue") text(0,0.42,"standard normal distribution",col="red") #F Distribution (pp. 12) rm(list=ls()) x = seq(0,5,0.01) y = df(x,df1=5,df2=10) y1 = df(x,df1=10,df2=3) plot(x,y,type="l",main="F Distribution",col="blue") lines(x,y1,col="red") text(1.5,0.6,"(m,n)=(5,10)",col="blue") text(0.8,0.25,"(m,n)=(10,3)",col="red")