Why is "relevel" not working with my logistic regression?

20,705

I'm not sure what's going on here. There isn't enough information in your question to tell. (You might want to read this thread: how-to-make-a-great-r-reproducible-example, and edit your Q, especially if my answer below is not helpful.) Also, I suspect you have a typo in

    DPROS <- as.`enter code here`factor(DPROS)  

I'm assuming you meant: DPROS <- as.factor(DPROS).

One possibility is that you had attached your data frame. Consider:

> set.seed(9)
> base1 = data.frame(CAPSULE=rnorm(100), AGE=rnorm(100), 
+                    DPROS=as.numeric(sample(1:4, 100, replace=T)),
+                    DCAPS=rnorm(100), PSA=rnorm(100))
> attach(base1)
> des(base1)

 No. of observations =  100 
  Variable      Class           Description
1 CAPSULE       numeric                    
2 AGE           numeric                    
3 DPROS         numeric                    
4 DCAPS         numeric                    
5 PSA           numeric    

> base1 <- within(base1, DPROS <- relevel(DPROS, ref = 4))
Error in relevel.default(DPROS, ref = 4) : 'relevel' only for factors

> DPROS <- as.factor(DPROS)
> base1 <- within(base1, DPROS <- relevel(DPROS, ref = 4))
Error in relevel.default(DPROS, ref = 4) : 'relevel' only for factors

> des(base1)

 No. of observations =  100 
  Variable      Class           Description
1 CAPSULE       numeric                    
2 AGE           numeric                    
3 DPROS         numeric                    
4 DCAPS         numeric                    
5 PSA           numeric                    

> is.factor(DPROS)
[1] TRUE

This reproduces the behavior you describe, as best I understand what happened to you.

If you try the following, you would see that it works properly:

> base1 <- within(base1, DPROS <- as.factor(DPROS))
> des(base1)

 No. of observations =  100 
  Variable      Class           Description
1 CAPSULE       numeric                    
2 AGE           numeric                    
3 DPROS         factor                     
4 DCAPS         numeric                    
5 PSA           numeric                    

> base1 <- within(base1, DPROS <- relevel(DPROS, ref = 4))

If this is what happened, it is an intrinsic part of the way attached data frames work in R. You can change the variable that's been attached without changing the actual variable in the data frame. As @GavinSimpson notes, it is generally best to avoid using attach.

Share:
20,705
RicardoC
Author by

RicardoC

Updated on June 10, 2020

Comments

  • RicardoC
    RicardoC almost 4 years

    I'm trying to use the instruction relevel to redefine the refrence category in a factor to the last category.

    At first, I got an error:

    base1 <- within(base1, DPROS <- relevel(DPROS, ref = 4))  
    Error in relevel.default(DPROS, ref = 4) : 'relevel' only for factors
    

    I used the des instruction of the epicalc package to check if DPROS was numeric or factor:

    des(base1)
    
    No. of observations =  380   
      Variable      Class           Description  
    1 CAPSULE       numeric                    
    2 AGE           numeric                    
    3 DPROS         numeric                    
    4 DCAPS         numeric                    
    5 PSA           numeric  
    

    I used as.factor to make DPROS a factor.

    DPROS <- as.factor(DPROS)
    

    But I still got the same error:

    base1 <- within(base1, DPROS <- relevel(DPROS, ref = 4))  
    Error in relevel.default(DPROS, ref = 4) : 'relevel' only for factors
    

    Using des it still said DPROS was numeric, but is.factor(DPROS) returned TRUE.

    What am I doing wrong?

  • Gavin Simpson
    Gavin Simpson almost 11 years
    It is not a weird quirk of attach it is documented behaviour. The key is that you attach a copy not the data to the search path.
  • gung - Reinstate Monica
    gung - Reinstate Monica almost 11 years
    Fair enough, @GavinSimpson, I'll change that. I didn't mean for it to come off as insulting; I only meant that, for someone who isn't very familiar w/ R, it seems inscrutable.