# Statistics functions in R # generate a bunch of Uniform(0,1) realizations U <- runif(20000) U hist(U) X <- log(U/(1-U)) hist(X) # Let's look at the normal distribution X <- rnorm(1000,mean=10,sd=2) # generating random values from normal distribution hist(X,freq = F) # dnorm() evaluates the probability density function x <- seq(2,18,length=200) pdfx <- dnorm(x,mean = 10, sd = 2) lines(pdfx ~ x, col = "blue") # make a plot of the standard normal density with the middle 95% # shaded # qnorm() the quantile function of the normal distribution x <- seq(-4,4,length=200) pdfx <- dnorm(x) # default mean is 0, and default sd = 1 plot(pdfx ~ x, type="l") lower <- qnorm(0.025) upper <- qnorm(0.975) x2 <- seq(lower, upper, length=100) x_poly <- c(lower,x2,upper) y_poly <- c(0,dnorm(x2),0) polygon(x = x_poly, y = y_poly, col = "red", border = NA) # pnorm() evaluates the cumulative distribution function # Make a z table???? zrow <- seq(0,3.5,by=0.1) # row values of z zcol <- seq(0,0.09,by=0.01) # column values nr <- length(zrow) # the number of rows nc <- length(zcol) # number of columns tab <- matrix(0,nrow=nr,ncol=nc) # set up an empty table for(i in 1:nr) for(j in 1:nc){ z <- zrow[i] + zcol[j] tab[i,j] <- 1 - pnorm(z) } tab <- round(tab,4) colnames(tab) <- zcol rownames(tab) <- zrow tab # binomial distribution # rbinom, dbinom, pbinom, and qbinom # For the number of successes in n independent Bernoulli trials n <- 50 p <- 1/2 # generate a single realization rbinom(1,size = n,prob = p) # size is how many Bernoulli trials N <- 2000 # generate N realizations X <- rbinom(N,size = n, prob = p) # summarize the sample: table(X) # tells me how many times each number was observed plot(table(X)) # this is good for discrete data.