# 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() v <- R.Version() print(paste0(v$language, " version ", v$major, ".", v$minor, " (", v$year, "-", v$month, "-", v$day, ")")) library(semTools) library(lavaan) library(psych) # get citation information citation("semTools", auto = TRUE) citation("lavaan", auto = TRUE) citation("psych", auto = TRUE) # 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) # descriptive statistics psych::describe(shen.data) # 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 status.lm <- lm(status ~ acculscl + percent, data = shen.data) status.vif = 1/(1 - summary(status.lm)$r.squared) status.vif percent.lm <- lm(percent ~ acculscl + status, data = shen.data) percent.vif = 1/(1 - summary(percent.lm)$r.squared) percent.vif # 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 income.vif # 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 job.vif # 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() write.csv(shen.data, "shen.csv", row.names = FALSE)