storing values in for loop

4 views (last 30 days)
Ben Hatrick
Ben Hatrick on 4 Jan 2022
Edited: Tyler F on 4 Jan 2022
I am currently working on a regression project using a subsampling method and am finding R^2 values for a particular data set. However, when I try to calculate the mean and the standard deviation for all the R62 values produed (over 1000 iterations) I get values from using only the last value. i.e std = o and mean = R^2 value from the 1000th iteration. As it stands my code is as below. Any help would be gretly appreciated.
B=1000;
for i = 1:B
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;
%Find Coefficiant of Determination
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

Answers (1)

Tyler F
Tyler F on 4 Jan 2022
Edited: Tyler F on 4 Jan 2022
You arent storing the value for each loop. Every time it loops the values for a and b overwrite. I cant run your code without your data but modifying the last lines to be:
a(i)=std(R2)
b(i)= mean(R2)
would put them into a vector.

Categories

Find more on Creating and Concatenating Matrices 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!