简体   繁体   中英

R - geeglm Error: contrasts can be applied only to factors with 2 or more levels

I have applied GEE to the following dataset (str as below). Everything is fine.

> str(cd4.5m2)
'data.frame':   1300 obs. of  7 variables:
 $ id       : Factor w/ 260 levels "1","5","29","32",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ Treatment: Factor w/ 4 levels "Alternating",..: 2 2 2 2 2 1 1 1 1 1 ...
 $ Age      : num  36.4 36.4 36.4 36.4 36.4 ...
 $ Gender   : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
 $ logcd4   : num  3.14 3.04 2.77 2.83 3.22 ...
 $ Week     : num  0 7.57 15.57 23.57 32.57 ...
 $ Time     : int  0 1 2 3 4 0 1 2 3 4 ...

I then transformed the outcome variable, reason being we want to monitor the change over time. So the str of the transformed data looks like below, which is almost exactly the same as the previous one (other than some name changes).

> str(cd4.5m1)
'data.frame':   1300 obs. of  6 variables:
 $ id             : Factor w/ 260 levels "1","5","29","32",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Treatment      : Factor w/ 4 levels "Alternating",..: 2 1 4 1 3 3 1 4 1 3 ...
 $ Age            : num  36.4 35.9 47.5 37.3 42.7 ...
 $ Gender         : Factor w/ 2 levels "Female","Male": 2 2 2 1 2 2 2 2 2 2 ...
 $ Week           : num  1 1 1 1 1 1 1 1 1 1 ...
 $ cd4.change.norm: num  0.572 0.572 0.572 0.572 0.572 ...

I then run the GEE again and it gives me the error.

> gee1.default <- geeglm(cd4.change.norm ~ Treatment, data=cd4.5m1, id=id, family=gaussian, corstr="unstructured")
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

I also tested all variables in the data, they all contain multiple values. So I'm completely lost here. I also saw a lot of posts on this Error, but none seem to be able to address my issue here. Help appreciated!

I changed the correlation structure to AR1, and it worked. I did test the correlation (decreased over time) and AR1 is the correct structure to use.

But normally unstructured should be the save option?

I just reordered my data and it works. I'd like to suggest you try reordering your data like cd4.5m1<-cd4.5m1[order(cd4.5m1$id),] . Credits:KDG

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM