Specifying the data directory

ddir <- "../../data"

Loading the rstan package

library(rstan)
Loading required package: ggplot2
Loading required package: StanHeaders
rstan (Version 2.17.3, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)

Creating a data object containing observed responses (y)

## Reading in the job placement data
dat <- read.csv(file.path(ddir, "job_placement.csv"), header = FALSE)

## Assigning variable names
colnames(dat) <- c("id", "wjcalc", "wjspl",
                   "wratspl", "wratcalc", "waiscalc",
                   "waisspl", "edlevel", "newschl",
                   "suspend", "expelled", "haveld", "female", "age")
summary(dat)
       id             wjcalc            wjspl          wratspl        
 Min.   :  1.00   Min.   :   13.0   Min.   :19.00   Min.   :   19.00  
 1st Qu.: 89.25   1st Qu.:   20.0   1st Qu.:37.00   1st Qu.:   32.00  
 Median :173.50   Median :   24.0   Median :42.00   Median :   36.00  
 Mean   :170.57   Mean   :  644.8   Mean   :41.67   Mean   :  346.93  
 3rd Qu.:253.75   3rd Qu.:   27.0   3rd Qu.:47.00   3rd Qu.:   41.75  
 Max.   :335.00   Max.   :99999.0   Max.   :57.00   Max.   :99999.00  
    wratcalc        waiscalc          waisspl           edlevel       
 Min.   :23.00   Min.   :    6.0   Min.   :   16.0   Min.   :    7.0  
 1st Qu.:34.00   1st Qu.:    8.0   1st Qu.:   33.0   1st Qu.:   10.0  
 Median :38.00   Median :   10.0   Median :   38.0   Median :   12.0  
 Mean   :38.92   Mean   :  632.1   Mean   :  658.1   Mean   :  632.1  
 3rd Qu.:43.00   3rd Qu.:   13.0   3rd Qu.:   42.0   3rd Qu.:   12.0  
 Max.   :54.00   Max.   :99999.0   Max.   :99999.0   Max.   :99999.0  
    newschl         suspend         expelled         haveld       
 Min.   :    0   Min.   :    0   Min.   :    0   Min.   :    0.0  
 1st Qu.:    0   1st Qu.:    0   1st Qu.:    0   1st Qu.:    0.0  
 Median :    1   Median :    1   Median :    0   Median :    0.0  
 Mean   : 2174   Mean   : 1864   Mean   : 1863   Mean   :  310.7  
 3rd Qu.:    1   3rd Qu.:    1   3rd Qu.:    0   3rd Qu.:    0.0  
 Max.   :99999   Max.   :99999   Max.   :99999   Max.   :99999.0  
     female            age      
 Min.   :0.0000   Min.   :16.0  
 1st Qu.:0.0000   1st Qu.:18.0  
 Median :0.0000   Median :19.0  
 Mean   :0.3137   Mean   :19.7  
 3rd Qu.:1.0000   3rd Qu.:21.0  
 Max.   :1.0000   Max.   :27.0  
## Extracting variables for the current example
dat.2 <- dat[ , c("wratcalc", "wjcalc", "waiscalc",
                  "wratspl", "wjspl", "waisspl")]

## Recoding 99999 to NA
dat.2[dat.2 == 99999] <- NA

## Creating a data set without NAs
dat.3 <- na.omit(dat.2)

## The data set containing observed responses (y) for the current example
summary(dat.3)
    wratcalc         wjcalc         waiscalc        wratspl          wjspl     
 Min.   :23.00   Min.   :13.00   Min.   : 6.00   Min.   :19.00   Min.   :19.0  
 1st Qu.:34.00   1st Qu.:20.00   1st Qu.: 8.00   1st Qu.:32.00   1st Qu.:37.0  
 Median :38.00   Median :24.00   Median :10.00   Median :36.00   Median :42.0  
 Mean   :38.89   Mean   :23.81   Mean   :11.03   Mean   :36.53   Mean   :41.7  
 3rd Qu.:43.00   3rd Qu.:27.00   3rd Qu.:13.00   3rd Qu.:42.00   3rd Qu.:47.0  
 Max.   :54.00   Max.   :36.00   Max.   :21.00   Max.   :57.00   Max.   :57.0  
    waisspl    
 Min.   :16.0  
 1st Qu.:33.0  
 Median :38.0  
 Mean   :37.2  
 3rd Qu.:42.0  
 Max.   :52.0  

Creating a data input object for rstan

