Gaussian Psychometric function - Prepare data

10 views (last 30 days)
TS
TS on 13 Jan 2021
Answered: Anurag Ojha on 3 Oct 2024
Good morning,
sorry I am new to Matlab and stuck on thins problem for weeks already. Probably t is a basic question and I would be so happy if someone could help.
I am trying to fit a psychometric gaussian function on my data (have 30 different data sets). The important table columns basically look like this:
z1 z2 z3 z4
___ __ __ __
-1 0 0 1
0 2 2 0
1 0 2 1
2 0 2 1
3 2 0 1
4 2 0 1
5 0 2 1
6 4 0 0
7 3 4 0
So z1 is just the number of trials, z2 is a condition (ranging from 1-5) and z3 is another sencond condition (0, 2, 4). z4 is the reaction itself (so 0 and 1).
To fit the curve I thought that I use the following:
[m,s] = normfit(Z4);
y = normpdf(Z4,m,s);
plot(Z4,y,'.');
However, how can I include the other two conditions so I have trials ranging from 1-5 and 0,2,4. I would have to calculate the mean and standard deviation only for these trials? Or is the overall function incorrect?
So thankful!!

Answers (2)

Hari
Hari on 22 Apr 2024
Hi TS,
I understand that you are looking to fit a Gaussian psychometric function to your dataset, which contains 30 different data sets. Further, you want to incorporate two conditions into your analysis for fitting the curve.
I assume, you have the Statistical and Machine Learning Toolbox installed, as it is necessary for some of the functions like "normfit" and "normpdf". You can follow the below steps:
  • Understanding the Data Structure: Your data consists of trials (z1), two conditions (z2 and z3), and reactions (z4). To incorporate conditions into your analysis, you need to segment your data based on these conditions before fitting the Gaussian model.
  • Segmenting the Data Based on Conditions: You can use logical indexing to segment your data based on the conditions. For example, to select data where z2 is 1 and z3 is 0, you would use:
idx = (data.z2 == 1) & (data.z3 == 0);
selectedData = data.z4(idx);
  • Fitting the Gaussian Model: After segmenting the data based on conditions, you can fit the Gaussian model to "selectedData". This involves calculating the mean and standard deviation for the segmented data and then using these parameters to generate a probability density function.
[m, s] = normfit(selectedData);
y = normpdf(selectedData, m, s);
plot(selectedData, y, '.');
  • Iterating Over All Conditions: To include all conditions, you should repeat the segmentation and fitting process for each unique combination of z2 and z3 conditions. This might involve looping through the unique values of z2 and z3, segmenting the data accordingly, and fitting the model each time.
Refer to the documentation of "normfit" for understanding how to fit a normal distribution to your data: https://www.mathworks.com/help/stats/normfit.html
For generating the probability density function, see the "normpdf" function documentation: https://www.mathworks.com/help/stats/normpdf.html
To learn more about logical indexing in MATLAB, which is useful for segmenting your data, visit:
Hope this helps!

Anurag Ojha
Anurag Ojha on 3 Oct 2024
Hey
To fit a psychometric Gaussian function to your data while considering the additional conditions (z2 and z3), your approach needs to account for the influence of these conditions on the data, as they are likely influencing the response z4. Simply applying a normal distribution (normpdf) to z4 without considering these factors may not capture the relationship you're looking for.
You can refer to following code I have written and make adjustment as per your use case:
% Example data as given
Z = [-1, 0, 0, 1;
0, 2, 2, 0;
1, 0, 2, 1;
2, 0, 2, 1;
3, 2, 0, 1;
4, 2, 0, 1;
5, 0, 2, 1;
6, 4, 0, 0;
7, 3, 4, 0];
% Extract z2 and z3 (conditions) and z4 (responses)
z2 = Z(:, 2);
z3 = Z(:, 3);
z4 = Z(:, 4);
% Unique values of z2 and z3 for grouping
unique_z2 = unique(z2);
unique_z3 = unique(z3);
% Plot the Gaussian fit for each combination of z2 and z3
figure;
hold on; % Hold to plot all conditions on the same graph
% Color map for better distinction of different curves
colors = lines(length(unique_z2) * length(unique_z3));
color_idx = 1;
% Loop through each combination of z2 and z3
for i = 1:length(unique_z2)
for j = 1:length(unique_z3)
% Find the rows that match the current z2 and z3 values
condition_idx = (z2 == unique_z2(i)) & (z3 == unique_z3(j));
Z4_condition = z4(condition_idx); % Extract the z4 values for this condition
% Check if there are any data points for this condition
if ~isempty(Z4_condition)
% Fit a Gaussian distribution to z4 values
[m, s] = normfit(Z4_condition); % Estimate mean and standard deviation
% Generate x values for plotting
x_values = min(Z4_condition):0.1:max(Z4_condition);
% Get Gaussian PDF for these x values
y_values = normpdf(x_values, m, s);
% Plot the results
plot(x_values, y_values, 'Color', colors(color_idx,:), 'DisplayName', ...
['z2 = ', num2str(unique_z2(i)), ', z3 = ', num2str(unique_z3(j))], 'LineWidth', 1.5);
% Plot the actual data points as well for reference
plot(Z4_condition, zeros(size(Z4_condition)), 'o', 'MarkerFaceColor', colors(color_idx,:), 'MarkerEdgeColor', 'k');
% Debugging: Print the data used for fitting
disp(['Condition z2 = ', num2str(unique_z2(i)), ', z3 = ', num2str(unique_z3(j))]);
disp(['Mean: ', num2str(m), ', Std Dev: ', num2str(s)]);
disp(['Z4 values: ', num2str(Z4_condition')]);
color_idx = color_idx + 1; % Move to the next color for the next plot
end
end
end
Condition z2 = 0, z3 = 0
Mean: 1, Std Dev: 0
Z4 values: 1
Condition z2 = 0, z3 = 2
Mean: 1, Std Dev: 0
Z4 values: 1 1 1
Condition z2 = 2, z3 = 0
Mean: 1, Std Dev: 0
Z4 values: 1 1
Condition z2 = 2, z3 = 2
Mean: 0, Std Dev: 0
Z4 values: 0
Condition z2 = 3, z3 = 4
Mean: 0, Std Dev: 0
Z4 values: 0
Condition z2 = 4, z3 = 0
Mean: 0, Std Dev: 0
Z4 values: 0
% Add plot labels and legend
xlabel('Z4 (Response)');
ylabel('Probability Density');
title('Gaussian Fit for Different Conditions (z2, z3)');
legend show; % Show the legend with conditions
hold off;

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!