Error(s) in re-building vignettes:
  ...
--- re-building ‘matching.Rnw’ using knitr_notangle
\section{Monte Carlo experiments} \label{Section:MonteCarloResults}

The first part of this section presents a simple simulation study of sorting bias. The second part presents Monte Carlo evidence of the correction method proposed in \citet{Klein2015a} (see Section \ref{Section:Multi-indexSS}) and implemented in \proglang{R} package \pkg{matchingMarkets}. It further provides Monte Carlo studies on the robustness of the proposed estimator in small samples.


\subsection{A simple example}

I first provide a brief overview of the basic functionality of the \pkg{matchingMarkets} package and introduce the model specification used in the Monte Carlo experiments. 
%The required \proglang{R} packages are installed from the CRAN archive. 

\subsubsection{Individual-level data}
The \code{stabsim} function simulates individual-level, independent variables. The code below generates data for \code{m=200} markets with \code{gpm=2} groups per market and group size \code{ind=5}.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
R> ## Simulate individual-level, independent variables
R> library(matchingMarkets, quietly = TRUE)
R> idata <- stabsim(m=200, ind=5, seed=123, gpm=2)
R> head(idata)
  m.id g.id         wst  R
1    1    1 -0.56047565 NA
2    1    1 -0.23017749 NA
3    1    1  1.55870831 NA
4    1    1  0.07050839 NA
5    1    1  0.12928774 NA
6    1    2  1.71506499 NA
\end{verbatim}
\end{kframe}
\end{knitrout}
\noindent The resulting data frame contains a market and group identifiers \code{m.id} and \code{g.id} and the independent variable \code{wst} $\sim N(0,1)$. The dependent variable \code{R} depends on the error terms and is still undefined at this stage. 

\subsubsection{Group-level data}

Next we apply the function \code{stabit} that serves three purposes. 
\begin{itemize}
\item First, it specifies the list of variables to be included in \code{selection} and \code{outcome} equations and generates group-level variables based on group members' individual characteristics. For example, the operation \code{add="wst"} produces the average of variable \code{wst} for all group members. 
\item Second, if \code{simulation="NTU"}, it draws standard normal, group-level unobservables \code{eta} and \code{xi} to enter selection and outcome equation and selects equilibrium groups based on the group formation game with non-transferable utility, assuming pairwise aligned preferences as in \citet{Klein2015a}. In the case of two groups per market, this selection rule results in one dominant group with the maximum group valuation and one group comprised of the residual agents.
\item Third, the argument \code{method="model.frame"} specifies that only the group-level model matrices be generated. Other options are estimators using \code{"NTU"} for selection correction using non-transferable utility matching as selection rule or \code{"outcome"} for estimation of the outcome equation only.
\end{itemize}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
R> ## Simulate group-level variables
R> mdata <- stabit(x=idata, simulation="NTU", method="model.frame",
+                 selection = list(add="wst"), 
+                 outcome   = list(add="wst"))
\end{verbatim}
\end{kframe}
\end{knitrout}
The resulting object \code{mdata} is a list containing data for selection and outcome equations in \code{SEL} and \code{OUT}, respectively. \code{SEL} contains 252,000 rows, one for each of $5 \choose 10$ = 252 feasible groups in each of the 200 markets. A group's valuation is given by \code{V = +1*wst.add + eta}. The variable \code{D} indicates which groups are observed in equilibrium \code{D=1} and which are not \code{D=0}.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
R> head(mdata$OUT, 4)
  m.id g.id intercept    wst.add          R         xi    epsilon
1    1    1         1  0.3698086  1.5670690 -0.8497043  1.5670690
2    1    2         1 -0.2205573 -2.4953827 -1.1373036 -2.4953827
3    2    1         1  0.7198747  1.3680982 -1.1198997  1.3680982
4    2    2         1 -0.3026307  0.3707344 -0.7822018  0.3707344
\end{verbatim}
\end{kframe}
\end{knitrout}
The outcome data in \code{OUT} contains 400 rows, one for each of 2 equilibrium groups per market. The group outcome is given by \code{R = 0*wst.add + epsilon} with \code{epsilon = eta + xi}.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
R> head(mdata$SEL, 4)
  m.id g.id reg.id    wst.add D         V        eta
1    1    1      2  0.3698086 1  2.786582  2.4167734
2    1    2      1 -0.2205573 1 -1.578636 -1.3580791
3    1    3    128  0.1935703 0 -0.150347 -0.3439172
4    1    4    129  0.2221432 0  1.820652  1.5985088
\end{verbatim}
\end{kframe}
\end{knitrout}


\subsubsection{Bias from sorting}

Now, estimating the outcome equation of this model with OLS yields upward biased estimates of the slope coefficient $\beta$ (see Figure \ref{Fig:Matching-on-unobservables}a).

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
R> ## Naive OLS estimation
R> lm1 <- lm(R ~ wst.add, data=mdata$OUT)
R> summary(lm1)

Call:
lm(formula = R ~ wst.add, data = mdata$OUT)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.6304 -1.1023 -0.0448  1.2038  4.6228 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  1.32169    0.08377  15.777  < 2e-16 ***
wst.add      1.28172    0.16387   7.822 4.73e-14 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.673 on 398 degrees of freedom
Multiple R-squared:  0.1332,	Adjusted R-squared:  0.1311 
F-statistic: 61.18 on 1 and 398 DF,  p-value: 4.731e-14
\end{verbatim}
\end{kframe}
\end{knitrout}
\noindent The source of this bias is the positive correlation between \code{epsilon} and the exogenous variable \code{wst.add} (see Figure \ref{Fig:Matching-on-unobservables}b).

\begin{figure}[htbp!]
  \caption{Matching on unobservables}
  \label{Fig:Matching-on-unobservables}
  \centering
  \begin{subfigure}{.46\textwidth}
    \caption{Positive sorting bias of the independent variable}




























Quitting from lines 65-75 [predictions] (././Sections/4_MonteCarloResults.Rnw)

Quitting from lines 68-68 [MC] (matching.Rnw)
Error: processing vignette 'matching.Rnw' failed with diagnostics:
there is no package called 'ggplot2'
--- failed re-building ‘matching.Rnw’

SUMMARY: processing the following file failed:
  ‘matching.Rnw’

Error: Vignette re-building failed.
Execution halted