Maximum Likelihood Estimation for Geometric Distribution in R

# Data Visualization of Log Likelihood function for Geometric Distribution, estimating parameter p


x <- 4 #trial number of first success

#creates pmf
loglikelihood.fn <- function(p){
  return(log(p * (1-p)^(x-1)))
}

p.grid<-seq(0, 1, 0.001)

# Creates line plot
# For plot function, type 'l' means line plot 
plot(p.grid, loglikelihood.fn(p.grid), type = 'l',
     main = 'Maximum Likelihood Estimation for Geometric distribution', xlab = 'parameter p', ylab = 'log-likelihood function')


# v creates a vertical line at 1/x, representing the estimation of p
abline(v = 1/x, col = 'red')