How can I make a graph like this for ANOVA 2-Way
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm trying to obtain this graph from the example here for my ANOVA data: https://www.mathworks.com/matlabcentral/answers/488962-repeated-measures-anova-matlab?s_tid=srchtitle. However, I am having a hard time understanding the code. This is the code I've written for my data so far.
clear
clc
table1 = readtable('assignment3simple.xlsx');
table1matrix = table1{:,2:7};
anova2(table1matrix, 2)
figure;
boxplot(table1matrix,{'20°C', '30°C', '40°C', '50°C', '60°C', '70°C'})
xlabel('Operating Temperature');
ylabel('Power Output (w)');
title('Box Plot of Power Output at Different Operating Temperatures');
I've already got the graph for the interaction but getting the individual one is confusing me. Any help would be greatly appreciated. Thank you.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1737031/image.png)
Note: This is the code used for the figure. (I am not using the same data, just trying to understand the code and apply it to mine)
bpdata = [];
for i = 1:max(Q2.PATIENT) %assuming patient numbers are 1:max
bpdata = [bpdata, Q2{Q2.PATIENT==i,3:8},nan(size(unique(Q2.TREAT)))];
end
figure()
boxplot(bpdata)
arrayfun(@xline,7:7:size(bpdata,2))
xlabel('6 treatment times across 11 patients')
ylabel('measurement value')
title('Data pooled between treatment factors')
set(gca,'XTick', [])
(Courtesy of Adam Danz)
3 Commenti
dpb
il 18 Lug 2024
I don't see how it has anything to do with your current dataset??? It's building a columnar dataset by some grouping variable in a totally different dataset.
What, precisely, are you trying to plot from your current data?
Risposta accettata
Voss
il 18 Lug 2024
The code that constructs bpdata takes the table data for each patient in turn and puts it together into a 2-row matrix with a column of NaNs in between each patient's data (so that there are gaps in the boxplot which is where the vertical lines will go).
% initialize bpdata to be an empty array
bpdata = [];
% loop over each patient, assuming patient numbers are 1:max (which is not the case for your SolarIrradiance values)
for i = 1:max(Q2.PATIENT)
% append to bpdata a matrix consisting of patient i's data from the table followed by a column of NaNs
bpdata = [bpdata, Q2{Q2.PATIENT==i,3:8}, nan(size(unique(Q2.TREAT)))];
% ^^^^^^^^^^^^^^^^^^^^^................................ data from table Q2 for PATIENT i
% ^^^^^^^^^^^^^^^^^^^^^^^^^^^... a column of NaNs
end
An implicit assumption is that each patient number appears the same number of times in the table. If that assumption is violated, you'd run into an error trying to append say a 3-row matrix for a patient that appears in 3 rows of the table with say the 2-row matrix bpdata if each previous patient had appeared in only 2 rows. Your data table also conforms to this assumption, i.e., each SolarIrradiance value appears the same number of times in the table (2 times). However, I'll write a more general version that handles the situation that values appear different numbers of times in the table.
The explicit assumption that the patient numbers are 1:max(Q2.PATIENT) is violated by your data, i.e., SolarIrradiance values are 200, 400, 600, 800, 1000, not 1,2,3,4,...,1000. So the code will have to be adjusted for that.
table1 = readtable('assignment3simple.xlsx');
table1matrix = table1{:,2:7};
anova2(table1matrix, 2)
figure;
boxplot(table1matrix,{'20°C', '30°C', '40°C', '50°C', '60°C', '70°C'})
xlabel('Operating Temperature');
ylabel('Power Output (W)');
title('Box Plot of Power Output at Different Operating Temperatures');
% initialize bpdata to be an empty array with one row and zero columns
bpdata = zeros(1,0);
% initially bpdata has one row
Nrows = 1;
% get the set of unique SolarIrradiance values
Irr = unique(table1.SolarIrradiance);
% number of unique SolarIrradiance values
NIrr = numel(Irr);
% for each unique value
for ii = 1:NIrr
% get the table data for this SolarIrradiance value
newdata = table1{table1.SolarIrradiance==Irr(ii),2:end};
% get the number of rows it has
newNrows = size(newdata,1);
if newNrows > Nrows
% if the number of rows is greater than the number of rows bpdata has,
% extend bpdata with rows of NaNs to match the number of rows of newdata
% and store newNrows as the new Nrows value
Nrows = newNrows;
bpdata(end+1:Nrows,:) = NaN;
elseif newNrows < Nrows
% if the number of rows is less than the number of rows bpdata has,
% extend newdata with rows of NaNs to match the number of rows of bpdata
newdata(end+1:Nrows,:) = NaN;
end
% append the new data and a column of NaNs to bpdata
bpdata = [bpdata, newdata, NaN(Nrows,1)];
end
% remove the last column of NaNs
bpdata(:,end) = [];
figure()
boxplot(bpdata)
NT = size(table1,2)-1; % NT: number of temperatures
xline(NT+1:NT+1:size(bpdata,2)) % put vertical lines in between the groups
xlabel(sprintf('%d Temperatures Across %d Irradiance Values',NT,NIrr))
ylabel('Power Output (W)')
title('Data pooled')
set(gca,'XTick', [])
0 Commenti
Più risposte (0)
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!