Effacer les filtres
Effacer les filtres

Error in paired t-test between corresponding elements.

2 vues (au cours des 30 derniers jours)
Haya Ali
Haya Ali le 25 Mar 2024
Commenté : Haya Ali le 26 Mar 2024
I am trying to perform paired t-test between corresponding elements of group1 and group2. It should provide non-NaN p-values for each corresponding pair but it is providing NAN values. Please help to resolve the error. Below is my code.
% Define the data for two groups
group1 = [10, 15, 20, 25, 30];
group2 = [12, 18, 22, 28, 32];
% Initialize array to store p-values
p_values = zeros(size(group1));
% Check if the sizes of the two groups are the same
if numel(group1) ~= numel(group2)
error('The sizes of the two groups must be the same.');
end
% Perform paired t-test for each pair of corresponding elements
for i = 1:numel(group1)
fprintf('Performing t-test for Node %d...\n', i);
% Perform paired t-test for the current pair
[~, p_values(i)] = ttest(group1(i), group2(i));
end
% Display the p-values for each corresponding pair
for i = 1:numel(group1)
fprintf('Node %d: p-value = %f\n', i, p_values(i));
end
  1 commentaire
VBBV
VBBV le 26 Mar 2024
The input data for samples need to be vectors when using ttest or ttest2 functions to evaluate the p-values

Connectez-vous pour commenter.

Réponse acceptée

VBBV
VBBV le 25 Mar 2024
Modifié(e) : VBBV le 25 Mar 2024
% Define the data for two groups
group1 = [10, 15, 20, 25, 30];
group2 = [12, 18, 22, 28, 32];
% Initialize array to store p-values
p_values = zeros(size(group1));
% Check if the sizes of the two groups are the same
if numel(group1) ~= numel(group2)
error('The sizes of the two groups must be the same.');
end
% Perform paired t-test for each pair of corresponding elements
for i = 1:numel(group1)
fprintf('Performing t-test for Node %d...\n', i);
% Perform paired t-test for the current pair
[~, p_values(i)] = ttest(group1, group2(i)); % use a vector for paired comparison test
end
Performing t-test for Node 1... Performing t-test for Node 2... Performing t-test for Node 3... Performing t-test for Node 4... Performing t-test for Node 5...
% Display the p-values for each corresponding pair
for i = 1:numel(group1)
fprintf('Node %d: p-value = %f\n', i, p_values(i));
end
Node 1: p-value = 0.086418 Node 2: p-value = 0.601832 Node 3: p-value = 0.601832 Node 4: p-value = 0.086418 Node 5: p-value = 0.027426
p_values
p_values = 1x5
0.0864 0.6018 0.6018 0.0864 0.0274
  4 commentaires
VBBV
VBBV le 26 Mar 2024
Hi @Haya Ali, for two sample t-test, check the ttest2 function instead of regular ttest
Haya Ali
Haya Ali le 26 Mar 2024
Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by