flps

library(flps)
#> Version: 0.1.0
#> 
#> It is a demo.

CRAN status

Fully Latent Principal Stratification (FLPS)

Fully Latent Principal Stratification (FLPS) is an extension of principal stratification.

Install

Install the latest release from CRAN:

devtools::install_github("sooyongl/flps")

The documentation is available at here.

If compiling errors occur, see here.

Basic working example

Running with the package

set.seed(10000)
inp_data <- flps::makeInpData(
  N       = 200,  # sample size
  R2Y     = 0.2,  # r^2 of outcome
  R2eta   = 0.5,  # r^2 of eta by one covariates
  omega   = 0.2,  # the effect of eta
  tau0    = 0.23, # direct effect
  tau1    = -0.16,# interaction effect between Z and eta
  betaL   = 0.2,
  betaY   = 0.4,
  lambda  = 0.8,  # the proportion of administered items
  nitem    = 10,   # the total number of items
  nfac    = 1,    # the number of latent factors
  lvmodel = 'rasch' # tag for latent variable model; case-sensitive (use lower-case letters)
)

makeInpData() creates input data for running FLPS.

# Input data matrix
data.table::data.table(inp_data)
#>                Y Z          X1        eta1 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
#>   1: -0.72862564 1 -0.20087849  0.49705730  0  1  0  0  1  0  1  0  1   1
#>   2:  0.43624761 1 -0.81367558  0.09644683  1  1  1  1  1  1  0  0  0   1
#>   3:  0.71005101 1 -0.09306958 -0.30660832  1  1  1  0  0  1  1  1  0   1
#>   4:  0.01947398 1 -0.08743884 -0.37419814  0  1  1  0  0  0  1  0  0   0
#>   5: -1.00950577 1 -2.16774891 -1.81547040  0  1  0  0  0  0  0  0  0   0
#>  ---                                                                     
#> 196: -0.47559245 0  0.23763106 -0.27108910 NA NA NA NA NA NA NA NA NA  NA
#> 197:  0.46877629 0 -0.03646065  1.12609970 NA NA NA NA NA NA NA NA NA  NA
#> 198:  0.78717334 0  0.06867924  0.07008599 NA NA NA NA NA NA NA NA NA  NA
#> 199:  0.56380180 0  0.56467755  0.34826071 NA NA NA NA NA NA NA NA NA  NA
#> 200:  0.36809486 0  0.82158503 -0.35012492 NA NA NA NA NA NA NA NA NA  NA

Now, provide information about your model. runFLPS internally coverts inp_data into the data format for rstan given the information, and runs FLPS.

To avoid having to compile the Stan code every time you run it, you can pre-compile the code using the modelBuilder() function along with the relevant measurement model. This function will compile the Stan code and store the resulting stanmodel object in the flps package directory (If error, update the latest Rcpp package; After running modelBuilder(), it is recommended to refresh R session). The next time you run runFLPS(), the code will skip the compilation step, making your analysis faster and more efficient. Otherwise, it will take a while for runFLPS() to compile the Stan code.

modelBuilder(type = "rasch")

Also, if you have any errors, try the latest rstan and StanHeaders packages

remove.packages(c("rstan", "StanHeaders"))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
res <- runFLPS(
  inp_data = inp_data,
  outcome = "Y",
  group = "Z",
  covariate = c("X1"),
  lv_type = "rasch",
  lv_model = "F =~ v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10",
  stan_options = list(iter = 1000, warmup = 500, cores = 1, chains = 2)
)

The flps_plot() shows the plot related to FLPS models


flps_plot(res, type = "causal")


flps_plot(res, type = "latent")