0

I need to compute Hotelling T2 and SPE (Q), after the PCA analysis. I did it using the pca function from library mdatools, but I see the PC computed are different from the one computed by prcomp or princomp functions. Why?

library(mdatools)
NF4.3.pca4 <- pca(NF4.3, ncomp = 15, center = T, scale = T)
res <- NF4.3.pca4$calres

NF4.3.pca <- prcomp(NF4.3, center = T, scale. = T) #different eigenvalues

Is there another way to calculate T2 and SPE from principal components?

Data:

ASSORB_CAT1;ASSORB_CAT3;ASSORB_VOLANO;AZOTO_IN
0.03662109;23.55957;-12.30469;39.3
0;25.36621;-11.09619;39.2
-0.02441406;21.92383;-11.26709;39.2
-0.02441406;23.10791;-11.07178;39.1
-0.04882813;22.81494;-10.57129;39.59975
0;24.24316;-11.23047;39.89737
0;22.63184;-11.43799;39.8
-0.04882813;24.34082;-13.61084;39.5
0;21.83838;-11.1084;39.4
0;24.3042;-12.08496;39.3
0;24.67041;-12.40234;39.3
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

I will explain on an other dataset, as I do not have access to what you are analysing. The two methods that you applied on my dataset are identical. What can be confusing though is that if you look at stats_pca$sdev it is a root square of the vector of eigenvalues, whereas mdatools_pca$eigenvals reports eigenvalues themselves.

library(mdatools)
data("mtcars")
stats_pca <- prcomp(mtcars, center=TRUE, scale.=TRUE)
mdatools_pca <- mdatools::pca(mtcars, center=TRUE, scale=TRUE)

all.equal(sqrt(mdatools_pca$eigenvals)[1:length(stats_pca$sdev)], stats_pca$sdev)
# TRUE

If you want to go on with Hotelling's T2 I would recommend this read: PCA and Hotelling's T^2 for confidence intervall in R.

Garrus990
  • 88
  • 7
  • I see "Mean relative difference: 0.3862249" with my data – Andrea Contardi May 21 '19 at 15:58
  • I've made some investigation and it seems that it is not your fault. Implementation in the `mdatools` library has (probably) a defect with scaling. If a column has a negative mean (as is in your case 2 columns have) it is not scaled. That is why the values differ. You can either disable scaling or provide precomputed vector of standard deviations in `pca` function to get identical results as in `prcomp`. I opened an issue on that error: https://github.com/svkucheryavski/mdatools/issues/59. – Garrus990 May 22 '19 at 09:58