numFactor: Factor with numeric interpretable levels.

Description Usage Arguments Details Value Examples

View source: R/utils_covstruct.R

Description

Create a factor with numeric interpretable factor levels.

Usage

1
2
3

Arguments

x

Vector, matrix or data.frame that constitute the coordinates.

...

Additional vectors, matrices or data.frames that constitute the coordinates.

levels

Character vector to parse into numeric values.

Details

Some glmmTMB covariance structures require extra information, such as temporal or spatial coordinates. numFactor allows to associate such extra information as part of a factor via the factor levels. The original numeric coordinates are recoverable without loss of precision using the function parseNumLevels. Factor levels are sorted coordinate wise from left to right: first coordinate is fastest running.

Value

Factor with specialized coding of levels.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## 1D example
numFactor(sample(1:5,20,TRUE))
## 2D example
coords <- cbind( sample(1:5,20,TRUE), sample(1:5,20,TRUE) )
(f <- numFactor(coords))
parseNumLevels(levels(f)) ## Sorted
## Used as part of a model.matrix
model.matrix( ~f )
## parseNumLevels( colnames(model.matrix( ~f )) )
## Error: 'Failed to parse numeric levels: (Intercept)'
parseNumLevels( colnames(model.matrix( ~ f-1 )) )

Example output

 [1] (4) (4) (4) (5) (5) (3) (3) (1) (4) (5) (1) (5) (4) (1) (5) (5) (2) (2) (2)
[20] (2)
Levels: (1) (2) (3) (4) (5)
 [1] (5,4) (5,5) (2,3) (1,5) (2,3) (4,5) (2,1) (3,5) (2,1) (1,3) (3,5) (4,3)
[13] (5,1) (1,1) (4,3) (3,5) (1,2) (2,1) (5,3) (5,5)
13 Levels: (1,1) (2,1) (5,1) (1,2) (1,3) (2,3) (4,3) (5,3) (5,4) ... (5,5)
      [,1] [,2]
 [1,]    1    1
 [2,]    2    1
 [3,]    5    1
 [4,]    1    2
 [5,]    1    3
 [6,]    2    3
 [7,]    4    3
 [8,]    5    3
 [9,]    5    4
[10,]    1    5
[11,]    3    5
[12,]    4    5
[13,]    5    5
   (Intercept) f(2,1) f(5,1) f(1,2) f(1,3) f(2,3) f(4,3) f(5,3) f(5,4) f(1,5)
1            1      0      0      0      0      0      0      0      1      0
2            1      0      0      0      0      0      0      0      0      0
3            1      0      0      0      0      1      0      0      0      0
4            1      0      0      0      0      0      0      0      0      1
5            1      0      0      0      0      1      0      0      0      0
6            1      0      0      0      0      0      0      0      0      0
7            1      1      0      0      0      0      0      0      0      0
8            1      0      0      0      0      0      0      0      0      0
9            1      1      0      0      0      0      0      0      0      0
10           1      0      0      0      1      0      0      0      0      0
11           1      0      0      0      0      0      0      0      0      0
12           1      0      0      0      0      0      1      0      0      0
13           1      0      1      0      0      0      0      0      0      0
14           1      0      0      0      0      0      0      0      0      0
15           1      0      0      0      0      0      1      0      0      0
16           1      0      0      0      0      0      0      0      0      0
17           1      0      0      1      0      0      0      0      0      0
18           1      1      0      0      0      0      0      0      0      0
19           1      0      0      0      0      0      0      1      0      0
20           1      0      0      0      0      0      0      0      0      0
   f(3,5) f(4,5) f(5,5)
1       0      0      0
2       0      0      1
3       0      0      0
4       0      0      0
5       0      0      0
6       0      1      0
7       0      0      0
8       1      0      0
9       0      0      0
10      0      0      0
11      1      0      0
12      0      0      0
13      0      0      0
14      0      0      0
15      0      0      0
16      1      0      0
17      0      0      0
18      0      0      0
19      0      0      0
20      0      0      1
attr(,"assign")
 [1] 0 1 1 1 1 1 1 1 1 1 1 1 1
attr(,"contrasts")
attr(,"contrasts")$f
[1] "contr.treatment"

      [,1] [,2]
 [1,]    1    1
 [2,]    2    1
 [3,]    5    1
 [4,]    1    2
 [5,]    1    3
 [6,]    2    3
 [7,]    4    3
 [8,]    5    3
 [9,]    5    4
[10,]    1    5
[11,]    3    5
[12,]    4    5
[13,]    5    5

glmmTMB documentation built on July 20, 2021, 9:06 a.m.