Clear Filters
Clear Filters

How to apply PCA for my dataset

5 views (last 30 days)
shagun
shagun on 19 Apr 2024
Commented: shagun on 22 Apr 2024
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
shagun
shagun on 19 Apr 2024
thanks, but how should i put variable name on my graph.
Taylor
Taylor on 22 Apr 2024
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
shagun
shagun on 22 Apr 2024
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.
shagun
shagun on 22 Apr 2024
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.

Tags

Community Treasure Hunt

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

Start Hunting!