0
votes

I have some experience in using PCA, but this is the first time I am attempting to use PCA for spectral data...

I have a large data with spectra where I used prcomp command to calculated PCA for the whole dataset. My results show that 3 components explain 99% of the variance.

I would like to plot the contribution of each of the three PCA components at every wavelength (in steps of 4, 200-1000 nm) like the example of a plot 2 I found on this site: https://learnche.org/pid/latent-variable-modelling/principal-component-analysis/pca-example-analysis-of-spectral-data

Does anyone have a code how I could do this in R?

Thank you

1

1 Answers

0
votes

I believe the matrix of variable loadings is found in model.pca$rotation, see prcomp documentation. So something like this should do (using the example on your linked website):

file <- 'http://openmv.net/file/tablet-spectra.csv'
spectra <- read.csv(file, header = FALSE)
n.comp <- 4

model.pca <- prcomp(spectra[,2:651],
                    center = TRUE,
                    scale =TRUE,
                    rank. = n.comp)
summary(model.pca)


par(mfrow=c(n.comp,1))
sapply(1:n.comp, function(comp){
  plot(2:651, model.pca$rotation[,comp], type='l', lwd=2,
       main=paste("Comp.", comp), xlab="Wavelength INDEX")

})

I don't have the wavelength values, so I used the indices of the array here ; output below.

enter image description here