# NOT RUN {
# }
# NOT RUN {
library(mactivate)
set.seed(777)
d <- 20
N <- 50000
X <- matrix(rnorm(N*d, 0, 1), N, d)
colnames(X) <- paste0("x", I(1:d))
############# primary effect slopes
b <- rep_len( c(-1, 1), d )
ystar <-
X %*% b +
1 * (X[ , 1]) * (X[ , 2]) * (X[ , 3]) -
1 * (X[ , 2]) * (X[ , 3]) * (X[ , 4]) * (X[ , 5])
Xall <- X
errs <- rnorm(N, 0, 1)
errs <- 3 * (errs - mean(errs)) / sd(errs)
sd(errs)
y <- ystar + errs ### response
yall <- y
Nall <- N
############# hybrid example
### this control setting will exit too quickly
### compare this with example below
xcmact <-
f_control_mactivate(
param_sensitivity = 10^5,
w0_seed = 0.1,
max_internal_iter = 500,
w_col_search = "one",
ss_stop = 10^(-5),
escape_rate = 1.01,
Wadj = 1/1,
lambda = 1/1000,
tol = 10^(-5)
)
m_tot <- 4
Uall <- Xall
xxnow <- Sys.time()
xxls_out <-
f_fit_hybrid_01(
X = Xall,
y = yall,
m_tot = m_tot,
U = Uall,
m_start = 1,
mact_control = xcmact,
verbosity = 1
)
cat( difftime(Sys.time(), xxnow, units="mins"), "\n" )
yhatG <- predict(object=xxls_out, X0=Xall, U0=Uall, mcols=m_tot )
sqrt( mean( (yall - yhatG)^2 ) )
####################### this control setting should fit
####################### (will take a few minutes)
xcmact <-
f_control_mactivate(
param_sensitivity = 10^10, ### make more sensitive
w0_seed = 0.1,
max_internal_iter = 500,
w_col_search = "one",
ss_stop = 10^(-14), ### make stopping insensitive
escape_rate = 1.001, #### discourage quitting descent
Wadj = 1/1,
lambda = 1/10000,
tol = 10^(-14) ### make tolerance very small
)
m_tot <- 4
Uall <- Xall
xxnow <- Sys.time()
xxls_out <-
f_fit_hybrid_01(
X = Xall,
y = yall,
m_tot = m_tot,
U = Uall,
m_start = 1,
mact_control = xcmact,
verbosity = 1
)
cat( difftime(Sys.time(), xxnow, units="mins"), "\n" )
yhatG <- predict(object=xxls_out, X0=Xall, U0=Uall, mcols=m_tot )
sqrt( mean( (yall - yhatG)^2 ) )
xxls_out
Xstar <- f_mactivate(U=Uall, W=xxls_out[[ m_tot+1 ]][[ "What" ]])
colnames(Xstar) <- paste0("xstar_", seq(1, m_tot))
Xall <- cbind(Xall, Xstar)
xlm <- lm(yall~Xall)
summary(xlm)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab