My one-sample t-test isn't working as I need it to.
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ruslan Avramenko
am 26 Apr. 2024
Bearbeitet: Voss
am 30 Apr. 2024
Hello everyone. I need to calculate the p-value for each sample with different dimensions and compare them with the reference value, which is the largest sample at 100 mm. I need to perform a one-sample t-test with a significance level of 0.05. Here is my code. I don't know why it always outputs NaN for the 80 mm value. I would like to fix this.
My Code:
% Clear the workspace, command window, and close any open figures
clc;
clear all;
close all;
% Input data
data = [67.142880, 69.218820, 67.379050, 69.943850, 67.407470]; % Data values
rozmery = [100, 80, 60, 40, 20]; % Dimensions
reference_value = data(1); % Reference value
% Significance level
alpha = 0.05;
% Initialize an array to store p-values
p_values = zeros(1, length(data) - 1);
% Perform t-tests and calculate p-values for each subsequent data point
for i = 2:length(data)
% Perform a one-sample t-test
[~, p_values(i-1)] = ttest2(data(1:i-1), data(i), 'Alpha', alpha);
end
% Plot the graph
figure;
plot(rozmery(2:end), p_values, '-o', 'LineWidth', 2); % Plot dimensions against p-values
xlabel('Dimensions (mm)'); % X-axis label
ylabel('P-values'); % Y-axis label
title('P-values vs Dimensions'); % Title of the plot
grid on; % Turn on the grid
% Add labels to the plot
for i = 2:length(rozmery)
text(rozmery(i), p_values(i-1), sprintf('(%d, %f)', rozmery(i), p_values(i-1)),...
'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'left');
% Add text annotations for each point on the plot
end
% Display the corresponding dimension values and p-values
for i = 2:length(rozmery)
fprintf('P-value for dimension %d mm: %f\n', rozmery(i), p_values(i-1));
end
% Find the point where the p-value falls below the significance level
index_rustu = find(p_values < alpha, 1);
% Display the point where the p-value falls below the significance level
if ~isempty(index_rustu)
hold on;
plot(rozmery(index_rustu + 1), p_values(index_rustu), 'r*', 'MarkerSize', 10);
hold off;
fprintf('Point where the p-value falls below the significance level: %d mm\n', rozmery(index_rustu + 1));
fprintf('P-value at this point: %f\n', p_values(index_rustu));
else
disp('No point found where the p-value falls below the significance level.');
end
0 Kommentare
Akzeptierte Antwort
arushi
am 30 Apr. 2024
Hi Ruslan,
The code uses "ttest2" for a two-sample t-test, but you mentioned needing a one-sample t-test to compare each sample with the reference value. This discrepancy might be one of the reasons why you're encountering unexpected results, such as `NaN` for the 80 mm value. The `NaN` could arise because you're comparing a single value against another single value at some point, which is not valid for a two-sample t-test.
To fix this, you should use "ttest" instead of "ttest2". The "ttest" function is designed for one-sample t-tests or paired samples, which seems more aligned with your requirement to compare each sample against the reference value.
Please refer to the following documentation of one-sample and two-sample t-test for more information-
Hope this helps.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!