Elin
Elin

Reputation: 61

how to save the estimated coefficients obtained from fitlm MATLAB

As the title shows, I am using "fitlm" in Matlab and it works perfectly fine. When I run the code, the estimated coefficients are written out like:

mdl1 = 


Linear regression model:
    y ~ 1 + x1

Estimated Coefficients:
                   Estimate       SE        tStat     pValue
                   ________    _________    ______    ______

    (Intercept)    2.1999      0.0043976    500.25    0     
    x1             591.68        0.31096    1902.8    0     


Number of observations: 24800, Error degrees of freedom: 24798
Root Mean Squared Error: 0.693
R-squared: 0.993,  Adjusted R-Squared 0.993
F-statistic vs. constant model: 3.62e+06, p-value = 0

How can I save these data to a file or a table?

Thanks

Upvotes: 2

Views: 6826

Answers (2)

Superpronker
Superpronker

Reputation: 329

An alternative method is

lm.Coefficients({'x1'}, 'Estimate')

This retains the "headers" with the variable names. The index in the table works like one in a pandas dataframe, so if you want to pick out a subset of available variables, you write

lm.Coefficients({'x1', 'x4'}, {'Estimate', 'SE'})

Multiple of such tables can be concatenated to be shown together.

Upvotes: 0

rayryeng
rayryeng

Reputation: 104545

You can get the coefficients by accessing the Coefficients field from your fitlm object and retrieving the Estimate field:

Here's an example using the hald dataset in MATLAB:

>> load hald;
>> lm = fitlm(ingredients,heat)

lm = 


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

Estimated Coefficients:
                   Estimate      SE        tStat       pValue 
                   ________    _______    ________    ________

    (Intercept)      62.405     70.071      0.8906     0.39913
    x1               1.5511    0.74477      2.0827    0.070822
    x2              0.51017    0.72379     0.70486      0.5009
    x3              0.10191    0.75471     0.13503     0.89592
    x4             -0.14406    0.70905    -0.20317     0.84407

>> lm.Coefficients.Estimate

ans =

   62.4054
    1.5511
    0.5102
    0.1019
   -0.1441

Upvotes: 3

Related Questions