Question

I would like to run a logistic regression with specific group (range 0f values) of a categorial variable.I did the following steps: 1. I cut the variable to groups:

cut_Var3 <- cut(dat$Var3,breaks=c(0,3,6,9))

the outcome of table(cut_Var3) gave me this output (cut_Var3 was turned into a factor):

# (0,3] (3,6] (6,9] 
#    5     4     4 

I wanted to do a logistic regression with other variable but in separate for the level of (3,6) only. So I'll be able to run the regression on the 4 observations of the second group. 2. I tried to write this line of code (and also other variations):

ff <- glm( TargetVar ~ relevel(cut_Var3,3:6), data = dat)

but with no luck.

What should I do in order to run it properly?

attached is an example data set:

dat <- read.table(text = " TargetVar  Var1    Var2       Var3
 0        0        0         7
 0        0        1         1
 0        1        0         3
 0        1        1         7
 1        0        0         5
 1        0        1         1
 1        1        0         0
 1        1        1         6
 0        0        0         8
 0        0        1         5
 1        1        1         4
 0        0        1         2
 1        0        0         9
 1        1        1         2  ", header = TRUE)
Was it helpful?

Solution

For relevel you need to specify the level label exactly as it appear in the factor:

glm( TargetVar ~ relevel(cut_Var3,"(3,6]"), data = dat)

Call:  glm(formula = TargetVar ~ relevel(cut_Var3, "(3,6]"), data = dat)

Coefficients:
                    (Intercept)  relevel(cut_Var3, "(3,6]")(0,3]  
                           0.75                            -0.35  
relevel(cut_Var3, "(3,6]")(6,9]  
                          -0.50  

Degrees of Freedom: 12 Total (i.e. Null);  10 Residual
  (1 observation deleted due to missingness)
Null Deviance:      3.231 
Residual Deviance: 2.7  AIC: 24.46
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top