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

x3 is categorical variable (yes/no). The corner point (which is a part of the Intercept) becomes automatically 'no' for this variable. I want to change the corner point to 'yes'. How do I do that?

EDIT for future reader with same problem: Change the the level of x3


Some code

> 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
econmajorr
  • 291
  • 1
  • 4
  • 10
  • 3
    ahh, dont use `attach` ever. I know introductions to R use it but it creates problems. See the example: `attach(mtcars) ; lm(mpg ~ wt) ; mtcars$newvariable <- 1:2 ; lm(mpg ~ newvariable)` . As the `newvariable` is created after `attach`ing the dataset it cannot be found by `lm`. To get it to work you would need to attach the data again...but continually doing this will inevitably lead to conflict. – user20650 Feb 07 '16 at 14:24
  • 1
    @user20650 is my new hero. Thanks! – econmajorr Feb 07 '16 at 18:11

2 Answers2

2

You just need to reorder the factor levels

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

glm uses this ordering.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
CPhil
  • 917
  • 5
  • 11
  • hmm it didn't work. changes the level of the factor and checked it by looking at the output of: levels(x3). But my fit is still the same – econmajorr Feb 07 '16 at 10:32
  • Sorry - I tested it on my side and it worked. Could you post a reproducible example? – CPhil Feb 07 '16 at 10:52
  • This should work, perhaps, there is a dataframe... so `dat$x3 = factor(dat$x3, levels = c("yes","no")` – user20650 Feb 07 '16 at 11:15
  • I posted my code as a comment below because I couldn't post it all here. I hope I have enough so you can see what I'm trying to do here – econmajorr Feb 07 '16 at 13:10
  • I don't have your `dat` data frame, but I retested, and the solution works on my side. Sorry, I can't help further. Note that is question is a duplicate of [this one](http://stackoverflow.com/questions/3872070/how-to-force-r-to-use-a-specified-factor-level-as-reference-in-a-regression) – CPhil Feb 07 '16 at 13:49
0

If, and only if, x3 is just a bunch of 1's and 0's you could just switch the values.

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

Then just use x3_no in the multiple regression.

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

You may also want to update this function so you have less errors.

attach <- function(...){
cat("Don't attach your data")
}
mfidino
  • 3,030
  • 1
  • 9
  • 13