+ Reply to Thread
Results 1 to 3 of 3

VBA code for PCA

  1. #1
    Registered User
    Join Date
    06-14-2008
    Posts
    12

    VBA code for PCA

    Hi everyone,

    I'm just a newbee to VBA/Excel. For a project, I have to write a function to calculate the eigenvectors and eigenvalues of a matrix using the "Power Method". I've tried searching the web for some help but there's not much out there. Essentially, I want to input a matrix from the excel spreadsheet along with a tolerance level (i.e. two inputs).

    The function then calculates the covariance matrix, call this M (m x m), of the input data. You then make an initial guess of the eigenvector, let's say this is a vector of 1's (m x 1) (call this x), you multiply the two together to get

    y=Mx

    You then calculate beta which is the element of y with the largest modulus. And, recalculate x as

    x=(1/beta)y

    and then calculate new y, y=Mx

    This is done iteratively until the difference between the old x and new x is less than the tolerance level.

    The normalised x, i.e. (1/sqrt(sum(x^2)))*x , call this v, is the first prinicipal component and the last value of beta is the associated eigenvalue.

    A new M is calculated as Mnew = M-beta*v*transpose(v)

    And, the whole procedure is repeat for Mnew to get the second prinicipal component and associated eigenvalue. The is done m times.

    I want to output all the eigenvectors (prinicipal components) and eigenvalues to some location in the spreadsheet.


    I would be extremely grateful if someone could assist me with writing this function or providing some pointers.


    Regards,
    John

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello johnboy12,

    Please change the title of this thread. You have another thread with a similar title. If you don't change the title of the post I will either lock it or delete it. Please read the forum rules about proper posting.

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    06-14-2008
    Posts
    12

    VBA code for PCA

    OK will do Leith.
    Last edited by johnboy12; 06-21-2008 at 05:39 PM. Reason: changing title

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1