/
as_matrix.R
148 lines (146 loc) · 4.68 KB
/
as_matrix.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#' Coerce a BAS list object into a matrix.
#'
#' Models, coefficients, and standard errors in objects of class 'bas' are
#' represented as a list of lists to reduce storage by omitting the zero
#' entries. These functions coerce the list object to a matrix and fill in the
#' zeros to facilitate other computations.
#'
#' \code{list2matrix.bas(x, which)} is equivalent to
#' \code{list2matrix.which(x)}, however, the latter uses sapply rather than a
#' loop.
#' \code{list2matrix.which} and \code{which.matrix} both coerce
#' \code{x$which} into a matrix.
#' @aliases list2matrix
#' @param x a 'bas' object
#' @param what name of bas list to coerce
#' @param which.models a vector of indices use to extract a subset
#' @return a matrix representation of \code{x$what}, with number of rows equal
#' to the length of which.models or total number of models and number of
#' columns \code{x$n.vars}
#' @author Merlise Clyde \email{clyde@duke.edu}
#' @seealso \code{\link{bas}}
#' @keywords regression
#'
#'
#' @examples
#'
#' data(Hald)
#' hald.bic <- bas.lm(Y ~ ., data=Hald, prior="BIC",
#' initprobs= "eplogp")
#' coef <- list2matrix.bas(hald.bic, "mle") # extract all coefficients
#' se <- list2matrix.bas(hald.bic, "mle.se")
#' models <- list2matrix.which(hald.bic) #matrix of model indicators
#' models <- which.matrix(hald.bic$which, hald.bic$n.vars) #matrix of model indicators
#'
#' @rdname list2matrix
#' @family as.matrix methods
#' @export
list2matrix.bas <- function(x, what, which.models = NULL) {
namesx <- x$namesx
if (is.null(which.models)) which.models <- 1:x$n.models
listobj <- x[[what]][which.models]
which <- x$which[which.models]
n.models <- length(which.models)
p <- length(namesx)
mat <- matrix(0, nrow = n.models, ncol = p)
for (i in 1:n.models) {
mat[i, which[[i]] + 1] <- listobj[[i]]
}
colnames(mat) <- namesx
return(mat)
}
#' Coerce a BAS list object into a matrix.
#'
#' Models, coefficients, and standard errors in objects of class 'bas' are
#' represented as a list of lists to reduce storage by omitting the zero
#' entries. These functions coerce the list object to a matrix and fill in the
#' zeros to facilitate other computations.
#'
#' \code{list2matrix.bas(x, which)} is equivalent to
#' \code{list2matrix.which(x)}, however, the latter uses sapply rather than a
#' loop.
#' \code{list2matrix.which} and \code{which.matrix} both coerce
#' \code{x$which} into a matrix.
#'
#' @param x a 'bas' object
#' @param which.models a vector of indices use to extract a subset
#' @return a matrix representation of \code{x$what}, with number of rows equal
#' to the length of which.models or total number of models and number of
#' columns \code{x$n.vars}
#' @author Merlise Clyde \email{clyde@@duke.edu}
#' @seealso \code{\link{bas}}
#' @keywords regression
#'
#'
#' @examples
#'
#' data(Hald)
#' Hald.bic <- bas.lm(Y ~ ., data=Hald, prior="BIC", initprobs="eplogp")
#' coef <- list2matrix.bas(Hald.bic, "mle") # extract all ols coefficients
#' se <- list2matrix.bas(Hald.bic, "mle.se")
#' models <- list2matrix.which(Hald.bic) #matrix of model indicators
#' models <- which.matrix(Hald.bic$which, Hald.bic$n.vars) #matrix of model indicators
#'
#' @rdname list2matrix.which
#' @family as.matrix methods
#' @export
list2matrix.which <- function(x, which.models = NULL) {
namesx <- x$namesx
listobj <- x$which
if (!is.null(which.models)) {
listobj <- listobj[which.models]
}
p <- length(namesx)
mat <- t(sapply(
listobj,
function(x, dimp) {
xx <- rep(0, dimp)
xx[x + 1] <- 1
xx
},
p
))
colnames(mat) <- namesx
mat
}
#' Coerce a BAS list object of models into a matrix.
#'
#' This function coerces the list object of models to a matrix and fill in the
#' zeros to facilitate other computations.
#'
#' \code{which.matrix} coerces
#' \code{x$which} into a matrix.
#'
#' @aliases which.matrix
#' @param which a 'bas' model object \code{x$which}
#' @param n.vars the total number of predictors, \code{x$n.vars}
#' @return a matrix representation of \code{x$which}, with number of rows equal
#' to the length of which.models or total number of models and number of
#' columns \code{x$n.vars}
#' @author Merlise Clyde \email{clyde@@duke.edu}
#' @seealso \code{\link{bas}}
#' @keywords regression
#'
#'
#' @examples
#'
#' data(Hald)
#' Hald.bic <- bas.lm(Y ~ ., data=Hald, prior="BIC", initprobs="eplogp")
#' # matrix of model indicators
#' models <- which.matrix(Hald.bic$which, Hald.bic$n.vars)
#'
#' @rdname which.matrix
#' @family as.matrix methods
#' @export
which.matrix <- function(which, n.vars) {
mat <- t(sapply(
which,
function(x, dimp) {
xx <- rep(0, dimp)
xx[x + 1] <- 1
xx
},
n.vars
))
mat
}