-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
74 lines (57 loc) · 2.18 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# R2spa
<!-- badges: start -->
[![R-CMD-check](https://github.com/Gengrui-Zhang/R2spa/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/Gengrui-Zhang/R2spa/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
`R2spa` is a free and open-source R package that performs two-stage path analysis (2S-PA). With 2S-PA, researchers can perform path analysis by first obtaining factor scores and then adjusting for measurement errors using estimates of observation-specific reliability or standard error of those factor scores. As a viable alternative to SEM, 2S-PA has been shown to give equally-good estimates as SEM in relatively simple models and large sample sizes, as well as to give more accurate parameter estimates, has better control of Type I error rates, and has substantially less convergence problems in more complex models or small sample sizes.
## Installation
This package is still in developmental stage and can be installed on GitHub with:
```{r eval=FALSE}
# install.packages("remotes")
remotes::install_github("Gengrui-Zhang/R2spa")
```
## Example
```{r message=FALSE}
library(lavaan)
library(R2spa)
# Joint model
model <- '
# latent variable definitions
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
# regression
dem60 ~ ind60
'
```
```{r}
# 2S-PA
# Stage 1: Get factor scores and standard errors for each latent construct
fs_dat_ind60 <- get_fs(data = PoliticalDemocracy,
model = "ind60 =~ x1 + x2 + x3")
fs_dat_dem60 <- get_fs(data = PoliticalDemocracy,
model = "dem60 =~ y1 + y2 + y3 + y4")
fs_dat <- cbind(fs_dat_ind60, fs_dat_dem60)
# get_fs() gives a dataframe with factor scores and standard errors
head(fs_dat)
```
```{r}
# Stage 2: Perform 2S-PA
tspa_fit <- tspa(
model = "dem60 ~ ind60",
data = fs_dat,
se_fs = list(ind60 = 0.1213615, dem60 = 0.6756472)
)
parameterestimates(tspa_fit)
```
This package is based upon work supported by the National Science Foundation under Grant No. 2141790.
<!-- `devtools::build_readme()` -->