1

I have read the maxLik document on how to do constrained optimization. However, I do not understand how I can do it. I have a custom likelihood function as below. The value of rho should be between 0 and 1 (making that two constraints). Now how exactly do I put those constraints? I have 3 parameters.

I have seen an almost similar question here with 3 constraints and 3 parameters but I am really a novice and do not understand the proposed hints on how to include the constraints ? how to use maxLik() to do the constrained optimization in R

require(maxLik)

data<- matrix(rnorm(3600,5,1),ncol=20)

Y=data[,c(1:20)]
Y <- as.matrix(Y, ncol=20)
p=4
T=nrow(Y)
X <- Y[p:(T-1),1:4]
unos <- rep(1,T)
X <- cbind(unos, X)

set.seed(101)


loglik <- function(theta) {
  eta <- theta[1]
  n <- theta[2]
  rho <- theta[3]

coefis=as.matrix(c(mu0=0.0112, mu1=0.0115, mu2=0.009, mu3=0.021, 
mu4=0.01237),ncol=1) #coefficients for the intercept and four lags
resi= Y- X%*%coefis 

  y <- Custom_lik(resi, eta, n, rho, T)  #my custom likelihood function
  return(-y[[1]])
}

m <- maxLik(loglik, start=c(eta=1.1, n=1.5, rho=0.5)) 
Masimba
  • 31
  • 3
  • 1
    You should provide a link to the question with which you have difficulty. You should also provide a sample dataset. Use a dataset from one of the available packages if you cannot provide your own data for some reason. The help pages Examples sections often reference datasets. Make sure you read them to the end. Sometimes the answer can be found at the very end of the Examples section. – IRTFM Sep 19 '22 at 01:50
  • Thanks @IRTFM. I have added the link and example data in case. – Masimba Sep 19 '22 at 02:42
  • I would have thought that residuals would be `abs( deviations)` but am not near a console at the moment. I’ll take a stab at this later today. – IRTFM Sep 20 '22 at 18:24
  • Pretty sure the current effort is erring out at `X%*%coefis`. X is a 276 x 10 matrix and coefis is a 5 x 1 matrix. Since the problem being simulated is not described there's not a lot that can be said further. – IRTFM Sep 20 '22 at 20:08
  • And if the incorrect conformation in the matrix multiplication step weren't enough I see problems ahead when the interpreter gets to `Custom_lik`. WTF is that???? – IRTFM Sep 21 '22 at 05:23
  • @IRTFM thank you very much for trying to assist me. In my haste to get help i made soo many mistakes in providing sample data. – Masimba Sep 21 '22 at 07:46
  • Custom_lik is my log likelihood function. My challenge is that I dont know how to add my contraint to the maxLik function. I need rho to be greater than 0 but less than 1. I have now tried to add the constraints as below ` A<-matrix(c(1,0,0, 0,0,1),2,3,byrow=TRUE); B<-c(0,1); ineqCon<-list(ineqA=A,ineqB=B); m <- maxLik(loglik, start = c(b2=b2, n=n, rho=rho), constraints =ineqCon) however it is clearly wrong as my parameter values are not in correct range – Masimba Sep 21 '22 at 07:53

0 Answers0