%\VignetteIndexEntry{Installation for Users}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\hypersetup{colorlinks=true}
\usepackage{menukeys} 
\usepackage{graphicx}
\usepackage{float}
\usepackage{listings,textcomp} % for R code blocks

\title{User Installation Instructions Manual for \texttt{dynr}}
\author{Michael D. Hunter, Sy-Miin Chow,  Lu Ou, Sukruth N. Reddy,\\ Meng Chen, Lyinying Ji,\& Xiaoyue Xiong}
\date{\today}

\begin{document}
\SweaveOpts{concordance=TRUE}

\maketitle
The \texttt{dynr} software is a package for R written in R and C.  It has utilities which allow users to create C code for linear and nonlinear dynamic models, including regime-switching models, without ever having to actually write C code.  Various functions in R are called by the user to specify the model desired.  These functions in turn write code in C based on the user's input.  These functions are then compiled and shared with the rest of the \texttt{dynr} C code to estimate the model, obtaining free parameter estimates, standard errors, and latent variable estimates.



\section{\Large Instructions for Installing and Using \texttt{dynr} on a Windows Computer}
Because the \texttt{dynr} package compiles C code in response to user input, more setup is required for the \texttt{dynr} package than for many others. The general requirements are as follows:

\begin{enumerate}
\item Rtools must be installed so that C code can be compiled on Windows. See Section \ref{sec:Rtools}.
\item Additional GSL libraries must be installed so that the C code can use GSL for matrix multiplication routines. See Section \ref{sec:gsl}.
\item The environment variable for the system Path must be modified to include R and Rtools. See Section \ref{sec:rpath}.
\item An environment variable for GSL called LIB\_GSL must be created. See Section \ref{sec:libgsl}.
\end{enumerate}

Each of these steps is detailed below.  We acknowledge that this additional setup can be bothersome, but we believe the ease of use for the rest of the package and the wide variety of models it is possible to fit with it will compensate for this initial burden.  Hopefully you will agree!

