storing values in for loop

3 ビュー (過去 30 日間)
Ben Hatrick
Ben Hatrick 2022 年 1 月 4 日
編集済み: Tyler F 2022 年 1 月 4 日
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

回答 (1 件)

Tyler F
Tyler F 2022 年 1 月 4 日
編集済み: Tyler F 2022 年 1 月 4 日
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.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by