### Note: command below would have worked if computer had been connected ### to the internet > foo <- read.table("http://www.stat.umn.edu/geyer/somedata.txt", header = TRUE) unable to resolve 'www.stat.umn.edu'. Error in file(file, "r") : cannot open URL `http://www.stat.umn.edu/geyer/somedata.txt' > help(nlm) > mlogl <- function(theta, x) sum(log(1 + (x - theta)^2)) > mlogl2 <- function(theta, x) sum(- dcauchy(x, theta, log = TRUE)) > n <- 30 > x <- rcauchy(n) > out <- nlm(mlogl, median(x)) Error in log(1 + (x - theta)^2) : Argument "x" is missing, with no default > out <- nlm(mlogl, median(x), x = x) > out $minimum [1] 52.88128 $estimate [1] -0.2703223 $gradient [1] 8.171241e-07 $code [1] 1 $iterations [1] 3 > median(x) [1] -0.2562955 > out <- nlm(mlogl, median(x), x = x, print.level = 2, hessian = TRUE) iteration = 0 Step: [1] 0 Parameter: [1] -0.2562955 Function Value [1] 52.88282 Gradient: [1] 0.2193642 iteration = 1 Step: [1] -0.02193642 Parameter: [1] -0.2782320 Function Value [1] 52.88177 Gradient: [1] -0.1230431 iteration = 2 Step: [1] 0.007882791 Parameter: [1] -0.2703492 Function Value [1] 52.88128 Gradient: [1] -0.0004175575 iteration = 3 Parameter: [1] -0.2703223 Function Value [1] 52.88128 Gradient: [1] 8.171241e-07 Relative gradient close to zero. Current iterate is probably solution. > out $minimum [1] 52.88128 $estimate [1] -0.2703223 $gradient [1] 8.171241e-07 $hessian [,1] [1,] 15.58790 $code [1] 1 $iterations [1] 3 > 1 / sqrt(out$hessian) [,1] [1,] 0.2532831 > 1 / sqrt(out$hessian) #### asymptotic standard dev. of MLE [,1] [1,] 0.2532831 > > out2 <- nlm(mlogl2, median(x), x = x) > out$minimum [1] 52.88128 > out2$minimum [1] 87.22318 > out2$estimate [1] -0.2703223 > out$estimate [1] -0.2703223 ### this where we edited the file to create the function mlogl3 mlogl3 <- function(theta, x) { if(length(theta) != 2) stop("Oops!") sum(- dcauchy(x, location = theta[1], scale = theta[2], log = TRUE)) } n <- 30 x <- rcauchy(n) out <- nlm(mlogl, median(x)) ### here we go back to printout > mlogl3 <- function(theta, x) { + if(length(theta) != 2) stop("Oops!") + sum(- dcauchy(x, location = theta[1], scale = theta[2], log = TRUE)) + } > n <- 30 > x <- rcauchy(n) > mlogl3 function(theta, x) { if(length(theta) != 2) stop("Oops!") sum(- dcauchy(x, location = theta[1], scale = theta[2], log = TRUE)) } > qcauchy(3/4) - qcauchy(1/4) [1] 2 > theta.start <- c(median(x), IQR(x) / 2) > theta.start [1] 0.004699155 1.029976175 > x <- rcauchy(n) > theta.start <- c(median(x), IQR(x) / 2) > theta.start [1] -0.5334394 1.0217893 > nlm(mlogl3, theta.start, hessian = TRUE) Error in dcauchy(x, location = theta[1], scale = theta[2], log = TRUE) : Argument "x" is missing, with no default > nlm(mlogl3, theta.start, hessian = TRUE, x = x) $minimum [1] 77.25726 $estimate [1] -0.3856592 1.2444707 $gradient [1] -2.33058e-06 8.53014e-06 $hessian [,1] [,2] [1,] 8.4991797 0.4600109 [2,] 0.4600109 10.8685235 $code [1] 1 $iterations [1] 6 > out3 <- nlm(mlogl3, theta.start, hessian = TRUE, x = x) > solve(out3$hessian) ### asymptotic variance of MLE [,1] [,2] [1,] 0.117928566 -0.004991333 [2,] -0.004991333 0.092220076 > sqrt(diag(solve(out3$hessian))) ### asymptotic s. d. of MLE [1] 0.3434073 0.3036776 > out$estimate Error: Object "out" not found > out3$estimate [1] -0.3856592 1.2444707 >