Error in paired t-test between corresponding elements.

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 comentario

VBBV
VBBV el 26 de Mzo. de 2024
The input data for samples need to be vectors when using ttest or ttest2 functions to evaluate the p-values

Iniciar sesión para comentar.

 Respuesta aceptada

VBBV
VBBV el 25 de Mzo. de 2024
Editada: VBBV el 25 de Mzo. de 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 comentarios

Haya Ali
Haya Ali el 26 de Mzo. de 2024
Editada: Haya Ali el 26 de Mzo. de 2024
Thank you so much. May I ask what have you altered in the code? Since for the following data I have performed the altered code provided by you and it doesnt seems to give right results because element 3 in both groups are same so p value shouldn't be 0.02.
close all; clear all; clc;
% Define the data for the paired samples
group1 = [19, 19, 20, 18, 19, 19, 20, 21, 20, 16, 18, 21, 20, 18, 18, 20, 20, 18, 20, 19, 22, 21, 19];
group2 = [19, 16, 20, 21, 19, 19, 18, 18, 17, 15, 21, 21, 20, 19, 20, 22, 20, 16, 20, 15, 17, 19, 20];
% Define the data for two groups
% group1 = [10, 15, 20, 25, 30];
% group2 = [12, 18, 22, 28, 32];
close all; clear all; clc;
% Define the data for the paired samples
group1 = [19, 19, 20, 18, 19, 19, 20, 21, 20, 16, 18, 21, 20, 18, 18, 20, 20, 18, 20, 19, 22, 21, 19];
group2 = [19, 16, 20, 21, 19, 19, 18, 18, 17, 15, 21, 21, 20, 19, 20, 22, 20, 16, 20, 15, 17, 19, 20];
% 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)] = ttest2(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... Performing t-test for Node 6... Performing t-test for Node 7... Performing t-test for Node 8... Performing t-test for Node 9... Performing t-test for Node 10... Performing t-test for Node 11... Performing t-test for Node 12... Performing t-test for Node 13... Performing t-test for Node 14... Performing t-test for Node 15... Performing t-test for Node 16... Performing t-test for Node 17... Performing t-test for Node 18... Performing t-test for Node 19... Performing t-test for Node 20... Performing t-test for Node 21... Performing t-test for Node 22... Performing t-test for Node 23...
% 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.801068 Node 2: p-value = 0.022479 Node 3: p-value = 0.637238 Node 4: p-value = 0.238596 Node 5: p-value = 0.801068 Node 6: p-value = 0.801068 Node 7: p-value = 0.333778 Node 8: p-value = 0.333778 Node 9: p-value = 0.099202 Node 10: p-value = 0.004252 Node 11: p-value = 0.238596 Node 12: p-value = 0.238596 Node 13: p-value = 0.637238 Node 14: p-value = 0.801068 Node 15: p-value = 0.637238 Node 16: p-value = 0.064717 Node 17: p-value = 0.637238 Node 18: p-value = 0.022479 Node 19: p-value = 0.637238 Node 20: p-value = 0.004252 Node 21: p-value = 0.099202 Node 22: p-value = 0.801068 Node 23: p-value = 0.637238
p_values
p_values = 1x23
0.8011 0.0225 0.6372 0.2386 0.8011 0.8011 0.3338 0.3338 0.0992 0.0043 0.2386 0.2386 0.6372 0.8011 0.6372 0.0647 0.6372 0.0225 0.6372 0.0043 0.0992 0.8011 0.6372
VBBV
VBBV el 26 de Mzo. de 2024
Hi @Haya Ali, for two sample t-test, check the ttest2 function instead of regular ttest
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 25 de Mzo. de 2024

Comentada:

el 26 de Mzo. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by