####################################### # Probability Distributions # ####################################### #Many probability distributions are available in Splus #The online SPLUS manual lists them #Go to the Help pulldown, select Online Manuals, then #Guide to Statistics Volume 1. This brings up a pdf #file; select Chapter 3 "Probability". #Select page 56, "Splus Probability Functions" #to find out about the four types of probability #function; they are # The r-function r**** : used to produce a random sample # The p-function p**** : used to produce the cdf # The d-function d**** : used to produce the pmf/pdf # The q-function q**** : used to produce the inverse cdf #For the Normal or Gaussian distribution #Random number generation x.normalsample<-rnorm(1000,mean=0,sd=1) #a sample of size 1000 from the N(0,1) x.normalsample<-rnorm(5000,mean=10,sd=3) #a sample of size 5000 from the N(10,3^2) #To inspect this sample, use the "hist" histogram function hist(x.normalsample) #CDF qvec<-seq(from=-2.5,to=2.5,by=0.1) #The values at which we want to work out the cdf. pvec<-pnorm(qvec,mean=0,sd=1) #The cdf plot(qvec,pvec,type="l") #PDF xvec<-seq(from=-2.5,to=2.5,by=0.1) yvec<-dnorm(xvec,mean=0,sd=1) #The pdf plot(xvec,yvec,type="l") #Inverse cdf pvec<-c(1:99)/100 qvec<-qnorm(pvec,mean=0,sd=1) #The inverse cdf plot(pvec,qvec,type="l") ############################################## #Illustrations of other distributions #Student-t xvec<-seq(from=-3.5,to=3.5,by=0.1) yvec<-dt(xvec,df=5) #Five degrees of freedom plot(xvec,yvec,type="l") #To look at a comparison of different Student-t distributions df.vec<-c(1,2,3,4,5,10,20,50) plot(xvec,yvec,type="n") for(i in 1:length(df.vec)){ yvec<-dt(xvec,df=df.vec[i]) #Degrees of freedom determined by elements of df.vec lines(xvec,yvec) } #Chi-squared xvec<-seq(from=0,to=20,by=0.1) yvec<-dchisq(xvec,df=5) #Five degrees of freedom plot(xvec,yvec,type="l") #Fisher-F xvec<-seq(from=0,to=10,by=0.1) yvec<-df(xvec,df1=5,df2=8) #Five and ten degrees of freedom plot(xvec,yvec,type="l") #Discrete distributions are also available; here are the random #number generators #Binomial : rbinom(10,n=20,p=0.3) #Poisson : rpois(10,lambda=2) #Geometric : rgeom(10,p=0.1) #Note the SPLUS Geometric distribution is slightly different from the standard one #It records the number of "failures" BEFORE the first success in a Bernoulli #sequence; to transform to the "standard" Geometric, add 1 to all elements #in the sample