模型矩阵具有多重共线性

时间:2016-01-06 09:03:44

标签: r nlme

我在lme中使用R函数来运行模型:

f0 <- lme(value ~ -1 + cs_d0 + va_d0 + cs_d0:visitmse + va_d0:visitmse + cs_d0:ageb + va_d0:ageb +
+           cs_d0:lesionse + va_d0:lesionse, random =  
+            ~ -1 + (cs_d0 + va_d0) + visitmse:( cs_d0 + va_d0)|pat , weights = 
+             varIdent(form=~1| cs_d0), control=lmeControl(opt="optim"), data =D0Train)

但我遇到了错误

Error in MEEM(object, conLin, control$niterEM) : 
  Singularity in backsolve at level 0, block 1

当我搜索那个并试图查看模型矩阵时,我发现了这个问题:

> m <- model.matrix(value ~ -1 + cs_d0 + va_d0 + cs_d0:visitmse + va_d0:visitmse + cs_d0:ageb + va_d0:ageb +
+           cs_d0:lesionse + va_d0:lesionse + va_d0:gender   ,data =D0Train)
> m[1:10,]
   cs_d0 va_d0 cs_d0:visitmse va_d0:visitmse cs_d0:ageb va_d0:ageb cs_d0:lesionse0 cs_d0:lesionse1 va_d0:lesionse1 va_d0:gender2
1      1     0            0.0              0         70          0               0               1               0             0
2      1     0            0.5              0         70          0               0               1               0             0
3      1     0            1.0              0         70          0               0               1               0             0
4      1     0            9.0              0         70          0               0               1               0             0
5      1     0            0.0              0         80          0               1               0               0             0
6      1     0            3.0              0         80          0               1               0               0             0
7      1     0            6.0              0         80          0               1               0               0             0
8      1     0            0.0              0         77          0               1               0               0             0
9      1     0            3.0              0         77          0               1               0               0             0
10     1     0            6.0              0         77          0               1               0               0             0

病变是具有两个水平(0,1),

的因子
str(D0Train$lesionse)
 Factor w/ 2 levels "0","1": 2 2 2 2 1 1 1 1 1 1 ...

但在模型(cs_d0:lesionse0 , cs_d0:lesionse1)中似乎有两个以上的级别。

这是一个例子:

# lesion type
lesion = factor(c(rep(0,150),rep(1,50))) 
# gender
sex = factor(c(rep(0,150),rep(1,50)), label=c("Female","Male"))
# visiting time by months
time = rep(c(0,3,6,9),time = 4, 200)
# subjects 
subject = rep(1:50, each = 4)
# first  response variable "identity"
x_1 = c(rep(0, 100), rep(1,100)) 
# second  response variable "identity"
x_2 = c(rep(1, 100), rep(0,100))
# values of both reponses variables (x_1, x_2)
value = c(rnorm(100,20,1),rnorm(100,48,1))
# variables refer to reponses variables (x_1, x_2)
variable = factor(c(rep(0,150),rep(1,50)), label=c("X2","X1"))

df = data.frame(subject , time, sex, lesion, x_1,x_2,value, variable)

library(nlme)
# fit the model that each response variable has intercept and slope (time) for each random and fixed effects
# as well as fixed effects slopes for sex and lesion, and each response has different variance 
f=  lme(value ~ -1 + x_1 + x_2 + x_1:time + x_2:time +  x_1:lesion + x_2:lesion + 
               x_1:sex + x_2:sex, random = ~ -1 + (x_1 + x_2) + time:( x_1 + x_2)|subject , 
       weights = varIdent(form=~1| x_1), control=lmeControl(opt="optim"), data =df)

fm = model.matrix(value ~ -1 + x_1 + x_2 + x_1:time + x_2:time +  x_1:lesion + x_2:lesion + 
               x_1:sex + x_2:sex,data=df)

您可以帮助我或提供一些建议吗?

1 个答案:

答案 0 :(得分:0)

根据您的示例数据,错误是正确的。你有一个奇点。

x_1:timex_2:time是重复的列 - 当x_1:time == 0,x_2:time == time时。当x_1:time == timex_2:time == 0时,x_1x_2的主要影响也是如此。

您的模型过于规范。