Conflicting Results When Manually Calculating First Principal Component using prcomp
I am calculating the PCA for the iris dataset as follows:
data(iris)
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
This is the first row of the iris dataset:
head(iris, 1)
#Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
For the first row, I can see that the value of the first principal component is -2.257141:
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
#[1,] -2.257141 -0.4784238 0.1272796 0.02408751
But when I try extract the loadings:
ir.pca$rotation[, 1]
Sepal.Length Sepal.Width Petal.Length Petal.Width
0.5210659 -0.2693474 0.5804131 0.5648565
and calculate the first principal component myself:
0.5210659 * 5.1 + -0.2693474 * 3.5 + 0.5804131 * 1.4 + 0.5648565 * 0.2
I get a different result of 2.64027.
Why is that?
r pca predict
add a comment |
I am calculating the PCA for the iris dataset as follows:
data(iris)
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
This is the first row of the iris dataset:
head(iris, 1)
#Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
For the first row, I can see that the value of the first principal component is -2.257141:
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
#[1,] -2.257141 -0.4784238 0.1272796 0.02408751
But when I try extract the loadings:
ir.pca$rotation[, 1]
Sepal.Length Sepal.Width Petal.Length Petal.Width
0.5210659 -0.2693474 0.5804131 0.5648565
and calculate the first principal component myself:
0.5210659 * 5.1 + -0.2693474 * 3.5 + 0.5804131 * 1.4 + 0.5648565 * 0.2
I get a different result of 2.64027.
Why is that?
r pca predict
add a comment |
I am calculating the PCA for the iris dataset as follows:
data(iris)
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
This is the first row of the iris dataset:
head(iris, 1)
#Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
For the first row, I can see that the value of the first principal component is -2.257141:
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
#[1,] -2.257141 -0.4784238 0.1272796 0.02408751
But when I try extract the loadings:
ir.pca$rotation[, 1]
Sepal.Length Sepal.Width Petal.Length Petal.Width
0.5210659 -0.2693474 0.5804131 0.5648565
and calculate the first principal component myself:
0.5210659 * 5.1 + -0.2693474 * 3.5 + 0.5804131 * 1.4 + 0.5648565 * 0.2
I get a different result of 2.64027.
Why is that?
r pca predict
I am calculating the PCA for the iris dataset as follows:
data(iris)
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
This is the first row of the iris dataset:
head(iris, 1)
#Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
For the first row, I can see that the value of the first principal component is -2.257141:
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
#[1,] -2.257141 -0.4784238 0.1272796 0.02408751
But when I try extract the loadings:
ir.pca$rotation[, 1]
Sepal.Length Sepal.Width Petal.Length Petal.Width
0.5210659 -0.2693474 0.5804131 0.5648565
and calculate the first principal component myself:
0.5210659 * 5.1 + -0.2693474 * 3.5 + 0.5804131 * 1.4 + 0.5648565 * 0.2
I get a different result of 2.64027.
Why is that?
r pca predict
r pca predict
edited Jan 3 at 14:22
AkselA
4,66921326
4,66921326
asked Jan 3 at 13:43
orrymrorrymr
532824
532824
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Scaling is the issue.
Either drop scaling in the prcomp()
call
data(iris)
ir.pca <- prcomp(iris[, 1:4], center = FALSE, scale. = FALSE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -5.912747 2.302033 0.007401536 0.003087706
ir.pca$rotation[, 1] %*% t(iris[1, 1:4])
# 1
# [1,] -5.912747
Or scale iris
before you manually apply the loadings
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -2.257141 -0.4784238 0.1272796 0.02408751
ir.pca$rotation[, 1] %*% scale(iris[, 1:4])[1,]
# [,1]
# [1,] -2.257141
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%2f54023496%2fconflicting-results-when-manually-calculating-first-principal-component-using-pr%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
Scaling is the issue.
Either drop scaling in the prcomp()
call
data(iris)
ir.pca <- prcomp(iris[, 1:4], center = FALSE, scale. = FALSE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -5.912747 2.302033 0.007401536 0.003087706
ir.pca$rotation[, 1] %*% t(iris[1, 1:4])
# 1
# [1,] -5.912747
Or scale iris
before you manually apply the loadings
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -2.257141 -0.4784238 0.1272796 0.02408751
ir.pca$rotation[, 1] %*% scale(iris[, 1:4])[1,]
# [,1]
# [1,] -2.257141
add a comment |
Scaling is the issue.
Either drop scaling in the prcomp()
call
data(iris)
ir.pca <- prcomp(iris[, 1:4], center = FALSE, scale. = FALSE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -5.912747 2.302033 0.007401536 0.003087706
ir.pca$rotation[, 1] %*% t(iris[1, 1:4])
# 1
# [1,] -5.912747
Or scale iris
before you manually apply the loadings
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -2.257141 -0.4784238 0.1272796 0.02408751
ir.pca$rotation[, 1] %*% scale(iris[, 1:4])[1,]
# [,1]
# [1,] -2.257141
add a comment |
Scaling is the issue.
Either drop scaling in the prcomp()
call
data(iris)
ir.pca <- prcomp(iris[, 1:4], center = FALSE, scale. = FALSE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -5.912747 2.302033 0.007401536 0.003087706
ir.pca$rotation[, 1] %*% t(iris[1, 1:4])
# 1
# [1,] -5.912747
Or scale iris
before you manually apply the loadings
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -2.257141 -0.4784238 0.1272796 0.02408751
ir.pca$rotation[, 1] %*% scale(iris[, 1:4])[1,]
# [,1]
# [1,] -2.257141
Scaling is the issue.
Either drop scaling in the prcomp()
call
data(iris)
ir.pca <- prcomp(iris[, 1:4], center = FALSE, scale. = FALSE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -5.912747 2.302033 0.007401536 0.003087706
ir.pca$rotation[, 1] %*% t(iris[1, 1:4])
# 1
# [1,] -5.912747
Or scale iris
before you manually apply the loadings
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
head(ir.pca$x, 1)
# PC1 PC2 PC3 PC4
# [1,] -2.257141 -0.4784238 0.1272796 0.02408751
ir.pca$rotation[, 1] %*% scale(iris[, 1:4])[1,]
# [,1]
# [1,] -2.257141
answered Jan 3 at 14:17
AkselAAkselA
4,66921326
4,66921326
add a comment |
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%2f54023496%2fconflicting-results-when-manually-calculating-first-principal-component-using-pr%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