# My one-sample t-test isn't working as I need it to.

조회 수: 21 (최근 30일)
Ruslan Avramenko 2024년 4월 26일
편집: Voss 2024년 4월 30일
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

댓글을 달려면 로그인하십시오.

### 채택된 답변

arushi 2024년 4월 30일
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.
Hope this helps.
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Ruslan Avramenko 2024년 4월 30일
Hi Arushi,
I've already tried using ttest instead of ttest2. The code gave me different results, but the problem remained the same. I didn't obtain results for 80 mm.
Here is my code:
for i = 2:length(data)
[~, p_values(i-1)] = ttest(data(1:i-1), reference_value, 'Alpha', alpha);
end
Thank you for help

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by