Last active
August 24, 2019 18:19
-
-
Save psyguy/1a3eed7ce19fedec0b45d12543469e74 to your computer and use it in GitHub Desktop.
StackOverflow - 2910-08-24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # loading stuff ----------------------------------------------------------- | |
| list.of.packages <- c("tidyverse", | |
| "plyr", | |
| "qgraph", | |
| "dplyr", | |
| "knitr", | |
| "kableExtra", | |
| "psych", | |
| "semPlot", | |
| "lavaan") | |
| new.packages <- | |
| list.of.packages[!(list.of.packages %in% installed.packages()[, "Package"])] | |
| if (length(new.packages)) { | |
| install.packages(new.packages, repos='http://cran.us.r-project.org') | |
| } | |
| tmp <- lapply(list.of.packages, require, character.only = TRUE) | |
| rm(list.of.packages, new.packages, tmp) | |
| # knitr::opts_chunk$set(echo = FALSE) | |
| # rm(list = ls()) | |
| # selecting items and reading data ---------------------------------------------- | |
| items <- list( | |
| discrimination = c("dscrrce", | |
| "dscrntn", | |
| "dscrrlg", | |
| "dscrlng", | |
| "dscretn", | |
| "dscrage", | |
| "dscrgnd", | |
| "dscrsex", | |
| "dscrdsb", | |
| "dscroth" | |
| ), | |
| trust_social = c("ppltrst", | |
| "pplfair", | |
| "pplhlp" | |
| ), | |
| trust_political = c("trstprl", | |
| "trstlgl", | |
| "trstplc", | |
| "trstplt", | |
| "trstprt" | |
| ), | |
| hope_political = c("psppsgva", | |
| "actrolga", | |
| "psppipla", | |
| "cptppola"), | |
| interest_political = "polintr" | |
| ) | |
| items_ordered <- items$hope_political %>% unlist() %>% as.character() | |
| data <- read.csv("https://github.com/psyguy/sem-course-project/raw/master/data/ess2016_selected.csv") | |
| # cleaning/recoding the data ---------------------------------------------- | |
| d <- data | |
| # removing invalid/missing data | |
| d[d>10] <- NA | |
| d$polintr[d$polintr>4] <- NA | |
| d[,20:23][d[,20:23]>5] <- NA | |
| # reverse-coding polint | |
| d$polintr <- (d$polintr-5) %>% abs() | |
| dscrscore <- d %>% select(contains("dscr")) %>% rowSums() | |
| d <- d %>% cbind(dscrscore) | |
| d <- d %>% filter(cntry == "BE") %>% na.omit() | |
| # making model syntaxes --------------------------------------------------- | |
| m.5 <- " | |
| # 1. latent variable definitions | |
| hope_political =~ NA*psppsgva + actrolga + psppipla + cptppola | |
| trust_social =~ NA*ppltrst + pplfair + pplhlp | |
| trust_political =~ NA*trstprl + trstlgl + trstplc + trstplt + trstprt | |
| optimism_political =~ NA*psppsgva + actrolga + psppipla + cptppola + | |
| trstprl + trstlgl + trstplc + trstplt + trstprt | |
| # 2. regressions | |
| hope_political + trust_political ~ polintr | |
| hope_political + trust_social + trust_political ~ dscrscore | |
| # 3. (co)variances | |
| hope_political ~~ 1*hope_political | |
| trust_social ~~ 1*trust_social | |
| trust_political ~~ 1*trust_political | |
| # optimism_general ~~ 1*optimism_general | |
| optimism_political ~~ 1*optimism_political | |
| hope_political ~~ trust_social + trust_political | |
| trust_social ~~ trust_political | |
| # 4. intercepts | |
| psppsgva + actrolga + psppipla + cptppola ~ 1 | |
| ppltrst + pplfair + pplhlp ~ 1 | |
| trstprl + trstlgl + trstplc + trstplt + trstprt ~ 1 | |
| " | |
| # fitting and plotting model --------------------------------------------------- | |
| f <- lavaan(m.5, d, | |
| ordered = c(items_ordered, as.character(unlist(items$discrimination))), | |
| std.lv = TRUE, | |
| auto.var=TRUE) | |
| semPaths(f, | |
| title = FALSE, | |
| curvePivot = TRUE, | |
| what = "std", | |
| rotation = 2, | |
| layout = "tree2", | |
| optimizeLatRes = TRUE, | |
| intercepts = FALSE, | |
| edge.label.cex = 0.95, | |
| exoVar=FALSE, | |
| sizeMan=5, | |
| sizeLat=7, | |
| nCharNodes=5, | |
| residuals=FALSE, | |
| fixedStyle=1, | |
| freeStyle=1, | |
| curvePivot = FALSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment