Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The immediate problem is that in</p> <pre><code>cump[i]=sum(eigenvalue[1:i])/total </code></pre> <p>you refer to <code>eigenvalue</code> which does not exist. I presume you meant to use <code>eig</code> here instead:</p> <pre><code>cump[i]=sum(eig[1:i])/total </code></pre> <p>From the comment the error seems to be:</p> <pre><code>as.vector(x, mode) : cannot coerce type 'closure' to vector of type 'any' </code></pre> <p>This results I suspect because you call the function without specifying <code>scale</code>. R will then find the <code>scale</code> function (a closure) and that can't be coerced to a type needed for the <code>if()</code> statement. An easy way to solve this is to do one of:</p> <pre><code>lab3 &lt;- function(cov, scale = FALSE) { .... </code></pre> <p>or</p> <pre><code>lab3 &lt;- function(cov) { if(missing(scale)) scale &lt;- FALSE .... </code></pre> <p>with the first form preferred.</p> <p>There are other issues;</p> <ol> <li><p>surely you want</p> <pre><code>if(scale) cov &lt;- cov2cor(cov) </code></pre> <p>? I.e. only if you want all variables scaled to zero mean unit variance is correlation matrix required,</p></li> <li><p>the <code>for</code> loop can be done more efficiently with these two lines:</p> <pre><code>indp &lt;- eig / total cump &lt;- cumsum(indp) </code></pre> <p>You there don't need the <code>for</code> loop at all, and you don't need to set up <code>indp</code> and <code>cump</code> first either.</p></li> <li>you call <code>eigen()</code> twice. Better call it once and save the entire returned object. The subset that for the bits you want.</li> </ol> <p>If I solve all these issues then we have the following function:</p> <pre><code>lab3 &lt;- function(cov, scale=FALSE){ if (scale) cov &lt;- cov2cor(cov) ed &lt;- eigen(cov) eig &lt;- ed$values total &lt;- sum(eig) stdev &lt;- sqrt(eig) rotation &lt;-ed$vectors indp &lt;- eig / total cump &lt;- cumsum(eig) list(stdev, rotation, indp, cump) } </code></pre> <p>Which works:</p> <pre><code>&gt; lab3(cov(iris[, 1:4])) [[1]] [1] 2.0562689 0.4926162 0.2796596 0.1543862 [[2]] [,1] [,2] [,3] [,4] [1,] 0.36138659 -0.65658877 -0.58202985 0.3154872 [2,] -0.08452251 -0.73016143 0.59791083 -0.3197231 [3,] 0.85667061 0.17337266 0.07623608 -0.4798390 [4,] 0.35828920 0.07548102 0.54583143 0.7536574 [[3]] [1] 0.924618723 0.053066483 0.017102610 0.005212184 [[4]] [1] 4.228242 4.470912 4.549122 4.572957 </code></pre> <p>Finally, I'll note that doing PCA via a singular value decomposition is considered better than via an eigen decomposition for reasons of numerical stability. And you can do all of this via <code>princomp</code> or preferably <code>prcomp</code>, both in base R.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload