Question

I'm using lm = fitlm(X,y,'linear') it works nicely and outputs

lm = 


Linear regression model:
    y ~ 1 + x1 + x2 + x3

Estimated Coefficients:
               Estimate    SE           tStat      pValue    
    (Intercept)      2.1338      0.27403     7.7869    1.6357e-13
    x1              0.07202      0.01757     4.0991    5.5484e-05
    x2             -0.35927      0.12078    -2.9746     0.0032094
    x3             0.020363    0.0041479     4.9092    1.6168e-06


Number of observations: 264, Error degrees of freedom: 260
Root Mean Squared Error: 0.835
R-squared: 0.154,  Adjusted R-Squared 0.144
F-statistic vs. constant model: 15.8, p-value = 1.93e-09

somethings like this. However i want to get the F-statistic value of each model (in a loop) and export to a file. My problem is.. I can't find the variable of lm that contains F-statistic and it's pvalue; Also lm.Steps is empty..

================================================================================ And another question, under what condition should I use this regression results? if x1 is the component i want after regressing out other component and residuals, should I say x1 is responsible for y when p-value of x1 is less than 0.05 or p-value of the model is less than 0.05?

Was it helpful?

Solution

As suggested in the documentation for fitlm you can use the anova function on your model. Then extract the values (these will be for all x values) and save by whatever method you prefer:

tbl = anova(lm);

% something like this for just your desired values
A = [double(tbl.F),double(tbl.pValue)];
csvwrite('output.csv',A);

% or this dumps the entire result of anova to file
tbl2 = dataset2table(tbl);
writetable(tbl2, 'output.csv');

Okay for the other variant - if you want to use multiple inputs of X then you can use the summary option on anova and extract the F and p values from that:

X = cell array of inputs of length n;

F = zeros(n,1);
p = zeros(n,1);

for m = 1:n;
    lm = fitlm(X{n},y,'linear')
    tbl = anova(lm,'summary');

    % you may want to check these indices but should be the right points:

    F(n) = double(tbl(2,4));
    p(n) = double(tbl(2,5)); 
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top