2016-07-18 69 views
3

在R,擬合GLM後你可以得到含殘餘偏差和零偏差,它告訴你你的模型是如何好相比只有截距項的模型,對於例如模型摘要信息:如何在Matlab fitglm中獲得R的零值和殘差等值?

model <- glm(formula = am ~ mpg + qsec, data=mtcars, family=binomial) 

我們:

> summary(model) 
... 
    Null deviance: 43.2297 on 31 degrees of freedom 
Residual deviance: 7.5043 on 29 degrees of freedom 
AIC: 13.504 
... 

在Matlab中,當您使用fitglm返回GeneralizedLinearModel類,它具有含殘留偏差一個Deviance屬性的對象。但是,我找不到與無效偏差直接相關的任何內容。計算這個最簡單的方法是什麼?

例Matlab代碼:

load fisheriris.mat 
model = fitglm(meas(:, 1), ismember(species, {'setosa'}), 'Distribution', 'binomial') 

生產:

model = 


Generalized Linear regression model: 
    logit(y) ~ 1 + x1 
    Distribution = Binomial 

Estimated Coefficients: 
         Estimate    SE     tStat     pValue  
        _________________ _________________ _________________ ____________________ 

    (Intercept)  27.8285213954246  4.8275686220899  5.76450042948896 8.19000695766331e-09 
    x1    -5.17569812610148 0.893399843474784 -5.79326061438645 6.90328570107794e-09 


150 observations, 148 error degrees of freedom 
Dispersion: 1 
Chi^2-statistic vs. constant model: 119, p-value = 9.87e-28 

model.Deviance殘餘偏差:

>> model.Deviance 

ans = 

      71.8363992272217 

回答

0

如果到fitglm該呼叫與一個表和用於使用Wilkinson表示法指定迴歸,然後結果爲GeneralizedLinearModel對象model具有允許我們檢索用於擬合模型的表格,響應名稱和分佈的屬性。

null_deviance_model = model.fit(model.Variables, ... 
     [model.ResponseName, ' ~ 1'], 'Distribution', model.Distribution.Name); 

來自R空偏離由null_deviance_model.Deviance給出:

由於從R中的零偏差與截距裝配在模型的只是偏差,我們可以通過使用上述信息擬合null_deviance_model發現它。

我不確定這是否擴展到使用矩陣和向量作爲協變量/響應的迴歸。

2

我寫了一個GLM類Matlab的賦予完全相同的結果:

Generalized Linear Models in Matlab (same results as in R)

例如,樣本數據伽馬分佈的對數鏈路GLM給出了這樣的在R:

Call: 
glm(formula = MilesPerGallon ~ Horsepower + Acceleration + Cylinders, 
    family = Gamma(link = log), data = data) 

Deviance Residuals: 
     Min   1Q  Median   3Q  Max 
-0.116817 -0.075084 0.004179 0.060545 0.197108 

Coefficients: 
       Estimate Std. Error z value Pr(>|z|)  
(Intercept) 4.955205 0.509903 9.718 < 2e-16 *** 
Horsepower -0.017605 0.004352 -4.046 5.21e-05 *** 
Acceleration -0.026137 0.015540 -1.682 0.0926 . 
Cylinders  0.093277 0.054458 1.713 0.0867 . 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

(Dispersion parameter for Gamma family taken to be 0.0133) 

    Null deviance: 0.388832 on 10 degrees of freedom 
Residual deviance: 0.093288 on 7 degrees of freedom 
AIC: 64.05 

Number of Fisher Scoring iterations: 4 

Pearson MSE: 0.008783281 
Deviance MSE: 0.008480725 
McFadden R^2: 0.7600815 

使用該軟件包,此相同的估計在Matlab中給出以下結果:

:: convergence in 4 iterations 
------------------------------------------------------------------------------------------ 
    dependent: MilesPerGallon 
    independent: (Intercept),Horsepower,Acceleration,Cylinders 
------------------------------------------------------------------------------------------ 
    log(E[MilesPerGallon]) = ß1×(Intercept) + ß2×Horsepower + ß3×Acceleration + ß4×Cylinders 
------------------------------------------------------------------------------------------ 
distribution: GAMMA 
     link: LOG 
     weight: - 
     offset: - 
============================================================ 
    Variable Estimate  S.E. z-value Pr(>|z|) 
============================================================ 
    (Intercept)  4.955  0.510 9.708  0.00000 
    Horsepower  -0.018  0.004 -4.042  0.00005 
    Acceleration  -0.026  0.016 -1.680  0.09290 
    Cylinders  0.093  0.055 1.711  0.08706 
============================================================ 
    Residual deviance:  0.0933  Deviance MSE: 0.0085 
    Null deviance:   0.3888  Pearson MSE: 0.0088 
    Dispersion:   0.0133  Deviance IC: 0.1026 
    McFadden R^2:   0.7601  Residual df: 7.0000 
============================================================ 

所以大致相同的輸出。希望這可以幫助別人。

相關問題