Các hệ số ước tính sẽ cùng chủ đề với điều kiện bạn tạo các biến giả (tức là các số) phù hợp với R. Ví dụ: cho phép 'tạo dữ liệu giả và phù hợp với hệ số sử dụng Poisson glm. Lưu ý rằng glchức năng tạo ra một biến yếu tố.
> counts <- c(18,17,15,20,10,20,25,13,12)
> outcome <- gl(3,1,9)
> outcome
[1] 1 2 3 1 2 3 1 2 3
Levels: 1 2 3
> class(outcome)
[1] "factor"
> glm.1<- glm(counts ~ outcome, family = poisson())
> summary(glm.1)
Call:
glm(formula = counts ~ outcome, family = poisson())
Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-0.9666  -0.6713  -0.1696   0.8471   1.0494  
Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   3.0445     0.1260  24.165   <2e-16 ***
outcome2     -0.4543     0.2022  -2.247   0.0246 *  
outcome3     -0.2930     0.1927  -1.520   0.1285    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
    Null deviance: 10.5814  on 8  degrees of freedom
Residual deviance:  5.1291  on 6  degrees of freedom
AIC: 52.761
Number of Fisher Scoring iterations: 4
Vì kết quả có ba cấp độ, tôi tạo hai biến giả (dummy.1 = 0 if result = 2 và dummy.2 = 1 if result = 3) và tái sử dụng các giá trị số này:
> dummy.1=rep(0,9)
> dummy.2=rep(0,9)
> dummy.1[outcome==2]=1
> dummy.2[outcome==3]=1
> glm.2<- glm(counts ~ dummy.1+dummy.2, family = poisson())
> summary(glm.2)
Call:
glm(formula = counts ~ dummy.1 + dummy.2, family = poisson())
Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-0.9666  -0.6713  -0.1696   0.8471   1.0494  
Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   3.0445     0.1260  24.165   <2e-16 ***
dummy.1      -0.4543     0.2022  -2.247   0.0246 *  
dummy.2      -0.2930     0.1927  -1.520   0.1285    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
    Null deviance: 10.5814  on 8  degrees of freedom
Residual deviance:  5.1291  on 6  degrees of freedom
AIC: 52.761
Number of Fisher Scoring iterations: 4
Như bạn có thể thấy các hệ số ước tính là như nhau. Nhưng bạn cần cẩn thận khi tạo các biến giả nếu bạn muốn có kết quả tương tự. Ví dụ: nếu tôi tạo hai biến giả là (dummy.1 = 0 if result = 1 và dummy.2 = 1 if result = 2) thì kết quả ước tính sẽ khác nhau như sau:
> dummy.1=rep(0,9)
> dummy.2=rep(0,9)
> dummy.1[outcome==1]=1
> dummy.2[outcome==2]=1
> glm.3<- glm(counts ~ dummy.1+dummy.2, family = poisson())
> summary(glm.3)
Call:
glm(formula = counts ~ dummy.1 + dummy.2, family = poisson())
Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-0.9666  -0.6713  -0.1696   0.8471   1.0494  
Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   2.7515     0.1459   18.86   <2e-16 ***
dummy.1       0.2930     0.1927    1.52    0.128    
dummy.2      -0.1613     0.2151   -0.75    0.453    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
    Null deviance: 10.5814  on 8  degrees of freedom
Residual deviance:  5.1291  on 6  degrees of freedom
AIC: 52.761
Number of Fisher Scoring iterations: 4
Điều này là do khi bạn thêm outcomebiến trong glm.1, R theo mặc định sẽ tạo hai biến giả là outcome2và outcome3định nghĩa chúng tương tự dummy.1và dummy.2trong glm.2, tức là mức kết quả đầu tiên là khi tất cả các biến giả ( outcome2và outcome3) khác được đặt thành số không.