1
- # ------------------------------------------------
2
- # Cross-validation, preliminary
1
+ # Cross-validation
2
+
3
+ # --------------------------------------
4
+ # Functions for creating cv folds
5
+ # -------------------------------------
3
6
4
7
cvMakeFolds <- function (x , nfolds = 5 ) {
5
8
inds <- sample(1 : nrow(x ), replace = FALSE )
@@ -15,9 +18,23 @@ cvMakeFolds <- function(x, nfolds = 5) {
15
18
return (folds )
16
19
}
17
20
18
- # ###########################################
19
- # Can this be optimized using svdu_thresh? #
20
- # ###########################################
21
+ # To interface with glmnet
22
+ foldid <- function (folds ) {
23
+ n <- sum(sapply(folds , length ))
24
+ glmnetfoldid <- rep(0 , n )
25
+ for (ind in 1 : length(folds )) {
26
+ glmnetfoldid [folds [[ind ]]] <- ind
27
+ }
28
+ glmnetfoldid
29
+ }
30
+
31
+ # --------------------------------------
32
+ # Functions for computing quadratic form for cv-error
33
+ # --------------------------------------
34
+
35
+ # There seems to be a problem here, picking overly conservative models
36
+
37
+ # Can this be optimized using svdu_thresh?
21
38
cvHatMatrix <- function (x , folds , active.sets ) {
22
39
nfolds <- length(folds )
23
40
lapply(1 : nfolds , function (f ) {
@@ -61,6 +78,11 @@ cvRSSquad <- function(x, folds, active.sets) {
61
78
return (Q )
62
79
}
63
80
81
+
82
+ # --------------------------------------
83
+ # Functions for forward stepwise
84
+ # --------------------------------------
85
+
64
86
cvfs <- function (x , y , index = 1 : ncol(x ), maxsteps , sigma = NULL , intercept = TRUE , center = TRUE , normalize = TRUE , nfolds = 5 ) {
65
87
66
88
n <- nrow(x )
@@ -131,47 +153,58 @@ cvfs <- function(x, y, index = 1:ncol(x), maxsteps, sigma = NULL, intercept = TR
131
153
}
132
154
133
155
134
- cvlar <- function (x , y ) { # other args
156
+ # --------------------------------------
157
+ # Functions for lar
158
+ # --------------------------------------
159
+
160
+ cvlar <- function (x , y , maxsteps ) { # other args
135
161
folds <- cvMakeFolds(x )
136
162
models <- lapply(folds , function (fold ) {
137
- x.train <- X
138
- y.train <- Y
163
+ x.train <- x
164
+ y.train <- y
139
165
x.train [fold ,] <- 0
140
166
y.train [fold ] <- 0
141
- x.test <- X [fold ,]
142
- y.test <- Y [fold ]
143
- larpath.train <- lar(x.train , y.train , maxsteps = maxsteps , intercept = F , normalize = F )
144
- return (lff )
167
+ x.test <- x [fold ,]
168
+ y.test <- y [fold ]
169
+ larpath.train <- lar(x.train , y.train , maxsteps = maxsteps , intercept = F , normalize = F )
170
+ return (larpath.train )
145
171
})
146
172
147
173
active.sets <- lapply(models , function (model ) model $ action )
148
174
lambdas <- lapply(models , function (model ) model $ lambda )
149
175
lmin <- min(unlist(lambdas ))
150
176
151
- # Interpolate lambda grid or parametrize by steps?
152
- # interpolation probably requires re-writing cvRSSquads for
153
- # penalized fits in order to make sense
177
+ # Interpolate lambda grid or parametrize by steps?
178
+ # interpolation probably requires re-writing cvRSSquads for
179
+ # penalized fits in order to make sense
154
180
155
- # do steps for now just to have something that works?
181
+ # do steps for now just to have something that works?
156
182
157
183
RSSquads <- list ()
158
184
for (s in 1 : maxsteps ) {
159
185
initial.active <- lapply(active.sets , function (a ) a [1 : s ])
160
- RSSquads [[s ]] <- cvRSSquad(X , folds , initial.active )
186
+ RSSquads [[s ]] <- cvRSSquad(x , folds , initial.active )
161
187
}
162
188
163
- RSSs <- lapply(RSSquads , function (Q ) t(Y ) %*% Q %*% Y )
189
+ RSSs <- lapply(RSSquads , function (Q ) t(y ) %*% Q %*% y )
164
190
sstar <- which.min(RSSs )
165
191
quadstar <- RSSquads [sstar ][[1 ]]
166
192
167
- RSSquads <- lapply(RSSquads , function (quad ) quad - quadstar )
168
- RSSquads [[sstar ]] <- NULL # remove the all zeroes case
193
+ # Need to add these later?
194
+ # RSSquads <- lapply(RSSquads, function(quad) quad - quadstar)
195
+ # RSSquads[[sstar]] <- NULL # remove the all zeroes case
169
196
170
- fit <- lar(X , Y , maxsteps = sstar , intercept = F , normalize = F )
197
+ fit <- lar(x , y , maxsteps = sstar , intercept = F , normalize = F )
171
198
172
- # Very tall Gamma encoding all cv-model paths
199
+ # Very tall Gamma encoding all cv-model paths
173
200
Gamma <- do.call(rbind , lapply(models , function (model ) return (model $ Gamma )))
174
-
175
- # more to do here
201
+ fit $ Gamma <- rbind(fit $ Gamma , Gamma )
202
+ fit $ khat <- sstar
203
+ fit $ folds <- folds
204
+ # more to do here
205
+ return (fit )
176
206
}
177
207
208
+ cvlarInf <- function (obj ) {
209
+ larInf(obj , type = " all" , k = obj $ khat )
210
+ }
0 commit comments