%\documentclass[compress,article]{beamer}
\documentclass[hyperref={colorlinks=true,urlcolor=blue,linkcolor=blue}]{beamer} 
\mode<presentation>
%\mode<article>    %does everything without the frame headers and footers

%\usepackage{beamerthemesplit} %Activate for custom appearance
\usetheme{Darmstadt}   %no footer but includes subsection headings
%\usetheme{Frankfurt}  % no footer does not include subsection headings
%\usetheme{Copenhagen}
%\usetheme{Warsaw}
%\usefonttheme[onlylarge]{structurebold}
\setbeamerfont*{frametitle}{size=\normalsize,series=\bfseries}
%\setbeameroption{previous slide on second screen =right}

%\setbeamertemplate{navigation symbols}{}
\usepackage{/Library/Frameworks/R.framework/Versions/2.7/Resources/share/texmf/Sweave}
\usepackage[authoryear,round,longnamesfirst]{natbib} 
\usepackage{amsthm}
\bibliographystyle{newapa}   %

\usepackage{tikz}
\usetikzlibrary{arrows,snakes,backgrounds}

\newcommand{\wrc}[1]{\marginpar{\textcolor{blue}{#1}}}   %bill's comments
\newcommand{\wra}[1]{\textcolor{blue}{#1}}   %bill's additions
\newcommand{\jwc}[1]{\marginpar{\textcolor{red}{#1}}}    %Josh's comments    
\newcommand{\jwa}[1]{\textcolor{red}{#1}}   %josh's additions
%
% To count the words in the text, convert to doc mode, select what is to be counted and then issue the command
%     pbpaste | wc -w     in X11 window
%

\newcommand{\pkg}[1]{{\normalfont\fontseries{b}\selectfont #1}}
\let\proglang=\textsf
\newcommand{\R}{\proglang{R}}
%\newcommand{\pkg}[1]{{\normalfont\fontseries{b}\selectfont #1}}
\newcommand{\Rfunction}[1]{{\texttt{#1}}} 
\newcommand{\fun}[1]{{\texttt{#1}\index{#1}\index{R function!#1}}}
\newcommand{\pfun}[1]{{\texttt{#1}\index{#1}\index{R function!#1}\index{R function!psych package!#1}}}\newcommand{\Rc}[1]{{\texttt{#1}}}    %R command  same as Robject
\newcommand{\Robject}[1]{{\texttt{#1}}} 
\newcommand{\Rpkg}[1]{{\textit{#1}\index{#1}\index{R package!#1}}}   %different from pkg  - which is better?
\newcommand{\iemph}[1]{{\emph{#1}\index{#1}}} 
\newcommand{\vect}[1]{\boldsymbol{#1}}

\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[frame number]


\title{Psychology 454: Latent Variable Modeling}
\author{Advanced modeling with lavaan}
\institute{Department of Psychology\\Northwestern University\\Evanston, Illinois   USA\\[10pt]
\includegraphics[width=1in, height=.6in]{/Volumes/WR/Bill/Documents/letters//NU_Logo_purple.jpg}
}

\date{\today}

\begin{document}
\frame{\titlepage}

%\section[Outline]{}

\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}

\begin{frame}
\frametitle{More examples  from MPlus manual}
\begin{itemize}
\item The \Rpkg{lavaan} introduction \url{http://users.ugent.be/~yrosseel/lavaan/lavaanIntroduction.pdf} gives examples from the MPlus User's guide \url{http://www.statmodel.com/ugexcerpts.shtml}
\begin{itemize} 
\item Regression and Path analysis
\item Growth Modeling
\item CFA and SEM
\end{itemize}
\item The data examples may be downloaded and stored in a local directory.
\item Then, set the working directory to that folder/directory, e.g.
\begin{itemize}
\item \texttt{dir <- "/Volumes/WR/bill/Downloads/chapter3"}
\item \texttt{setwd(dir)}
\end{itemize}
\end{itemize}
\end{frame}


\section{Regression and Path Analysis}
\subsection{A simple regression}
\begin{frame}[fragile]
\frametitle{Example A.1: Regression and Path Analysis}

\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}
dir <- "/Volumes/WR/bill/Downloads/chapter3"
setwd(dir)
# ex3.1
Data <- read.table("ex3.1.dat")
names(Data) <- c("y1","x1","x2")
model.ex3.1 <- ' y1 ~ x1 + x2 '
fit <- sem(model.ex3.1, data=Data)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}
\begin{Soutput}
                   Estimate  Std.err  Z-value  P(>|z|)
Regressions:
  y1 ~
    x1                0.969    0.042   23.357    0.000
    x2                0.649    0.044   14.627    0.000
Variances:
    y1                0.941    0.060   15.811    0.000
#compare to linear regression    
   > lm(y1~x1+x2,data=Data)
Call:
lm(formula = y1 ~ x1 + x2, data = Data)
Coefficients:
(Intercept)           x1           x2  
     0.5110       0.9695       0.6490  
\end{Soutput}
\end{Schunk}
\end{scriptsize}    
\end{frame}

\subsection{A more complicated regression}
\begin{frame}[fragile]
\frametitle{Example 3.11 -- 3 criteria, 3 predictors}
 \includegraphics{../figures/splom3-11.pdf}
\end{frame}



\begin{frame}[fragile]
 \frametitle{More complicated regression - 3 dependent variables}

\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}
# ex3.11
Data <- read.table("ex3.11.dat")
names(Data) <- c("y1","y2","y3",
"x1","x2","x3")
model.ex3.11 <- ' y1 + y2  + y3 ~ x1 + x2 + x3 '
fit <- sem(model.ex3.11, data=Data)
summary(fit, standardized=TRUE)
\end{Sinput}

\begin{Soutput}

\end{Soutput}
\end{Schunk}
\end{scriptsize}  
\begin{tikzpicture}
\tikzstyle{parent}=[rectangle, draw, minimum size=.6cm]
\tikzstyle{child}=[rectangle, draw, minimum size=0.6cm]

 \node (X1) at (1, 4) [parent] {$X_{1}$};
 \node (X2) at (1, 2) [parent] {$X_{2}$};
 \node (X3) at (1, 0) [parent] {$X_{3}$};
 \node (y1) at (3, 3) [child] {$Y_1$};
 \node (y2) at (3, 1) [child] {$Y_2$};
 \node (y3) at (5, 2) [child] {$Y_3$};
 \draw [->] (X1) to (y1);
 \draw [->] (X2) to (y1);
 \draw [->,] (X2) to (y2);
 \draw [->,] (X2) to (y2);
  \draw [->,] (X2) to (y3);
 
  \draw [->,] (X3) to (y2);
% \draw [->,] (y2) to (y3);
% \draw [->,] (y1) to (y3);

 \path[->] (X1) edge [bend left=20] (y3);
 \path[->] (X3) edge [bend right=20] (y3);



  \path[<->] (X1) edge [bend right=20] (X2);
 
 \path[<->] (X2) edge [bend right=30] (X3);
  \path[<->] (X1) edge [bend right=40] (X3);
\end{tikzpicture}  
\end{frame}



\begin{frame}[fragile]
 \frametitle{3 criteria, 3 predictors}

\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Regressions:
  y1 ~
    x1                0.992    0.043   22.979    0.000    0.992    0.254
    x2                2.001    0.045   44.618    0.000    2.001    0.495
    x3                3.052    0.045   68.274    0.000    3.052    0.758
  y2 ~
    x1                2.935    0.050   59.002    0.000    2.935    0.759
    x2                1.992    0.052   38.556    0.000    1.992    0.497
    x3                1.023    0.051   19.869    0.000    1.023    0.256
  y3 ~
    x1                2.672    0.071   37.819    0.000    2.672    0.507
    x2                3.544    0.073   48.301    0.000    3.544    0.649
    x3                2.318    0.073   31.686    0.000    2.318    0.426

Covariances:
  y1 ~~
    y2                0.002    0.055    0.032    0.975    0.002    0.001
    y3                0.529    0.081    6.511    0.000    0.529    0.304
  y2 ~~
    y3                1.104    0.102   10.803    0.000    1.104    0.552

Variances:
    y1                1.061    0.067   15.811    0.000    1.061    0.061
    y2                1.408    0.089   15.811    0.000    1.408    0.082
    y3                2.842    0.180   15.811    0.000    2.842    0.089

\end{Soutput}
\end{Schunk}
\end{tiny}    
\end{frame}

\begin{frame}[fragile]
 \frametitle{Compare to regression}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
> mat.regress(Data,c("x1","x2","x3"),c("y1","y2","y3"))
Call: mat.regress(m = Data, x = c("x1", "x2", "x3"), y = c("y1", "y2",  "y3"))

Multiple Regression from matrix input 

Beta weights 
     y1   y2   y3
x1 0.99 2.93 2.67
x2 2.00 1.99 3.54
x3 3.05 1.02 2.32

Multiple R 
  y1   y2   y3 
0.97 0.96 0.95 

Multiple R2 
  y1   y2   y3 
0.94 0.92 0.91 

 SE of Beta weights 
     y1   y2   y3
x1 0.04 0.05 0.07
x2 0.05 0.05 0.07
x3 0.04 0.05 0.07

 t of Beta Weights 
      y1    y2    y3
x1 22.89 58.77 37.67
x2 44.44 38.40 48.11
x3 68.00 19.79 31.56

\end{Sinput}
\begin{Soutput}

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Probability of t < 
   y1 y2 y3
x1  0  0  0
x2  0  0  0
x3  0  0  0

 Shrunken R2 
  y1   y2   y3 
0.94 0.92 0.91 

Standard Error of R2  
    y1     y2     y3 
0.0052 0.0070 0.0076 

F 
     y1      y2      y3 
2550.11 1843.07 1682.39 

Probability of F < 
y1 y2 y3 
 0  0  0 

 degrees of freedom of regression 
[1]   3 496


\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\begin{frame}[fragile]
 \frametitle{More complicated regression - a path model}

\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}
# ex3.11
Data <- read.table("ex3.11.dat")
names(Data) <- c("y1","y2","y3",
"x1","x2","x3")
model.ex3.11 <- ' y1 + y2 ~ x1 + x2 + x3
y3 ~ y1 + y2 + x2 '
fit <- sem(model.ex3.11, data=Data)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}

\begin{Soutput}

\end{Soutput}
\end{Schunk}
\end{scriptsize}  

\tikzstyle{parent}=[rectangle, draw, minimum size=.6cm]
\tikzstyle{child}=[rectangle, draw, minimum size=0.6cm]
\begin{tikzpicture}
 \node (X1) at (1, 4) [parent] {$X_{1}$};
 \node (X2) at (1, 2) [parent] {$X_{2}$};
 \node (X3) at (1, 0) [parent] {$X_{3}$};
 \node (y1) at (3, 3) [child] {$Y_1$};
 \node (y2) at (3, 1) [child] {$Y_2$};
 \node (y3) at (5, 2) [child] {$Y_3$};
 \draw [->] (X1) to (y1);
 \draw [->] (X2) to (y1);
 \draw [->,] (X2) to (y2);
 \draw [->,] (X2) to (y2);
  \draw [->,] (X2) to (y3);
 
  \draw [->,] (X3) to (y2);
 \draw [->,] (y2) to (y3);
 \draw [->,] (y1) to (y3);
  
  \path[<->] (X1) edge [bend right=20] (X2);
 
 \path[<->] (X2) edge [bend right=30] (X3);
  \path[<->] (X1) edge [bend right=40] (X3);
\end{tikzpicture}  
\end{frame}



\begin{frame}[fragile]
 \frametitle{}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
# ex3.11
\end{Sinput}
\begin{Soutput}
Lavaan (0.4-7) converged normally after 46 iterations

  Number of observations                           500

  Estimator                                         ML
  Minimum Function Chi-square                    0.757
  Degrees of freedom                                 3
  P-value                                        0.860

Chi-square test baseline model:

  Minimum Function Chi-square                 4107.449
  Degrees of freedom                                12
  P-value                                        0.000

Full model versus baseline model:
  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.002

Loglikelihood and Information Criteria:
  Loglikelihood user model (H0)              -4556.552
  Loglikelihood unrestricted model (H1)      -4556.174

  Number of free parameters                         12
  Akaike (AIC)                                9137.105
  Bayesian (BIC)                              9187.680
  Sample-size adjusted Bayesian (BIC)         9149.591

Root Mean Square Error of Approximation:
  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.040
  P-value RMSEA <= 0.05                          0.972

Standardized Root Mean Square Residual:
  SRMR                                           0.001

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
                  Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Regressions:
  y1 ~
    x1                0.992    0.043   22.979    0.000    0.992    0.254
    x2                2.001    0.045   44.618    0.000    2.001    0.495
    x3                3.052    0.045   68.274    0.000    3.052    0.758
  y2 ~
    x1                2.935    0.050   59.002    0.000    2.935    0.759
    x2                1.992    0.052   38.556    0.000    1.992    0.497
    x3                1.023    0.051   19.869    0.000    1.023    0.256
  y3 ~
    y1                0.507    0.020   25.494    0.000    0.507    0.375
    y2                0.746    0.020   37.918    0.000    0.746    0.547
    x2                1.046    0.072   14.539    0.000    1.046    0.192

Variances:
    y1                1.061    0.067   15.811    0.000    1.061    0.061
    y2                1.408    0.089   15.811    0.000    1.408    0.082
    y3                1.717    0.109   15.811    0.000    1.717    0.054
\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\begin{frame}[fragile]
 \frametitle{But this is just a more tedious regression}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
mat.regress(Data,c("x1","x2","x3"),c("y1","y2"))
\end{Sinput}
\begin{Soutput}
Call: mat.regress(m = Data, x = c("x1", "x2", "x3"),
          y = c("y1", "y2"))

Multiple Regression from matrix input 

Beta weights 
     y1   y2
x1 0.99 2.93
x2 2.00 1.99
x3 3.05 1.02

Multiple R 
  y1   y2 
0.97 0.96 

Multiple R2 
  y1   y2 
0.94 0.92 

 SE of Beta weights 
     y1   y2
x1 0.04 0.05
x2 0.05 0.05
x3 0.04 0.05

F 
     y1      y2 
2550.11 1843.07 

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
 mat.regress(Data,c("y1","y2","x2"),c("y3"))

\end{Sinput}
\begin{Soutput}
Call: mat.regress(m = Data, x = c("y1", "y2", "x2"),
            y = c("y3"))

Multiple Regression from matrix input 

Beta weights 
        y3
y1 0.51
y2 0.75
x2 1.05

Multiple R 
   y3
0.97 

Multiple R2 
<NA> 
0.95 

 SE of Beta weights 
     y3
y1 0.02
y2 0.02
x2 0.07

F 
     y3
2892.53 


\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\section{CFA and SEM}
\subsection{Example 5.1  6 variables }
\begin{frame}[fragile]
 \frametitle{First, get the examples}

\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}
#In browser, download Chapter 5 examples from \url{http://www.statmodel.com/ugexcerpts.shtml}
#Set working directory to new folder using the Misc menu or

dir    <- "/Volumes/WR/bill/Downloads/chapter5"
setwd(dir)
#show it
getwd()
[1] "/Volumes/WR/bill/Downloads/chapter5"
# ex5.1
Data <- read.table("ex5.1.dat")
names(Data) <- paste("y", 1:6, sep="")
describe(Data)
\end{Sinput}
\begin{Soutput}

   var   n  mean   sd median trimmed  mad   min  max range  skew kurtosis   se
y1   1 500 -0.02 1.41  -0.04   -0.01 1.49 -3.96 3.59  7.55 -0.05    -0.35 0.06
y2   2 500  0.03 1.40   0.04    0.04 1.35 -5.19 3.70  8.90 -0.14     0.12 0.06
y3   3 500  0.03 1.40  -0.06    0.00 1.41 -3.91 4.16  8.07  0.17    -0.12 0.06
y4   4 500 -0.02 1.43  -0.07   -0.03 1.33 -4.55 4.29  8.83 -0.02     0.26 0.06
y5   5 500 -0.02 1.31  -0.01   -0.01 1.34 -3.73 4.15  7.88 -0.04    -0.08 0.06
y6   6 500  0.05 1.30   0.02    0.02 1.34 -3.42 4.11  7.53  0.21    -0.02 0.06

\end{Soutput}
\end{Schunk}
\end{scriptsize}    
\end{frame}

\begin{frame}
\frametitle{Graph example 5.1}
\includegraphics{../figures/splom5-1.pdf}
\end{frame}


\begin{frame}[fragile]
 \frametitle{Exploratory factor analysis}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
> fa(Data,2)
Loading required package: GPArotation
Factor Analysis using method =  minres
Call: fa(r = Data, nfactors = 2)
Standardized loadings based upon correlation matrix
     MR1   MR2   h2   u2
y1  0.68  0.01 0.46 0.54
y2  0.77 -0.02 0.59 0.41
y3  0.70  0.01 0.49 0.51
y4  0.00  0.61 0.37 0.63
y5 -0.02  0.70 0.50 0.50
y6  0.04  0.61 0.37 0.63

                MR1  MR2
SS loadings    1.54 1.24
Proportion Var 0.26 0.21
Cumulative Var 0.26 0.46

 With factor correlations of 
      MR1   MR2
MR1  1.00 -0.04
MR2 -0.04  1.00


\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.58\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Test of the hypothesis that 2 factors are sufficient.

The degrees of freedom for the null model are  15  and the 
          objective function was  1.19 with Chi Square of  592.34
The degrees of freedom for the model are 4  and the 
             objective function was  0 

The root mean square of the residuals is  0.01 
The df corrected root mean square of the residuals is  0.02 
The number of observations was  500  with 
            Chi Square =  2.18  with prob <  0.7 

Tucker Lewis Index of factoring reliability =  1.012
RMSEA index =  0  and the 90 % confidence intervals are  0 0.024
BIC =  -22.68
Fit based upon off diagonal values = 1
Measures of factor score adequacy             
                                                MR1  MR2
Correlation of scores with factors             0.87 0.83
Multiple R square of scores with factors       0.76 0.68
Minimum correlation of possible factor scores  0.53 0.37

\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\begin{frame}[fragile]
 \frametitle{Confirmatory Factor Analysis is almost identical to EFA because cross loadings are trivial}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
model.ex5.1 <- ' f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6 '
fit <- cfa(model.ex5.1, data=Data,std.ov=TRUE,std.lv=TRUE)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}
\begin{Soutput}
Lavaan (0.4-7) converged normally after 16 iterations
  Number of observations                           500
  Estimator                                         ML
  Minimum Function Chi-square                    3.896
  Degrees of freedom                                 8
  P-value                                        0.866
Chi-square test baseline model:
  Minimum Function Chi-square                  596.921
  Degrees of freedom                                15
  P-value                                        0.000
Full model versus baseline model:
  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.013
Loglikelihood and Information Criteria:
  Loglikelihood user model (H0)              -3957.300
  Loglikelihood unrestricted model (H1)      -3955.352
  Number of free parameters                         13
  Akaike (AIC)                                7940.600
  Bayesian (BIC)                              7995.390
  Sample-size adjusted Bayesian (BIC)         7954.127
Root Mean Square Error of Approximation:
  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.027
  P-value RMSEA <= 0.05                          0.995
Standardized Root Mean Square Residual:
  SRMR                                           0.016


\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  f1 =~
    y1                0.678    0.047   14.508    0.000    0.678    0.678
    y2                0.768    0.047   16.244    0.000    0.768    0.769
    y3                0.694    0.047   14.832    0.000    0.694    0.695
  f2 =~
    y4                0.608    0.053   11.481    0.000    0.608    0.609
    y5                0.706    0.056   12.700    0.000    0.706    0.707
    y6                0.603    0.053   11.411    0.000    0.603    0.604

Covariances:
  f1 ~~
    f2               -0.036    0.062   -0.585    0.559   -0.036   -0.036

Variances:
    y1                0.539    0.048   11.124    0.000    0.539    0.540
    y2                0.408    0.051    7.981    0.000    0.408    0.409
    y3                0.516    0.049   10.600    0.000    0.516    0.517
    y4                0.628    0.058   10.880    0.000    0.628    0.629
    y5                0.500    0.065    7.736    0.000    0.500    0.501
    y6                0.634    0.057   11.035    0.000    0.634    0.635
    f1                1.000                               1.000    1.000
    f2                1.000                               1.000    1.000
\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\subsection{Example 5.6 12 variables, 4 correlated factors}

\begin{frame}[fragile]
 \frametitle{}

\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}
Data <- read.table("ex5.6.dat")
names(Data) <- paste("y", 1:12, sep="")
describe(Data)
pairs.panels(Data,gap=0)
\end{Sinput}
\begin{Soutput}
    var   n  mean   sd median trimmed  mad   min  max range  skew kurtosis   se
y1    1 500  0.01 1.31   0.02    0.01 1.38 -3.87 3.39  7.27 -0.04    -0.25 0.06
y2    2 500  0.03 1.02   0.01    0.03 1.01 -2.93 2.75  5.68  0.00    -0.17 0.05
y3    3 500  0.00 0.97   0.01   -0.02 1.03 -2.22 2.79  5.01  0.16    -0.36 0.04
y4    4 500  0.10 1.35   0.04    0.11 1.26 -3.91 4.19  8.11 -0.07     0.04 0.06
y5    5 500  0.08 1.01   0.11    0.09 1.00 -3.34 3.70  7.04 -0.12     0.18 0.05
y6    6 500  0.08 1.02   0.02    0.06 1.04 -2.46 2.95  5.41  0.16    -0.20 0.05
y7    7 500  0.02 1.38   0.05    0.04 1.37 -3.78 4.16  7.94 -0.06    -0.12 0.06
y8    8 500  0.03 1.04   0.08    0.03 1.04 -3.17 2.99  6.16 -0.11    -0.03 0.05
y9    9 500  0.03 1.03  -0.03    0.03 1.05 -2.98 2.92  5.90  0.04    -0.19 0.05
y10  10 500 -0.02 1.38   0.05   -0.02 1.31 -4.79 3.95  8.74  0.01    -0.04 0.06
y11  11 500  0.01 1.06   0.01    0.01 1.07 -2.62 3.46  6.08 -0.01    -0.16 0.05
y12  12 500  0.01 1.01  -0.07    0.00 1.01 -3.18 2.88  6.06  0.06    -0.07 0.05
\end{Soutput}
\end{Schunk}
\end{scriptsize}    
\end{frame}

\begin{frame}
\frametitle{Example 5.6: 12 variables, ? factors}
\includegraphics{../figures/splom-ex5-11.png}
\end{frame}

\begin{frame}
\frametitle{Parallel Analysis}
\begin{scriptsize}
\texttt{fa.parallel(Data)  \\
Parallel analysis suggests that the number of factors =  4  and the number of components =  4 }
\end{scriptsize}

\includegraphics[width=6.8cm]{../figures/wk9-parallel.pdf}
\end{frame}

\begin{frame}[fragile]
 \frametitle{Extract 4 factors using EFA}
\begin{columns}
\column{.4\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
fa(Data,4)
\end{Sinput}
\begin{Soutput}
Factor Analysis using method =  minres
Call: fa(r = Data, nfactors = 4)
Standardized loadings based upon correlation matrix
      MR1   MR2   MR4   MR3   h2   u2
y1   0.05 -0.01  0.87  0.00 0.79 0.21
y2  -0.01  0.02  0.87  0.00 0.77 0.23
y3  -0.04 -0.01  0.83  0.01 0.67 0.33
y4   0.00  0.01  0.01  0.88 0.80 0.20
y5  -0.02  0.01 -0.01  0.87 0.74 0.26
y6   0.02 -0.03  0.00  0.82 0.69 0.31
y7   0.92  0.01 -0.01  0.00 0.86 0.14
y8   0.87 -0.01 -0.02  0.01 0.74 0.26
y9   0.85  0.00  0.03 -0.01 0.74 0.26
y10  0.00  0.90 -0.03  0.02 0.81 0.19
y11 -0.01  0.88  0.00  0.00 0.76 0.24
y12  0.02  0.82  0.04 -0.03 0.68 0.32

                MR1  MR2  MR4  MR3
SS loadings    2.34 2.26 2.23 2.22
Proportion Var 0.19 0.19 0.19 0.19
Cumulative Var 0.19 0.38 0.57 0.75

 With factor correlations of 
     MR1  MR2  MR4  MR3
MR1 1.00 0.29 0.34 0.35
MR2 0.29 1.00 0.29 0.22
MR4 0.34 0.29 1.00 0.30
MR3 0.35 0.22 0.30 1.00

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.7\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Test of the hypothesis that 4 factors are sufficient.

The degrees of freedom for the null model are  66  and the 
          objective function was  8.02 with Chi Square of  3965.23
The degrees of freedom for the model are 24  and the
            objective function was  0.06 

The root mean square of the residuals is  0.01 
The df corrected root mean square of the residuals is  0.01 
The number of observations was  500  with 
                     Chi Square =  27.97  with prob <  0.26 

Tucker Lewis Index of factoring reliability =  0.997
RMSEA index =  0.019  and the 
            90 % confidence intervals are  0.019 0.023
BIC =  -121.18
Fit based upon off diagonal values = 1
Measures of factor score adequacy             
                                                MR1  MR2  MR4  MR3
Correlation of scores with factors             0.96 0.95 0.95 0.95
Multiple R square of scores with factors       0.92 0.91 0.90 0.90
Minimum correlation of possible factor scores  0.84 0.82 0.80 0.80
 
\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\begin{frame}
\frametitle{Show the 4 factor exploratory solution}
\texttt{fa.diagram(fa(Data,4),cut=.2)}
\includegraphics{../figures/wk9-ex5-11-fa.pdf}
\end{frame}

\begin{frame}[fragile]
 \frametitle{4 factors using CFA }
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
Data <- read.table("ex5.6.dat")
names(Data) <- paste("y", 1:12, sep="")
model.ex5.6 <- ' f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6
f3 =~ y7 + y8 + y9
f4 =~ y10 + y11 + y12'
fit <- cfa(model.ex5.6, data=Data, estimator="ML",
          std.ov=TRUE,std.lv=TRUE)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}
\begin{Soutput}
  Number of observations                           500

  Estimator                                         ML
  Minimum Function Chi-square                   45.780
  Degrees of freedom                                48
  P-value                                        0.564
Chi-square test baseline model:
  Minimum Function Chi-square                 4012.035
  Degrees of freedom                                66
  P-value                                        0.000
Full model versus baseline model:
  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.001

...
Root Mean Square Error of Approximation:
  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.027
  P-value RMSEA <= 0.05                          1.000
  
Standardized Root Mean Square Residual:
  SRMR                                           0.017
  
  
  
  
  
  
  
  
  
  
  
\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  f1 =~
    y1                0.892    0.037   24.413    0.000    0.892    0.893
    y2                0.872    0.037   23.576    0.000    0.872    0.872
    y3                0.811    0.038   21.238    0.000    0.811    0.811
  f2 =~
    y4                0.893    0.037   24.421    0.000    0.893    0.894
    y5                0.857    0.037   22.997    0.000    0.857    0.858
    y6                0.827    0.038   21.846    0.000    0.827    0.828
  f3 =~
    y7                0.924    0.035   26.284    0.000    0.924    0.925
    y8                0.859    0.037   23.425    0.000    0.859    0.860
    y9                0.857    0.037   23.367    0.000    0.857    0.858
  f4 =~
    y10               0.899    0.036   24.819    0.000    0.899    0.900
    y11               0.872    0.037   23.721    0.000    0.872    0.873
    y12               0.826    0.038   21.896    0.000    0.826    0.827
Covariances:
  f1 ~~
    f2                0.310    0.045    6.817    0.000    0.310    0.310
    f3                0.351    0.044    8.050    0.000    0.351    0.351
    f4                0.292    0.046    6.374    0.000    0.292    0.292
  f2 ~~
    f3                0.348    0.044    7.952    0.000    0.348    0.348
    f4                0.226    0.047    4.790    0.000    0.226    0.226
  f3 ~~
    f4                0.292    0.045    6.456    0.000    0.292    0.292

Variances:
    y1                0.202    0.024    8.288    0.000    0.202    0.202
    y2                0.238    0.025    9.548    0.000    0.238    0.239
    y3                0.341    0.028   12.261    0.000    0.341    0.342
    y4                0.201    0.024    8.232    0.000    0.201    0.202
    y5                0.263    0.026   10.305    0.000    0.263    0.264
    y6                0.314    0.027   11.657    0.000    0.314    0.315
    y7                0.143    0.020    7.094    0.000    0.143    0.144
    y8                0.261    0.023   11.401    0.000    0.261    0.261
    y9                0.263    0.023   11.468    0.000    0.263    0.264
    y10               0.190    0.023    8.122    0.000    0.190    0.190
    y11               0.237    0.024    9.804    0.000    0.237    0.237
    y12               0.316    0.026   12.002    0.000    0.316    0.317
    f1                1.000                               1.000    1.000
    f2                1.000                               1.000    1.000
    f3                1.000                               1.000    1.000
    f4                1.000                               1.000    1.000

\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\subsection{Consider a higher order structure}

\begin{frame}[fragile]
 \frametitle{Omega solution}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
om.5_6 <- omega(Data,4,sl=FALSE)
\end{Sinput}
\begin{Soutput}
Omega 
Call: omega(m = Data, nfactors = 4)
Alpha:                 0.85 
G.6:                   0.92 
Omega Hierarchical:    0.6 
Omega H asymptotic:    0.63 
Omega Total            0.95 

Schmid Leiman Factor loadings greater than  0.2 
       g   F1*   F2*   F3*   F4*   h2   u2   p2
y1  0.53                    0.72 0.79 0.21 0.35
y2  0.50                    0.71 0.77 0.23 0.33
y3  0.45                    0.68 0.67 0.33 0.30
y4  0.48              0.75       0.80 0.20 0.29
y5  0.45              0.74       0.74 0.26 0.27
y6  0.44              0.70       0.68 0.32 0.28
y7  0.57  0.72                   0.86 0.14 0.39
y8  0.53  0.68                   0.74 0.26 0.38
y9  0.54  0.67                   0.74 0.26 0.40
y10 0.42        0.80             0.82 0.18 0.21
y11 0.40        0.78             0.76 0.24 0.21
y12 0.40        0.72             0.68 0.32 0.23

With eigenvalues of:
  g F1* F2* F3* F4* 
2.8 1.4 1.8 1.6 1.5 

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.65\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Soutput}
general/max  1.56   max/min =   1.23
mean percent general =  0.3    with sd =  0.07 and cv of  0.22 

The degrees of freedom are 24  and the fit is  0.06 
The number of observations was  500  with Chi Square =  27.97  
            with prob <  0.26
The root mean square of the residuals is  0.01 
The df corrected root mean square of the residuals is  0.01
RMSEA index =  0.019  and the 90 %
                 confidence intervals are  0.019 0.023
BIC =  -121.18

Compare this with the adequacy of just a general factor and 
                  no group factors
The degrees of freedom for just the general factor are 54  
                                  and the fit is  5.33 
The number of observations was  500  with 
                      Chi Square =  2630.39  with prob <  0
The root mean square of the residuals is  0.16 
The df corrected root mean square of the residuals is  0.25 

RMSEA index =  0.311  and the 90 % confidence intervals are  0.31 0.312
BIC =  2294.8 

Measures of factor score adequacy             
                                                 g  F1* F2*  F3*  F4*
Correlation of scores with factors            0.78 0.83 0.9 0.87 0.85
Multiple R square of scores with factors      0.61 0.69 0.8 0.75 0.72
Minimum correlation of factor score estimates 0.22 0.38 0.6 0.50 0.43



\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\begin{frame}
\frametitle{Two omega solutions}
\begin{columns}
\column{.7\textwidth}
\includegraphics{../figures/wk9-om.pdf}
\column{.6\textwidth}
\includegraphics{../figures/wk9-omsl.pdf}

\end{columns}
\end{frame}

\begin{frame}[fragile]
 \frametitle{Higher order model with CFA}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
Data <- read.table("ex5.6.dat")
names(Data) <- paste("y", 1:12, sep="")
model.ex5.6 <- ' f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6
f3 =~ y7 + y8 + y9
f4 =~ y10 + y11 + y12
f5 =~ f1 + f2 + f3 + f4'
fit <- cfa(model.ex5.6, data=Data, estimator="ML",
          std.ov=TRUE,std.lv=TRUE)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}
\begin{Soutput}
 Number of observations                           500
  Estimator                                         ML
  Minimum Function Chi-square                   46.743
  Degrees of freedom                                50
  P-value                                        0.605
Chi-square test baseline model:
  Minimum Function Chi-square                 4012.035
  Degrees of freedom                                66
  P-value                                        0.000
Full model versus baseline model:
  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.001

...

Root Mean Square Error of Approximation:
  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.026
  P-value RMSEA <= 0.05                          1.000

Standardized Root Mean Square Residual:

  SRMR                                           0.020








\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.55\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  f1 =~
    y1                0.727    0.041   17.731    0.000    0.893    0.894
    y2                0.709    0.041   17.433    0.000    0.871    0.872
    y3                0.660    0.040   16.439    0.000    0.810    0.811
  f2 =~
    y4                0.755    0.039   19.212    0.000    0.893    0.894
    y5                0.725    0.039   18.553    0.000    0.857    0.858
    y6                0.699    0.039   17.932    0.000    0.827    0.828
  f3 =~
    y7                0.721    0.044   16.557    0.000    0.924    0.925
    y8                0.670    0.042   15.845    0.000    0.859    0.859
    y9                0.669    0.042   15.829    0.000    0.857    0.858
  f4 =~
    y10               0.795    0.038   21.154    0.000    0.899    0.900
    y11               0.771    0.038   20.491    0.000    0.872    0.873
    y12               0.730    0.038   19.283    0.000    0.825    0.826
  f5 =~
    f1                0.713    0.098    7.270    0.000    0.580    0.580
    f2                0.632    0.088    7.192    0.000    0.534    0.534
    f3                0.802    0.111    7.214    0.000    0.626    0.626
    f4                0.528    0.078    6.785    0.000    0.467    0.467

Variances:
    y1                0.201    0.024    8.252    0.000    0.201    0.202
    y2                0.239    0.025    9.570    0.000    0.239    0.240
    y3                0.341    0.028   12.265    0.000    0.341    0.342
    y4                0.201    0.024    8.221    0.000    0.201    0.201
    y5                0.263    0.026   10.298    0.000    0.263    0.264
    y6                0.314    0.027   11.666    0.000    0.314    0.315
    y7                0.144    0.020    7.096    0.000    0.144    0.144
    y8                0.261    0.023   11.404    0.000    0.261    0.261
    y9                0.263    0.023   11.462    0.000    0.263    0.263
    y10               0.189    0.023    8.087    0.000    0.189    0.189
    y11               0.237    0.024    9.812    0.000    0.237    0.238
    y12               0.317    0.026   12.014    0.000    0.317    0.317
    f1                1.000                               0.663    0.663
    f2                1.000                               0.714    0.714
    f3                1.000                               0.609    0.609
    f4                1.000                               0.782    0.782
    f5                1.000                               1.000    1.000

\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\begin{frame}[fragile]
 \frametitle{Try a Schmid Leiman model}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
Data <- read.table("ex5.6.dat")
names(Data) <- paste("y", 1:12, sep="")
model.ex5.6 <- ' f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6
f3 =~ y7 + y8 + y9
f4 =~ y10 + y11 + y12
g =~  y1 +  y2 + y3 +  y4 + y5 + y6 + y7 + 
                  y8 + y9 + y10 + y11 + y12'
fit <- cfa(model.ex5.6, data=Data, estimator="ML",
          std.ov=TRUE,std.lv=TRUE,orthogonal=TRUE)
          
\end{Sinput}
\begin{Soutput}
Number of observations                           500

  Estimator                                         ML
  Minimum Function Chi-square                   41.640
  Degrees of freedom                                42
  P-value                                        0.487

Chi-square test baseline model:

  Minimum Function Chi-square                 4012.035
  Degrees of freedom                                66
  P-value                                        0.000

Full model versus baseline model:

  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.000

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -6522.428
  Loglikelihood unrestricted model (H1)      -6501.608

  Number of free parameters                         36
  Akaike (AIC)                               13116.855
  Bayesian (BIC)                             13268.581
  Sample-size adjusted Bayesian (BIC)        13154.315

Root Mean Square Error of Approximation:

  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.030
  P-value RMSEA <= 0.05                          1.000

Standardized Root Mean Square Residual:

  SRMR                                           0.018


\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
                  Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  f1 =~
    y1                0.704    0.045   15.768    0.000    0.704    0.705
    y2                0.709    0.045   15.887    0.000    0.709    0.710
    y3                0.694    0.045   15.314    0.000    0.694    0.695
  f2 =~
    y4                0.752    0.042   18.112    0.000    0.752    0.753
    y5                0.742    0.042   17.667    0.000    0.742    0.743
    y6                0.705    0.043   16.453    0.000    0.705    0.705
  f3 =~
    y7                0.713    0.047   15.248    0.000    0.713    0.714
    y8                0.679    0.048   14.207    0.000    0.679    0.680
    y9                0.646    0.049   13.211    0.000    0.646    0.646
  f4 =~
    y10               0.798    0.039   20.266    0.000    0.798    0.799
    y11               0.781    0.040   19.546    0.000    0.781    0.782
    y12               0.718    0.041   17.473    0.000    0.718    0.719
  g =~
    y1                0.543    0.057    9.490    0.000    0.543    0.544
    y2                0.508    0.058    8.798    0.000    0.508    0.508
    y3                0.431    0.059    7.356    0.000    0.431    0.431
    y4                0.476    0.056    8.448    0.000    0.476    0.476
    y5                0.435    0.057    7.644    0.000    0.435    0.435
    y6                0.433    0.057    7.618    0.000    0.433    0.434
    y7                0.588    0.059   10.039    0.000    0.588    0.588
    y8                0.528    0.059    8.898    0.000    0.528    0.529
    y9                0.563    0.059    9.565    0.000    0.563    0.564
    y10               0.414    0.056    7.374    0.000    0.414    0.415
    y11               0.392    0.056    6.944    0.000    0.392    0.392
    y12               0.405    0.056    7.201    0.000    0.405    0.406

Covariances:
  f1 ~~
    f2                0.000                               0.000    0.000
    f3                0.000                               0.000    0.000
    f4                0.000                               0.000    0.000
    g                 0.000                               0.000    0.000
  f2 ~~
    f3                0.000                               0.000    0.000
    f4                0.000                               0.000    0.000
    g                 0.000                               0.000    0.000
  f3 ~~
    f4                0.000                               0.000    0.000
    g                 0.000                               0.000    0.000
  f4 ~~
    g                 0.000                               0.000    0.000


\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\begin{frame}[fragile]
 \frametitle{Compare the omega solution with cfa higher order model}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Schmid Leiman Factor loadings greater than  0.2 
       g   F1*   F2*   F3*   F4*   h2   u2   p2
y1  0.53                    0.72 0.79 0.21 0.35
y2  0.50                    0.71 0.77 0.23 0.33
y3  0.45                    0.68 0.67 0.33 0.30
y4  0.48              0.75       0.80 0.20 0.29
y5  0.45              0.74       0.74 0.26 0.27
y6  0.44              0.70       0.68 0.32 0.28
y7  0.57  0.72                   0.86 0.14 0.39
y8  0.53  0.68                   0.74 0.26 0.38
y9  0.54  0.67                   0.74 0.26 0.40
y10 0.42        0.80             0.82 0.18 0.21
y11 0.40        0.78             0.76 0.24 0.21
y12 0.40        0.72             0.68 0.32 0.23

With eigenvalues of:
  g F1* F2* F3* F4* 
2.8 1.4 1.8 1.6 1.5 

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Latent variables:
  f1 =~
    y1                0.704    0.045   15.768    0.000    0.704    0.705
    y2                0.709    0.045   15.887    0.000    0.709    0.710
    y3                0.694    0.045   15.314    0.000    0.694    0.695
  f2 =~
    y4                0.752    0.042   18.112    0.000    0.752    0.753
    y5                0.742    0.042   17.667    0.000    0.742    0.743
    y6                0.705    0.043   16.453    0.000    0.705    0.705
  f3 =~
    y7                0.713    0.047   15.248    0.000    0.713    0.714
    y8                0.679    0.048   14.207    0.000    0.679    0.680
    y9                0.646    0.049   13.211    0.000    0.646    0.646
  f4 =~
    y10               0.798    0.039   20.266    0.000    0.798    0.799
    y11               0.781    0.040   19.546    0.000    0.781    0.782
    y12               0.718    0.041   17.473    0.000    0.718    0.719
  g =~
    y1                0.543    0.057    9.490    0.000    0.543    0.544
    y2                0.508    0.058    8.798    0.000    0.508    0.508
    y3                0.431    0.059    7.356    0.000    0.431    0.431
    y4                0.476    0.056    8.448    0.000    0.476    0.476
    y5                0.435    0.057    7.644    0.000    0.435    0.435
    y6                0.433    0.057    7.618    0.000    0.433    0.434
    y7                0.588    0.059   10.039    0.000    0.588    0.588
    y8                0.528    0.059    8.898    0.000    0.528    0.529
    y9                0.563    0.059    9.565    0.000    0.563    0.564
    y10               0.414    0.056    7.374    0.000    0.414    0.415
    y11               0.392    0.056    6.944    0.000    0.392    0.392
    y12               0.405    0.056    7.201    0.000    0.405    0.406

\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\subsection{Latent variables with covariates  -- Multiple Indicators - Multiple Causes (MIMIC)}

\begin{frame}[fragile]
\frametitle{A MIMIC model}
Consider the case of 6 variables with two latent factors that have 3 covariates (MPlus Example 5.8)
\begin{columns}
\column{.5\textwidth}
\begin{tikzpicture}
\tikzstyle{parent}=[circle, draw, minimum size=.6cm]
\tikzstyle{child}=[rectangle, draw, minimum size=0.6cm]

 \node (X1) at (1, 4) [child] {$X_{1}$};
 \node (X2) at (1, 2) [child] {$X_{2}$};
 \node (X3) at (1, 0) [child] {$X_{3}$};
 \node (f1) at (3, 3) [parent] {$f_1$};
 \node (f2) at (3, 1) [parent] {$f_2$};
 \node (y1) at (5, 4.5) [child] {$Y_1$};
 \node (y2) at (5, 3.6) [child] {$Y_2$};
  \node (y3) at (5, 2.7) [child] {$Y_3$};
 \node (y4) at (5, 1.8) [child] {$Y_4$};
  \node (y5) at (5, .9) [child] {$Y_5$};
 \node (y6) at (5, 0) [child] {$Y_6$};
 \draw [->] (X1) to (f1);
 \draw [->] (X2) to (f1);
 \draw [->,] (X1) to (f2);
 \draw [->,] (X2) to (f2);
  \draw [->,] (X3) to (f1);
 \draw [->,] (X3) to (f2);
   \draw [->,] (f1) to (y1);
   \draw [->,] (f1) to (y2);
  \draw [->,] (f1) to (y3);
 
 \draw [->,] (f2) to (y4);
   \draw [->,] (f2) to (y5);
  \draw [->,] (f2) to (y6);
  
% \draw [->,] (y2) to (y3);
% \draw [->,] (y1) to (y3);
\path[<->] (f1) edge [bend right=20] (f2); 




 \path[<->] (X1) edge [bend right=20] (X2); 
 \path[<->] (X2) edge [bend right=30] (X3);
 \path[<->] (X1) edge [bend right=40] (X3);
\end{tikzpicture}  
\column{.5\textwidth}
\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}
Data <- read.table("ex5.8.dat")
names(Data) <- c(paste("y", 1:6, sep=""), 
          paste("x", 1:3, sep=""))
model.ex5.8 <- ' f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6
f1 + f2 ~ x1 + x2 + x3 '
fit <- cfa(model.ex5.8, data=Data, 
           estimator="ML",std.lv=TRUE)
summary(fit, standardized=TRUE, 
                 fit.measures=TRUE)
\end{Sinput}
\end{Schunk}
\end{scriptsize}
\end{columns}
\end{frame}

\begin{frame}
\frametitle{The MIMIC correlations 2 - factors}
\includegraphics{../figures/wk9mimic.png}
\end{frame}


\begin{frame}[fragile]
 \frametitle{A MIMIC model}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Lavaan (0.4-7) converged normally after 27 iterations

  Number of observations                           500

  Estimator                                         ML
  Minimum Function Chi-square                   20.023
  Degrees of freedom                                20
  P-value                                        0.457

Chi-square test baseline model:

  Minimum Function Chi-square                 4176.601
  Degrees of freedom                                33
  P-value                                        0.000

Full model versus baseline model:

  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.000

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -6585.177
  Loglikelihood unrestricted model (H1)      -6575.166

  Number of free parameters                         19
  Akaike (AIC)                               13208.355
  Bayesian (BIC)                             13288.432
  Sample-size adjusted Bayesian (BIC)        13228.125

Root Mean Square Error of Approximation:

  RMSEA                                          0.002
  90 Percent Confidence Interval          0.000  0.038
  P-value RMSEA <= 0.05                          0.994

Standardized Root Mean Square Residual:

  SRMR                                           0.008

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  f1 =~
    y1                0.849    0.036   23.407    0.000    1.643    0.914
    y2                0.876    0.037   23.627    0.000    1.696    0.921
    y3                0.867    0.037   23.733    0.000    1.679    0.925
  f2 =~
    y4                0.802    0.035   23.067    0.000    1.748    0.922
    y5                0.780    0.033   23.504    0.000    1.700    0.937
    y6                0.783    0.034   23.046    0.000    1.707    0.921

Regressions:
  f1 ~
    x1                0.585    0.037   15.786    0.000    0.302    0.517
    x2                0.667    0.043   15.414    0.000    0.345    0.497
    x3                0.814    0.058   13.950    0.000    0.420    0.429
  f2 ~
    x1                0.846    0.045   18.783    0.000    0.388    0.663
    x2                0.750    0.046   16.237    0.000    0.344    0.496
    x3                0.539    0.054    9.975    0.000    0.247    0.252

Covariances:
  f1 ~~
    f2                0.336    0.051    6.621    0.000    0.336    0.336

Variances:
    y1                0.529    0.046   11.505    0.000    0.529    0.164
    y2                0.512    0.046   11.056    0.000    0.512    0.151
    y3                0.477    0.044   10.806    0.000    0.477    0.145
    y4                0.541    0.046   11.678    0.000    0.541    0.151
    y5                0.399    0.038   10.490    0.000    0.399    0.121
    y6                0.522    0.044   11.722    0.000    0.522    0.152
    f1                1.000                               0.267    0.267
    f2                1.000                               0.210    0.210

\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\begin{frame}[fragile]
 \frametitle{Consider the two factor measurement model }
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
f2 <- fa(Data[1:6],2)
\end{Sinput}
\begin{Soutput}
Factor Analysis using method =  minres
Call: fa(r = Data[1:6], nfactors = 2)
Standardized loadings based 
         upon correlation matrix
     MR1   MR2   h2   u2
y1 -0.01  0.93 0.84 0.16
y2  0.00  0.93 0.85 0.15
y3  0.03  0.90 0.85 0.15
y4  0.88  0.05 0.85 0.15
y5  0.99 -0.05 0.90 0.10
y6  0.89  0.03 0.84 0.16

                MR1  MR2
SS loadings    2.56 2.56
Proportion Var 0.43 0.43
Cumulative Var 0.43 0.85

 With factor correlations of 
     MR1  MR2
MR1 1.00 0.81
MR2 0.81 1.00

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.6\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Test of the hypothesis that 2 factors are sufficient.

The degrees of freedom for the null model are  15  and the 
       objective function  was  6.59 with Chi Square of  3270.23
The degrees of freedom for the model are 4  and the
                         objective function was  0.01 

The root mean square of the residuals is  0 
The df corrected root mean square of the residuals is  0.01 
The number of observations was  500  with 
                              Chi Square =  3.33  with prob <  0.5 

Tucker Lewis Index of factoring reliability =  1.001
RMSEA index =  0  and the 90 % 
                   confidence intervals are  0 0.024
BIC =  -21.53
Fit based upon off diagonal values = 1
Measures of factor score adequacy             
                                                MR1  MR2
Correlation of scores with factors             0.98 0.97
Multiple R square of scores with factors       0.95 0.95
Minimum correlation of possible factor scores  0.91 0.90
\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 

Note the drastic difference in correlations from the two factor model versus the MIMIC model.
\end{frame}

\subsection{More interesting SEMs}

\begin{frame}[fragile]
 \frametitle{Consider the following 12 variables}

\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}
> Data <- read.table("ex5.11.dat")
> names(Data) <- paste("y", 1:12, sep="")

> round(cor(Data),2)
\end{Sinput}
\begin{Soutput}

       y1    y2    y3    y4    y5    y6   y7   y8   y9  y10  y11  y12
y1   1.00  0.52  0.41 -0.02  0.00 -0.02 0.27 0.24 0.26 0.11 0.14 0.13
y2   0.52  1.00  0.52  0.01 -0.03  0.00 0.23 0.18 0.28 0.14 0.15 0.18
y3   0.41  0.52  1.00  0.02 -0.11 -0.01 0.16 0.17 0.20 0.09 0.08 0.12
y4  -0.02  0.01  0.02  1.00  0.44  0.39 0.34 0.34 0.30 0.16 0.12 0.08
y5   0.00 -0.03 -0.11  0.44  1.00  0.42 0.28 0.27 0.25 0.15 0.09 0.08
y6  -0.02  0.00 -0.01  0.39  0.42  1.00 0.30 0.31 0.34 0.11 0.13 0.12
y7   0.27  0.23  0.16  0.34  0.28  0.30 1.00 0.56 0.55 0.32 0.27 0.27
y8   0.24  0.18  0.17  0.34  0.27  0.31 0.56 1.00 0.53 0.28 0.25 0.23
y9   0.26  0.28  0.20  0.30  0.25  0.34 0.55 0.53 1.00 0.29 0.24 0.29
y10  0.11  0.14  0.09  0.16  0.15  0.11 0.32 0.28 0.29 1.00 0.42 0.31
y11  0.14  0.15  0.08  0.12  0.09  0.13 0.27 0.25 0.24 0.42 1.00 0.33
y12  0.13  0.18  0.12  0.08  0.08  0.12 0.27 0.23 0.29 0.31 0.33 1.00
\end{Soutput}
\end{Schunk}
\end{scriptsize}    
\end{frame}

\begin{frame}
\frametitle{12 variables, how many factors?}
fa.parallel(Data)

\includegraphics[width=8cm]{../figures/wk9-parallel12.pdf}
Parallel analysis suggests that the number of factors =  4  and the number of components =  3 
\end{frame}

\begin{frame}[fragile]
 \frametitle{Try 3 factors}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
f3 <- fa(Data,3)
\end{Sinput}
\begin{Soutput}
> f3 <- fa(Data,3)
> f3
Factor Analysis using method =  minres
Call: fa(r = Data, nfactors = 3)
Standardized loadings based upon correlation matrix
      MR1   MR2   MR3   h2   u2
y1   0.03  0.68 -0.02 0.46 0.54
y2  -0.02  0.76  0.00 0.57 0.43
y3  -0.03  0.67 -0.06 0.42 0.58
y4   0.65 -0.09 -0.07 0.38 0.62
y5   0.62 -0.15 -0.08 0.35 0.65
y6   0.63 -0.10 -0.07 0.36 0.64
y7   0.54  0.18  0.21 0.53 0.47
y8   0.55  0.17  0.16 0.48 0.52
y9   0.53  0.24  0.16 0.51 0.49
y10  0.03 -0.06  0.63 0.39 0.61
y11 -0.04 -0.05  0.66 0.39 0.61
y12  0.02  0.04  0.49 0.26 0.74

                MR1  MR2  MR3
SS loadings    2.17 1.68 1.27
Proportion Var 0.18 0.14 0.11
Cumulative Var 0.18 0.32 0.43

 With factor correlations of 
     MR1  MR2  MR3
MR1 1.00 0.17 0.43
MR2 0.17 1.00 0.37
MR3 0.43 0.37 1.00

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.6\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Test of the hypothesis that 3 factors are sufficient.

The degrees of freedom for the null model are  66  and the
          objective function was  3.05 with Chi Square of  1506.62
The degrees of freedom for the model are 33  and the 
                 objective function was  0.17 

The root mean square of the residuals is  0.02 
The df corrected root mean square of the residuals is  0.04 
The number of observations was  500  with 
                               Chi Square =  81.42  with prob <  5.7e-06 

Tucker Lewis Index of factoring reliability =  0.932
RMSEA index =  0.055  and the 90 % confidence intervals are  0.054 0.056
BIC =  -123.66
Fit based upon off diagonal values = 0.99
Measures of factor score adequacy             
                                                MR1  MR2  MR3
Correlation of scores with factors             0.89 0.88 0.83
Multiple R square of scores with factors       0.80 0.77 0.70
Minimum correlation of possible factor scores  0.59 0.55 0.39

\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\begin{frame}[fragile]
 \frametitle{Try 4 factor solution}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
> f4 <- fa(Data,4)
> f4
\end{Sinput}
\begin{Soutput}

Factor Analysis using method =  minres
Call: fa(r = Data, nfactors = 4)
Standardized loadings based upon correlation matrix
      MR1   MR2   MR4   MR3   h2   u2
y1   0.21  0.56 -0.11 -0.04 0.43 0.57
y2  -0.07  0.85  0.05  0.04 0.69 0.31
y3   0.07  0.61 -0.07 -0.04 0.40 0.60
y4   0.12  0.00  0.58 -0.02 0.42 0.58
y5  -0.05  0.01  0.71  0.01 0.47 0.53
y6   0.11 -0.01  0.55 -0.01 0.38 0.62
y7   0.72  0.00  0.03  0.06 0.59 0.41
y8   0.74 -0.04  0.02  0.00 0.55 0.45
y9   0.62  0.10  0.08  0.04 0.53 0.47
y10  0.06 -0.02  0.02  0.60 0.40 0.60
y11 -0.03  0.00 -0.01  0.68 0.44 0.56
y12  0.11  0.05 -0.04  0.44 0.26 0.74

                MR1  MR2  MR4  MR3
SS loadings    1.75 1.48 1.25 1.08
Proportion Var 0.15 0.12 0.10 0.09
Cumulative Var 0.15 0.27 0.37 0.46

 With factor correlations of 
     MR1   MR2   MR4  MR3
MR1 1.00  0.38  0.54 0.52
MR2 0.38  1.00 -0.07 0.26
MR4 0.54 -0.07  1.00 0.26
MR3 0.52  0.26  0.26 1.00

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.6\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Test of the hypothesis that 4 factors are sufficient.

The degrees of freedom for the null model are  66  and the 
                 objective function was  3.05 with Chi Square of  1506.62
The degrees of freedom for the model are 24  and 
                       the objective function was  0.06 

The root mean square of the residuals is  0.01 
The df corrected root mean square of the residuals is  0.03 
The number of observations was  500  with 
                   Chi Square =  27.21  with prob <  0.29 

Tucker Lewis Index of factoring reliability =  0.994
RMSEA index =  0.017  and the 90 % 
                     confidence intervals are  0.017 0.021
BIC =  -121.94
Fit based upon off diagonal values = 1
Measures of factor score adequacy             
                                                MR1  MR2  MR4  MR3
Correlation of scores with factors             0.90 0.89 0.85 0.82
Multiple R square of scores with factors       0.81 0.79 0.72 0.67
Minimum correlation of possible factor scores  0.63 0.58 0.44 0.35
\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\begin{frame}
\frametitle{4 latent variables -- somewhat correlated}
diagram(f4,cut=.3)
\includegraphics{../figures/wk9-f4.pdf}
\end{frame}

\begin{frame}[fragile]
\frametitle{Four latent variables with a particular structure}
\begin{columns}
\column{.4\textwidth}
\begin{tikzpicture}
\tikzstyle{parent}=[circle, draw, minimum size=.6cm]
\tikzstyle{child}=[rectangle, draw, minimum size=0.6cm]

 \node (y1) at (1, 5) [child] {$Y_{1}$};
 \node (y2) at (1, 4) [child] {$Y_{2}$};
 \node (y3) at (1, 3) [child] {$Y_{3}$};
  \node (y4) at (1, 2) [child] {$Y_{4}$};
 \node (y5) at (1, 1) [child] {$Y_{5}$};
 \node (y6) at (1, 0) [child] {$Y_{6}$};
 
  \node (y7) at (4, 6) [child] {$Y_{7}$};
 \node (y8) at (5, 6) [child] {$Y_{8}$};
 \node (y9) at (6, 6) [child] {$Y_{9}$};
  \node (y10) at (7, 6) [child] {$Y_{10}$};
 \node (y11) at (8, 6) [child] {$Y_{11}$};
 \node (y12) at (9, 6) [child] {$Y_{12}$};
 
 \node (f1) at (3, 4) [parent] {$f_1$};
 \node (f2) at (3, 1) [parent] {$f_2$};
  \node (f3) at (5, 2.5) [parent] {$f_3$};
 \node (f4) at (8, 2.5) [parent] {$f_4$};
 
 \draw [->] (f1) to (y1);
  \draw [->] (f1) to (y2);
  \draw [->] (f1) to (y3);
  
  \draw [->] (f1) to (f3);
  \draw [->] (f2) to (f3);
  
 \draw [->] (f2) to (y4);
 \draw [->] (f2) to (y5);
  \draw [->] (f2) to (y6);
  
   \draw [->] (f3) to (y7);
  \draw [->] (f3) to (y8);
  \draw [->] (f3) to (y9);
    \draw [->] (f3) to (f4);
  
 \draw [->] (f4) to (y10);
 \draw [->] (f4) to (y11);
  \draw [->] (f4) to (y12);

  
  
% \draw [->,] (y2) to (y3);
% \draw [->,] (y1) to (y3);
\path[<->] (f1) edge [bend right=20] (f2); 




\end{tikzpicture}  

\column{.5\textwidth}


\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}














model.ex5.11 <- ' 
f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6
f3 =~ y7 + y8 + y9
f4 =~ y10 + y11 + y12
f3 ~ f1 + f2
f4 ~ f3 '

\end{Sinput}
\end{Schunk}
\end{scriptsize}
\end{columns}
\end{frame}


\begin{frame}[fragile]
 \frametitle{Fit the sem model}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
fit <- sem(model.ex5.11, data=Data, estimator="ML",
                                    std.ov=TRUE,std.lv=TRUE)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}
\begin{Soutput}
Number of observations                           500

  Estimator                                         ML
  Minimum Function Chi-square                   53.704
  Degrees of freedom                                50
  P-value                                        0.334

Chi-square test baseline model:

  Minimum Function Chi-square                 1524.403
  Degrees of freedom                                66
  P-value                                        0.000

Full model versus baseline model:

  Comparative Fit Index (CFI)                    0.997
  Tucker-Lewis Index (TLI)                       0.997

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -7772.275
  Loglikelihood unrestricted model (H1)      -7745.424

  Number of free parameters                         28
  Akaike (AIC)                               15600.551
  Bayesian (BIC)                             15718.560
  Sample-size adjusted Bayesian (BIC)        15629.686

Root Mean Square Error of Approximation:

  RMSEA                                          0.012
  90 Percent Confidence Interval          0.000  0.032
  P-value RMSEA <= 0.05                          1.000

Standardized Root Mean Square Residual:

  SRMR                                           0.029










\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  f1 =~
    y1                0.678    0.046   14.621    0.000    0.678    0.679
    y2                0.779    0.046   16.826    0.000    0.779    0.780
    y3                0.637    0.047   13.680    0.000    0.637    0.637
  f2 =~
    y4                0.659    0.048   13.706    0.000    0.659    0.660
    y5                0.643    0.048   13.349    0.000    0.643    0.644
    y6                0.632    0.048   13.107    0.000    0.632    0.633
  f3 =~
    y7                0.487    0.040   12.109    0.000    0.766    0.766
    y8                0.459    0.039   11.769    0.000    0.722    0.723
    y9                0.468    0.039   11.884    0.000    0.735    0.736
  f4 =~
    y10               0.518    0.048   10.806    0.000    0.645    0.645
    y11               0.502    0.047   10.636    0.000    0.624    0.625
    y12               0.418    0.045    9.298    0.000    0.521    0.521

Regressions:
  f3 ~
    f1                0.713    0.102    6.962    0.000    0.454    0.454
    f2                1.004    0.127    7.906    0.000    0.639    0.639
  f4 ~
    f3                0.471    0.066    7.128    0.000    0.595    0.595

Covariances:
  f1 ~~
    f2               -0.034    0.062   -0.546    0.585   -0.034   -0.034

Variances:
    y1                0.538    0.048   11.236    0.000    0.538    0.539
    y2                0.391    0.049    7.900    0.000    0.391    0.392
    y3                0.593    0.048   12.266    0.000    0.593    0.594
    y4                0.563    0.051   11.097    0.000    0.563    0.564
    y5                0.585    0.051   11.497    0.000    0.585    0.586
    y6                0.599    0.051   11.747    0.000    0.599    0.600
    y7                0.412    0.038   10.800    0.000    0.412    0.413
    y8                0.477    0.040   11.976    0.000    0.477    0.478
    y9                0.458    0.039   11.658    0.000    0.458    0.459
    y10               0.582    0.057   10.178    0.000    0.582    0.583
    y11               0.609    0.057   10.751    0.000    0.609    0.610
    y12               0.727    0.056   12.933    0.000    0.727    0.728
    f1                1.000                               1.000    1.000
    f2                1.000                               1.000    1.000
    f3                1.000                               0.405    0.405
    f4                1.000                               0.646    0.646

\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\begin{frame}
\frametitle{A SEM diagram using lavaan.diagram (from psych)}
\includegraphics[width=12cm]{../figures/wk9-lv5-11.pdf}
\end{frame}

\subsection{MIMIC with multiple groups}

\begin{frame}
\frametitle{Multiple group models can improve power}
\begin{itemize}
\item Can improve power by ``borrowing strength'' from other samples
\item Consider the case of figural invariance across groups
\begin{itemize} 
\item Items have same loadings on latent variables
\item Latent variable correlations differ across groups
\item Borrow strength in fixing figural invariance, and then study the difference in latent correlations
\end{itemize}

\end{itemize}
\end{frame}

\begin{frame}[fragile]
 \frametitle{Consider the MIMIC model of Example 5.8, but with a grouping variable }
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
Data <- read.table("ex5.14.dat")
names(Data) <- c("y1","y2","y3","y4","y5","y6", 
                     "x1","x2","x3", "g")
model.ex5.14 <- ' f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6
f1 + f2 ~ x1 + x2 + x3 '
fit <- cfa(model.ex5.14, data=Data, group="g", 
   meanstructure=FALSE,group.equal=c("loadings"),
          group.partial=c("f1=~y3"),std.ov=TRUE)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}
\begin{Soutput}
Lavaan (0.4-7) converged normally after 47 iterations

  Number of observations per group         
  1                                                500
  2                                                600
  Estimator                                         ML
  Minimum Function Chi-square                   39.132
  Degrees of freedom                                45
  P-value                                        0.718
Chi-square for each group:
  1                                             20.522
  2                                             18.610
Chi-square test baseline model:
  Minimum Function Chi-square                 8731.537
  Degrees of freedom                                72
  P-value                                        0.000
Full model versus baseline model:
  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.001
Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -9685.163
  Loglikelihood unrestricted model (H1)      -9665.597

  Number of free parameters                         33
  Akaike (AIC)                               19436.325
  Bayesian (BIC)                             19601.426
  Sample-size adjusted Bayesian (BIC)        19496.611

Root Mean Square Error of Approximation:

  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.022
  P-value RMSEA <= 0.05                          1.000

Standardized Root Mean Square Residual:

  SRMR                                           0.009









\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
Group 1 [1]:

                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  f1 =~
    y1                0.480    0.014   33.915    0.000    0.920    0.916
    y2                0.481    0.014   34.007    0.000    0.921    0.921
    y3                0.483    0.016   29.612    0.000    0.926    0.925
  f2 =~
    y4                0.423    0.012   34.369    0.000    0.918    0.921
    y5                0.431    0.012   35.073    0.000    0.934    0.937
    y6                0.429    0.012   34.882    0.000    0.930    0.923

Regressions:
  f1 ~
    x1                0.988    0.057   17.388    0.000    0.515    0.515
    x2                0.951    0.056   16.886    0.000    0.496    0.496
    x3                0.821    0.055   14.979    0.000    0.428    0.428
  f2 ~
    x1                1.438    0.064   22.624    0.000    0.663    0.662
    x2                1.076    0.058   18.504    0.000    0.496    0.496
    x3                0.547    0.053   10.398    0.000    0.252    0.252

Covariances:
  f1 ~~
    f2                0.339    0.050    6.787    0.000    0.339    0.339

Variances:
    y1                0.163    0.014   11.538    0.000    0.163    0.161
    y2                0.151    0.014   11.177    0.000    0.151    0.151
    y3                0.144    0.013   10.796    0.000    0.144    0.144
    y4                0.151    0.013   11.836    0.000    0.151    0.152
    y5                0.121    0.011   10.692    0.000    0.121    0.122
    y6                0.150    0.013   11.723    0.000    0.150    0.148
    f1                1.000                               0.272    0.272
    f2                1.000                               0.213    0.213
    
    
                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  f1 =~
    y1                0.480    0.014   33.915    0.000    0.914    0.919
    y2                0.481    0.014   34.007    0.000    0.915    0.917
    y3                0.395    0.018   21.769    0.000    0.752    0.753
  f2 =~
    y4                0.423    0.012   34.369    0.000    0.919    0.919
    y5                0.431    0.012   35.073    0.000    0.936    0.935
    y6                0.429    0.012   34.882    0.000    0.931    0.937

Regressions:
  f1 ~
    x1                0.892    0.053   16.870    0.000    0.468    0.468
    x2                1.026    0.055   18.764    0.000    0.539    0.538
    x3                0.798    0.052   15.479    0.000    0.419    0.419
  f2 ~
    x1                1.379    0.059   23.459    0.000    0.635    0.635
    x2                1.094    0.054   20.151    0.000    0.504    0.503
    x3                0.627    0.049   12.910    0.000    0.289    0.288


\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}

\section{Growth Models}
\begin{frame}
\frametitle{Growth models}
\begin{itemize}
\item If traits change over time, one can try to model the growth in the means structure
\begin{itemize} 
\item Intercepts are set to be equal
\item Slope parameter models changes in the means
\item Variables can be observed or latent
\end{itemize}
\item Can also model more complicated growth functions
\begin{itemize} 
\item Quadratic growth
\item Piecewise growth
\end{itemize}
\end{itemize}
\end{frame}

\subsection{Linear growth}
\begin{frame}[fragile]
\begin{columns}
\column{.4\textwidth}
\frametitle{Consider the following growth model}
\begin{tikzpicture}
\tikzstyle{parent}=[circle, draw, minimum size=.6cm]
\tikzstyle{child}=[rectangle, draw, minimum size=0.6cm]

 \node (y11) at (1, 4) [child] {$y_{11}$};
 \node (y12) at (2,4) [child] {$y_{12}$};
 \node (y13) at (3, 4) [child] {$y_{13}$};
 \node (y14) at (4,4) [child] {$y_{14}$};
 \node (i) at (1, 1) [parent] {$i$};
 \node (s) at (2, 1) [parent] {$s$};

 \draw [->] (i) to (y11);
 \draw [->] (i) to (y12);
 \draw [->,] (i) to (y13);
 \draw [->,] (i) to (y14);
 
 \draw [->] (s) to (y11);
 \draw [->] (s) to (y12);
 \draw [->,] (s) to (y13);
 \draw [->,] (s) to (y14); 
 
\path[<->] (i) edge [bend right=20] (s); 




\end{tikzpicture}  
\column{.8\textwidth}
\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}
Data <- read.table("ex6.1.dat")
names(Data) <- c("y11","y12","y13","y14")
\end{Sinput}

\begin{Soutput}
> round(cor(Data),2)
     y11  y12  y13  y14
y11 1.00 0.67 0.59 0.56
y12 0.67 1.00 0.78 0.74
y13 0.59 0.78 1.00 0.85
y14 0.56 0.74 0.85 1.00
> describe(Data)
    var   n mean   sd median trimmed  mad   min  max range  skew kurtosis   se
y11   1 500 0.51 1.20   0.56    0.54 1.27 -2.69 3.60  6.29 -0.17    -0.35 0.05
y12   2 500 1.57 1.41   1.56    1.58 1.28 -3.06 5.96  9.03 -0.08     0.02 0.06
y13   3 500 2.57 1.71   2.52    2.58 1.82 -2.75 8.43 11.17 -0.03    -0.24 0.08
y14   4 500 3.60 2.08   3.61    3.62 2.10 -2.36 9.18 11.54 -0.09    -0.11 0.09

\end{Soutput}
\end{Schunk}
\end{scriptsize}

\end{columns}

\end{frame}


\begin{frame}[fragile]
 \frametitle{Growth model for example 6.1}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
model.ex6.1 <- ' i =~ 1*y11 + 1*y12 + 1*y13 + 1*y14
s =~ 0*y11 + 1*y12 + 2*y13 + 3*y14 '
fit <- growth(model.ex6.1, data=Data)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}
\begin{Soutput}
Lavaan (0.4-7) converged normally after 27 iterations

  Number of observations                           500

  Estimator                                         ML
  Minimum Function Chi-square                    4.593
  Degrees of freedom                                 5
  P-value                                        0.468

Chi-square test baseline model:

  Minimum Function Chi-square                 1439.722
  Degrees of freedom                                 6
  P-value                                        0.000

Full model versus baseline model:

  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.000

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -3016.386
  Loglikelihood unrestricted model (H1)      -3014.089

  Number of free parameters                          9
  Akaike (AIC)                                6050.772
  Bayesian (BIC)                              6088.703
  Sample-size adjusted Bayesian (BIC)         6060.137

Root Mean Square Error of Approximation:

  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.060
  P-value RMSEA <= 0.05                          0.894

Standardized Root Mean Square Residual:

  SRMR                                           0.009

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}

                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  i =~
    y11               1.000                               0.994    0.822
    y12               1.000                               0.994    0.710
    y13               1.000                               0.994    0.585
    y14               1.000                               0.994    0.477
  s =~
    y11               0.000                               0.000    0.000
    y12               1.000                               0.473    0.338
    y13               2.000                               0.946    0.557
    y14               3.000                               1.419    0.681

Covariances:
  i ~~
    s                 0.133    0.033    4.057    0.000    0.283    0.283

Intercepts:
    y11               0.000                               0.000    0.000
    y12               0.000                               0.000    0.000
    y13               0.000                               0.000    0.000
    y14               0.000                               0.000    0.000
    i                 0.523    0.051   10.153    0.000    0.526    0.526
    s                 1.026    0.025   40.268    0.000    2.170    2.170

Variances:
    y11               0.475    0.059    7.989    0.000    0.475    0.324
    y12               0.482    0.040   11.994    0.000    0.482    0.246
    y13               0.473    0.047   10.007    0.000    0.473    0.164
    y14               0.545    0.084    6.471    0.000    0.545    0.125
    i                 0.989    0.089   11.097    0.000    1.000    1.000
    s                 0.224    0.023    9.891    0.000    1.000    1.000
    
    
    
    
    
    
    

\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\subsection{A quadratic growth model}

\begin{frame}[fragile]
\begin{columns}
\column{.4\textwidth}
\frametitle{Consider the following growth model}
\begin{tikzpicture}
\tikzstyle{parent}=[circle, draw, minimum size=.6cm]
\tikzstyle{child}=[rectangle, draw, minimum size=0.6cm]

 \node (y11) at (1, 4) [child] {$y_{11}$};
 \node (y12) at (2,4) [child] {$y_{12}$};
 \node (y13) at (3, 4) [child] {$y_{13}$};
 \node (y14) at (4,4) [child] {$y_{14}$};
 \node (i) at (1, 1) [parent] {$i$};
 \node (s) at (2, 1) [parent] {$s$};
  \node (q) at (3, 1) [parent] {$q$};

 \draw [->] (i) to (y11);
 \draw [->] (i) to (y12);
 \draw [->,] (i) to (y13);
 \draw [->,] (i) to (y14);
 
 \draw [->] (s) to (y11);
 \draw [->] (s) to (y12);
 \draw [->,] (s) to (y13);
 \draw [->,] (s) to (y14); 
 
  \draw [->] (q) to (y12);
 \draw [->,] (q) to (y13);
 \draw [->,] (q) to (y14); 
 
\path[<->] (i) edge [bend right=20] (s); 
\path[<->] (i) edge [bend right=40] (q); 
\path[<->] (s) edge [bend right=20] (q); 




\end{tikzpicture}  
\column{.8\textwidth}
\begin{scriptsize}
\begin{Schunk}
\begin{Sinput}
Data <- read.table("ex6.9.dat")
names(Data) <- c("y11","y12","y13","y14")
\end{Sinput}

\begin{Soutput}
> round(cor(Data),2)
     y11  y12  y13  y14
y11 1.00 0.50 0.33 0.24
y12 0.50 1.00 0.68 0.58
y13 0.33 0.68 1.00 0.89
y14 0.24 0.58 0.89 1.00
> describe(Data)
    var   n mean   sd median trimmed  mad   min   max range  skew kurtosis   se
y11   1 500 0.52 1.39   0.57    0.55 1.46 -3.19  4.08  7.26 -0.17    -0.35 0.06
y12   2 500 2.09 1.71   2.08    2.10 1.59 -4.25  7.61 11.86 -0.08     0.22 0.08
y13   3 500 4.62 2.82   4.38    4.64 3.04 -3.56 14.28 17.84 -0.01    -0.24 0.13
y14   4 500 8.24 4.99   8.19    8.29 5.20 -6.18 22.45 28.63 -0.09    -0.24 0.22

\end{Soutput}
\end{Schunk}
\end{scriptsize}

\end{columns}

\end{frame}


\begin{frame}[fragile]
 \frametitle{Estimating quadratic and linear growth}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
model.ex6.9 <- '
i =~ 1*y11 + 1*y12 + 1*y13 + 1*y14
s =~ 0*y11 + 1*y12 + 2*y13 + 3*y14
q =~ 0*y11 + 1*y12 + 4*y13 + 9*y14
'
fit <- growth(model.ex6.9, data=Data)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}
\begin{Soutput}
Lavaan (0.4-7) converged normally after 61 iterations

  Number of observations                           500

  Estimator                                         ML
  Minimum Function Chi-square                    0.475
  Degrees of freedom                                 1
  P-value                                        0.490

Chi-square test baseline model:

  Minimum Function Chi-square                 1231.275
  Degrees of freedom                                 6
  P-value                                        0.000

Full model versus baseline model:

  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.003

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -3975.519
  Loglikelihood unrestricted model (H1)      -3975.281

  Number of free parameters                         13
  Akaike (AIC)                                7977.038
  Bayesian (BIC)                              8031.828
  Sample-size adjusted Bayesian (BIC)         7990.565

Root Mean Square Error of Approximation:

  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.104
  P-value RMSEA <= 0.05                          0.701

Standardized Root Mean Square Residual:

  SRMR                                           0.004

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}

                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  i =~
    y11               1.000                               1.116    0.803
    y12               1.000                               1.116    0.655
    y13               1.000                               1.116    0.396
    y14               1.000                               1.116    0.224
  s =~
    y11               0.000                               0.000    0.000
    y12               1.000                               0.999    0.586
    y13               2.000                               1.998    0.709
    y14               3.000                               2.998    0.601
  q =~
    y11               0.000                               0.000    0.000
    y12               1.000                               0.531    0.311
    y13               4.000                               2.122    0.753
    y14               9.000                               4.775    0.957

Covariances:
  i ~~
    s                -0.173    0.300   -0.578    0.563   -0.155   -0.155
    q                 0.101    0.080    1.269    0.204    0.170    0.170
  s ~~
    q                -0.179    0.117   -1.525    0.127   -0.337   -0.337

Intercepts:
    y11               0.000                               0.000    0.000
    y12               0.000                               0.000    0.000
    y13               0.000                               0.000    0.000
    y14               0.000                               0.000    0.000
    i                 0.521    0.062    8.446    0.000    0.467    0.467
    s                 1.035    0.077   13.462    0.000    1.036    1.036
    q                 0.512    0.031   16.449    0.000    0.965    0.965

Variances:
    y11               0.685    0.265    2.591    0.010    0.685    0.355
    y12               0.882    0.114    7.708    0.000    0.882    0.304
    y13               0.946    0.183    5.180    0.000    0.946    0.119
    y14               0.738    0.944    0.781    0.435    0.738    0.030
    i                 1.246    0.273    4.571    0.000    1.000    1.000
    s                 0.998    0.358    2.789    0.005    1.000    1.000
    q                 0.282    0.060    4.668    0.000    1.000    1.000










\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\subsection{Piecewise growth}
\begin{frame}[fragile]
 \frametitle{Piecewise growth}
\begin{columns}
\column{.5\textwidth}
\begin{tikzpicture}
\tikzstyle{parent}=[circle, draw, minimum size=.6cm]
\tikzstyle{child}=[rectangle, draw, minimum size=0.6cm]

 \node (y1) at (1, 4) [child] {$y_{1}$};
 \node (y2) at (2,4) [child] {$y_{2}$};
 \node (y3) at (3, 4) [child] {$y_{3}$};
 \node (y4) at (4,4) [child] {$y_{4}$};
 \node (y5) at (5,4) [child] {$y_{5}$};
 \node (i) at (1, 1) [parent] {$i$};
 \node (s1) at (2, 1) [parent] {$s_{1}$};
  \node (s2) at (3, 1) [parent] {$s_{2}$};

 \draw [->] (i) to (y1);
 \draw [->] (i) to (y2);
 \draw [->,] (i) to (y3);
 \draw [->,] (i) to (y4);
  \draw [->,] (i) to (y5);
 
 \draw [->] (s1) to (y1);
 \draw [->] (s1) to (y12);
 \draw [->,] (s1) to (y3);
 \draw [->,] (s1) to (y4); 
  \draw [->,] (s1) to (y5); 
 
 \draw [->,] (s2) to (y4); 
  \draw [->,] (s2) to (y5); 
 
\path[<->] (i) edge [bend right=20] (s); 
\path[<->] (i) edge [bend right=40] (q); 
\path[<->] (s) edge [bend right=20] (q); 




\end{tikzpicture}  

\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
> Data <- read.table("ex6.11.dat")
> names(Data) <- c("y1","y2","y3","y4","y5")
> round(cor(Data),2)
> describe(Data)
\end{Sinput}
\begin{Soutput}
     y1   y2   y3   y4   y5
y1 1.00 0.63 0.51 0.53 0.49
y2 0.63 1.00 0.70 0.68 0.62
y3 0.51 0.70 1.00 0.75 0.66
y4 0.53 0.68 0.75 1.00 0.79
y5 0.49 0.62 0.66 0.79 1.00

   var   n mean   sd median trimmed  mad   min   max range  skew kurtosis   se
y1   1 500 0.44 1.17   0.37    0.44 1.17 -2.64  3.61  6.25  0.03    -0.35 0.05
y2   2 500 1.58 1.32   1.52    1.58 1.29 -2.28  5.76  8.04  0.07    -0.14 0.06
y3   3 500 2.59 1.52   2.58    2.60 1.56 -1.46  7.15  8.60  0.03    -0.21 0.07
y4   4 500 4.54 1.58   4.46    4.54 1.60 -0.25  9.11  9.36 -0.05     0.01 0.07
y5   5 500 6.54 1.81   6.63    6.54 1.79  1.54 12.29 10.76  0.03     0.20 0.08

\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\begin{frame}[fragile]
 \frametitle{Modeling piecewise growth}
\begin{columns}
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}
modelex6.11 <- '
i =~ 1*y1 + 1*y2 + 1*y3 + 1*y4 + 1*y5
s1 =~ 0*y1 + 1*y2 + 2*y3 + 2*y4 + 2*y5
s2 =~ 0*y1 + 0*y2 + 0*y3 + 1*y4 + 2*y5
'
fit <- growth(modelex6.11, data=Data)
summary(fit, standardized=TRUE, fit.measures=TRUE)
\end{Sinput}
\begin{Soutput}

  Number of observations                           500

  Estimator                                         ML
  Minimum Function Chi-square                    5.244
  Degrees of freedom                                 6
  P-value                                        0.513

Chi-square test baseline model:

  Minimum Function Chi-square                 1587.697
  Degrees of freedom                                10
  P-value                                        0.000

Full model versus baseline model:

  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.001

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -3706.171
  Loglikelihood unrestricted model (H1)      -3703.549

  Number of free parameters                         14
  Akaike (AIC)                                7440.342
  Bayesian (BIC)                              7499.346
  Sample-size adjusted Bayesian (BIC)         7454.909

Root Mean Square Error of Approximation:

  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.054
  P-value RMSEA <= 0.05                          0.929

Standardized Root Mean Square Residual:

  SRMR                                           0.014

\end{Soutput}
\end{Schunk}
\end{tiny}    
\column{.5\textwidth}
\begin{tiny}
\begin{Schunk}
\begin{Sinput}

\end{Sinput}
\begin{Soutput}
                  Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Latent variables:
  i =~
    y1                1.000                               0.992    0.845
    y2                1.000                               0.992    0.763
    y3                1.000                               0.992    0.650
    y4                1.000                               0.992    0.625
    y5                1.000                               0.992    0.550
  s1 =~
    y1                0.000                               0.000    0.000
    y2                1.000                               0.489    0.376
    y3                2.000                               0.979    0.642
    y4                2.000                               0.979    0.616
    y5                2.000                               0.979    0.543
  s2 =~
    y1                0.000                               0.000    0.000
    y2                0.000                               0.000    0.000
    y3                0.000                               0.000    0.000
    y4                1.000                               0.468    0.295
    y5                2.000                               0.936    0.519

Covariances:
  i ~~
    s1               -0.029    0.049   -0.592    0.554   -0.060   -0.060
    s2                0.059    0.036    1.644    0.100    0.128    0.128
  s1 ~~
    s2               -0.031    0.026   -1.186    0.236   -0.135   -0.135

Intercepts:
    y1                0.000                               0.000    0.000
    y2                0.000                               0.000    0.000
    y3                0.000                               0.000    0.000
    y4                0.000                               0.000    0.000
    y5                0.000                               0.000    0.000
    i                 0.462    0.051    8.976    0.000    0.466    0.466
    s1                1.071    0.030   36.016    0.000    2.189    2.189
    s2                1.957    0.030   64.432    0.000    4.182    4.182


\end{Soutput}
\end{Schunk}
\end{tiny}   
\end{columns} 
\end{frame}


\def\newblock{\hskip .11em plus .33em minus .07em}
\section{References}
\bibliography{/Volumes/WR/bill/Documents/Active/book/all} 

\end{document}