> # principles and practice of sem, 5th ed. > # rex b. kline, guilford press, 2023 > > # chapter 16, table 16.2, analysis 3 > # generate standardized scores with correlations > # that exactly match those in table 16.1 > > # to avoid the problem that some R packages share the same name > # for different functions, all functions are specified next as > > # package::function > > # which prevents masking, or the default hiding of > # a function with a redundant name from a package used next > > date() [1] "Fri Apr 14 09:48:23 2023" > > v <- R.Version() > print(paste0(v$language, " version ", v$major, ".", + v$minor, " (", v$year, "-", v$month, "-", v$day, ")")) [1] "R version 4.2.3 (2023-03-15)" > > library(semTools) Loading required package: lavaan This is lavaan 0.6-15 lavaan is FREE software! Please report any bugs. ############################################################################### This is semTools 0.5-6 All users of R (or SEM) are invited to submit functions or ideas for functions. ############################################################################### > library(lavaan) > library(psych) Attaching package: ‘psych’ The following objects are masked from ‘package:semTools’: reliability, skew The following object is masked from ‘package:lavaan’: cor2cov > > # get citation information > citation("semTools", auto = TRUE) To cite package ‘semTools’ in publications use: Jorgensen TD, Pornprasertmanit S, Schoemann AM, Rosseel Y (2022). _semTools: Useful Tools for Structural Equation Modeling_. R package version 0.5-6, . A BibTeX entry for LaTeX users is @Manual{, title = {semTools: Useful Tools for Structural Equation Modeling}, author = {Terrence D. Jorgensen and Sunthud Pornprasertmanit and Alexander M. Schoemann and Yves Rosseel}, year = {2022}, note = {R package version 0.5-6}, url = {https://CRAN.R-project.org/package=semTools}, } > citation("lavaan", auto = TRUE) To cite package ‘lavaan’ in publications use: Rosseel Y, Jorgensen TD, Rockwood N (2023). _lavaan: Latent Variable Analysis_. R package version 0.6-15, . A BibTeX entry for LaTeX users is @Manual{, title = {lavaan: Latent Variable Analysis}, author = {Yves Rosseel and Terrence D. Jorgensen and Nicholas Rockwood}, year = {2023}, note = {R package version 0.6-15}, url = {https://CRAN.R-project.org/package=lavaan}, } > citation("psych", auto = TRUE) To cite package ‘psych’ in publications use: Revelle W (2023). _psych: Procedures for Psychological, Psychometric, and Personality Research_. R package version 2.3.3, . A BibTeX entry for LaTeX users is @Manual{, title = {psych: Procedures for Psychological, Psychometric, and Personality Research}, author = {William Revelle}, year = {2023}, note = {R package version 2.3.3}, url = {https://CRAN.R-project.org/package=psych}, } > > # variable order is acculscl, status, percent, educ, income, > # interpers, job, scl90d > # read correlation matrix > shen.cor <- matrix(c(1.00, .44, .69, .21, .23, .12, .09, .03, + .44,1.00, .54, .08, .15, .08, .06, .02, + .69, .54,1.00, .16, .19, .08, .04,-.02, + .21, .08, .16,1.00, .19, .08, .01,-.07, + .23, .15, .19, .19,1.00,-.03,-.02,-.11, + .12, .08, .08, .08,-.03,1.00, .38, .37, + .09, .06, .04, .01,-.02, .38,1.00, .46, + .03, .02,-.02,-.07,-.11, .37, .46,1.00), + ncol = 8, nrow = 8) > > # generate raw scores and save to dataframe > shen.data <- semTools::kd(shen.cor, 983, type = "exact") > > # rename columns in data frame and display correlation matrix > names(shen.data) <- c("acculscl", "status", "percent", "educ", "income", + "interpers", "job", "scl90d") > > # display correlation matrix > cor(shen.data) acculscl status percent educ income interpers job scl90d acculscl 1.00 0.44 0.69 0.21 0.23 0.12 0.09 0.03 status 0.44 1.00 0.54 0.08 0.15 0.08 0.06 0.02 percent 0.69 0.54 1.00 0.16 0.19 0.08 0.04 -0.02 educ 0.21 0.08 0.16 1.00 0.19 0.08 0.01 -0.07 income 0.23 0.15 0.19 0.19 1.00 -0.03 -0.02 -0.11 interpers 0.12 0.08 0.08 0.08 -0.03 1.00 0.38 0.37 job 0.09 0.06 0.04 0.01 -0.02 0.38 1.00 0.46 scl90d 0.03 0.02 -0.02 -0.07 -0.11 0.37 0.46 1.00 > > # descriptive statistics > psych::describe(shen.data) vars n mean sd median trimmed mad min max range skew kurtosis acculscl 1 983 0 1 0.02 0.00 0.98 -3.74 3.85 7.59 -0.04 0.03 status 2 983 0 1 0.05 0.00 0.96 -3.23 3.18 6.42 0.02 -0.04 percent 3 983 0 1 0.01 0.01 1.03 -3.70 2.95 6.65 -0.07 -0.09 educ 4 983 0 1 -0.04 -0.01 0.93 -3.35 3.33 6.68 0.10 0.33 income 5 983 0 1 0.00 0.01 1.03 -4.02 2.91 6.93 -0.09 -0.09 interpers 6 983 0 1 -0.01 0.00 0.98 -2.93 3.52 6.46 0.02 0.10 job 7 983 0 1 0.00 -0.01 1.00 -2.75 3.23 5.98 0.09 -0.06 scl90d 8 983 0 1 0.01 0.01 0.99 -2.95 3.05 6.00 -0.05 -0.14 se acculscl 0.03 status 0.03 percent 0.03 educ 0.03 income 0.03 interpers 0.03 job 0.03 scl90d 0.03 > > # vif within blocks of multiple indicators > # regress each indicator on the rest > # extract r-squared > # vif = 1/(1 - r-squared) > > # calculate vif for multiple indicators > # of a composite > > # acculturation indicators (3) > acculscl.lm <- lm(acculscl ~ status + percent, data = shen.data) > acculscl.vif = 1/(1 - summary(acculscl.lm)$r.squared) > acculscl.vif [1] 1.932415 > > status.lm <- lm(status ~ acculscl + percent, data = shen.data) > status.vif = 1/(1 - summary(status.lm)$r.squared) > status.vif [1] 1.429125 > > percent.lm <- lm(percent ~ acculscl + status, data = shen.data) > percent.vif = 1/(1 - summary(percent.lm)$r.squared) > percent.vif [1] 2.199745 > > # ses indicators (2) > # same vif for both variables > # r <- cor(shen.data$educ, shen.data$income) > educ.vif = 1/(1 - cor(shen.data$educ, shen.data$income)**2) > income.vif = educ.vif > educ.vif [1] 1.037452 > income.vif [1] 1.037452 > > # stress indicators (2) > # same vif for both variables > interpers.vif = 1/(1 - cor(shen.data$interpers, shen.data$job)**2) > job.vif = interpers.vif > interpers.vif [1] 1.16877 > job.vif [1] 1.16877 > > # write data frame to external .csv file > > # note that function kd() generates a different set of > # standardized scores each time it is run, but the > # correlation matrix is alweays the same > > # ** IMPORTANT ** > # change the path name listed next for your own computer to > # reproduce this analysis > # set working directory for writing the output file > setwd(file.path("C:", "Users", "Rex Kline", "Desktop")) > # show working directory > getwd() [1] "C:/Users/Rex Kline/Desktop" > write.csv(shen.data, "shen.csv", row.names = FALSE)