Skip to content

Commit 74e3c48

Browse files
author
Joshua Loftus
committed
Simulation to compare saturated and selected models in fs
1 parent bbfa85e commit 74e3c48

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

forLater/josh/sim.selectedmodel.R

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
library(selectiveInference)
2+
library(intervals)
3+
setwd("~/Dropbox/work/R-software/forLater/josh")
4+
source("selectiveInference/R/cv.R")
5+
source("../../selectiveInference/R/funs.groupfs.R")
6+
source("../../selectiveInference/R/funs.quadratic.R")
7+
source("../../selectiveInference/R/funs.common.R")
8+
source("../../selectiveInference/R/funs.fs.R")
9+
source("../../selectiveInference/R/funs.lar.R")
10+
source("../../selectiveInference/R/funs.inf.R")
11+
library(MASS)
12+
pinv = ginv
13+
14+
set.seed(19)
15+
niters <- 500
16+
known <- TRUE
17+
n <- 50
18+
p <- 100
19+
maxsteps <- 8
20+
sparsity <- 5
21+
snr <- 2
22+
index <- 1:p
23+
24+
x <- matrix(rnorm(n*p), nrow=n)
25+
26+
instance <- function(n, p, sparsity, snr, maxsteps) {
27+
y <- rnorm(n)
28+
if (sparsity > 0) {
29+
beta <- rep(0, p)
30+
beta[1:sparsity] <- snr * sample(c(-1,1), sparsity, replace=T)
31+
y <- y + x %*% beta
32+
}
33+
y <- y - mean(y)
34+
fit <- groupfs(x, y, index, maxsteps=maxsteps, sigma=1, intercept=F, center=F, normalize=F)
35+
fitfs <- fs(x, y, maxsteps=maxsteps, intercept=F, normalize=F)
36+
if (any(fit$action != fitfs$action)) stop("Model paths did not agree")
37+
pvfs <- fsInf(fitfs, sigma=1, k = maxsteps, type = "all")
38+
pv <- groupfsInf(fit)
39+
return(list(vars = fit$action, pvals = pv$pv, selpvals = pvfs$pv))
40+
}
41+
42+
time <- system.time({
43+
output <- replicate(niters, instance(n, p, sparsity, snr, maxsteps))
44+
})
45+
46+
vars <- do.call(c, list(output[1,]))
47+
pvals <- do.call(c, list(output[2,]))
48+
selpvals <- do.call(c, list(output[3,]))
49+
50+
save(vars, pvals, selpvals,
51+
file = paste0("results/selected",
52+
"_", ifelse(known, "TC", "TF"),
53+
"_n", n,
54+
"_p", p,
55+
"_sparsity", sparsity,
56+
"_snr", as.character(snr),
57+
".RData"))
58+
59+
print(time)
60+

0 commit comments

Comments
 (0)