How can I import and work with correlation matrix as the only data source in PCA and PCF in R Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How can I create a correlation matrix in R?How to reconstruct the original data matrix after applying PCA in RHow can I view the source code for a function?PCA analysis using Correlation Matrix as input in RHow to retrieve eigenvalues & eigenvectors from Raster PCA in R?MATLAB perform PCA on the correlation matrixuse correlation matrix in robust PCA functions RHow can you create a correlation matrix in PCA on Python?PCA with zero and high correlation in dataDon't know why eigen() gives a vectors of wrong sign and the loading matrix is just vector
false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'
Short story about an alien named Ushtu(?) coming from a future Earth, when ours was destroyed by a nuclear explosion
Like totally amazing interchangeable sister outfit accessory swapping or whatever
Who's this lady in the war room?
Why is ArcGIS Pro not symbolizing my entire range of values?
Reflections in a Square
What helicopter has the most rotor blades?
Meaning of "Not holding on that level of emuna/bitachon"
What documents does someone with a long-term visa need to travel to another Schengen country?
Does the Pact of the Blade warlock feature allow me to customize the properties of the pact weapon I create?
Are bags of holding fireproof?
Are there any AGPL-style licences that require source code modifications to be public?
Converting a text document with special format to Pandas DataFrame
Is Bran literally the world's memory?
Pointing to problems without suggesting solutions
Has a Nobel Peace laureate ever been accused of war crimes?
How do I deal with an erroneously large refund?
Why not use the yoke to control yaw, as well as pitch and roll?
When speaking, how do you change your mind mid-sentence?
What is the evidence that custom checks in Northern Ireland are going to result in violence?
Assertions In A Mock Callout Test
Who can become a wight?
Does traveling In The United States require a passport or can I use my green card if not a US citizen?
Why do people think Winterfell crypts is the safest place for women, children & old people?
How can I import and work with correlation matrix as the only data source in PCA and PCF in R
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How can I create a correlation matrix in R?How to reconstruct the original data matrix after applying PCA in RHow can I view the source code for a function?PCA analysis using Correlation Matrix as input in RHow to retrieve eigenvalues & eigenvectors from Raster PCA in R?MATLAB perform PCA on the correlation matrixuse correlation matrix in robust PCA functions RHow can you create a correlation matrix in PCA on Python?PCA with zero and high correlation in dataDon't know why eigen() gives a vectors of wrong sign and the loading matrix is just vector
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am new to R, and am working on a problem of *mporting and working with correlation matrix as the only data source in PCA and PCF in R
I have referred to stack overflow answer banks and even books, I could not find any hints, it make it like R only work with variables data file whereas in SAS you can simply input the correlation matrix and get the PCA and PCF result easily. Hope I am wrong.
I tried to look at stack overflow answer banks, and they are mostly about how to calculate the cor matrix or eigenvalue decomposition.
Below is my attempts:
setwd("D:/BlueHDD/MAQAB/RStudio/R/PCA/Intelligence")
mydata <- read.csv("Intelligence.csv",na.strings = ".")
head(mydata)
X M P C E H F
1 M 1.000 0.620 0.540 0.320 0.284 0.370
2 P 0.620 1.000 0.510 0.380 0.351 0.430
3 C 0.540 0.510 1.000 0.360 0.336 0.405
4 E 0.320 0.380 0.360 1.000 0.686 0.730
5 H 0.284 0.351 0.336 0.686 1.000 0.735
6 F 0.370 0.430 0.405 0.730 0.735 1.000
ii <- as.matrix(mydata[,2:7])
rownames(ii)<- c ("M","P","C","E","H","F")
colnames(ii)<- c ("M","P","C","E","H","F")
head(ii)
M P C E H F
M 1.000 0.620 0.540 0.320 0.284 0.370
P 0.620 1.000 0.510 0.380 0.351 0.430
C 0.540 0.510 1.000 0.360 0.336 0.405
E 0.320 0.380 0.360 1.000 0.686 0.730
H 0.284 0.351 0.336 0.686 1.000 0.735
F 0.370 0.430 0.405 0.730 0.735 1.000
myPCA <- eigen(ii)
head(myPCA)
$values
[1] 3.3670861 1.1941791 0.5070061 0.3718472 0.3131559 0.2467257
$vectors
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.3677678 -0.5098401 0.266985551 0.72768020 0.047584025 -0.04178482
[2,] -0.3913477 -0.4092063 0.485916591 -0.66464527 -0.005392018 -0.03872816
[3,] -0.3719504 -0.3825819 -0.831626240 -0.15204371 -0.003331423 -0.02352388
[4,] -0.4321872 0.3748248 0.021531885 0.06531777 -0.742970281 -0.34056682
[5,] -0.4219572 0.4214599 0.002730054 0.01174474 0.665109730 -0.44922966
[6,] -0.4565228 0.3288196 0.023032686 0.03473540 0.057617669 0.82365511
myPCA$values
[1] 3.3670861 1.1941791 0.5070061 0.3718472 0.3131559 0.2467257
myPCA$vectors
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.3677678 -0.5098401 0.266985551 0.72768020 0.047584025 -0.04178482
[2,] -0.3913477 -0.4092063 0.485916591 -0.66464527 -0.005392018 -0.03872816
[3,] -0.3719504 -0.3825819 -0.831626240 -0.15204371 -0.003331423 -0.02352388
[4,] -0.4321872 0.3748248 0.021531885 0.06531777 -0.742970281 -0.34056682
[5,] -0.4219572 0.4214599 0.002730054 0.01174474 0.665109730 -0.44922966
[6,] -0.4565228 0.3288196 0.023032686 0.03473540 0.057617669 0.82365511
The problem now in the vector, all the "+" and "-" are opposite !
Also, from here, I don't know how to get the loading matrix. I tried the below but fails:
fit <- princomp(ii)
summary(fit) # print variance accounted for
loadings(fit) # pc loadings
plot(fit,type="lines") # scree plot
fit$scores # the principal components
biplot(fit)
r pca
add a comment |
I am new to R, and am working on a problem of *mporting and working with correlation matrix as the only data source in PCA and PCF in R
I have referred to stack overflow answer banks and even books, I could not find any hints, it make it like R only work with variables data file whereas in SAS you can simply input the correlation matrix and get the PCA and PCF result easily. Hope I am wrong.
I tried to look at stack overflow answer banks, and they are mostly about how to calculate the cor matrix or eigenvalue decomposition.
Below is my attempts:
setwd("D:/BlueHDD/MAQAB/RStudio/R/PCA/Intelligence")
mydata <- read.csv("Intelligence.csv",na.strings = ".")
head(mydata)
X M P C E H F
1 M 1.000 0.620 0.540 0.320 0.284 0.370
2 P 0.620 1.000 0.510 0.380 0.351 0.430
3 C 0.540 0.510 1.000 0.360 0.336 0.405
4 E 0.320 0.380 0.360 1.000 0.686 0.730
5 H 0.284 0.351 0.336 0.686 1.000 0.735
6 F 0.370 0.430 0.405 0.730 0.735 1.000
ii <- as.matrix(mydata[,2:7])
rownames(ii)<- c ("M","P","C","E","H","F")
colnames(ii)<- c ("M","P","C","E","H","F")
head(ii)
M P C E H F
M 1.000 0.620 0.540 0.320 0.284 0.370
P 0.620 1.000 0.510 0.380 0.351 0.430
C 0.540 0.510 1.000 0.360 0.336 0.405
E 0.320 0.380 0.360 1.000 0.686 0.730
H 0.284 0.351 0.336 0.686 1.000 0.735
F 0.370 0.430 0.405 0.730 0.735 1.000
myPCA <- eigen(ii)
head(myPCA)
$values
[1] 3.3670861 1.1941791 0.5070061 0.3718472 0.3131559 0.2467257
$vectors
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.3677678 -0.5098401 0.266985551 0.72768020 0.047584025 -0.04178482
[2,] -0.3913477 -0.4092063 0.485916591 -0.66464527 -0.005392018 -0.03872816
[3,] -0.3719504 -0.3825819 -0.831626240 -0.15204371 -0.003331423 -0.02352388
[4,] -0.4321872 0.3748248 0.021531885 0.06531777 -0.742970281 -0.34056682
[5,] -0.4219572 0.4214599 0.002730054 0.01174474 0.665109730 -0.44922966
[6,] -0.4565228 0.3288196 0.023032686 0.03473540 0.057617669 0.82365511
myPCA$values
[1] 3.3670861 1.1941791 0.5070061 0.3718472 0.3131559 0.2467257
myPCA$vectors
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.3677678 -0.5098401 0.266985551 0.72768020 0.047584025 -0.04178482
[2,] -0.3913477 -0.4092063 0.485916591 -0.66464527 -0.005392018 -0.03872816
[3,] -0.3719504 -0.3825819 -0.831626240 -0.15204371 -0.003331423 -0.02352388
[4,] -0.4321872 0.3748248 0.021531885 0.06531777 -0.742970281 -0.34056682
[5,] -0.4219572 0.4214599 0.002730054 0.01174474 0.665109730 -0.44922966
[6,] -0.4565228 0.3288196 0.023032686 0.03473540 0.057617669 0.82365511
The problem now in the vector, all the "+" and "-" are opposite !
Also, from here, I don't know how to get the loading matrix. I tried the below but fails:
fit <- princomp(ii)
summary(fit) # print variance accounted for
loadings(fit) # pc loadings
plot(fit,type="lines") # scree plot
fit$scores # the principal components
biplot(fit)
r pca
Can you give a little more detail, maybe an example? eigendecomposition of a correlation matrix is (scaled) PCA ...
– Ben Bolker
Mar 9 at 2:26
Understood eigendecomposition of a correlation matrix is (scaled) PCA.
– Wilks
Mar 9 at 3:19
My problem is that the only data source is a correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:20
Below is my attempts:
– Wilks
Mar 9 at 3:42
add a comment |
I am new to R, and am working on a problem of *mporting and working with correlation matrix as the only data source in PCA and PCF in R
I have referred to stack overflow answer banks and even books, I could not find any hints, it make it like R only work with variables data file whereas in SAS you can simply input the correlation matrix and get the PCA and PCF result easily. Hope I am wrong.
I tried to look at stack overflow answer banks, and they are mostly about how to calculate the cor matrix or eigenvalue decomposition.
Below is my attempts:
setwd("D:/BlueHDD/MAQAB/RStudio/R/PCA/Intelligence")
mydata <- read.csv("Intelligence.csv",na.strings = ".")
head(mydata)
X M P C E H F
1 M 1.000 0.620 0.540 0.320 0.284 0.370
2 P 0.620 1.000 0.510 0.380 0.351 0.430
3 C 0.540 0.510 1.000 0.360 0.336 0.405
4 E 0.320 0.380 0.360 1.000 0.686 0.730
5 H 0.284 0.351 0.336 0.686 1.000 0.735
6 F 0.370 0.430 0.405 0.730 0.735 1.000
ii <- as.matrix(mydata[,2:7])
rownames(ii)<- c ("M","P","C","E","H","F")
colnames(ii)<- c ("M","P","C","E","H","F")
head(ii)
M P C E H F
M 1.000 0.620 0.540 0.320 0.284 0.370
P 0.620 1.000 0.510 0.380 0.351 0.430
C 0.540 0.510 1.000 0.360 0.336 0.405
E 0.320 0.380 0.360 1.000 0.686 0.730
H 0.284 0.351 0.336 0.686 1.000 0.735
F 0.370 0.430 0.405 0.730 0.735 1.000
myPCA <- eigen(ii)
head(myPCA)
$values
[1] 3.3670861 1.1941791 0.5070061 0.3718472 0.3131559 0.2467257
$vectors
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.3677678 -0.5098401 0.266985551 0.72768020 0.047584025 -0.04178482
[2,] -0.3913477 -0.4092063 0.485916591 -0.66464527 -0.005392018 -0.03872816
[3,] -0.3719504 -0.3825819 -0.831626240 -0.15204371 -0.003331423 -0.02352388
[4,] -0.4321872 0.3748248 0.021531885 0.06531777 -0.742970281 -0.34056682
[5,] -0.4219572 0.4214599 0.002730054 0.01174474 0.665109730 -0.44922966
[6,] -0.4565228 0.3288196 0.023032686 0.03473540 0.057617669 0.82365511
myPCA$values
[1] 3.3670861 1.1941791 0.5070061 0.3718472 0.3131559 0.2467257
myPCA$vectors
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.3677678 -0.5098401 0.266985551 0.72768020 0.047584025 -0.04178482
[2,] -0.3913477 -0.4092063 0.485916591 -0.66464527 -0.005392018 -0.03872816
[3,] -0.3719504 -0.3825819 -0.831626240 -0.15204371 -0.003331423 -0.02352388
[4,] -0.4321872 0.3748248 0.021531885 0.06531777 -0.742970281 -0.34056682
[5,] -0.4219572 0.4214599 0.002730054 0.01174474 0.665109730 -0.44922966
[6,] -0.4565228 0.3288196 0.023032686 0.03473540 0.057617669 0.82365511
The problem now in the vector, all the "+" and "-" are opposite !
Also, from here, I don't know how to get the loading matrix. I tried the below but fails:
fit <- princomp(ii)
summary(fit) # print variance accounted for
loadings(fit) # pc loadings
plot(fit,type="lines") # scree plot
fit$scores # the principal components
biplot(fit)
r pca
I am new to R, and am working on a problem of *mporting and working with correlation matrix as the only data source in PCA and PCF in R
I have referred to stack overflow answer banks and even books, I could not find any hints, it make it like R only work with variables data file whereas in SAS you can simply input the correlation matrix and get the PCA and PCF result easily. Hope I am wrong.
I tried to look at stack overflow answer banks, and they are mostly about how to calculate the cor matrix or eigenvalue decomposition.
Below is my attempts:
setwd("D:/BlueHDD/MAQAB/RStudio/R/PCA/Intelligence")
mydata <- read.csv("Intelligence.csv",na.strings = ".")
head(mydata)
X M P C E H F
1 M 1.000 0.620 0.540 0.320 0.284 0.370
2 P 0.620 1.000 0.510 0.380 0.351 0.430
3 C 0.540 0.510 1.000 0.360 0.336 0.405
4 E 0.320 0.380 0.360 1.000 0.686 0.730
5 H 0.284 0.351 0.336 0.686 1.000 0.735
6 F 0.370 0.430 0.405 0.730 0.735 1.000
ii <- as.matrix(mydata[,2:7])
rownames(ii)<- c ("M","P","C","E","H","F")
colnames(ii)<- c ("M","P","C","E","H","F")
head(ii)
M P C E H F
M 1.000 0.620 0.540 0.320 0.284 0.370
P 0.620 1.000 0.510 0.380 0.351 0.430
C 0.540 0.510 1.000 0.360 0.336 0.405
E 0.320 0.380 0.360 1.000 0.686 0.730
H 0.284 0.351 0.336 0.686 1.000 0.735
F 0.370 0.430 0.405 0.730 0.735 1.000
myPCA <- eigen(ii)
head(myPCA)
$values
[1] 3.3670861 1.1941791 0.5070061 0.3718472 0.3131559 0.2467257
$vectors
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.3677678 -0.5098401 0.266985551 0.72768020 0.047584025 -0.04178482
[2,] -0.3913477 -0.4092063 0.485916591 -0.66464527 -0.005392018 -0.03872816
[3,] -0.3719504 -0.3825819 -0.831626240 -0.15204371 -0.003331423 -0.02352388
[4,] -0.4321872 0.3748248 0.021531885 0.06531777 -0.742970281 -0.34056682
[5,] -0.4219572 0.4214599 0.002730054 0.01174474 0.665109730 -0.44922966
[6,] -0.4565228 0.3288196 0.023032686 0.03473540 0.057617669 0.82365511
myPCA$values
[1] 3.3670861 1.1941791 0.5070061 0.3718472 0.3131559 0.2467257
myPCA$vectors
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.3677678 -0.5098401 0.266985551 0.72768020 0.047584025 -0.04178482
[2,] -0.3913477 -0.4092063 0.485916591 -0.66464527 -0.005392018 -0.03872816
[3,] -0.3719504 -0.3825819 -0.831626240 -0.15204371 -0.003331423 -0.02352388
[4,] -0.4321872 0.3748248 0.021531885 0.06531777 -0.742970281 -0.34056682
[5,] -0.4219572 0.4214599 0.002730054 0.01174474 0.665109730 -0.44922966
[6,] -0.4565228 0.3288196 0.023032686 0.03473540 0.057617669 0.82365511
The problem now in the vector, all the "+" and "-" are opposite !
Also, from here, I don't know how to get the loading matrix. I tried the below but fails:
fit <- princomp(ii)
summary(fit) # print variance accounted for
loadings(fit) # pc loadings
plot(fit,type="lines") # scree plot
fit$scores # the principal components
biplot(fit)
r pca
r pca
edited Mar 10 at 16:18
halfer
14.8k759118
14.8k759118
asked Mar 9 at 2:09
WilksWilks
11
11
Can you give a little more detail, maybe an example? eigendecomposition of a correlation matrix is (scaled) PCA ...
– Ben Bolker
Mar 9 at 2:26
Understood eigendecomposition of a correlation matrix is (scaled) PCA.
– Wilks
Mar 9 at 3:19
My problem is that the only data source is a correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:20
Below is my attempts:
– Wilks
Mar 9 at 3:42
add a comment |
Can you give a little more detail, maybe an example? eigendecomposition of a correlation matrix is (scaled) PCA ...
– Ben Bolker
Mar 9 at 2:26
Understood eigendecomposition of a correlation matrix is (scaled) PCA.
– Wilks
Mar 9 at 3:19
My problem is that the only data source is a correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:20
Below is my attempts:
– Wilks
Mar 9 at 3:42
Can you give a little more detail, maybe an example? eigendecomposition of a correlation matrix is (scaled) PCA ...
– Ben Bolker
Mar 9 at 2:26
Can you give a little more detail, maybe an example? eigendecomposition of a correlation matrix is (scaled) PCA ...
– Ben Bolker
Mar 9 at 2:26
Understood eigendecomposition of a correlation matrix is (scaled) PCA.
– Wilks
Mar 9 at 3:19
Understood eigendecomposition of a correlation matrix is (scaled) PCA.
– Wilks
Mar 9 at 3:19
My problem is that the only data source is a correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:20
My problem is that the only data source is a correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:20
Below is my attempts:
– Wilks
Mar 9 at 3:42
Below is my attempts:
– Wilks
Mar 9 at 3:42
add a comment |
1 Answer
1
active
oldest
votes
You can perform PCA in R with the princomp
function. The documentation says that if you supply the argument covmat
it will compute the principal components from the covariance matrix. But it also works to use this argument with the correlation matrix.
Here is a simple example using the iris data.
## principal components from the original data
princomp(iris[,1:4], cor=T)
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
Now suppose that you only have a correlation matrix
## from correlation matrix
CM = cor(iris[,1:4])
myPCA = princomp(covmat=CM)
myPCA
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
You get the same answer either way. If you want the loadings, they are stored in the myPCA structure.
myPCA$loadings
Loadings:
Comp.1 Comp.2 Comp.3 Comp.4
Sepal.Length 0.521 0.377 0.720 0.261
Sepal.Width -0.269 0.923 -0.244 -0.124
Petal.Length 0.580 -0.142 -0.801
Petal.Width 0.565 -0.634 0.524
Comp.1 Comp.2 Comp.3 Comp.4
SS loadings 1.00 1.00 1.00 1.00
Proportion Var 0.25 0.25 0.25 0.25
Cumulative Var 0.25 0.50 0.75 1.00
iris.csv is an original variable dataset. My problem is I only have the correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:27
Yes, but CM is just a correlation matrix.
– G5W
Mar 9 at 11:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073323%2fhow-can-i-import-and-work-with-correlation-matrix-as-the-only-data-source-in-pca%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can perform PCA in R with the princomp
function. The documentation says that if you supply the argument covmat
it will compute the principal components from the covariance matrix. But it also works to use this argument with the correlation matrix.
Here is a simple example using the iris data.
## principal components from the original data
princomp(iris[,1:4], cor=T)
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
Now suppose that you only have a correlation matrix
## from correlation matrix
CM = cor(iris[,1:4])
myPCA = princomp(covmat=CM)
myPCA
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
You get the same answer either way. If you want the loadings, they are stored in the myPCA structure.
myPCA$loadings
Loadings:
Comp.1 Comp.2 Comp.3 Comp.4
Sepal.Length 0.521 0.377 0.720 0.261
Sepal.Width -0.269 0.923 -0.244 -0.124
Petal.Length 0.580 -0.142 -0.801
Petal.Width 0.565 -0.634 0.524
Comp.1 Comp.2 Comp.3 Comp.4
SS loadings 1.00 1.00 1.00 1.00
Proportion Var 0.25 0.25 0.25 0.25
Cumulative Var 0.25 0.50 0.75 1.00
iris.csv is an original variable dataset. My problem is I only have the correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:27
Yes, but CM is just a correlation matrix.
– G5W
Mar 9 at 11:50
add a comment |
You can perform PCA in R with the princomp
function. The documentation says that if you supply the argument covmat
it will compute the principal components from the covariance matrix. But it also works to use this argument with the correlation matrix.
Here is a simple example using the iris data.
## principal components from the original data
princomp(iris[,1:4], cor=T)
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
Now suppose that you only have a correlation matrix
## from correlation matrix
CM = cor(iris[,1:4])
myPCA = princomp(covmat=CM)
myPCA
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
You get the same answer either way. If you want the loadings, they are stored in the myPCA structure.
myPCA$loadings
Loadings:
Comp.1 Comp.2 Comp.3 Comp.4
Sepal.Length 0.521 0.377 0.720 0.261
Sepal.Width -0.269 0.923 -0.244 -0.124
Petal.Length 0.580 -0.142 -0.801
Petal.Width 0.565 -0.634 0.524
Comp.1 Comp.2 Comp.3 Comp.4
SS loadings 1.00 1.00 1.00 1.00
Proportion Var 0.25 0.25 0.25 0.25
Cumulative Var 0.25 0.50 0.75 1.00
iris.csv is an original variable dataset. My problem is I only have the correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:27
Yes, but CM is just a correlation matrix.
– G5W
Mar 9 at 11:50
add a comment |
You can perform PCA in R with the princomp
function. The documentation says that if you supply the argument covmat
it will compute the principal components from the covariance matrix. But it also works to use this argument with the correlation matrix.
Here is a simple example using the iris data.
## principal components from the original data
princomp(iris[,1:4], cor=T)
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
Now suppose that you only have a correlation matrix
## from correlation matrix
CM = cor(iris[,1:4])
myPCA = princomp(covmat=CM)
myPCA
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
You get the same answer either way. If you want the loadings, they are stored in the myPCA structure.
myPCA$loadings
Loadings:
Comp.1 Comp.2 Comp.3 Comp.4
Sepal.Length 0.521 0.377 0.720 0.261
Sepal.Width -0.269 0.923 -0.244 -0.124
Petal.Length 0.580 -0.142 -0.801
Petal.Width 0.565 -0.634 0.524
Comp.1 Comp.2 Comp.3 Comp.4
SS loadings 1.00 1.00 1.00 1.00
Proportion Var 0.25 0.25 0.25 0.25
Cumulative Var 0.25 0.50 0.75 1.00
You can perform PCA in R with the princomp
function. The documentation says that if you supply the argument covmat
it will compute the principal components from the covariance matrix. But it also works to use this argument with the correlation matrix.
Here is a simple example using the iris data.
## principal components from the original data
princomp(iris[,1:4], cor=T)
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
Now suppose that you only have a correlation matrix
## from correlation matrix
CM = cor(iris[,1:4])
myPCA = princomp(covmat=CM)
myPCA
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
You get the same answer either way. If you want the loadings, they are stored in the myPCA structure.
myPCA$loadings
Loadings:
Comp.1 Comp.2 Comp.3 Comp.4
Sepal.Length 0.521 0.377 0.720 0.261
Sepal.Width -0.269 0.923 -0.244 -0.124
Petal.Length 0.580 -0.142 -0.801
Petal.Width 0.565 -0.634 0.524
Comp.1 Comp.2 Comp.3 Comp.4
SS loadings 1.00 1.00 1.00 1.00
Proportion Var 0.25 0.25 0.25 0.25
Cumulative Var 0.25 0.50 0.75 1.00
edited Mar 9 at 11:52
answered Mar 9 at 2:28
G5WG5W
23.8k92344
23.8k92344
iris.csv is an original variable dataset. My problem is I only have the correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:27
Yes, but CM is just a correlation matrix.
– G5W
Mar 9 at 11:50
add a comment |
iris.csv is an original variable dataset. My problem is I only have the correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:27
Yes, but CM is just a correlation matrix.
– G5W
Mar 9 at 11:50
iris.csv is an original variable dataset. My problem is I only have the correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:27
iris.csv is an original variable dataset. My problem is I only have the correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:27
Yes, but CM is just a correlation matrix.
– G5W
Mar 9 at 11:50
Yes, but CM is just a correlation matrix.
– G5W
Mar 9 at 11:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073323%2fhow-can-i-import-and-work-with-correlation-matrix-as-the-only-data-source-in-pca%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you give a little more detail, maybe an example? eigendecomposition of a correlation matrix is (scaled) PCA ...
– Ben Bolker
Mar 9 at 2:26
Understood eigendecomposition of a correlation matrix is (scaled) PCA.
– Wilks
Mar 9 at 3:19
My problem is that the only data source is a correlation matrix. And I could not find the way to import it in R and manipulate it in R.
– Wilks
Mar 9 at 3:20
Below is my attempts:
– Wilks
Mar 9 at 3:42