如何保存从 fitlm MATLAB 获得的估计系数

how to save the estimated coefficients obtained from fitlm MATLAB

如标题所示,我在 Matlab 中使用 "fitlm",它工作得很好。当我 运行 代码时,估计的系数写成这样:

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

如何将这些数据保存到文件或 table?

谢谢

您可以通过从 fitlm 对象访问 Coefficients 字段并检索 Estimate 字段来获取系数:

下面是一个在 MATLAB 中使用 hald 数据集的示例:

>> 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

另一种方法是

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

这保留了带有变量名的“headers”。 table 中的索引像 pandas 数据框中的索引一样工作,所以如果你想挑选出可用变量的子集,你可以写

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

多个这样的 table 可以连接在一起显示。