library(rmarkdown); library(knitr)
library(dplyr)
library(psych); library(corrplot); library(GPArotation)
library(lavaan); #library(semPlot); library(moments)
HEALTHDATA <- read.csv("https://www.dropbox.com/s/sb8s1i4h1z7g7fh/?dl=1")
attach(HEALTHDATA)
set.seed(37)
Construct a vector of starting values, BETASTART, that default to 0.5, for the non-zero entries in the matrix of regression coefficients between the DVs, BETA
BETASTART <- rep(0.5, 4)
Use the vector of starting values above to compute the matrix of regression coefficients between the DVs, BETA (hint: the command dimnames() can be used to set the names of each row and column)
BETA <- matrix(0, nrow = 6, ncol = 6)
dimnames(BETA) <- list(c("Isol", "Troub", "S-H", "Thots", "Psych Dist", "Suicidality"), c("Isol", "Troub", "S-H", "Thots", "Psych Dist", "Suicidality"))
BETA[1, 5] = BETASTART[1]
BETA[2, 5] = BETASTART[2]
BETA[3, 6] = BETASTART[3]
BETA[4, 6] = BETASTART[4]
BETA
## Isol Troub S-H Thots Psych Dist Suicidality
## Isol 0 0 0 0 0.5 0.0
## Troub 0 0 0 0 0.5 0.0
## S-H 0 0 0 0 0.0 0.5
## Thots 0 0 0 0 0.0 0.5
## Psych Dist 0 0 0 0 0.0 0.0
## Suicidality 0 0 0 0 0.0 0.0
Construct a vector of starting values, GAMMASTART, that default to 0.5, for the non-zero entries in the matrix of regression coefficients from the IVs to the DVs, GAMMA
GAMMASTART <- rep(0.5, 10)
Use the vector of starting values above to compute the matrix of regression coefficients from the IVs to the DVs, GAMMA
GAMMA <- matrix(0, nrow = 6, ncol = 8)
dimnames(GAMMA) <- list(c("Isol", "Troub", "S-H", "Thots", "Psych Dist", "Suicidality"), c("Trauma", "Sleep", "E1", "E2", "E3", "E4", "D1", "D2"))
GAMMA[1, 3] = GAMMASTART[1]
GAMMA[2, 4] = GAMMASTART[2]
GAMMA[3, 5] = GAMMASTART[3]
GAMMA[4, 6] = GAMMASTART[4]
GAMMA[5, 1] = GAMMASTART[5]
GAMMA[5, 2] = GAMMASTART[6]
GAMMA[5, 7] = GAMMASTART[7]
GAMMA[6, 1] = GAMMASTART[8]
GAMMA[6, 2] = GAMMASTART[9]
GAMMA[6, 8] = GAMMASTART[10]
GAMMA
## Trauma Sleep E1 E2 E3 E4 D1 D2
## Isol 0.0 0.0 0.5 0.0 0.0 0.0 0.0 0.0
## Troub 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0
## S-H 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0
## Thots 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0
## Psych Dist 0.5 0.5 0.0 0.0 0.0 0.0 0.5 0.0
## Suicidality 0.5 0.5 0.0 0.0 0.0 0.0 0.0 0.5
Construct a vector of starting values, PHISTART, that default to 0.5, for the non-zero entries in the matrix of variances among the IVs, PHI
PHISTART <- rep(0.5, 8)
Use the vector of starting values above to compute the matrix of variances among the IVs, PHI
PHI <- matrix(0, nrow = 8, ncol = 8)
dimnames(PHI) <- list(c("Trauma", "Sleep", "E1", "E2", "E3", "E4", "D1", "D2"), c("Trauma", "Sleep", "E1", "E2", "E3", "E4", "D1", "D2"))
PHI[1, 1] = PHISTART[1]
PHI[2, 2] = PHISTART[2]
PHI[3, 3] = PHISTART[3]
PHI[4, 4] = PHISTART[4]
PHI[5, 5] = PHISTART[5]
PHI[6, 6] = PHISTART[6]
PHI[7, 7] = PHISTART[7]
PHI[8, 8] = PHISTART[8]
PHI
## Trauma Sleep E1 E2 E3 E4 D1 D2
## Trauma 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0
## Sleep 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0
## E1 0.0 0.0 0.5 0.0 0.0 0.0 0.0 0.0
## E2 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0
## E3 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0
## E4 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0
## D1 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0
## D2 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5
Explain what the matrices above mean in terms of the total number of parameters to be estimated for the hypothesized model above
22, which is the sum of all non-zero perameters.
Construct the selection matrix for the DVs, G_y
GYSTART <- rep(1, 4)
G_y <- matrix(0, nrow = 4, ncol = 6)
dimnames(G_y) <- list(c("Isol", "Troub", "S-H", "Thots"), c("Isol", "Troub", "S-H", "Thots", "Psych Dist", "Suicidality"))
G_y[1,1] <- GYSTART[1]
G_y[2,2] <- GYSTART[2]
G_y[3,3] <- GYSTART[3]
G_y[4,4] <- GYSTART[4]
G_y
## Isol Troub S-H Thots Psych Dist Suicidality
## Isol 1 0 0 0 0 0
## Troub 0 1 0 0 0 0
## S-H 0 0 1 0 0 0
## Thots 0 0 0 1 0 0
Construct the selection matrix for the IVs, G_x
GXSTART <- rep(1, 2)
G_x <- matrix(0, nrow = 2, ncol = 8)
dimnames(G_x) <- list(c("Psych Dist", "Suicidality"), c("Trauma", "Sleep", "E1", "E2", "E3", "E4", "D1", "D2"))
G_x[1,1] <- GXSTART[1]
G_x[2,2] <- GXSTART[2]
G_x
## Trauma Sleep E1 E2 E3 E4 D1 D2
## Psych Dist 1 0 0 0 0 0 0 0
## Suicidality 0 1 0 0 0 0 0 0
Use diag() to construct an identity matrix, I, of the same dimensions as the matrix of regression coefficients between the DVs, BETA, above
I <- diag(1, nrow = 6)
Use the matrixes above to compute the quadrant of the estimated population covariance matrix, SH, that corresponds to the interaction between the DVs, SH_yy (hint: this is the top left quadrant)
SH_yy <- G_y %*% solve(I - BETA) %*% GAMMA %*% PHI %*% t(GAMMA) %*% t(solve(I - BETA)) %*% t(G_y)
SH_yy
## Isol Troub S-H Thots
## Isol 0.21875 0.09375 0.06250 0.06250
## Troub 0.09375 0.21875 0.06250 0.06250
## S-H 0.06250 0.06250 0.21875 0.09375
## Thots 0.06250 0.06250 0.09375 0.21875
Use the matrixes above to compute the quadrant of the estimated population covariance matrix, SH, that corresponds to the interaction between the DVs and IVs, SH_yx (hint: this is the top right quadrant)
SH_yx <- G_y %*% solve(I-BETA) %*% GAMMA %*% PHI %*% t(G_x)
SH_yx
## Psych Dist Suicidality
## Isol 0.125 0.125
## Troub 0.125 0.125
## S-H 0.125 0.125
## Thots 0.125 0.125
Use the matrixes above to compute the quadrant of the estimated population covariance matrix, SH, that corresponds to the interaction between the IVs and DVs, SH_xy (hint: this is the bottom left quadrant)
SH_xy <- t(SH_yx)
SH_xy
## Isol Troub S-H Thots
## Psych Dist 0.125 0.125 0.125 0.125
## Suicidality 0.125 0.125 0.125 0.125
Use the matrixes above to compute the quadrant of the estimated population covariance matrix, SH, that corresponds to the interaction between the IVs, SH_xx (hint: this is the bottom right quadrant)
SH_xx <- G_x %*% PHI %*% t(G_x)
SH_xx
## Psych Dist Suicidality
## Psych Dist 0.5 0.0
## Suicidality 0.0 0.5
Use rbind() and cbind() to combine the four quadrants above into the complete estimated population covariance matrix, SH, based on the starting values of 0.5 specified above
SH_y <- cbind(SH_yy, SH_yx)
SH_x <- cbind(SH_xy, SH_xx)
SH <- rbind(SH_y, SH_x)
SH
## Isol Troub S-H Thots Psych Dist Suicidality
## Isol 0.21875 0.09375 0.06250 0.06250 0.125 0.125
## Troub 0.09375 0.21875 0.06250 0.06250 0.125 0.125
## S-H 0.06250 0.06250 0.21875 0.09375 0.125 0.125
## Thots 0.06250 0.06250 0.09375 0.21875 0.125 0.125
## Psych Dist 0.12500 0.12500 0.12500 0.12500 0.500 0.000
## Suicidality 0.12500 0.12500 0.12500 0.12500 0.000 0.500
Use cbind() along with cov() to compute the (missing value) observed sample covariance matrix, S, for the interaction between ISOLATED, TROUBLED, SELFHARM, THOUGHTS, TRAUMA, and SLEEP
S <- cov(cbind(ISOLATED, TROUBLED, SELFHARM, THOUGHTS, TRAUMA, SLEEP), use = "pairwise.complete.obs")
S
## ISOLATED TROUBLED SELFHARM THOUGHTS TRAUMA SLEEP
## ISOLATED 0.09203732 0.03292405 0.01803685 0.02675912 0.1619459 -0.01645363
## TROUBLED 0.03292405 0.08603026 0.01548007 0.01965926 0.1949260 -0.02363915
## SELFHARM 0.01803685 0.01548007 0.18335916 0.08414752 0.1707942 -0.01764608
## THOUGHTS 0.02675912 0.01965926 0.08414752 0.22723028 0.3108219 -0.03020436
## TRAUMA 0.16194593 0.19492597 0.17079416 0.31082189 7.3207109 -0.55705698
## SLEEP -0.01645363 -0.02363915 -0.01764608 -0.03020436 -0.5570570 0.24858473
Use the observed sample covariance matrix, S, and the estimated population covariance matrix, SH, above to compute the final residual matrix, DIFF0
(DIFF0 <- S - SH)
## ISOLATED TROUBLED SELFHARM THOUGHTS TRAUMA
## ISOLATED -0.12671268 -0.06082595 -0.044463149 -0.035740878 0.03694593
## TROUBLED -0.06082595 -0.13271974 -0.047019928 -0.042840740 0.06992597
## SELFHARM -0.04446315 -0.04701993 -0.035390839 -0.009602476 0.04579416
## THOUGHTS -0.03574088 -0.04284074 -0.009602476 0.008480281 0.18582189
## TRAUMA 0.03694593 0.06992597 0.045794163 0.185821887 6.82071086
## SLEEP -0.14145363 -0.14863915 -0.142646084 -0.155204363 -0.55705698
## SLEEP
## ISOLATED -0.1414536
## TROUBLED -0.1486391
## SELFHARM -0.1426461
## THOUGHTS -0.1552044
## TRAUMA -0.5570570
## SLEEP -0.2514153
Explain what the residual matrix above means in terms of how successful the hypothesized structural equation model, based on the starting values of 0.5 specified above, was in reproducing the original covariance structure between ISOLATED, TROUBLED, SELFHARM, THOUGHTS, TRAUMA, and SLEEP
It worked well in a few places, but very poorly in others.
Adjust the starting values used for the non-zero entries in the matrix of regression coefficients between the DVs, BETA, from 0.5 to 0.6 and re-compute the final residual matrix, DIFF1
BETASTART <- rep(0.6, 4)
BETA <- matrix(0, nrow = 6, ncol = 6)
dimnames(BETA) <- list(c("Isol", "Troub", "S-H", "Thots", "Psych Dist", "Suicidality"), c("Isol", "Troub", "S-H", "Thots", "Psych Dist", "Suicidality"))
BETA[1, 5] = BETASTART[1]
BETA[2, 5] = BETASTART[2]
BETA[3, 6] = BETASTART[3]
BETA[4, 6] = BETASTART[4]
SH_yy <- G_y %*% solve(I - BETA) %*% GAMMA %*% PHI %*% t(GAMMA) %*% t(solve(I - BETA)) %*% t(G_y)
SH_yx <- G_y %*% solve(I-BETA) %*% GAMMA %*% PHI %*% t(G_x)
SH_xy <- t(SH_yx)
SH_y <- cbind(SH_yy, SH_yx)
SH_x <- cbind(SH_xy, SH_xx)
SH <- rbind(SH_y, SH_x)
(DIFF1 <- S - SH)
## ISOLATED TROUBLED SELFHARM THOUGHTS TRAUMA SLEEP
## ISOLATED -0.16796268 -0.10207595 -0.07196315 -0.06324088 0.01194593 -0.1664536
## TROUBLED -0.10207595 -0.17396974 -0.07451993 -0.07034074 0.04492597 -0.1736391
## SELFHARM -0.07196315 -0.07451993 -0.07664084 -0.05085248 0.02079416 -0.1676461
## THOUGHTS -0.06324088 -0.07034074 -0.05085248 -0.03276972 0.16082189 -0.1802044
## TRAUMA 0.01194593 0.04492597 0.02079416 0.16082189 6.82071086 -0.5570570
## SLEEP -0.16645363 -0.17363915 -0.16764608 -0.18020436 -0.55705698 -0.2514153
Explain what the nenwly-computed residual matrix above means in terms of whether or not adjusting the regression coefficients from 0.5 to 0.6 improved how successful the hypothesized structural equation model was in reproducing the original covariance structure between ISOLATED, TROUBLED, SELFHARM, THOUGHTS, TRAUMA, and SLEEP
Overall, changing the BETA starting values from 0.5 to 0.6 made the output equation model worse.