matlab:矢量化和fitlm

时间:2017-01-25 14:49:34

标签: matlab vectorization

我有nlinfit的矢量化问题。 设A =(n,p)观测矩阵,t(1,p)为解释变量。 对于前

    t=[0 1 2 3 4 5 6 7]

    A=[3.12E-04 7.73E-04 3.58E-04 5.05E-04 4.02E-04 5.20E-04 1.84E-04 3.70E-04
    3.38E-04 3.34E-04 3.28E-04 4.98E-04 5.19E-04 5.05E-04 1.97E-04 2.88E-04
    1.09E-04 3.64E-04 1.82E-04 2.91E-04 1.82E-04 3.62E-04 4.65E-04 3.89E-04
    2.70E-04 3.37E-04 2.03E-04 1.70E-04 1.37E-04 2.08E-04 1.05E-04 2.45E-04
    3.70E-04 3.34E-04 2.63E-04 3.21E-04 2.52E-04 2.81E-04 6.25E+09 2.51E-04
    3.11E-04 3.68E-04 3.65E-04 2.71E-04 2.69E-04 1.49E-04 2.97E-04 4.70E-04
    5.48E-04 4.12E-04 5.55E-04 5.94E-04 6.10E-04 5.44E-04 5.67E-04 4.53E-04
    ....
     ]

我想估算A的每一行的线性模型而不循环并避免循环

for i=1:7
    ml[i]=fitlm(A(i,:),t);
end

感谢您的帮助!

吕克

2 个答案:

答案 0 :(得分:0)

我相信你的探索是关于如何适应fitlm的工作,对于矩阵:

让我们使用matlab的hald示例:

>> load hald
>> Description

Description =

== Portland Cement Data ==                                

Multiple regression data                                  

ingredients (%):                                          
column1: 3CaO.Al2O3 (tricalcium aluminate)                
column2: 3CaO.SiO2 (tricalcium silicate)                  
column3: 4CaO.Al2O3.Fe2O3 (tetracalcium aluminoferrite)   
column4: 2CaO.SiO2 (beta-dicalcium silicate)              

heat (cal/gm):                                            
heat of hardening after 180 days                          

Source:                                                   
Woods,H., H. Steinour, H. Starke,                         
"Effect of Composition of Portland Cement on Heat Evolved 
during Hardening," Industrial and Engineering Chemistry,  
v.24 no.11 (1932), pp.1207-1214.                          

Reference:                                                
Hald,A., Statistical Theory with Engineering Applications,
Wiley, 1960.                                              

>> ingredients

ingredients =

     7    26     6    60
     1    29    15    52
    11    56     8    20
    11    31     8    47
     7    52     6    33
    11    55     9    22
     3    71    17     6
     1    31    22    44
     2    54    18    22
    21    47     4    26
     1    40    23    34
    11    66     9    12
    10    68     8    12

>> heat

heat =

   78.5000
   74.3000
  104.3000
   87.6000
   95.9000
  109.2000
  102.7000
   72.5000
   93.1000
  115.9000
   83.8000
  113.3000
  109.4000

这意味着您在组件中具有基质成分列%成分

>> sum(ingredients(1,:))

ans =

    99 % so it is near 100%

并且行是产品和热矢量的13个度量,观察时的热量。

>> mdl = fitlm(ingredients,heat)

mdl = 


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


Number of observations: 13, Error degrees of freedom: 8
Root Mean Squared Error: 2.45
R-squared: 0.982,  Adjusted R-Squared 0.974
F-statistic vs. constant model: 111, p-value = 4.76e-07

所以在你的情况下,分别测量每个观察是没有意义的。只是与观察数量相同的元素。 看看here

mdl = fitllm(A,t)

答案 1 :(得分:0)

使用sapply和findgroups解决了问题!