Fraction of explained variance in the R function

114 views
Skip to first unread message

Luca Albergante

unread,
Sep 9, 2016, 6:15:35 AM9/9/16
to flashpca-users
Hi all,

I've playing around with the R version of flashpca and it work very well! However I have a small issue: I noticed that the command line version provides information concerning the percentage of explained variance for each component (via the pve.txt file), but I don't seem to be able to get this information directly from the R function. Is there a way to do that?

Best wishes,
Luca

Gad Abraham

unread,
Sep 11, 2016, 8:37:08 PM9/11/16
to Luca Albergante, flashpca-users
Hi Luca,

At the moment it's not returned from the R function, but you can do it
yourself by dividing the eigenvalues by the sum of squares of the
matrix entries and divide by n-1, as long as you use the same
standardisation as used for the PCA. For example,

f <- flashpca(X, ndim=10, tol=1e-6)
s <- sum(scale(X, center=f$center, scale=f$scale)^2) / (nrow(X) - 1)
pve <- f$values / s
cumsum(pve)

Gad
> --
> You received this message because you are subscribed to the Google Groups
> "flashpca-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to flashpca-user...@googlegroups.com.
> To post to this group, send email to flashpc...@googlegroups.com.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/flashpca-users/f0c8b412-7587-46fc-b180-2c9489646894%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nicole Torosin

unread,
Apr 17, 2019, 11:26:28 AM4/17/19
to flashpca-users
Hello, 

I have been attempting these operations and I keep getting the error: 
Error in scale.default(X, center = f$center, scale = f$scale) : 
  length of 'center' must equal the number of columns of 'x'

I only changed ndim to 6 to accomodate my file f <- flashpca(X, ndim=6, tol=1e-6) 

Any suggestions about what to do to avoid this error?

Thank you, 
Nicole 

On Sunday, September 11, 2016 at 6:37:08 PM UTC-6, Gad Abraham wrote:
Hi Luca,

At the moment it's not returned from the R function, but you can do it
yourself by dividing the eigenvalues by the sum of squares of the
matrix entries and divide by n-1, as long as you use the same
standardisation as used for the PCA. For example,

f <- flashpca(X, ndim=10, tol=1e-6)
s <- sum(scale(X, center=f$center, scale=f$scale)^2) / (nrow(X) - 1)
pve <- f$values / s
cumsum(pve)

Gad


On 9 September 2016 at 20:15, Luca Albergante <luca.al...@gmail.com> wrote:
> Hi all,
>
> I've playing around with the R version of flashpca and it work very well!
> However I have a small issue: I noticed that the command line version
> provides information concerning the percentage of explained variance for
> each component (via the pve.txt file), but I don't seem to be able to get
> this information directly from the R function. Is there a way to do that?
>
> Best wishes,
> Luca
>
> --
> You received this message because you are subscribed to the Google Groups
> "flashpca-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Gad Abraham

unread,
Apr 22, 2019, 11:26:11 PM4/22/19
to Nicole Torosin, flashpca-users
Hi Nicole,

Can you paste all the steps here to reproduce your problem?

Gad
> To unsubscribe from this group and stop receiving emails from it, send an email to flashpca-user...@googlegroups.com.
> To post to this group, send email to flashpc...@googlegroups.com.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/flashpca-users/b2cbe65b-1974-4a25-8197-47ffa957b726%40googlegroups.com.

Nicole Torosin

unread,
Apr 24, 2019, 5:50:48 PM4/24/19
to flashpca-users
library(flashpcaR)
ACTLRs <- ("AC_TLRs")
 f <- flashpca(ACTLRs, ndim=4, tol=1e-6)
s <- sum(scale(ACTLRs,center=f$center,scale=f$scale)^2)/(nrow(ACTLRs)-1)
And then I receive the following error:
Error in scale.default(X, center = f$center, scale = f$scale) : 
  length of 'center' must equal the number of columns of 'x'

Nicole

Gad Abraham

unread,
Apr 24, 2019, 10:26:50 PM4/24/19
to Nicole Torosin, flashpca-users
That's because you can't call scale() on a character, it has to be a
matrix (my example code my for a matrix of genotypes X).

If your data is small enough to load into R, then you can just read it
into a matrix, and do as above. Otherwise, it's not currently
supported (although easy to do in principle if I get the time...)
> To unsubscribe from this group and stop receiving emails from it, send an email to flashpca-user...@googlegroups.com.
> To post to this group, send email to flashpc...@googlegroups.com.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/flashpca-users/85991e5b-ade6-4c6e-b4eb-bfb542dae11d%40googlegroups.com.

Nicole Torosin

unread,
Apr 25, 2019, 10:17:03 AM4/25/19
to flashpca-users
Hi Gad, 

Thank you so much for clarifying. How would I convert my PLINK data into a numeric matrix? Or the original vcf file?

Nicole  

Gad Abraham

unread,
Apr 26, 2019, 8:42:25 AM4/26/19
to Nicole Torosin, flashpca-users
Forgot another alternative: if you don't mind running the commandline
flashpca, you can get the proportion of variance explained in the file
pve.txt. It's computed internally, but simply not returned in the
current version of flashpcaR...
> To unsubscribe from this group and stop receiving emails from it, send an email to flashpca-user...@googlegroups.com.
> To post to this group, send email to flashpc...@googlegroups.com.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/flashpca-users/445dd263-5604-4e64-84aa-1195ffb2f589%40googlegroups.com.

Nicole Torosin

unread,
Apr 26, 2019, 11:15:12 AM4/26/19
to flashpca-users
Unfortunately, I have tried multiple times and I cannot get flashpca installed to run from the command line. 

Nicole

Gad Abraham

unread,
May 9, 2019, 12:01:45 AM5/9/19
to Nicole Torosin, flashpca-users
Have you tried the precompiled binaries?
> To unsubscribe from this group and stop receiving emails from it, send an email to flashpca-user...@googlegroups.com.
> To post to this group, send email to flashpc...@googlegroups.com.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/flashpca-users/34fa652a-d51c-4bb9-b9b1-b86432eb4090%40googlegroups.com.

Nicole Torosin

unread,
May 13, 2019, 1:09:25 PM5/13/19
to flashpca-users
Hi Gad, 

I found these and have gotten that to work, thank you!

Nicole
Reply all
Reply to author
Forward
0 new messages