[R] Singular model.matrix of nested designs

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed Sep 23 00:00:23 CEST 2009



Peng Yu wrote:
> Hi,
> 
> I want to do ANOVA for nested designs like following. I don't
> understand why the matrix (t(X) %*% X) is singular. Can somebody help
> me understand it?


Yes:

A=1 is never combined with B=4, B=5, or B=6
A=2 is never combined with B=1, B=2, or B=3

hence you cannot estimate your model since the corresponding columns in 
your Design matrix are all zero.

Uwe Ligges




> Regards,
> Peng
> 
>> a=2
>> b=3
>> n=4
>> A = as.vector(sapply(1:a,function(x){rep(x,b*n)}))
>> B = as.vector(sapply(1:(a*b), function(x){rep(x,n)}))
>> cbind(A,B)
>      A B
> [1,] 1 1
> [2,] 1 1
> [3,] 1 1
> [4,] 1 1
> [5,] 1 2
> [6,] 1 2
> [7,] 1 2
> [8,] 1 2
> [9,] 1 3
> [10,] 1 3
> [11,] 1 3
> [12,] 1 3
> [13,] 2 4
> [14,] 2 4
> [15,] 2 4
> [16,] 2 4
> [17,] 2 5
> [18,] 2 5
> [19,] 2 5
> [20,] 2 5
> [21,] 2 6
> [22,] 2 6
> [23,] 2 6
> [24,] 2 6
>> Y = A + B + rnorm(a*b*n)
> 
>> fr = data.frame(Y=Y,A=as.factor(A),B=as.factor(B))
>> afit=aov(Y ~ A / B - 1,fr)
>> X=model.matrix(afit)
>> solve(t(X) %*% X)
> Error in solve.default(t(X) %*% X) :
>   Lapack routine dgesv: system is exactly singular
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list