data_for_stan = list(N = nrow(dat.3),          ## sample size
                     k = ncol(dat.3),          ## number of indicator
                     y = as.matrix(dat.3),     ## observed responses
                     n_xi = 2,                 ## number of factor(s)
                     str_loading = c(1, 1, 1,  ## loading structure
                                     2, 2, 2))

Bayesian MCMC estimation using rstan in three steps

This three-step approach is recommended for debug purposes

Step 1 - Using the stanc() function to translate Stan code to C++

## The tr_01 object contains C++ translation for cfa-01.stan
tr_01 <- stanc("cfa-01.stan")

Step 2 - Using the stan_model() function to create an S4 class model object

## The so_01 is an S4 class model object created for tr_01
## Warnings will show up - move to the next step if the warnings are nonfatal
so_01  <- stan_model(stanc_ret = tr_01, verbose=FALSE)
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/math/tools/config.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/config.hpp:39:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/config/compiler/clang.hpp:200:11: warning: 'BOOST_NO_CXX11_RVALUE_REFERENCES' macro redefined [-Wmacro-redefined]
#  define BOOST_NO_CXX11_RVALUE_REFERENCES
          ^
<command line>:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
        ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Core:531:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Dense:2:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/LU:47:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Dense:3:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Cholesky:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Jacobi:29:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Dense:3:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Cholesky:43:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Dense:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/QR:17:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Householder:27:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Dense:5:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/SVD:48:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Dense:6:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Geometry:58:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Dense:7:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Eigenvalues:58:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:36:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/operator_unary_plus.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/scal/fun/constants.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/math/constants/constants.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/math/tools/convert_from_string.hpp:15:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/lexical_cast.hpp:32:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/lexical_cast/try_lexical_convert.hpp:42:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/lexical_cast/detail/converter_lexical.hpp:52:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/container/container_fwd.hpp:61:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/container/detail/std_fwd.hpp:27:1: warning: inline namespaces are a C++11 feature [-Wc++11-inline-namespace]
BOOST_MOVE_STD_NS_BEG
^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/move/detail/std_ns_begin.hpp:18:34: note: expanded from macro 'BOOST_MOVE_STD_NS_BEG'
   #define BOOST_MOVE_STD_NS_BEG _LIBCPP_BEGIN_NAMESPACE_STD
                                 ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__config:439:52: note: expanded from macro '_LIBCPP_BEGIN_NAMESPACE_STD'
#define _LIBCPP_BEGIN_NAMESPACE_STD namespace std {inline namespace _LIBCPP_NAMESPACE {
                                                   ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:83:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/csr_extract_u.hpp:6:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Sparse:26:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/SparseCore:66:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:83:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/csr_extract_u.hpp:6:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Sparse:27:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/OrderingMethods:71:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:83:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/csr_extract_u.hpp:6:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Sparse:29:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/SparseCholesky:43:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:83:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/csr_extract_u.hpp:6:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Sparse:32:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/SparseQR:35:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:83:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/csr_extract_u.hpp:6:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/Sparse:33:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/IterativeLinearSolvers:46:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^
In file included from fileb1277ab9d216.cpp:743:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rstan/include/rstan/rstaninc.hpp:3:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rstan/include/rstan/stan_fit.hpp:36:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/services/optimize/bfgs.hpp:11:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/optimization/bfgs.hpp:9:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/optimization/lbfgs_update.hpp:6:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/circular_buffer.hpp:54:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/circular_buffer/details.hpp:20:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/move/move.hpp:30:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/move/iterator.hpp:27:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/move/detail/iterator_traits.hpp:29:1: warning: inline namespaces are a C++11 feature [-Wc++11-inline-namespace]
BOOST_MOVE_STD_NS_BEG
^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/move/detail/std_ns_begin.hpp:18:34: note: expanded from macro 'BOOST_MOVE_STD_NS_BEG'
   #define BOOST_MOVE_STD_NS_BEG _LIBCPP_BEGIN_NAMESPACE_STD
                                 ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__config:439:52: note: expanded from macro '_LIBCPP_BEGIN_NAMESPACE_STD'
