Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 767
RicardoC Avatar asked Jun 09 '13 16:06

RicardoC


1 Answers

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.

like image 200
gung - Reinstate Monica Avatar answered Sep 22 '22 17:09

gung - Reinstate Monica