12

this is my code:

    #define likelihood function (including an intercept/constant in the function.)
lltobit <- function(b,x,y) {
  sigma <-  b[3]
  y  <- as.matrix(y)
  x <- as.matrix(x)
  vecones <- rep(1,nrow(x)) 
  x <- cbind(vecones,x)
  bx <- x %*% b[1:2] 
  d <- y != 0 
  llik <- sum(d * ((-1/2)*(log(2*pi) + log(sigma^2) + ((y - bx)/sigma)^2)) 
              + (1-d) * (log(1 - pnorm(bx/sigma))))
  return(-llik)
}

n <- nrow(censored) #define number of variables 
y <- censored$y #define y and x for easier use
x1 <- as.matrix(censored$x)
x <-  cbind(rep(1,n),x1) #include constant/intercept 
bols <- (solve(t(x) %*% x)) %*% (t(x) %*% y) #compute ols estimator (XX) -1 XY
init <- rbind(as.matrix(bols[1:nrow(bols)]),1) #initial values 

init

tobit1 <- optim(init, lltobit, x=x, y=y, hessian=TRUE, method="BFGS")

where censored is my data table, including 200 (censored) values of y and 200 values of x.

Everything works, but when running the optim command, i get the following error:

tobit1 <- optim(init, lltobit, x=x, y=y, hessian=TRUE, method="BFGS")
Error in x %*% b[1:2] : non-conformable arguments

I know what it means, but since x is a 200 by 2 matrix, and b[1:2] a vector of 2 by 1, what goes wrong? I tried transposing both, and also the initial values vector, but nothing works. Can anyone help me?

pk_22
  • 288
  • 1
  • 2
  • 18
  • In your function, the line `x <- cbind(vecones,x)` adds a column to `x`. Since you are passing a two columns matrix, the new `x` will have 3 columns and then cannot be multiplied to `b[1:2]`. – nicola Oct 20 '15 at 13:03
  • 12
    If R tells you have non-conformable arguments, you can believe it. –  Oct 20 '15 at 13:10
  • @nicola No, originally x has only 1 column/it is a vector. Adding this extra column of ones, implies we now have 2. – pk_22 Oct 20 '15 at 16:12
  • @Pascal I know R won't lie to me, but nobody seems to be able to find the mistake in my code/why the error occurs. As far as I know, I can multiply a 200x2 by a 2x1. – pk_22 Oct 20 '15 at 16:14
  • 2
    @pvb1995 you are wrong. Execute your code. See that before calling `optim`, `x` is a `matrix` (can you see the `x <- cbind(rep(1,n),x1)` line?). Than, when `x` is passed to `lltobit`, another column is added and then the error. – nicola Oct 20 '15 at 16:17
  • @nicola thanks let me try this – pk_22 Oct 20 '15 at 17:27
  • @nicola it works! I only needed the matrix x before, for the initial values, and the original x vector for the optim. thanks for showing me – pk_22 Oct 20 '15 at 17:33

2 Answers2

17

I stumbled upon a similar problem today ("non-conformable arguments" error, even though everything seemed OK), and solution in my case was in basic rules for matrix-multiplication: i.e. number of columns of the left matrix must be the same as the number of rows of the right matrix = I had to switch order in multiplication equation. In other words, in matrix multiplication (unlike ordinary multiplication), A %*% B is not the same as B %*% A.

Jan
  • 171
  • 1
  • 3
0

I offer one case in Principal Component Regression (PCR) in R, today I met this problem when tring to fit test data with model. it returned an error:

> pcr.pred = predict(pcr.fit, test.data, ncomp=6)
Error in newX %*% B[-1, , i] : non-conformable arguments
In addition: Warning message:

The problem was that, the test data has a new level that is previously not contained in the train data. To find which level has the problem:

cols = colnames(train)
for (col in cols){
  if(class(ori.train[[col]]) == 'factor'){
    print(col)
    print(summary(train[[col]]))
    print(summary(test[[col]]))
  }
}

You can check which annoying attributes has this new level, then you can replace this 'new' attribute with other common values, save the data with write.csv and reload it, and you can run the PCR prediction.