--- title: "R Lecture " output: html_notebook --- Discrete distributions: Let's plot the probability mass function of some discrete distributions ```{r} x<-NULL for (i in 1:20){ x[i]<-dbinom(i,20,0.5) } plot(x) ``` ```{r} x<-NULL for (i in 1:20){ x[i]<-dgeom(i,0.5) } plot(x) ``` ```{r} x<-NULL for (i in 1:50){ x[i]<-dpois(i,5) } plot(x) ``` ```{r} x<-NULL for (i in 1:50){ x[i]<-dnbinom(i,20,0.5) } plot(x) ``` We can also plot the cdf's by prefixing with a p: ```{r} x<-NULL for (i in 1:20){ x[i]<-pbinom(i,20,0.5) } plot(x) ``` Exercise: Plot the cdf's of some other discrete random variables. Continuous Distributions: The common continuous distributions are norm, exp, gamma, unif, beta, t, chisq, f, cauchy. The prefix "p" gives the value of the cdf at a point. For example: ```{r} pnorm(0,0,1) #the z-score of 0 is 50% ``` ```{r} pt(3, 8) #The probability that t<=3 where t is a t-distributed random variable with 8 degrees of freedom. ``` Let's plot the pdf of the T distributions. We can do this by sampling the distribution with the R prefix, and then plotting a histogram: ```{r} x<-rt(10000,8) #create a vector with 1000 random samples of a t-distribution with 8 degrees of freedom. hist(x,breaks=100, prob=TRUE) #plot a histogram of these values lines(density(x),lwd=3, col="red") #Adds a density function based on the data. ``` ```{r} x<-rexp(10000,0.01) #create a vector with 1000 random samples of an exponential random variable with lambda = 0.01. hist(x,breaks=100,prob=TRUE) #plot a histogram of these values lines(density(x),lwd=3, col="red") ``` ```{r} x<-rchisq(20000,25) #create a vector with 2000 random samples of an chi^2 random variable with 25 degrees of freedom. hist(x,breaks=150, prob=TRUE) #plot a histogram of these values lines(density(x),lwd=3, col="red") ``` Quantile Function: The quantile function of a distribution is the inverse of the CDF. We have already used this impicitly when we find critical values. For example, given some alpha, z_alpha is the quantile function of the standard normal evaluated at 1-alpha. The quantile function uses the prefix "q". For example: ```{r} qnorm(.95) #give the critical value z such that P(Z5) ```