\subsection{\Large Instructions for Installation of R-tools }
\label{sec:Rtools}
\begin{enumerate}
\item If you already have R-tools on your machine, make sure the version of R-tools matches with your R version. R-tools versions newer than 3.0 should work fine with \texttt{dynr}.
\item Install R-tools through \url{https://cran.r-project.org/bin/windows/Rtools/}. Install the latest version of Rtools. 
\item The following instructions are based on rtools40.
\item Click RTools 4.0, then click ``rtools40-x86\_64.exe`` in the new webpage. You may be asked if you want to save or run a file ``rrtools40-x86\_64.exe``. Choose ``Save'' and save the file on the Desktop. Then double-click on the icon for the file to run it.
\item (If you are asked what language to install it in during installation - choose English.)
\item The next page says ``Select Destination Location`` at the top. By default, it will suggest to install R in ``C:\textbackslash rtools40'' on your computer. You may also install R-tools in  {\em any other directory where there are {\bf no spaces} in the words describing the directory}. Click ``Next'' at the bottom of the R tools Setup wizard window.
\item The next page says ``Select additional tasks`` at the top. Check the box to edit the system PATH. Click ``Next'' again.
\item The next page says ``Ready to install'' at the top. Click ``install''.
\item Rtools should now be installed. This will take about a minute. Click ``Finish''.
\end{enumerate}


\subsection{\Large Instructions for Installation of GSL}
\label{sec:gsl}
\begin{enumerate}
\item If you already have GSL libraries installed on your machine, you may skip this step and go to Section \ref{sec:libgsl}.  
\item To install GSL libraries, go to \href{http://www.stats.ox.ac.uk/pub/Rtools/libs.html}{http://www.stats.ox.ac.uk/pub/Rtools/libs.html}.
\item Download ``local323.zip'' (or the latest version) by clicking it.  The 323 refers to R 3.2.3.
\item Extract it into a new folder and copy and paste the extracted new folder into the same directory as R (``C:\textbackslash'') so that there would be two folders in that directory. One containing R-3.2.3 (or whatever version of R you have installed) and another local323.
\end{enumerate}


\subsection{\Large Setting up the GSL Environment Variable}
\label{sec:libgsl}
\begin{enumerate}
 \item Open - Control Panel\textbackslash System and Security\textbackslash System
 \item Click on Advanced system settings and then click on ``Environment Variables''
 \item Add a new {\em system} variable by clicking on New.  Note that this should be a {\em System} environment variable, not a {\em User} environment variable.
 \item Name the new variable as LIB\_GSL and set the variable value to the 
 directory containing the local323 files downloaded in Section \ref{sec:gsl} or the directory where your GSL libraries are installed. Example {C:/R/local323} but please note that the direction of these slashes is important. This slash / will work but {\em not} this one \textbackslash.
\end{enumerate}



\subsection{\Large Setting up R and Rtools Environment Variable}
\label{sec:rpath}
\begin{enumerate}
 \item Check if R can be run through CMD as follows. Put a simple R script (e.g., save a simple R command: print (``hello'') to a .r document as ``Hello.r'') to a known directory. Open the command prompt window. If you can't find it just do a search from the Start Menu for ``cmd'' and open ``Command Prompt.exe''. Then change directory to the location containing ``Hello.r'' (e.g., cd C:\textbackslash myfiles). Run the script by typing ``Rscript Hello.r''. If the file runs correctly, it should print out ``hello'' in the command prompt console.
 \item Close your command prompt window.
 \item If the file did not run correctly, follow the next 4 steps.
 \item Open - Control Panel\textbackslash System and Security\textbackslash System
 \item Click on Advanced system settings
 \item Check to make sure that your path variable specified during Rtools installation is specified correctly. In Windows 10 do so by clicking on ``Environment Variables'' and then in the ``System variables'' panel click on ``Path'' and then ``Edit''. Check that the following paths have been specified at the front of your {\em system} path variable or just move them all to way up to the top in the following order.
\\
 C:\textbackslash R\textbackslash R-4.1.1\textbackslash bin\textbackslash x64\\
\texttt{C:\textbackslash rtools40\textbackslash usr\textbackslash bin\\
 C:\textbackslash rtools\textbackslash mingw64\textbackslash bin\\
 }
 \item Repeat (1) and see if you can now run R from CMD. Make sure that you open a new CMD window after you have completed the steps above. Try typing in ``PATH'' in CMD to make sure that the newly added paths are indeed shown in the output.
\end{enumerate}



\subsection{\Large Wrapping up the Installation Procedure for Windows}
\label{wrapup}
\begin{enumerate}
\item Open RGui, Rstudio or whatever editor you use to run R. Please type the following code to check whether the gsl commands can be found correctly:\\ \texttt{shell("echo \%LIB\_GSL\%")}\\ If this returns something like ``C:/R/local323'', then everything worked fine.
 \item If that command returns something like ``\%LIB\_GSL\%'', then something might be wrong with the GSL installation (Return to Section \ref{sec:gsl}) or GSL path (Return to Section \ref{sec:libgsl}).
 \item If the command worked fine, follow the steps in section \ref{sec:cran} to finish the installation process in R.
\end{enumerate}


\section{\Large Instructions for Installing and Using \texttt{dynr} On Mac}

\subsection{\Large Instructions for Installing Prerequisites on Mac}
\label{sec:macpre}
\begin{enumerate}
\item Install Xcode with the command line tools from your Apple store. For this and all the subsequent steps, please read the installation instructions on the individual websites carefully to pick the correct software versions for your operating system.
\begin{itemize}
\item \textbf{NOTE:} If you have Big Sur as your Operating System, you do not need to install the command line tools. In fact, you need to remove it by navigating to /Library/Developer and remove Command Line Tools. 
\item Then, edit /Library/Frameworks/R.framework/Resources/etc/Makeconf and remove all compilers’ explicit link to a particular OS; e.g., change "CC=clang -mmacosx-version-min=10.16" to just "CC=clang." Please see the FAQ section on the \href{https://dynrr.github.io/FAQ.html}{dynr Github webpage} for further updates.
\end{itemize}
\item Install Macports by going to \url{https://www.macports.org/install.php}
\item Open the terminal window. In the terminal window install the gsl library by typing: ``sudo port install gsl''.
\item Follow the steps in section \ref{sec:maccheck} to verify that these steps worked properly.
\end{enumerate}

\subsection{\Large Checking the Installation for Mac}
\label{sec:maccheck}
\begin{enumerate}
\item Open RGui, Rstudio or whatever editor you use to run R. Please type the following code to check whether the gsl commands can be found correctly:\\ \texttt{system("gsl-config --cflags", intern=TRUE)}\\ When the command can not be found, and you know where it is stored (e.g., "/opt/local/bin"), we could then set the PATH variable by typing: \\
\texttt{Sys.setenv(PATH=paste0(Sys.getenv("PATH"), ":", "/opt/local/bin"))}\\ and then check again.
\item If the above failed, then something went wrong with one or several of the steps in Section \ref{sec:macpre}. Please go back and try repeating or checking that section.
\item Follow the steps in section \ref{sec:cran} to finish the installation process in R.
\end{enumerate}


\section{\Large Getting \texttt{dynr} from CRAN}
\label{sec:cran}
\begin{enumerate}
\item Open RGui, Rstudio, or whatever editor you use to run R.
\item Install \texttt{dynr} from CRAN as usual by typing install.packages(`\texttt{dynr}') and then require(`\texttt{dynr}').
\item To test if \texttt{dynr} is installed correctly, run one of the demo examples. For instance, type: \\
\texttt{demo(`LinearSDE',package=`\texttt{dynr}')}\\
\texttt{Press $<$Return$>$ to start the demo.}\\
Note that this specific demo requires the packages \textit{Matrix} and \textit{mvtnorm} so make sure that these have been installed.
\item If you're on Windows and everything worked fine until you tried to run the model, something probably went wrong with installing Rtools (Section \ref{sec:Rtools}) or installing GSL (Section \ref{sec:gsl}). Please refer to those sections for troubleshooting.
\end{enumerate}


\end{document}
