pp <- (0:100)/100 # Set up vector of points between 0 and 1 bernlik <- function(pp, n) { # Plots the likelihood function for n indep Bernoulli random variables par(mfcol=c(2,3)) # Sets up plots in an array of 2 rows, 3 columns for (sumx in 0:n) { lfcn <- pp^sumx * (1-pp)^(n-sumx) plot(pp,lfcn,xlab="p",ylab="L(p;x)",ylim=c(0,1),type="l") title(sub=paste("sum of x's = ",sumx)) } } bernlik(pp,5) # Likelihood function for Unif(0,theta) par(mfrow=c(1,1)) uniflike <- function(maxtheta,xvec) { # Plots the likelihood function for a uniform n <- length(xvec) theta <- (0:100)*maxtheta/100 lfcn <- 1/theta^n lfcn[theta < max(xvec)] <- 0 plot(theta,lfcn,xlab="theta",ylab="L(theta;x)",type="l") # To get rid of vertical line, can do: plot(theta,lfcn,xlab="theta",ylab="L(theta;x)",type="n") # Creates empty plot lines(theta[theta < max(xvec)],lfcn[theta= max(xvec)],lfcn[theta >= max(xvec)]) } xvec <- runif(5) xvec uniflike(2,xvec)