【问题标题】:how to save the estimated coefficients obtained from fitlm MATLAB如何保存从 fitlm MATLAB 获得的估计系数
【发布时间】:2016-01-18 18:57:21
【问题描述】:

正如标题所示,我在 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

如何将这些数据保存到文件或表格中?

谢谢

【问题讨论】:

    标签: matlab save linear-regression


    【解决方案1】:

    您可以通过访问 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
    

    【讨论】:

      【解决方案2】:

      另一种方法是

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

      这会保留带有变量名称的“标题”。表中的索引与 pandas 数据框中的索引类似,因此如果您想选择可用变量的子集,请编写

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

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

      【讨论】:

        猜你喜欢
        • 2014-06-15
        • 2018-07-15
        • 1970-01-01
        • 1970-01-01
        • 2016-07-14
        • 2013-04-12
        • 1970-01-01
        • 1970-01-01
        • 2012-01-29
        相关资源
        最近更新 更多