How to apply PCA for my dataset

Hi,
I am trying to apply pca on my dataset but I am not able to get bioplot. I just need bioplot for my data. Please help me. I am very new to Matlab.
I have attached by dataset
Thanks

 Accepted Answer

Taylor
Taylor on 19 Apr 2024
The Reduce Dimensionality Live Task is perfect for this. Select "show code" at the bottom of the task and from there you can modify the code to your needs.

6 Comments

Thankyou so much bit I am getting this error
Execution of script pca as a function is not supported:
thank you so much It works . I am extremely grateful
Also, I have a question of getting PC1 v/s PC2 but If i want PC1 V/S PC3, how can i get. Just two individual plot of pc1/pc2, pc1v/spc3.
Please let me know .
I will be extremely grateful
thanks
The second output (scores) from pca will contain your PCA scores. Each column corresponds to a component, and the percentage of variance explained by that component is stored in the corresponding row of the output explained.
Your data also has a lot of NaNs, so I would recommend looking at this section of the documentation as well.
thanks, but how should i put variable name on my graph.
You can use the legend, xlabel, and ylabel functions to label your plot.

Sign in to comment.

More Answers (1)

shagun
shagun on 22 Apr 2024
Thank you, But i want my variable name on the graph like same as attached in screenshot.
Thanks

3 Comments

You can use the VarLabels input to biplot.
I have tried but I am getting this error
load (Datagroup)
% Extract numeric variables from table
numericVars = varfun(@isnumeric,Datagroup,"output","uniform");
numericPredictorTable = Datagroup(:, numericVars);
% Convert table to matrix
numericPredictorMatrix = table2array(varfun(@double, numericPredictorTable));
% Principal Component Analysis
[coeffs, transformedData, ~, ~, explained] = pca(numericPredictorMatrix);
% Compute number of components from explained variance
enoughExplained = cumsum(explained)/sum(explained) >= 95/100;
numberOfComponentsToKeep = find(enoughExplained, 1);
disp("The number of components needed to explain at least 95% of the variance is "+ num2str(numberOfComponentsToKeep))
% Display results
figure
colorMap = colormap("lines");
barPlot = bar(explained);
hold on;
plot(cumsum(explained),"*-","Color",colorMap(3,:));
hold off;
barPlot(1).FaceColor = "flat";
barPlot(1).CData(1:numberOfComponentsToKeep,:) = repmat( ...
colorMap(2,:),[numberOfComponentsToKeep 1]);
label = ["Selected components"; sprintf("Cumulative explained\nvariance")];
legend(label,"Location","best");
title("Scree Plot of Explained Variances")
xlabel("Principal component")
ylabel("Variance explained (%)")
clear barPlot colorMap label explained
figure
scatter(transformedData(:,1),transformedData(:,2), "Marker", ".");
title("Scatter Plot of Principal Components")
xlabel("Component 1")
ylabel("Component 2")
variableNames = {'TSSLV10', 'TSSLV20', 'TSSLV30', 'TSSLV40', 'TSSLV50', 'TSSLV60', 'TSSLV70', 'TSSLV80', 'TSSLV90', ...
'TPLV10', 'TPLV20', 'TPLV30', 'TPLV40', 'TPLV50', 'TPLV60', 'TPLV70', 'TPLV80', 'TPLV90', ...
'SRPLV10', 'SRPLV20', 'SRPLV30', 'SRPLV40', 'SRPLV50', 'SRPLV60', 'SRPLV70', 'SRPLV80', 'SRPLV90', ...
'RDep10', 'RDep20', 'RDep30', 'RDep40', 'RDep50', 'RDep60', 'RDep70', 'RDep80', 'RDep90', ...
'AI10', 'AI20', 'AI30', 'AI40', 'AI50', 'AI60', 'AI70', 'AI80', 'AI90', ...
'RD10', 'RD20', 'RD30', 'RD40', 'RD50', 'RD60', 'RD70', 'RD80', 'RD90', 'TRD', 'AI', 'RD', 'ADP (HR)'};
figure
biplot(coeffs(:,[1 2],'VarLabels', variableNames));
title("PCA Biplot")
xlabel("Component 1")
ylabel("Component 2")
clear coeffs enoughExplained explained numericPredictorMatrix numericPredictorTable varLabels
transformedData = transformedData(:,1:numberOfComponentsToKeep);
clear numberOfComponentsToKeep
transformedData = array2table(transformedData);
transformedData = [transformedData,Datagroup(:,~numericVars)];
clear numericVars
Unable to use a value of type cell as an index.
I am getting this error of Unable to use a value of type cell as an indexUnable to use a value of type cell as an index

Sign in to comment.

Categories

Tags

Asked:

on 19 Apr 2024

Commented:

on 22 Apr 2024

Community Treasure Hunt

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

Start Hunting!