moffset.RdModify a matrix by shifting successive elements.
moffset(mat, roffset = 0, coffset = 0, postfix = "",
rprefix = "Row.", cprefix = "Col.")Data frame or matrix.
This ought to have at least three rows and three columns.
The elements are shifted in the order of c(mat),
i.e., going down successive columns, as the columns go
from left to right. Wrapping of values is done.
Numeric or character.
If numeric, the amount of shift (offset) for each row and column.
The default is no change to mat.
If character, the offset is computed by matching with
the row or column names.
For example, for the alcoff,
put roffset = "6" means that we make an effective day's
dataset start from 6:00 am, and this wraps around to
include midnight to 05.59 am on the next day.
Character. Modified rows and columns are renamed by pasting this argument to the end of each name. The default is no change.
Same as rcim.
This function allows a matrix to be rearranged so that
element (roffset + 1, coffset + 1)
becomes the (1, 1) element.
The elements are assumed to be ordered in the same way
as the elements of c(mat),
This function is applicable to, e.g.,
alcoff,
where it is useful to define the effective day
as starting
at some other hour than midnight, e.g., 6.00am.
This is because partying on Friday night continues on into
Saturday morning, therefore it is more interpretable to use
the effective day when considering a daily effect.
This is a data preprocessing function for rcim
and plotrcim0. The differences between
Rcim and moffset is that
Rcim only reorders the level of the
rows and columns
so that the data is shifted but not moved.
That is, a value in one row stays in that row,
and ditto for column.
But in moffset
values in one column can be moved to a previous column.
See the examples below.
A matrix of the same dimensional as its input.
The input mat should have row names and column names.
# Some day's data is moved to previous day:
moffset(alcoff, 3, 2, "*")
#> Wed* Thu* Fri* Sat* Sun* Mon* Tue*
#> 3 75 238 401 693 718 55 60
#> 4 48 145 223 346 410 25 38
#> 5 20 56 139 188 287 19 10
#> 6 9 55 70 155 213 13 9
#> 7 9 42 40 160 200 20 6
#> 8 12 29 40 79 96 5 8
#> 9 14 28 38 44 58 7 8
#> 10 20 36 38 52 69 7 13
#> 11 20 32 27 44 39 8 10
#> 12 8 39 37 73 59 10 13
#> 13 32 37 27 41 45 9 26
#> 14 31 46 42 58 53 22 41
#> 15 62 69 59 75 70 39 48
#> 16 71 85 55 96 95 28 48
#> 17 98 141 136 154 130 46 59
#> 18 117 185 223 236 121 53 100
#> 19 155 289 335 337 146 74 119
#> 20 283 508 591 490 166 74 135
#> 21 326 610 866 754 131 84 154
#> 22 345 765 976 1026 114 90 143
#> 23 363 899 1265 1179 159 110 169
#> 0* 324 827 1379 1332 121 98 165
#> 1* 278 619 1327 1356 97 92 157
#> 2* 229 410 979 1011 60 69 107
Rcim(alcoff, 3 + 1, 2 + 1) # Data does not move as much.
#> Wed Thu Fri Sat Sun Mon Tue
#> 3 75 238 401 693 718 55 60
#> 4 48 145 223 346 410 25 38
#> 5 20 56 139 188 287 19 10
#> 6 9 55 70 155 213 13 9
#> 7 9 42 40 160 200 20 6
#> 8 12 29 40 79 96 5 8
#> 9 14 28 38 44 58 7 8
#> 10 20 36 38 52 69 7 13
#> 11 20 32 27 44 39 8 10
#> 12 8 39 37 73 59 10 13
#> 13 32 37 27 41 45 9 26
#> 14 31 46 42 58 53 22 41
#> 15 62 69 59 75 70 39 48
#> 16 71 85 55 96 95 28 48
#> 17 98 141 136 154 130 46 59
#> 18 117 185 223 236 121 53 100
#> 19 155 289 335 337 146 74 119
#> 20 283 508 591 490 166 74 135
#> 21 326 610 866 754 131 84 154
#> 22 345 765 976 1026 114 90 143
#> 23 363 899 1265 1179 159 110 169
#> 0 165 324 827 1379 1332 121 98
#> 1 157 278 619 1327 1356 97 92
#> 2 107 229 410 979 1011 60 69
alcoff # Original data
#> Mon Tue Wed Thu Fri Sat Sun
#> 0 121 98 165 324 827 1379 1332
#> 1 97 92 157 278 619 1327 1356
#> 2 60 69 107 229 410 979 1011
#> 3 55 60 75 238 401 693 718
#> 4 25 38 48 145 223 346 410
#> 5 19 10 20 56 139 188 287
#> 6 13 9 9 55 70 155 213
#> 7 20 6 9 42 40 160 200
#> 8 5 8 12 29 40 79 96
#> 9 7 8 14 28 38 44 58
#> 10 7 13 20 36 38 52 69
#> 11 8 10 20 32 27 44 39
#> 12 10 13 8 39 37 73 59
#> 13 9 26 32 37 27 41 45
#> 14 22 41 31 46 42 58 53
#> 15 39 48 62 69 59 75 70
#> 16 28 48 71 85 55 96 95
#> 17 46 59 98 141 136 154 130
#> 18 53 100 117 185 223 236 121
#> 19 74 119 155 289 335 337 146
#> 20 74 135 283 508 591 490 166
#> 21 84 154 326 610 866 754 131
#> 22 90 143 345 765 976 1026 114
#> 23 110 169 363 899 1265 1179 159
moffset(alcoff, 3, 2, "*") -
Rcim(alcoff, 3+1, 2+1) # Note the differences
#> Wed* Thu* Fri* Sat* Sun* Mon* Tue*
#> 3 0 0 0 0 0 0 0
#> 4 0 0 0 0 0 0 0
#> 5 0 0 0 0 0 0 0
#> 6 0 0 0 0 0 0 0
#> 7 0 0 0 0 0 0 0
#> 8 0 0 0 0 0 0 0
#> 9 0 0 0 0 0 0 0
#> 10 0 0 0 0 0 0 0
#> 11 0 0 0 0 0 0 0
#> 12 0 0 0 0 0 0 0
#> 13 0 0 0 0 0 0 0
#> 14 0 0 0 0 0 0 0
#> 15 0 0 0 0 0 0 0
#> 16 0 0 0 0 0 0 0
#> 17 0 0 0 0 0 0 0
#> 18 0 0 0 0 0 0 0
#> 19 0 0 0 0 0 0 0
#> 20 0 0 0 0 0 0 0
#> 21 0 0 0 0 0 0 0
#> 22 0 0 0 0 0 0 0
#> 23 0 0 0 0 0 0 0
#> 0* 159 503 552 -47 -1211 -23 67
#> 1* 121 341 708 29 -1259 -5 65
#> 2* 122 181 569 32 -951 9 38
# An 'effective day' data set:
alcoff.e <- moffset(alcoff, roffset = "6", postfix = "*")
fit.o <- rcim(alcoff) # default baselines are 1st row and col
fit.e <- rcim(alcoff.e) # default baselines are 1st row and col
if (FALSE) par(mfrow = c(2, 2), mar = c(9, 4, 2, 1))
plot(fit.o, rsub = "Not very interpretable",
csub = "Not very interpretable")
plot(fit.e, rsub = "More interpretable",
csub = "More interpretable")
# \dontrun{}
# Some checking
all.equal(moffset(alcoff), alcoff) # Should be no change
#> [1] TRUE
moffset(alcoff, 1, 1, "*")
#> Tue* Wed* Thu* Fri* Sat* Sun* Mon*
#> 1 92 157 278 619 1327 1356 97
#> 2 69 107 229 410 979 1011 60
#> 3 60 75 238 401 693 718 55
#> 4 38 48 145 223 346 410 25
#> 5 10 20 56 139 188 287 19
#> 6 9 9 55 70 155 213 13
#> 7 6 9 42 40 160 200 20
#> 8 8 12 29 40 79 96 5
#> 9 8 14 28 38 44 58 7
#> 10 13 20 36 38 52 69 7
#> 11 10 20 32 27 44 39 8
#> 12 13 8 39 37 73 59 10
#> 13 26 32 37 27 41 45 9
#> 14 41 31 46 42 58 53 22
#> 15 48 62 69 59 75 70 39
#> 16 48 71 85 55 96 95 28
#> 17 59 98 141 136 154 130 46
#> 18 100 117 185 223 236 121 53
#> 19 119 155 289 335 337 146 74
#> 20 135 283 508 591 490 166 74
#> 21 154 326 610 866 754 131 84
#> 22 143 345 765 976 1026 114 90
#> 23 169 363 899 1265 1179 159 110
#> 0* 165 324 827 1379 1332 121 98
moffset(alcoff, 2, 3, "*")
#> Thu* Fri* Sat* Sun* Mon* Tue* Wed*
#> 2 229 410 979 1011 60 69 107
#> 3 238 401 693 718 55 60 75
#> 4 145 223 346 410 25 38 48
#> 5 56 139 188 287 19 10 20
#> 6 55 70 155 213 13 9 9
#> 7 42 40 160 200 20 6 9
#> 8 29 40 79 96 5 8 12
#> 9 28 38 44 58 7 8 14
#> 10 36 38 52 69 7 13 20
#> 11 32 27 44 39 8 10 20
#> 12 39 37 73 59 10 13 8
#> 13 37 27 41 45 9 26 32
#> 14 46 42 58 53 22 41 31
#> 15 69 59 75 70 39 48 62
#> 16 85 55 96 95 28 48 71
#> 17 141 136 154 130 46 59 98
#> 18 185 223 236 121 53 100 117
#> 19 289 335 337 146 74 119 155
#> 20 508 591 490 166 74 135 283
#> 21 610 866 754 131 84 154 326
#> 22 765 976 1026 114 90 143 345
#> 23 899 1265 1179 159 110 169 363
#> 0* 827 1379 1332 121 98 165 324
#> 1* 619 1327 1356 97 92 157 278
moffset(alcoff, 1, 0, "*")
#> Mon* Tue* Wed* Thu* Fri* Sat* Sun*
#> 1 97 92 157 278 619 1327 1356
#> 2 60 69 107 229 410 979 1011
#> 3 55 60 75 238 401 693 718
#> 4 25 38 48 145 223 346 410
#> 5 19 10 20 56 139 188 287
#> 6 13 9 9 55 70 155 213
#> 7 20 6 9 42 40 160 200
#> 8 5 8 12 29 40 79 96
#> 9 7 8 14 28 38 44 58
#> 10 7 13 20 36 38 52 69
#> 11 8 10 20 32 27 44 39
#> 12 10 13 8 39 37 73 59
#> 13 9 26 32 37 27 41 45
#> 14 22 41 31 46 42 58 53
#> 15 39 48 62 69 59 75 70
#> 16 28 48 71 85 55 96 95
#> 17 46 59 98 141 136 154 130
#> 18 53 100 117 185 223 236 121
#> 19 74 119 155 289 335 337 146
#> 20 74 135 283 508 591 490 166
#> 21 84 154 326 610 866 754 131
#> 22 90 143 345 765 976 1026 114
#> 23 110 169 363 899 1265 1179 159
#> 0* 98 165 324 827 1379 1332 121
moffset(alcoff, 0, 1, "*")
#> Tue Wed Thu Fri Sat Sun Mon
#> 0 98 165 324 827 1379 1332 121
#> 1 92 157 278 619 1327 1356 97
#> 2 69 107 229 410 979 1011 60
#> 3 60 75 238 401 693 718 55
#> 4 38 48 145 223 346 410 25
#> 5 10 20 56 139 188 287 19
#> 6 9 9 55 70 155 213 13
#> 7 6 9 42 40 160 200 20
#> 8 8 12 29 40 79 96 5
#> 9 8 14 28 38 44 58 7
#> 10 13 20 36 38 52 69 7
#> 11 10 20 32 27 44 39 8
#> 12 13 8 39 37 73 59 10
#> 13 26 32 37 27 41 45 9
#> 14 41 31 46 42 58 53 22
#> 15 48 62 69 59 75 70 39
#> 16 48 71 85 55 96 95 28
#> 17 59 98 141 136 154 130 46
#> 18 100 117 185 223 236 121 53
#> 19 119 155 289 335 337 146 74
#> 20 135 283 508 591 490 166 74
#> 21 154 326 610 866 754 131 84
#> 22 143 345 765 976 1026 114 90
#> 23 169 363 899 1265 1179 159 110
moffset(alcoff, "6", "Mon", "*") # This one is good
#> Mon* Tue* Wed* Thu* Fri* Sat* Sun*
#> 6 13 9 9 55 70 155 213
#> 7 20 6 9 42 40 160 200
#> 8 5 8 12 29 40 79 96
#> 9 7 8 14 28 38 44 58
#> 10 7 13 20 36 38 52 69
#> 11 8 10 20 32 27 44 39
#> 12 10 13 8 39 37 73 59
#> 13 9 26 32 37 27 41 45
#> 14 22 41 31 46 42 58 53
#> 15 39 48 62 69 59 75 70
#> 16 28 48 71 85 55 96 95
#> 17 46 59 98 141 136 154 130
#> 18 53 100 117 185 223 236 121
#> 19 74 119 155 289 335 337 146
#> 20 74 135 283 508 591 490 166
#> 21 84 154 326 610 866 754 131
#> 22 90 143 345 765 976 1026 114
#> 23 110 169 363 899 1265 1179 159
#> 0* 98 165 324 827 1379 1332 121
#> 1* 92 157 278 619 1327 1356 97
#> 2* 69 107 229 410 979 1011 60
#> 3* 60 75 238 401 693 718 55
#> 4* 38 48 145 223 346 410 25
#> 5* 10 20 56 139 188 287 19
# Customise row and column baselines
fit2 <- rcim(Rcim(alcoff.e, rbaseline = "11", cbaseline = "Mon*"))