# [R] matrix multivariate bootstrap: order of results in \$t component

John Fox jfox at mcmaster.ca
Wed Dec 21 21:29:36 CET 2011

Hi Michael,

There may be a cleverer way to do this without a loop, but I think that the
following does what you want:

> t <- matrix(1:(800*18), 800, 18, byrow=TRUE)  # for a reproducible example
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    2    3    4    5    6    7    8    9    10
[2,]   19   20   21   22   23   24   25   26   27    28
[3,]   37   38   39   40   41   42   43   44   45    46
[4,]   55   56   57   58   59   60   61   62   63    64
[5,]   73   74   75   76   77   78   79   80   81    82
[6,]   91   92   93   94   95   96   97   98   99   100
[,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
[1,]    11    12    13    14    15    16    17    18
[2,]    29    30    31    32    33    34    35    36
[3,]    47    48    49    50    51    52    53    54
[4,]    65    66    67    68    69    70    71    72
[5,]    83    84    85    86    87    88    89    90
[6,]   101   102   103   104   105   106   107   108

> tt <- matrix(0, 2400, 6)
> cols <- rep(1:6, 3)
> for (i in 1:800){
+   tt[((i - 1)*3 + 1):(i*3), cols] <- t[i, ]
+ }
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    4    7   10   13   16
[2,]    2    5    8   11   14   17
[3,]    3    6    9   12   15   18
[4,]   19   22   25   28   31   34
[5,]   20   23   26   29   32   35
[6,]   21   24   27   30   33   36

I'm pretty sure that the bootstrapped matrix is organized as you suggest,
since R matrices are stored in column-major order.

Best,
John

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Michael Friendly
> Sent: December-21-11 2:17 PM
> To: R-help
> Subject: [R] matrix multivariate bootstrap: order of results in \$t
> component
>
> [This question is hopefully straight-forward, but difficult to provide
> reproducible code.]
>
> I'm doing a multivariate bootstrap, using boot::boot(), where the
> output of the basic computation is a k x p matrix of coefficients,
> representing a tuning constant x variable, as shown in the \$t0
> component from my run, giving a 3 x 6 matrix
>
>  > lboot\$t0
>           GNP Unemployed Armed.Forces Population  Year GNP.deflator
> 0.00 -3.4472     -1.828      -0.6962   -0.34420 8.432      0.15738
> 0.01 -0.1798     -1.361      -0.5881   -1.00317 5.656     -0.02612
> 0.08  1.0907     -1.086      -0.4583   -0.08596 2.642      0.57025
>  >
>
> ?boot doesn't say how these are strung out to give the 1 x 18 vector in
> each row of lboot\$t, but I believe it is done columnwise -- is this
> correct?
>
> That is, I think that the column names for  the bootstrap results,
> lboot\$t, should vary most quickly down the columns:
>
>  > c(t(outer(colnames(lboot\$t0), rownames(lboot\$t0), paste, sep=':')))
>   [1] "GNP:0.00"          "GNP:0.01"          "GNP:0.08"
>   [4] "Unemployed:0.00"   "Unemployed:0.01"   "Unemployed:0.08"
>   [7] "Armed.Forces:0.00" "Armed.Forces:0.01" "Armed.Forces:0.08"
> [10] "Population:0.00"   "Population:0.01"   "Population:0.08"
> [13] "Year:0.00"         "Year:0.01"         "Year:0.08"
> [16] "GNP.deflator:0.00" "GNP.deflator:0.01" "GNP.deflator:0.08"
>  >
>
> So, the first bootstrap resample would correspond to
>
>  > lboot\$t[1,]
>           GNP:0.00          GNP:0.01          GNP:0.08
> Unemployed:0.00
>           -10.1495           -0.8622            1.0566           -
> 2.6131
>    Unemployed:0.01   Unemployed:0.08 Armed.Forces:0.00
> Armed.Forces:0.01
>            -1.5316           -1.1988           -0.9279           -
> 0.7141
> Armed.Forces:0.08   Population:0.00   Population:0.01   Population:0.08
>            -0.5748            1.8401           -0.9989           -
> 0.3155
>          Year:0.00         Year:0.01         Year:0.08
> GNP.deflator:0.00
>            12.8984            6.4378            2.7761
> 0.9417
> GNP.deflator:0.01 GNP.deflator:0.08
>             0.1871            0.9797
>
> Or, as a matrix:
>
>  > matrix(lboot\$t[1,], nrow=3)
>           [,1]   [,2]    [,3]    [,4]   [,5]   [,6]
> [1,] -10.1495 -2.613 -0.9279  1.8401 12.898 0.9417 [2,]  -0.8622 -1.532
> -0.7141 -0.9989  6.438 0.1871
> [3,]   1.0566 -1.199 -0.5748 -0.3155  2.776 0.9797
>  >
>
> Finally, if the above is correct, how can I easily take my R=800
> bootstrap estimates in the 800 x 18 matrix lboot\$t and restructure them
> in a (2400=800x3) x 6 matrix or data.frame corresponding to stacking
> the matrices like the above for each bootstrap sample?
>
> thanks,
> -Michael
>
> --
> Michael Friendly     Email: friendly AT yorku DOT ca
> Professor, Psychology Dept.
> York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
> 4700 Keele Street    Web:   http://www.datavis.ca
> Toronto, ONT  M3J 1P3 CANADA
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help