I'm trying to run a linear regression on cases that fit certain criteria, can you help?

1 Ansicht (letzte 30 Tage)
I have a matrix I downloaded from excel:
clear all
x = xlsread('excelspreadsheet.xlsx');
z = xlsread('BR5_BR4.xlsx', 'A:A');
x =
1.35 0.006051407 -0.001325416 -0.000295 -6.20E-05
1.13 -0.002442044 -7.60E-06 -4.50E-05 3.40E-05
1.04 0.005803107 -0.000637415 0.000394 0.000723
1.01 0.005687193 -0.000856177 0.000141 0.000628
0.86 -0.003891923 -0.000814317 0.001307 0.001105
0.82 -0.001361756 -0.000658948 -0.001778 -0.001301
0.73 -0.002613974 -0.000814317 0.001307 0.001383
0.68 -0.003173712 0.000683247 1.40E-05 0.000796
0.60 0.00154659 -0.000254445 2.80E-05 0.00033
I want go through the matrix and select rows where certain columns have values > or < 0. Then I want to run a regression on the rows that meet these terms. This is what I came up with, but it isn't working. Can anyone help?
if x( x(:,2)>0 & x(:,3)<0 & x(:,4)<0 & x(:,5)<0 , :)
md1 = lm(x, z);
end

Akzeptierte Antwort

Brendan Hamm
Brendan Hamm am 8 Nov. 2016
The if statement must have a scalar logical statement next to it (either true or false) but what you have is a numeric matrix.
If what you are trying to do is extract only rows where x(:,2) > 0 and x(:,3) < 0 and x(:,4) < 0 and x(:,5) < 0 for the regression, but it may be the case that this does not have any data, I would suggest.
rows = x(:,2)>0 & x(:,3)<0 & x(:,4)<0 & x(:,5)<0; % Logical vector with the rows which satisfy all conditions.
if any(rows) % True if there is at least 1 row which meats the condition.
mdl = fitlm(x(rows,:),z(rows)); % Fit with the rows which satisfy condition.
end
Really you wouldn't want only one row to satisfy the condition as you would require that x(rows,:) is an invertible matrix.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by