#define _LIBCPP_BEGIN_NAMESPACE_STD namespace std {inline namespace _LIBCPP_NAMESPACE {
                                                   ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:44:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:17: warning: unused function 'set_zero_all_adjoints' [-Wunused-function]
    static void set_zero_all_adjoints() {
                ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:45:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints_nested.hpp:17:17: warning: 'static' function 'set_zero_all_adjoints_nested' declared in header file should be declared 'static inline' [-Wunneeded-internal-declaration]
    static void set_zero_all_adjoints_nested() {
                ^
In file included from fileb1277ab9d216.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:58:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/autocorrelation.hpp:17:14: warning: function 'fft_next_good_size' is not needed and will not be emitted [-Wunneeded-internal-declaration]
      size_t fft_next_good_size(size_t N) {
             ^
19 warnings generated.

Step 3 - Using the sampling() function for MCMC sampling/estimation

post_01 <- sampling(object = so_01,       ## the S4 model object created in Step 2
                    data = data_for_stan, ## the data input (an list object)
                    chains = 2,           ## number of MCMC chains
                    iter = 4000,          ## total number of MCMC draws per chain
                    warmup = 1500)        ## number of warmup draws per chain

SAMPLING FOR MODEL 'cfa-01' NOW (CHAIN 1).

Gradient evaluation took 0.000435 seconds
1000 transitions using 10 leapfrog steps per transition would take 4.35 seconds.
Adjust your expectations accordingly!


Iteration:    1 / 4000 [  0%]  (Warmup)
Iteration:  400 / 4000 [ 10%]  (Warmup)
Iteration:  800 / 4000 [ 20%]  (Warmup)
Iteration: 1200 / 4000 [ 30%]  (Warmup)
Iteration: 1501 / 4000 [ 37%]  (Sampling)
Iteration: 1900 / 4000 [ 47%]  (Sampling)
Iteration: 2300 / 4000 [ 57%]  (Sampling)
Iteration: 2700 / 4000 [ 67%]  (Sampling)
Iteration: 3100 / 4000 [ 77%]  (Sampling)
Iteration: 3500 / 4000 [ 87%]  (Sampling)
Iteration: 3900 / 4000 [ 97%]  (Sampling)
Iteration: 4000 / 4000 [100%]  (Sampling)

 Elapsed Time: 10.5965 seconds (Warm-up)
               10.8877 seconds (Sampling)
               21.4842 seconds (Total)


SAMPLING FOR MODEL 'cfa-01' NOW (CHAIN 2).

Gradient evaluation took 0.000124 seconds
1000 transitions using 10 leapfrog steps per transition would take 1.24 seconds.
Adjust your expectations accordingly!


Iteration:    1 / 4000 [  0%]  (Warmup)
Iteration:  400 / 4000 [ 10%]  (Warmup)
Iteration:  800 / 4000 [ 20%]  (Warmup)
Iteration: 1200 / 4000 [ 30%]  (Warmup)
Iteration: 1501 / 4000 [ 37%]  (Sampling)
Iteration: 1900 / 4000 [ 47%]  (Sampling)
Iteration: 2300 / 4000 [ 57%]  (Sampling)
Iteration: 2700 / 4000 [ 67%]  (Sampling)
Iteration: 3100 / 4000 [ 77%]  (Sampling)
Iteration: 3500 / 4000 [ 87%]  (Sampling)
Iteration: 3900 / 4000 [ 97%]  (Sampling)
Iteration: 4000 / 4000 [100%]  (Sampling)

 Elapsed Time: 10.0005 seconds (Warm-up)
               11.1349 seconds (Sampling)
               21.1353 seconds (Total)

Alternatively, MCMC can be done in one step using the stan() function

post_02 <- stan(file = "cfa-01.stan", ## the Stan syntax file
                data = data_for_stan, ## data input for rstan
                chains = 2,           ## number of MCMC chains
                iter = 4000,          ## total number of MCMC draws per chain
                warmup = 1500)        ## number of warmup draws per chain

Examining the posterior distributions (MCMC draws)

The standardized factor loadings

## Trace plot for visualizing MCMC convergence
plot(post_01, plotfun = "trace", pars = c("loading_std"), inc_warmup = TRUE)

## Density plot for factor loadings
plot(post_01, pars = "loading_std", show_density = TRUE, ci_level = 0.5)
ci_level: 0.5 (50% intervals)
outer_level: 0.95 (95% intervals)

## Descriptive table for summarizing the posterior distribution
print(post_01, pars = "loading_std", probs = c(.025, .975),
      digits = 2, mode = TRUE, use_cache = FALSE)
Inference for Stan model: cfa-01.
2 chains, each with iter=4000; warmup=1500; thin=1; 
post-warmup draws per chain=2500, total post-warmup draws=5000.

               mean se_mean   sd 2.5% 97.5% n_eff Rhat
loading_std[1] 0.95       0 0.04 0.87  1.04   714    1
loading_std[2] 0.91       0 0.04 0.82  1.00  1039    1
loading_std[3] 0.72       0 0.05 0.63  0.82  2132    1
loading_std[4] 0.95       0 0.04 0.87  1.04   953    1
loading_std[5] 0.96       0 0.04 0.88  1.05   860    1
loading_std[6] 0.95       0 0.04 0.87  1.04   932    1

Samples were drawn using NUTS(diag_e) at Wed May 16 14:49:30 2018.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at 
convergence, Rhat=1).

The factor correlation

## Trace plot for visualizing MCMC convergence
## factor_cor[1,1] and factor_cor[2,2] were fixed to 1 for model identification
plot(post_01, plotfun = "trace", pars = c("factor_cor"), inc_warmup = TRUE)

## Descriptive table for summarizing the posterior distribution
print(post_01, pars = "factor_cor", probs = c(.025, .975),
      digits = 2, mode = TRUE, use_cache = FALSE)
Inference for Stan model: cfa-01.
2 chains, each with iter=4000; warmup=1500; thin=1; 
post-warmup draws per chain=2500, total post-warmup draws=5000.

                mean se_mean   sd 2.5% 97.5% n_eff Rhat
factor_cor[1,1] 1.00       0 0.00 1.00  1.00  5000  NaN
factor_cor[1,2] 0.56       0 0.04 0.47  0.63   696    1
factor_cor[2,1] 0.56       0 0.04 0.47  0.63   696    1
factor_cor[2,2] 1.00       0 0.00 1.00  1.00  3002    1

Samples were drawn using NUTS(diag_e) at Wed May 16 14:49:30 2018.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at 
convergence, Rhat=1).

Unstandardized indicator intercept

## Trace plot for visualizing MCMC convergence
plot(post_01, plotfun = "trace", pars = c("intercept_unstd"), inc_warmup = TRUE)

## Descriptive table for summarizing the posterior distribution
print(post_01, pars = 'intercept_unstd', probs = c(.025,.975),
      digits = 2, use_cache = FALSE)
Inference for Stan model: cfa-01.
2 chains, each with iter=4000; warmup=1500; thin=1; 
post-warmup draws per chain=2500, total post-warmup draws=5000.

                    mean se_mean   sd  2.5% 97.5% n_eff Rhat
intercept_unstd[1] 38.90    0.01 0.35 38.20 39.56   588    1
intercept_unstd[2] 23.82    0.01 0.25 23.33 24.30   631    1
intercept_unstd[3] 11.03    0.01 0.19 10.65 11.40   914    1
intercept_unstd[4] 36.55    0.02 0.38 35.80 37.30   630    1
intercept_unstd[5] 41.72    0.02 0.40 40.96 42.51   628    1
intercept_unstd[6] 37.22    0.01 0.38 36.49 37.95   631    1

Samples were drawn using NUTS(diag_e) at Wed May 16 14:49:30 2018.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at 
convergence, Rhat=1).

