How to calculate standard error of mean and weighted average?

51 次查看(过去 30 天)
I can't seem to get the standard error of mean or weighted average code to work for the given arrays
for ii = 1:n_col
sigma_f_bar(ii) = SEM(f(n_col),n);
end
[f_wav,sigma_f_wav] = weighted_average(f);
function sigma_x_bar = SEM(x,n)
sigma_x_bar = std(x)/sqrt(n);
end
function [x_wav,sigma_wav] = weighted_average(mat)
w = 1/(mean(std(mat))^2);
x_wav = sum(w.*mat)/sum(w);
sigma_wav = 1/sqrt(sum(w));
end

回答(1 个)

Rupesh
Rupesh 2024-2-15
编辑:Rupesh 2024-2-15
Hi Ananya
After reviewing code, I realised that there are 2 main issues which are causing the errors, one is loop iteration based upon assumption that you are dealing with matrices which is subset of array and other one is weighted average function.Also this will give you better idea about calculation in multiple dimensions.
  • In the loop where you try to calculate "sigma_f_bar", you are passing f(n_col) to the "SEM" function, which suggests that you are trying to pass the entire n_col-th column of f. However, the syntax f(n_col) might not be correct if f is a matrix; it should be f(:, n_col) in case of matrix to select the entire column.
  • In The "weighted_average" function, the issue is that the code to handle matrices might seem to have some issue. The w variable is calculated as a scalar, but it seems like you want to calculate a weight for each column of mat.
Below is modified version of code which gives the desired output .
% Standard Error of the Mean function
function sigma_x_bar = SEM(x, n)
sigma_x_bar = std(x) / sqrt(n);
end
% Weighted Average function
function [x_wav, sigma_wav] = weighted_average(mat)
n = size(mat, 1); % Number of observations
weights = 1 ./ (std(mat).^2); % Weights for each dataset (column)
x_wav = sum(bsxfun(@times, weights, mat)) / sum(weights); % Weighted average for each dataset
sigma_wav = 1 / sqrt(sum(weights)); % Standard error for the weighted average
end
You can see above that how i modified the "weighted_average function" and made sure that whatever mathematical operation are involved are done by taking into consideration about Matrix.
% Example matrix f with 5 observations and 3 datasets (columns)
f = rand(5, 3);
% Number of observations (rows) and datasets (columns) in f
n = size(f, 1); % Number of observations
n_col = size(f, 2); % Number of datasets
% Preallocate the array for standard error of the mean for each dataset
sigma_f_bar = zeros(1, n_col);
% Calculate the standard error of the mean for each dataset
for ii = 1:n_col
sigma_f_bar(ii) = SEM(f(:, ii), n);
end
% Calculate the weighted average and its standard error for the datasets
[f_wav, sigma_f_wav] = weighted_average(f);
% Display the results
disp('Standard Error of the Mean for each dataset:');
disp(sigma_f_bar);
disp('Weighted average of the datasets:');
disp(f_wav);
disp('Standard error of the weighted average:');
disp(sigma_f_wav);
Below is the output you can expect after execution of above script.
Standard Error of the Mean for each dataset:
0.1317 0.1050 0.1136
Weighted average of the datasets:
0.4588 1.3673 1.1296
Standard error of the weighted average:
0.1488
You can refer to following documents for more information regarding Matrix Operations for better understanding.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by