[R] optim() and starting values.
Dong-hyun Oh
r.arecibo at gmail.com
Mon Jun 16 18:26:57 CEST 2008
Dear UseRs,
I wrote the following function to estimate parameters using MLE.
------------------------------------------------
mlog <- function(theta, nx = 1, nz = 1, dt){
beta <- matrix(theta[1:(nx+1)], ncol = 1)
delta <- matrix(theta[(nx+2):(nx+nz+1)], ncol = 1)
sigma2 <- theta[nx+nz+2]
gamma <- theta[nx+nz+3]
y <- as.matrix(dt[, 1], ncol = 1)
x <- as.matrix(data.frame(1, as.matrix(dt[, 2:(nx+1)], ncol = 2)))
z <- as.matrix(dt[, (nx+2):(nx+nz+1)], ncol = nz)
d <- z %*% delta / (gamma * sigma2)^.5
mustar <- (1-gamma) * z %*% delta - gamma * ( y - x %*% beta)
sigmastar <- (gamma * (1-gamma) * sigma2)^.5
dstar <- mustar / sigmastar
loglik <- (-0.5 * nrow(x) *(log(2*pi) + log(sigma2))
-0.5 * sum(( y - x %*% beta + z %*% delta)^2/sigma2)
-sum(log(pnorm(d))) + sum(log(pnorm(dstar))))
return(-loglik)
}
--------------------------------------------------
To test my function, I created an artificial data set as follows:
--------------------------------------------------
x1 <- abs(rnorm(100))*100
x2 <- abs(rnorm(100))*10
z1 <- abs(rnorm(100))*5
z2 <- abs(rnorm(100))*7
y <- abs(0.3 + 0.3* log(x1) + 0.7* log(x2))
dat <- data.frame(log(y), log(x1), log(x2), z1, z2)
--------------------------------------------------
The following optimization results provides different estimates.
--------------------------------------------------
theta.start1 <- c(1.06, 0.08, 0.04, 0.097, 0.008, 0.08, 0.008)
theta.start2 <- c(1.06, 0.08, 0.04, 0.097, 0.00008, 0.0008, 0.0008)
out.optim <- optim(theta.start1, mlog, nx = 2, nz = 2, dt = dat,
hessian = T)
par.theta1 <- out.optim$par
out.optim <- optim(theta.start2, mlog, nx = 2, nz = 2, dt = dat,
hessian = T)
par.theta2 <- out.optim$par
---------------------------------------------------
How can I set up concrete starting values?
Any advices will be appreciated.
Looking forward to hearing from you.
Sincerely,
Dong-hyun Oh
More information about the R-help
mailing list