\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\usepackage{caption}
\usepackage{amsmath, amsthm} %%%%%%%%%%%%%%% MUST BE ADDED
\usepackage{supertabular}
\usepackage{wasysym}
\usepackage{setspace}

\usepackage{Sweave}

\usepackage{tabularx}
\newcolumntype{Y}{>{\footnotesize\raggedright\arraybackslash}X}

%\singlespacing
\onehalfspacing
%\doublespacing

\usepackage{natbib}

%\usepackage{color}
%\definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0}
%\definecolor{MyDarkRed}{rgb}{0.4,0.0,0.0} 
%\usepackage[colorlinks=true, urlcolor= MyDarkGreen, linkcolor= MyDarkRed ]{hyperref}
\usepackage{hyperref}

\DeclareCaptionLabelSeparator{space}

\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}
\textwidth = 6.5 in
\textheight = 9 in
\oddsidemargin = 0.0 in
\evensidemargin = 0.0 in
\topmargin = 0.0 in
\headheight = 0.0 in
\headsep = 0.0 in
\parskip = 0.2in
\parindent = 0.0in
\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}{Definition}



\newcommand{\ve}{\varepsilon}



\newcommand{\wt}{\widetilde}
\newcommand{\wh}{\widehat}
\newcommand{\0}{\mathbf{0}}

%\newcommand{\Fv}{\mathbf{F}}
%\newcommand{\Lv}{\mathbf{L}}
%\newcommand{\Ev}{\mathbf{E}}

\newcommand{\st}{\mathrm{ \:\: s.t. \:\: }}

\newcommand{\bs}[1]{\boldsymbol{#1}}
\newcommand{\bm}[1]{\mbox{\boldmath$#1$}}
\newcommand{\mr}[1]{\mathrm{#1}}
\newcommand{\mb}[1]{\mathbf{#1}}

\newcommand{\smss}[1]{^{_{#1}}}

\newcommand{\apri}{\smss{\,(-)}}
\newcommand{\apos}{\smss{\,(+)}}

\newcommand{\betaup}{\rotatebox[origin=c]{12}{$\beta$}}

\newcommand{\ttb}{\hspace{-0.01cm}}

\newcommand{\diag}{\mathsf{diag}}
\newcommand{\minz}{\mathsf{min}}
\newcommand{\maxz}{\mathsf{max}}
\newcommand{\zsin}{\mathsf{sin}}
\newcommand{\zcos}{\mathsf{cos}}

\newcommand{\SE}{\mathsf{SE}}
\newcommand{\range}{\mathsf{range}}

\newcommand{\ndxrng}[2]{#1 \,\!\! : \,\!\! #2}

\newenvironment{DZcaption}[2]%
               {\begin{list}{}{\leftmargin#1\rightmargin#2}\item{}}%
               {\end{list}}



\begin{document}

%\VignetteIndexEntry{mactivate-tutorial-1} %% this is like a shebang
%\XVignetteDepends{mvtnorm} %% this is like a shebang
%\XVignetteDepends{maps} %% this is like a shebang


\title{Package \texttt{mactivate} \\ Tutorial I}
\author{Dave Zes}
\maketitle


\section{Simple Example}

Let's simulate a tiny data set:


<<eval=TRUE, echo=FALSE>>=
options(width=49)
options(prompt=" ")
options(continue="   ")
@



<<a1, eval=TRUE>>=

library(mactivate)
set.seed(777)

## tiny
d <- 11
N <- 3000

X <- matrix(rnorm(N*d, 1, 1), N, d)
colnames(X) <- paste0("x", I(1:d))

b <- rep_len( c(-1, 1), d )

ystar <-
X %*% b +
1/3 * X[ , 1] * X[ , 2] * X[ , 3] -
1/3 * X[ , 3] * X[ , 4] * X[ , 5] * X[ , 6] +
1/2 * X[ , 8] * X[ , 9] -
2   * X[ , 1] * X[ , 2] * X[ , 7] * X[ , 11]

xtrue_formula <- eval(parse(text="y ~ . + x1:x2:x3 + x3:x4:x5:x6 + x8:x9 + x1:x2:x7:x11"))

xnoint_formula <- eval(parse(text="y ~ ."))

errs <- rnorm(N, 0, 3)

y <- ystar + errs

Xall <- X
yall <- y
Nall <- N

dfx <- data.frame("y"=yall, Xall)
@


Let's predict from primary effects only.

<<a2, eval=TRUE>>=
xlm <- lm(y ~ . , data=dfx)
yhat <- predict(xlm, newdata=dfx)
sqrt( mean( (yall -  yhat)^2 ) )
@


Now let us predict from true model.

<<a3, eval=TRUE>>=
xlm <- lm(xtrue_formula , data=dfx)
yhat <- predict(xlm, newdata=dfx)
sqrt( mean( (yall  -  yhat)^2 ) )
@



Configure hybrid fit control.

<<a4, eval=TRUE>>=
xcmact_hybrid <-
f_control_mactivate(
param_sensitivity = 10^10,
w0_seed           = 0.1,
w_col_search      = "one",
bool_headStart    = FALSE, ### gradient
max_internal_iter = 500, ##### small -- exits automatically, don't set this too small
ss_stop           = 10^(-8), ### small
escape_rate       = 1.01,
Wadj              = 1/1,
tol               = 10^(-8)
)
@



Now fit using hybrid algorithm.

<<a5, eval=TRUE, echo=TRUE, results=hide>>=

m_tot <- 5

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_hybrid,
verbosity = 5
)

cat( difftime(Sys.time(), xxnow, units="mins"), "\n" )
@




Now predict from our hybrid fitted model.

<<a6, eval=TRUE>>=
class(xxls_out)
yhatall <- predict(object=xxls_out, X0=Xall, U0=Uall, mcols=m_tot)
sqrt( mean( (yall - yhatall)^2 ) )
@



%\bibliographystyle{plainnat}

%\bibliographystyle{jes}

%\bibliographystyle{abbrv}

%\bibliography{mactivation_01}




 \end{document}











