\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{#Now to check the calculation using the theory above}
\hlstd{psi.prime.hat}
\end{alltt}
\begin{verbatim}
+         Z      Z:X1      Z:X2   Z:X1:X2 
+  6.761074 -1.040222  2.221214  1.583540
\end{verbatim}
\begin{alltt}
\hlstd{beta.hat}
\end{alltt}
\begin{verbatim}
+ (Intercept) 
+    4.288448
\end{verbatim}
\begin{alltt}
\hlkwd{coef}\hlstd{(fit0}\hlkwb{<-}\hlkwd{lm}\hlstd{(Y}\hlopt{~}\hlstd{Z}\hlopt{+}\hlstd{Z}\hlopt{:}\hlstd{X1}\hlopt{+}\hlstd{Z}\hlopt{:}\hlstd{X2}\hlopt{+}\hlstd{Z}\hlopt{:}\hlstd{X1}\hlopt{:}\hlstd{X2))}
\end{alltt}
\begin{verbatim}
+ (Intercept)           Z        Z:X1        Z:X2     Z:X1:X2 
+    4.288448    6.761074   -1.040222    2.221214    1.583540
\end{verbatim}
\begin{alltt}
\hlstd{alpha.hat}
\end{alltt}
\begin{verbatim}
+ (Intercept)          X1          X2       X1:X2 
+   5.3725997   1.2112540   0.1009883   3.0090512
\end{verbatim}
\begin{alltt}
\hlkwd{coef}\hlstd{(fit1}\hlkwb{<-}\hlkwd{glm}\hlstd{(Z}\hlopt{~}\hlstd{X1}\hlopt{*}\hlstd{X2,}\hlkwc{family}\hlstd{=binomial))}
\end{alltt}
\begin{verbatim}
+ (Intercept)          X1          X2       X1:X2 
+   5.3725997   1.2112540   0.1009883   3.0090512
\end{verbatim}
\begin{alltt}
\hlstd{eX}\hlkwb{<-}\hlkwd{fitted}\hlstd{(fit1)}
\hlstd{Y1}\hlkwb{<-}\hlstd{(Z}\hlopt{-}\hlstd{eX)}\hlopt{*}\hlstd{(Y}\hlopt{-}\hlstd{beta.hat)}
\hlstd{Xp}\hlkwb{<-}\hlkwd{cbind}\hlstd{(}\hlnum{1}\hlstd{,X1,X2,X1}\hlopt{*}\hlstd{X2)}
\hlstd{D}\hlkwb{<-}\hlkwd{diag}\hlstd{(Z}\hlopt{*}\hlstd{(Z}\hlopt{-}\hlstd{eX))}
\hlstd{psi.ests}\hlkwb{<-}\hlkwd{solve}\hlstd{((}\hlkwd{t}\hlstd{(Xp)} \hlopt{%*%} \hlstd{D)} \hlopt{%*%} \hlstd{Xp)} \hlopt{%*%} \hlkwd{t}\hlstd{(Xp)} \hlopt{%*%} \hlstd{Y1}
\hlkwd{cbind}\hlstd{(psi.hat,psi.ests)}  \hlcom{#Results match}
\end{alltt}
\begin{verbatim}
+           psi.hat          
+ Z       1.1247753 1.1247753
+ Z:X1    1.0759005 1.0759005
+ Z:X2    0.9522669 0.9522669
+ Z:X1:X2 1.1527323 1.1527323
\end{verbatim}
\end{kframe}
\end{knitrout}