title: Error in R: nonconformable arguments. Not true?
body: <p>this is my code: </p>
  
  <pre><code>    #define likelihood function (including an intercept/constant in the function.)
  lltobit &lt;- function(b,x,y) {
    sigma &lt;-  b[3]
    y  &lt;- as.matrix(y)
    x &lt;- as.matrix(x)
    vecones &lt;- rep(1,nrow(x)) 
    x &lt;- cbind(vecones,x)
    bx &lt;- x %*% b[1:2] 
    d &lt;- y != 0 
    llik &lt;- sum(d * ((-1/2)*(log(2*pi) + log(sigma^2) + ((y - bx)/sigma)^2)) 
                + (1-d) * (log(1 - pnorm(bx/sigma))))
    return(-llik)
  }
  
  n &lt;- nrow(censored) #define number of variables 
  y &lt;- censored$y #define y and x for easier use
  x1 &lt;- as.matrix(censored$x)
  x &lt;-  cbind(rep(1,n),x1) #include constant/intercept 
  bols &lt;- (solve(t(x) %*% x)) %*% (t(x) %*% y) #compute ols estimator (XX) -1 XY
  init &lt;- rbind(as.matrix(bols[1:nrow(bols)]),1) #initial values 
  
  init
  
  tobit1 &lt;- optim(init, lltobit, x=x, y=y, hessian=TRUE, method="BFGS")
  </code></pre>
  
  <p>where censored is my data table, including 200 (censored) values of y and 200 values of x. </p>
  
  <p>Everything works, but when running the optim command, i get the following error: </p>
  
  <pre><code>tobit1 &lt;- optim(init, lltobit, x=x, y=y, hessian=TRUE, method="BFGS")
  Error in x %*% b[1:2] : non-conformable arguments
  </code></pre>
  
  <p>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? </p>