Permutations function

3 views (last 30 days)
Jenny
Jenny on 1 Mar 2011
Dear community, I am writing to you because I need some help with programming in Matlab. I have to use Matlab for some statistical analysis but I haven't got much experience so far. I was wondering whether one of you could offer me some advice. I used a program to give me a matrix containing files of 30 different participants and their viewing duration of pictures for several trials (40 for each condition). Now I specified that the program should only include and average the times below 20000ms and above 10000ms. Numbers below or above should be labelled as "NaN". This is the script:
Matlab Code
% This program generates the time profile of the listening
% locked to the decision.
% Generate listening profile
Like_Decision = nan(20000,40,30);
Dislike_Decision = nan(20000,40,30);
for j=1:30
temp1 = dat(j).dat1;
temp2 = dat(j).dat2;
index1 = 0;
index2 = 0;
for i=[3:42 45:84],
overallTime = sum(temp2{i}(2:2:length(temp2{i})));
if overallTime > 300 && overallTime < 20000,
if temp1(i,4)==1
index1 = index1 + 1;
choice = temp1(i,6)-48;
Like_Decision(:,index1,j) = sequence1(temp2{i},choice);
else
index2 = index2 + 1;
choice = temp1(i,6)-48;
Dislike_Decision(:,index2,j) = sequence1(temp2{i},choice);
end
end
end
disp(['C1: ' num2str(index1) ', C2: ' num2str(index2)]);
end
end
function time = sequence1(rt,choice)
% This program will convert the rt sequence
% to a sequenece of 1s and 0s where
% 1 = choice
% 0 = non-choice
% Additionally we will also clip any rt to 20000 ms
time=[];
for i = 1:2:length(rt)-1
if rt(i) == choice
time = cat(1,time,ones(rt(i+1),1));
else
time = [time;zeros(rt(i+1),1)];
end
end
if length(time)>20000
time = time(end-20000+1:end);
else
time=cat(1,NaN(20000-length(time),1),time);
end
%time=downsample(time,5);
end
What I got to do now is to use that program in order to produce a permutation plot of these viewing times. So, the "for" loop should be repeated a 1000 times (for each condition: time1 (buy) or time2 (discard)) to get 1000 random histograms, then I was advised to use the sort(x) command to take off the extreme ends and produce a plot that shows the confidence intervals for all the simulated 1000 histograms.
Sigh, sorry it's quite an issue... Thank you very much and best wishes Chris
  3 Comments
Jan
Jan on 1 Mar 2011
Do you have a question?
Walter Roberson
Walter Roberson on 1 Mar 2011
I cannot tell from your question which array you want to select random values from. The seeming most obvious would be time(), but considering the range of values you allow for it, duplicate times would seem unlikely so it then becomes unclear what you would be histograming.

Sign in to comment.

Answers (2)

Matt Tearle
Matt Tearle on 2 Mar 2011
OK, still not sure if I quite get all the details (I think I'm getting mired in the terminology of samples vs trials vs participants etc), but I think I have enough to point you to randsample in Statistics Toolbox.
I think the simplest way of achieving what you want would be something along these lines:
y = rand(10,7);
col = size(y,2);
nboot = 500;
yboot = zeros(nboot,col);
for k=1:col
yboot(:,k) = randsample(y(:,k),nboot,true);
end
mean(y)
mean(yboot)
You could then perhaps use the distribution fitting functions to fit the data.
[mu,sigma,muci,sigmaci] = normfit(yboot)
  1 Comment
Jenny
Jenny on 5 Mar 2011
ok, thank you very much for your help, i really appreciate it! I'll show this to my supervisor and see from there. Jenny

Sign in to comment.


Matt Tearle
Matt Tearle on 2 Mar 2011
Mostly going to echo the comments made by The Usual Suspects. In particular, can you explain what you're trying to histogram, what you're randomly selecting from, and what the variables are (size, type, etc).
In the meantime, a couple of comments:
  1. it sounds like you're trying to do some kind of bootstrapping. Given that you're doing statistics, I assume you have Statistics Toolbox. In which case, you can look at bootstrp and bootci.
  2. it's hard to tell from the code (unformatted, and we don't have access to the data to try it), but it looks suspiciously like you could replace a lot of code with logical operations. In particular, the first part of sequence1 seems to do:
t = double(rt==choice);
  3 Comments
Matt Tearle
Matt Tearle on 2 Mar 2011
A little, but I'm still not fully clear. I see two variables, (like and dislike), each being 20000-by-40-by-30. But then you want to average over the first dimension (or at least a portion of it). So then you'd have two matrices, both 40-by-30, right? Then you'd like to sample the rows with replacement to bootstrap up to 1000-by-30? Then... something. I don't quite understand, at this point. If these were two vectors of 1000 points, I'd guess that you're trying to compare the two distributions for a significant difference.
Jenny
Jenny on 2 Mar 2011
Yes it's right until the bootstrap up to 1000 by 30 bit.
Then, for each variable independently, I would like to create a pseudo-distribution with the help of the 1000 by 30 randomised trials. This pseudo-distribution would be my null-hypothesis. If i then assume my null hypothesis as represented by the pseudo distribution was true I am interested what differences are there compared to the distribution of my original sample (the original 40 trials for my 30 participants). I am not interested in differences between those variables. I would just like to compare each against a hypothesized random distribution as represented by the 1000 simulated random samples (each has 40 trials and 30 participants as if I had done the experiment a 1000 times). I can see the differences very nicely if I plot all the 1000 samples into one plot with my original sample and then highlight the area of the 95% confidence interval. So each of the 1000 samples represents one point in my final plot. Adding all 1000 together I get basically one line made up of all the 1000 trials.
Hope this helps...would be very grateful

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!