Trying to make 2 data sets the same length
80 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have two datasets. One is a 1x102437 and the other is 1x41716. I am trying to make them the same length so that I can perform a paired t-test on the data. How do I make the 1x41716 the same length as the 1x102437? I have tries using interp1 but keep running into trouble with this.
Thanks!
0 Kommentare
Antworten (3)
Voss
am 28 Feb. 2023
Bearbeitet: Voss
am 28 Feb. 2023
If you want to "expand" the shorter dataset to match the length of the longer one using interp1, here's one way:
dataset1 = rand(1,51); % random data
dataset2 = rand(1,21);
n1 = numel(dataset1);
n2 = numel(dataset2);
x1 = 1:n1;
x2 = linspace(1,n1,n2);
dataset2_interp = interp1(x2,dataset2,x1);
subplot(2,1,1)
hold on
plot(dataset1,'.-b')
plot(dataset2,'.-r')
legend
title('Original')
subplot(2,1,2)
hold on
plot(dataset1,'.-b')
plot(dataset2_interp,'.-r')
legend
title('Dataset 2 Expanded')
0 Kommentare
William Rose
am 28 Feb. 2023
You can do it, but that does not mean you should do it.
On what basis do you justify the pairing of the samples from the long vector with the interoplated samples form the short vector?
As for generating an equal number of samples (but I don;t recommend doing it unless you have a good justification):
Let's call the vectors y1 (long) and y2 (short). Illustrate with example vectors that are 1000 times shorter than yours:
y1=rand(1,102); y2=rand(1,42);
To interpolate y2 to be as long as y1, you need associated x values. Create a vector x2:
x2=1:length(y2);
Create a query vector, xq:
xq=linspace(1,length(y2),length(y1));
Interpolate:
y2int=interp1(x2,y2,xq);
disp([length(y1),length(y2int)])
y2int has the same length as y1. But this does not mean each value in y2 is paired with a certain element in y1.
3 Kommentare
William Rose
am 28 Feb. 2023
@Matthew, that sounds like a great project!
What did you record during the two trials from the same subject? Why do trial 1 and trial 2 differ in length by such a large amount?
With a paired test, you subtract each value from its corresponding paired value. This reduces a potential source of variance. By using a paried t test, you reduce the chance of making a type II error.
Here is an example of how a paired test could be justified and used in your case: You recorded one cycle of activity with controller 1 and 1 cycle with controller 2. You used 102 samples in one cycle in one case, and 42 samples for one cycle in the other case. (I don't know why you would have such a different number of samples, but let's assume you did):
y1=10*sin((1:102)/(2*pi*102))+rand(1,102);
y2=10*sin((1:42)/(2*pi*42))+rand(1,42)+.1;
% Compare y1 to y2 with unpaired t test:
[h,p]=ttest2(y1,y2);
if h==1, fprintf('Unpaired: Samples have diferent means, p=%.3f\n',p);
else fprintf('Unpaired: Sample means do not differ, p=%.3f\n',p); end
Interpolate y2 to have 102 values, and compare the vectors using a paired t-test:
y2int=interp1(1:42,y2,linspace(1,42,102));
[h,p]=ttest(y1,y2int);
if h==1, fprintf('Paired: Samples have diferent means, p=%.3f\n',p);
else fprintf('Paired: Sample means do not differ, p=%.3f\n',p); end
I ran the code above six times. The result was the same with both tests in two cases. In the other four cases, the first test got the wrong answer ("Sample means do not differ") while the paired test got the right answer ("Samples have different means"). These results support the idea that, when properly used, the paired test reduces the chance of making a type II error.
If you prefer to continue this discussion offline, click on the envelope icon at the top right of the pop-up window that appears when you click the WR circle by my comment.
William Rose
am 28 Feb. 2023
@Matthew, you mentioned "The only problem with this [ttest2()] is that it assumes that the data sets come from two independent samples." The paired t test (ttest()) also assumes independence of the individual samples from one another, and it assumes or makes use of the built-in pairing of the data. That second assumption might be quesitoned when the raw data is not paired point-by point, as in this case. Both the paired (ttest) and unpaired (ttest2) tests assume the samples are normally distributed with equal variance. If you don;t want to make assumptions about normality and equal variance, use the Wilcoxon rank-sum test, also known as the Mann-Whitney U test, without interpolation, on the unequal-size samples.
[p,h]=ranksum(y1,y2);
If you want to interpolate, and you want to do a paired test, without the assumptions of normality and equal variance that are inherent in a t test, do the sign test, which is the paired equivalent of the rank-sum test:
[p,h]=signrank(y1,y2int);
Image Analyst
am 28 Feb. 2023
help ttest2
set1 = rand(1, 100);
set2 = rand(1, 50); % Second set has a different number of observations.
[h,p] = ttest2(set1, set2)
2 Kommentare
William Rose
am 28 Feb. 2023
@Image Analyst is right (as always, it seems to me) that ttest2 is a good option. Which is why I used it in my example above. It is more conservative than a paired t test, in the sense that it does not make any assumptions about paired-ness.
Siehe auch
Kategorien
Mehr zu Inputs finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!