MATLAB Answers

variance explained & pca

147 views (last 30 days)
Alberto Acri
Alberto Acri on 10 Jan 2021
Commented: Ive J on 15 Jan 2021
Hi! I want to report in a graph the variances explained as a function of PCs as shown in the graph. I have used the function "pareto(ExplVar)" but only the first 10 are represented and not all (there are 20 PCs in total). How can I represent them all?
Alberto Acri
Alberto Acri on 13 Jan 2021
The code is long. I have considered
p = pareto(ExplVar)
where ExplVar is an array 10000x1.

Sign in to comment.

Accepted Answer

Ive J
Ive J on 13 Jan 2021
Edited: Ive J on 13 Jan 2021
pareto only shows the first 10 bars at maximum. You can do it easily with help of cumsum:
[~, ~, ~, ~, explained] = pca(rand(100,20));
hold on
plot(1:numel(explained), cumsum(explained), 'o-', 'MarkerFaceColor', 'r')
yyaxis right
h = gca;
h.YAxis(2).Limits = [0 100];
h.YAxis(2).Color = h.YAxis(1).Color;
h.YAxis(2).TickLabel = strcat(h.YAxis(2).TickLabel, '%');
Ive J
Ive J on 15 Jan 2021
If you are calculating PCs with MATLAB pca built-in function, it can also return explained variances of PCs (explained in above example). If you want to show these explained variances (cumulatively), use explained; otherwise use PC scores if you prefer. It depends on your purposes of course (even you can use anything else to plot), but regardless, you can use my above example to reproduce similar graphs as pareto does, but without it's limitations (i.e. max 10 bars).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!