title: Calculating 'hat' matrix in R
body: <p>In calculating the 'hat' matrix in weighted least squares a part of the calculation is</p>
  
  <pre><code>X^T*W*X
  </code></pre>
  
  <p>However, I am unsure how one would do this in R</p>
  
  <p>See the following example:</p>
  
  <pre><code>x &lt;- matrix(c(1,2,3,4,5,6),nrow=3,ncol=2,byrow=T)
  xt &lt;- t(x)
  w &lt;- as.vector(c(7,8,9))
  
  xt*w%*%x
  </code></pre>
  
  <p>Which gives the error:</p>
  
  <pre><code>Error in xt * w %*% x : non-conformable arrays
  </code></pre>
  
  <p>Is there anything basic I have misunderstood?</p>
  
  <p>EDIT</p>
  
  <pre><code>xt%*%w%*%x
  </code></pre>
  
  <p>gives the error:</p>
  
  <pre><code>Error in xt %*% w %*% x : non-conformable arguments
  </code></pre>