Questions tagged [prcomp]

The R Stats Package function prcomp is used to perform a principal components analysis on the given data matrix. It returns the results as an object of class ```prcomp```.

The calculation is done by a singular value decomposition of the (centered and possibly scaled) data matrix, not by using eigen on the covariance matrix. This is generally the preferred method for numerical accuracy. The print method for these objects prints the results in a nice format and the plot method produces a scree plot.

Vignette: link

source code: link

44 questions
25
votes
3 answers

How to solve prcomp.default(): cannot rescale a constant/zero column to unit variance

I have a data set of 9 samples (rows) with 51608 variables (columns) and I keep getting the error whenever I try to scale it: This works fine pca = prcomp(pca_data) However, pca = prcomp(pca_data, scale = T) gives > Error in…
Brian Jackson
  • 409
  • 1
  • 5
  • 16
4
votes
0 answers

How to perform Principal Component Analysis for different discharge stations?

This is rather a conceptual question for me. I have station discharge data. A reproducible example is shown below: > A <- rnorm(n = 10) > B <- rnorm(n = 10) > C <- rnorm(n = 10) > D <- rnorm(n = 10) > Year <- seq(1981, 1990,1) > df <-…
Sayantan4796
  • 169
  • 1
  • 10
1
vote
1 answer

Turn dataset to matrix and keep the "sample" column to run prcomp in R

I have a dataset that looks like this: sample area trnsp a 2,455 134,23 b 2,009 176,32 c 1,997 200,01 d 2,309 149,87 If I run prcomp(data) I will get a must be numeric error because the sample column is a character. I was thinking…
1
vote
1 answer

How autoplot (ggplot) gets scores and loadings from prcomp

I know that there are lots of discussions out there that treat this subject matter... but every time I encounter this, I never find a consistent, satisfying answer. I'm trying to create a very basic graphical depiction of a principal components…
Ryan Utz
  • 51
  • 1
  • 6
1
vote
0 answers

How to fix "Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric" for PCA - prcomp?

So, I encountered this error trying to run the PCA via prcomp function on one of my datasets. So the code I use is: data(iris) myPr <- prcomp(iris[, -5], scale = TRUE) PCA <- cbind(iris, myPr$x) then a ggplot2 part for the graph. So, in this…
1
vote
0 answers

Change decimals for proportion of variance displayed in autoplot PCA

I would like to change the number of decimals for the Proportion of Variance displayed in the axes of a PCA plot using prcomp() and autoplot(). By default it's set to 2 decimals places (92.46%, 5.31%) but I'd like to round them to 1 decimal place…
elisemillar
  • 55
  • 1
  • 4
1
vote
1 answer

How to understand 'prcomp' result? '$ sdev'/'$ rotation'/'$ center'/'$ scale $ x'

How to understand 'prcomp' result? After running below code, we get prcomp result 'res.pca'. It include '$ sdev'/'$ rotation'/'$ center'/'$ scale $ x',how to understand all of them. Thanks. library(factoextra) data("decathlon2") decathlon.active <-…
anderwyang
  • 1,801
  • 4
  • 18
1
vote
1 answer

How to plot the 2nd and 3rd principle component after using prcomp

How to plot the second and third principal components after using prcomp? More variance is explained by the second and third principal components on my variables of greatest interest. Here is the code I'm using for the first and second: res.pca <-…
user2795569
  • 343
  • 2
  • 9
1
vote
1 answer

How to plot distance biplot and correlation biplot results of SVD/PCA in R?

I searched for a long time for a straightforward explanation of the distance vs correlation biplots, as well as an explanation of how to transform the standard outputs of PCA to achieve the two biplots. All the stack overflow explanations 1 2 3 4 I…
1
vote
1 answer

How to change loading labels of biplot in r?

I am struggling with changing the loading labels of biplot. I used prcomp function to run PCA and ggbiplot. I've tried to change it with loading.label.label = c("a","b","c","d"), but it didn't work. I have attached my code and plot below. k <-…
1
vote
2 answers

PCA in R - Do we need to reassign the elements of "prcomp" by multiplying negative sign?

A trainer did this in a video. He just gave a quick explanation that he does this because of R's default nature. However, I have never seen this application before. Is it correct, and why he does this? pca <- prcomp(data, scale=TRUE) pca$rotation <-…
1
vote
1 answer

R pairwise PCA function coverts X is nonnumeric object

I am writing a function that performs PCA on pairs of variables in an xts object until the correlation between all of the variables is less than 0.1. Here is the function that I wrote: PCA_Selection <- function(X, r=0.1){ M <- cor(X) # Creating…
benalbert342
  • 71
  • 1
  • 4
1
vote
0 answers

ggbiplot returning a blank red column

I have a microarray of 47,303 probes and 6 time points of treatment of samples. They are in a large matrix: dim(microarray) 47303 6 I've done the following: pca <- prcomp(x = t(microarray), scale. = T, center = T) Using ggplot2 I'm able to…
Anurag N. Sharma
  • 362
  • 2
  • 10
1
vote
1 answer

Apply PCA for data stored in list

My image data is stored in a list. For every pixel (626257) of my image I have a vector containing all the values corresponding to the different wavelengths (44 wavelengths). Now I would like to carry out a principal component analysis (PCA).…
stefan
  • 19
  • 5
1
vote
0 answers

My components functions are much shorter than the data given

I'm trying to run a PCA on data of kelp populations over 140 time points and space. However, my principal component functions only have 38 points in them whereas my data has 140. Shouldn't the PC functions be as long as the number of rows of data…
1
2 3