Unstandardized indicator residuals

## Trace plot for visualizing MCMC convergence
plot(post_01, plotfun = "trace", pars = c("residual_unstd"), inc_warmup = TRUE)

## Trace plot for visualizing MCMC convergence
print(post_01, pars = 'residual_unstd', probs = c(.025,.975),
      digits = 2, use_cache = FALSE)
Inference for Stan model: cfa-01.
2 chains, each with iter=4000; warmup=1500; thin=1; 
post-warmup draws per chain=2500, total post-warmup draws=5000.

                  mean se_mean   sd 2.5% 97.5% n_eff Rhat
residual_unstd[1] 1.99    0.02 0.28 1.40  2.49   206 1.01
residual_unstd[2] 1.98    0.01 0.14 1.69  2.27   550 1.01
residual_unstd[3] 2.33    0.00 0.10 2.14  2.54  5000 1.00
residual_unstd[4] 2.27    0.00 0.14 2.00  2.56  5000 1.00
residual_unstd[5] 2.16    0.00 0.15 1.87  2.45  3392 1.00
residual_unstd[6] 2.29    0.00 0.14 2.03  2.56  5000 1.00

Samples were drawn using NUTS(diag_e) at Wed May 16 14:49:30 2018.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at 
convergence, Rhat=1).

R Session Info

``{r warnings, echo = FALSE} sessionInfo() ```

Available under Created Commons license 3.0 CC BY