############################################################# # # Newton's method example for MLE # # Professor M. Zhu # Current version = Oct 2023 # ############################################################# rm(list=ls(all=TRUE)) # clear workspace myMLEfunc <- function(v, y, theta, max.iter=100, eps=1e-10) { # finds MLE for the censored Poisson model, # given data (v,y), # using Newton's method ... # written by M. Zhu for teaching illustration i = 0; convg = FALSE; newtheta = theta while (i < max.iter && !convg) { theta = newtheta print(theta) # take a look u = exp(-v*theta) g = sum(y*v*u/(1-u) - (1-y)*v) # first deriv H = -sum(y*v*v*u/(1-u)^2) # second deriv newtheta = theta - g/H convg = abs(theta-newtheta)