user2842390
user2842390

Reputation: 73

Linear Regression in R with 2-level factors error

I am trying to do linear regression in R with a 2-level factor of sex as a categorical variable, but R result in the error, may I ask how can I solve this? You can download my data here https://www.dropbox.com/s/jbuyearzlw5zf9q/covar.csv?dl=0

Your help is really appreciated!

> covar = read.csv("covar.csv")
> yv = as.numeric(covar$yv)
> sex = as.factor(covar$sex)
> model = lm(yv~sex)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels
> head(sex)
[1] 0 0 1 1 0 0
Levels: 0 1
> head(yv)
[1] 23 NA NA NA NA 23

Upvotes: 0

Views: 48

Answers (1)

G5W
G5W

Reputation: 37631

Unfortunately, 100% of the points in your data with sex=1 have yv=NA. Your data really only contains sex=0 data.

summary(covar[covar$sex==1,])
       yv              sex   
 Min.   : NA      Min.   :1  
 1st Qu.: NA      1st Qu.:1  
 Median : NA      Median :1  
 Mean   :NaN      Mean   :1  
 3rd Qu.: NA      3rd Qu.:1  
 Max.   : NA      Max.   :1  
 NA's   :187533          

table(covar$sex)
     0      1 
220685 187533 

sum(is.na(covar[covar$sex ==1,1]))
[1] 187533

Upvotes: 1

Related Questions