更改广义线性模型中的角点

时间:2016-02-07 08:37:41

标签: r statistics linear-regression

fit <- glm(formula=y~x1+x2+x3, family = binomial)

x3是分类变量(是/否)。角点(拦截的一部分)自动变为“不”。对于这个变量。我想将角点更改为“是”&#39;。我该怎么做?

为具有相同问题的未来读者编辑:更改x3的级别

一些代码

> attach(dat)
> levels(x9)
[1] "ja"  "nej"
> x9 <-factor(x9, levels = c("nej","ja"))
> levels(x9)                       
[1] "nej" "ja"               ###Changing the level was succesfull
> summary(glm(y~.,family = binomial, data=dat))
Call:
glm(formula = y ~ ., family = binomial, data = dat)
Deviance Residuals: 
Min       1Q   Median       3Q      Max  
-3.2508   0.2410   0.4698   0.6234   1.5827  
Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  4.79255    1.42304   3.368 0.000758 ***
x1          -3.19187    2.31703  -1.378 0.168336    
x2           1.55657    2.70719   0.575 0.565308    
x3          -3.27159    1.08943  -3.003 0.002673 ** 
x4nej        0.51869    0.41696   1.244 0.213505    
x5nej       -1.51137    0.75315  -2.007 0.044776 *  
x6nej        0.18231    0.30013   0.607 0.543565    
x7           0.08706    0.08027   1.085 0.278120    
x8b         -0.71031    0.30084  -2.361 0.018220 *  
x9nej        0.92448    0.38464   2.403 0.016240 *    ###OPS: I want: x9ja here
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)

Null deviance: 396.49  on 425  degrees of freedom
Residual deviance: 342.66  on 416  degrees of freedom
AIC: 362.66
Number of Fisher Scoring iterations: 6

2 个答案:

答案 0 :(得分:2)

您只需要重新排序因子级别

x3 = factor(x3, levels = c("yes","no"))

glm使用此排序。

答案 1 :(得分:0)

当且仅当x3只是一堆1和0时,您才可以切换值。

x3 <- c(1,1,0,0) # old x3
x3_no <- 1 - x3

然后在多元回归中使用x3_no

dat$x3_no <- 1 - dat$x3
glm(y ~ your_linear_predictor, family = binomial)

您可能还想更新此功能,以减少错误。

attach <- function(...){
cat("Don't attach your data")
}