2014-07-08 49 views
0

如何打印所有因子水平的glm系数,包括参考水平? 摘要(glm_obj)仅打印偏离参考值的值。在R中打印所有glm系数

我知道那些是0的,但我需要这样的整合,即告诉其他系统什么因素可以发生。

对不起,如果它太简单,找不到任何地方。

由于

为了说明问题,我面对:

> glm(Petal.Width~Species,data=iris) 

Call: glm(formula = Petal.Width ~ Species, data = iris) 

Coefficients: 
      (Intercept) Speciesversicolor Speciesvirginica 
       0.246    1.080    1.780 

Degrees of Freedom: 149 Total (i.e. Null); 147 Residual 
Null Deviance:  86.57 
Residual Deviance: 6.157 AIC: -45.29` 

以上的产率的模型的描述仅系数云芝和锦葵,这是,因为达诚所指出的,从点精绝模型本身的视图。然而,我需要与另一个应用程序共享该模型,该应用程序必须知道物种的期望水平(例如,一旦出现新的,未研究的水平,就发出警告)。

摘要()给出了相同的结果:

> summary(glm(Petal.Width~Species,data=iris)) 

Call: 
glm(formula = Petal.Width ~ Species, data = iris) 

Deviance Residuals: 
    Min  1Q Median  3Q  Max 
-0.626 -0.126 -0.026 0.154 0.474 

Coefficients: 
        Estimate Std. Error t value Pr(>|t|)  
(Intercept)  0.24600 0.02894 8.50 1.96e-14 *** 
Speciesversicolor 1.08000 0.04093 26.39 < 2e-16 *** 
Speciesvirginica 1.78000 0.04093 43.49 < 2e-16 *** 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

(Dispersion parameter for gaussian family taken to be 0.04188163) 

Null deviance: 86.5699 on 149 degrees of freedom 
Residual deviance: 6.1566 on 147 degrees of freedom 
AIC: -45.285 

Number of Fisher Scoring iterations: 2 
+1

参考水平没有得到系数。他们没有被打印,因为他们不存在。 – Dason

+0

Sure Dason,但请阅读下面的问题:我仍然希望能够打印这些系数,因为我将模型发送到另一个应用程序,并且需要知道存在什么系数。 – coulminer

+0

请考虑包括一个* small * [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),这样我们可以更好地理解和更容易地回答你的问题。 –

回答

1

回答为我自己因为我认为这种方法比所提出的更适合于更好的目的。

共享预测模型并不是summary.glm方法应该做的事情,因此summary(模型)对模型应用的数据没有太多的说明。

虽然有一个解决方案 - use PMML,它允许描述模型和它应该应用到的数据。

例子:

> library(pmml) 
> pmml(glm(Petal.Width~Species,data=iris)) 
<PMML version="4.2" xmlns="http://www.dmg.org/PMML-4_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dmg.org/PMML-4_2 http://www.dmg.org/v4-2/pmml-4-2.xsd"> 
<Header copyright="Copyright (c) 2014 dmitrijsl" description="Generalized Linear  Regression Model"> 
    <Extension name="user" value="dmitrijsl" extender="Rattle/PMML"/> 
    <Application name="Rattle/PMML" version="1.4"/> 
    <Timestamp>2014-07-15 15:07:51</Timestamp> 
</Header> 
<DataDictionary numberOfFields="2"> 
    <DataField name="Petal.Width" optype="continuous" dataType="double"/> 
    <DataField name="Species" optype="categorical" dataType="string"> 
    <Value value="setosa"/> 
    <Value value="versicolor"/> 
    <Value value="virginica"/> 
    </DataField> 
... 

现在还Setosa是列表中用于接收机系统知道会发生什么,该模型描述有:

... 
<ParameterList> 
<Parameter name="p0" label="(Intercept)"/> 
<Parameter name="p1" label="Speciesversicolor"/> 
<Parameter name="p2" label="Speciesvirginica"/> 
</ParameterList> 
<FactorList> 
<Predictor name="Species"/> 
</FactorList> 
<CovariateList/> 
<PPMatrix> 
<PPCell value="versicolor" predictorName="Species" parameterName="p1"/> 
<PPCell value="virginica" predictorName="Species" parameterName="p2"/> 
</PPMatrix> 
<ParamMatrix> 
<PCell parameterName="p0" df="1" beta="0.245999999999997"/> 
<PCell parameterName="p1" df="1" beta="1.08"/> 
<PCell parameterName="p2" df="1" beta="1.78"/> 
</ParamMatrix> 

2

你可以重新写summary.glm方法。您可以在控制台中输入summary.glm来查看源代码,也可以先使用sink将源代码转储到文件中。大多数显示方法都是用R编写的,因此您应该能够浏览代码并在必要时添加一行。

或者,您可以为参考级别定义一个额外的虚拟变量并将其添加到模型中。 R会给你一个警告,并将系数设置为NA。例如:

# no coefficient for the reference level 
l = lm(Sepal.Width~Species,iris) 

# make a dummy for the reference level 
iris$Speciessetosa = as.numeric(iris$Species == "setosa") 

# you get NA for the coefficient on new dummy 
l = lm(Sepal.Width~Species+Speciessetosa,iris) 

不幸的是,你不能只是设置l$coefficients[4] = 0,因为它不会在打印方法显示出来。这个不起作用的原因在源代码中很明显,我推荐通过它浏览。

如果你真的需要0而不是NA,您可以通过sed运行输出问题改变NA0就行了,甚至输出保存为的R character载体,并使用内置gsub功能,或者如果你只有这些中的几个,sink输出到文件并使用R编辑器中的查找和替换功能或像Word或Sublime等编辑器手动更改它。

0

所以我意识到这个问题是相当古老的,但一个简单的解决方案是使用dummy.coef功能

fit<-glm(Petal.Width~Species,data=iris) 
summary(fit) 
dummy.coef(fit) 

> dummy.coef(fit) 
Full coefficients are 

(Intercept):  0.246      
Species:  setosa versicolor virginica 
        0.00  1.08  1.78 

我希望这有助于!