MATLAB: Hi, i want to run this for loop data, data1, data2 arrays and have the variables R2 and R2_adj to be saved for every iteration. Ive had a lot of trouble with this and any help would be appreciated.

MATLAB: Hi, i want to run this for loop data, data1, data2 arrays and have the variables R2 and R2_adj to be saved for every iteration. Ive had a lot of trouble with this and any help would be appreciated.

for loopregressionrsquared

mpg = importdata('carmpgdat.txt', 't', 1);
VOL = mpg.data(:, 1);
HP = mpg.data(:, 2);
MPG = mpg.data(:, 3);
SP = mpg.data(:, 4);
WT = mpg.data(:, 5);
GPM = 1./MPG;
const = ones(82,1);
data = [const VOL HP SP WT];
data1 = [const HP WT];
data2 = [const SP WT];
for data = [data data1 data2]
[b, bint, r, rint, stats] = regress(MPG, data);
y_hat = data * b;
e = MPG - y_hat;
e2 = e.*e;
sse = sum(e2);
v = MPG - mean(MPG);
v2 = v.*v;
sst = sum(v2);
R2 = 1-(sse/sst);
n = size(data,1)
p = size(data,2)-1
R2_adj = 1-((sse/(n-p)) / (sst/(n-1)));
end

Best Answer

  • Add a counter
    counter = 1
    for k = [data data1 data2]
    % k will take on values of every element in the vector [data data1 data2]
    % k will not take on values 1,2,3,4,5, etc. unless those are in the vector like that.
    R2(counter) = .....
    R2_adj(counter) = .....
    counter = counter + 1;
    end
  • Add a Comment

    Your email address will not be published. Required fields are marked *