# Storing values from a for loop

3 ビュー (過去 30 日間)
Ben Hatrick 2022 年 1 月 4 日
コメント済み: Ben Hatrick 2022 年 1 月 4 日
Hello, Im currently using regression to analyse a large set of data. After calulating relevant residuals I have found the coefficiant of determination (called R2 in code below). I now want the experiment 1000 times and calculate the mean and standard deviation of the all the R2 values attained from random sampling. However, as a coding novice, I am struggling to save all the values of R2 found into an array to be analysed later. As it stands my code finds the standard deviation of each individaul R2 value which obviously gives a value of 0 as it is a singular data point. Please see current code below. Any Ideas?
B = 1000; % samples to take
for iter = 1:B
b_log = [R2];
N=size(gas_data,1);
idx = randperm(N);
idx_train = idx(1:floor(0.8*N)); % 0.8 => 80% of data for training
idx_test = idx(ceil(0.8*N):end);
gas_train_proxyCO = gas_data.PT08_S1_CO_(idx_train);
gas_test_proxyCO = gas_data.PT08_S1_CO_(idx_test);
gas_train_CO = gas_data.CO_GT_(idx_train);
gas_test_CO = gas_data.CO_GT_(idx_test);
x_diff = gas_test_proxyCO - mean(gas_test_proxyCO);
x_diff2 = x_diff .* x_diff;
Sxx = sum(x_diff2);
y_diff = gas_test_CO - mean(gas_test_CO);
y_diff2 = y_diff .* y_diff;
Syy = sum(y_diff2);
Sxy = sum(x_diff .* y_diff);
% now for correlation
r = Sxy / sqrt(Sxx*Syy);
% Compute beta's (b's)
b1 = Sxy / Sxx;
b0 = mean(gas_test_CO) - b1 * mean(gas_test_proxyCO);
% Predict:
gas_test_CO_hat = b0 + b1 * gas_test_proxyCO;
% Residuals
e = gas_test_CO - gas_test_CO_hat;
e2 = e.*e;
sse = sum(e2);
v = gas_test_CO - mean(gas_test_CO);
v2 = v.*v;
sst = sum(v2);
R2 = 1-(sse/sst)
a = std(R2)
b = mean(R2)
end

サインインしてコメントする。

### 回答 (1 件)

Jan 2022 年 1 月 4 日

B = 1000;
R2 = zeros(1, B); % Pre-allocation
for iter = 1:B
...
R2(iter) = 1 - sse / sst;
end
A problem is, that R2 is existing already, when the loop starts:
b_log = [R2];
The shown code does not clarify, what R2 is initially and overwriting it inside the loop might be a mistake.
By the way, [ ] is Matlab's operator for a concatenation. Concatenating R2 with nothing is a waste of time only.
Are you sure that this is wanted:
idx_train = idx(1:floor(0.8*N)); % 0.8 => 80% of data for training
idx_test = idx(ceil(0.8*N):end);
If N is a multiple of 10, floor(0.8*N) and ceil(0.8*N) are equal. Maybe you mean floor(0.8*N)+1 instead of ceil().
##### 2 件のコメント表示非表示 1 件の古いコメント
Ben Hatrick 2022 年 1 月 4 日
How do ithen find the mean/standard deviation of all the saved R2 values?

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by