R/myNRML.R

Defines functions myNRML

#' @title myNRML
#' @description Takes in your input to graphically represent the Newton-Raphson method of finding the maximum likelihood
#'
#' @param x0 initial value when n = 0
#' @param delta increments, smaller delta value leads to greater accuracy
#' @param llik likelihood
#' @param xrange x-limit of the plot (vector, i.e. c(0,15))
#' @param parameter the parameter you want to find and use as the x-label
#'
#' @return Plots displaying the Newton-Raphson method of finding the maximum likelihood
#' @export
#'
myNRML=function(x0,delta=0.001,llik,xrange,parameter="param"){
  f=function(x) (llik(x+delta)-llik(x))/delta
  fdash=function(x) (f(x+delta)-f(x))/delta
  d=1000
  i=0
  x=c()
  y=c()
  x[1]=x0
  y[1]=f(x[1])
  while(d > delta & i<100){
    i=i+1
    x[i+1]=x[i]-f(x[i])/fdash(x[i])
    y[i+1]=f(x[i+1])
    d=abs(y[i+1])
  }
  layout(matrix(1:2,nr=1,nc=2,byrow=TRUE),width=c(1,2))
  curve(llik(x), xlim=xrange,xlab=parameter,ylab="log Lik",main="Log Lik")
  curve(f(x),xlim=xrange,xaxt="n", xlab=parameter,ylab="derivative",main=  "Newton-Raphson Algorithm \n on the derivative")
  points(x,y,col="Red",pch=19,cex=1.5)
  axis(1,x,round(x,2),las=2)
  abline(h=0,col="Red")

  segments(x[1:(i-1)],y[1:(i-1)],x[2:i],rep(0,i-1),col="Blue",lwd=2)
  segments(x[2:i],rep(0,i-1),x[2:i],y[2:i],lwd=0.5,col="Green")

  list(x=x,y=y)
}
andrewtornado11/MATH4753 documentation built on May 10, 2022, 7:34 p.m.