null-basis.Rd
Finds the basis of the (right) null space of a matrix, a vector (a 1-column matrix) or a model object for which a model matrix can be extracted. I.e. finds basis for the (right) null space x : Mx = 0.
null_basis(object)
A matrix, a vector (a 1-column matrix) or a model object for
which a model matrix can be extracted (using model.matrix
).
A matrix (possibly with zero columns if the null space consists only of the zero vector).
M <- matrix(c(1,1,1,1,1,1,0,0,0,0,1,1), nrow=4)
null_basis(M)
#> [,1]
#> [1,] 0.5773503
#> [2,] -0.5773503
#> [3,] -0.5773503
MASS::Null(t(M))
#> [,1]
#> [1,] -0.5773503
#> [2,] 0.5773503
#> [3,] 0.5773503
M <- c(1,1,1,1)
null_basis(M)
#>
#> [1,]
MASS::Null(t(M))
#>
#> [1,]
m0 <- lm(breaks ~ wool + tension, data=warpbreaks)
null_basis(m0)
#>
#> [1,]
#> [2,]
#> [3,]
#> [4,]
MASS::Null(t(model.matrix(m0)))
#>
#> [1,]
#> [2,]
#> [3,]
#> [4,]
## Make balanced dataset
dat.bal <- expand.grid(list(A=factor(1:2), B=factor(1:3), C=factor(1:3)))
dat.bal$y <- rnorm(nrow(dat.bal))
## Make unbalanced dataset: 'B' is nested within 'C' so B=1 is only
## found when C=1 and B=2,3 are found in each C=2,3,4
dat.nst <- dat.bal
dat.nst$C <-factor(c(1,1,2,2,2,2,1,1,3,3,3,3,1,1,4,4,4,4))
xtabs(y ~ C+B+A , data=dat.nst)
#> , , A = 1
#>
#> B
#> C 1 2 3
#> 1 1.07305756 0.00000000 0.00000000
#> 2 0.00000000 0.02229473 -0.26265057
#> 3 0.00000000 0.84618466 -1.30511701
#> 4 0.00000000 -0.28689522 0.06730444
#>
#> , , A = 2
#>
#> B
#> C 1 2 3
#> 1 -1.89381818 0.00000000 0.00000000
#> 2 0.00000000 0.60361101 -0.52826408
#> 3 0.00000000 0.08171963 -0.94491206
#> 4 0.00000000 0.89496163 -0.16267634
#>
mod.bal <- lm(y ~ A + B*C, data=dat.bal)
mod.nst <- lm(y ~ A + B*C, data=dat.nst)
null_basis( mod.bal )
#>
#> [1,]
#> [2,]
#> [3,]
#> [4,]
#> [5,]
#> [6,]
#> [7,]
#> [8,]
#> [9,]
#> [10,]
null_basis( mod.nst )
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
#> [2,] 3.080869e-16 -9.381385e-16 -2.376571e-16 -1.110223e-17 6.272760e-16
#> [3,] -1.603826e-01 -4.431460e-01 2.606611e-01 2.913913e-01 6.780575e-03
#> [4,] -4.631342e-01 -3.110869e-01 -2.362670e-01 -8.798453e-02 1.297142e-02
#> [5,] 1.455504e-01 3.435181e-01 -1.276773e-02 2.914910e-02 5.515815e-01
#> [6,] 5.648760e-01 -3.719757e-02 -3.452989e-01 -1.033020e-02 6.810172e-02
#> [7,] 3.140523e-01 5.064779e-02 2.259764e-01 -5.270284e-01 1.201089e-01
#> [8,] 1.483214e-02 9.962787e-02 -2.478934e-01 -3.205404e-01 -5.583620e-01
#> [9,] 3.175838e-01 -3.243121e-02 2.490348e-01 5.883542e-02 -5.645529e-01
#> [10,] -4.044934e-01 4.803436e-01 8.463786e-02 -2.810611e-01 -7.488229e-02
#> [11,] -1.017418e-01 3.482845e-01 5.815660e-01 9.831473e-02 -8.107314e-02
#> [12,] -1.536697e-01 3.924982e-01 -4.866375e-01 2.356371e-01 -1.268895e-01
#> [13,] 1.490819e-01 2.604391e-01 1.029059e-02 6.150129e-01 -1.330803e-01
null_basis( model.matrix(mod.bal) )
#>
#> [1,]
#> [2,]
#> [3,]
#> [4,]
#> [5,]
#> [6,]
#> [7,]
#> [8,]
#> [9,]
#> [10,]
null_basis( model.matrix(mod.nst) )
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
#> [2,] 3.080869e-16 -9.381385e-16 -2.376571e-16 -1.110223e-17 6.272760e-16
#> [3,] -1.603826e-01 -4.431460e-01 2.606611e-01 2.913913e-01 6.780575e-03
#> [4,] -4.631342e-01 -3.110869e-01 -2.362670e-01 -8.798453e-02 1.297142e-02
#> [5,] 1.455504e-01 3.435181e-01 -1.276773e-02 2.914910e-02 5.515815e-01
#> [6,] 5.648760e-01 -3.719757e-02 -3.452989e-01 -1.033020e-02 6.810172e-02
#> [7,] 3.140523e-01 5.064779e-02 2.259764e-01 -5.270284e-01 1.201089e-01
#> [8,] 1.483214e-02 9.962787e-02 -2.478934e-01 -3.205404e-01 -5.583620e-01
#> [9,] 3.175838e-01 -3.243121e-02 2.490348e-01 5.883542e-02 -5.645529e-01
#> [10,] -4.044934e-01 4.803436e-01 8.463786e-02 -2.810611e-01 -7.488229e-02
#> [11,] -1.017418e-01 3.482845e-01 5.815660e-01 9.831473e-02 -8.107314e-02
#> [12,] -1.536697e-01 3.924982e-01 -4.866375e-01 2.356371e-01 -1.268895e-01
#> [13,] 1.490819e-01 2.604391e-01 1.029059e-02 6.150129e-01 -1.330803e-01
MASS::Null( t(model.matrix(mod.bal)) )
#>
#> [1,]
#> [2,]
#> [3,]
#> [4,]
#> [5,]
#> [6,]
#> [7,]
#> [8,]
#> [9,]
#> [10,]
MASS::Null( t(model.matrix(mod.nst)) )
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.00000000 0.000000000 0.000000000 0.00000000 0.000000000
#> [2,] 0.00000000 0.000000000 0.000000000 0.00000000 0.000000000
#> [3,] -0.18785831 -0.483709479 0.205423071 -0.20948165 0.140189852
#> [4,] -0.43077278 -0.254885878 0.000585391 0.17292743 -0.307512216
#> [5,] -0.19043018 0.487875281 -0.214460476 -0.16054152 0.313674397
#> [6,] 0.29824006 0.015891999 -0.587302075 -0.01748620 0.100076265
#> [7,] 0.23138403 0.317869371 -0.099970415 -0.44023329 -0.293369234
#> [8,] 0.37828849 -0.004165801 0.009037405 0.37002317 -0.453864249
#> [9,] 0.62120296 -0.232989403 0.213875085 -0.01238591 -0.006162181
#> [10,] -0.11038175 0.467817480 0.381879003 0.22696785 -0.240266118
#> [11,] 0.13253271 0.238993879 0.586716684 -0.15544123 0.207435950
#> [12,] -0.04352572 0.165840109 -0.105452656 0.64971493 0.153179382
#> [13,] 0.19938875 -0.062983493 0.099385024 0.26730585 0.600881450