Sunday, February 28, 2021

SEM in R

library(psych)
all=read.csv(choose.files())
#Calculating_Cronbach's Alpha
covid_concern<- alpha(data.frame(all[c("CO2", "CO3", "CO5", "CO7")]))
attitude<- alpha(data.frame(all[c("SN3", "PBC1", "PBC2")]))
social_norm<- alpha(data.frame(all[c("CO4", "AT7", "SN1", "SN2", "PMO1")]))
perc_beh_control<- alpha(data.frame(all[c("AT2", "AT4", "AT5", "AT6")]))
perc_mor_obligation<-alpha(data.frame(all[c("CO8", "AT1")]))

####sem

library(lavaan)
library(semPlot)

#all=read.csv(choose.files())

model1<- '
# Structural model
CO=~CO2+ CO3 +CO5 +CO7
AT=~SN3+ PBC1+ PBC2
SN=~CO4+ AT7+ SN1+ SN2+ PMO1
PBC=~AT2+AT4+AT5+AT6
PMO=~CO8+AT1

# Covariance structure of exogenous variables

# New parameters (indirect effect)
#Regression
AT~CO
SN~CO
PBC~CO

PMO~AT
PMO~SN
PMO~PBC
                  '
fit1<- sem(model1, data=all)
fit1

summary(fit1, rsquare = TRUE,
        fit.measures = TRUE,
        standardized = TRUE)


fitMeasures(fit1)

semPaths(fit1, what="paths", whatLabels = "stand",
         rotation = 2,
         layout = "spring",
         posCol = "black",
         edge.width = 0.5, 
         style = "Lisrel",
         fade = T,
         edge.label.position = 0.55)
###############

# Extract the correlation matrix
all.cor <- cor(all[], method = "pearson", use = "pairwise.complete.obs")
all.cor


# Correlogram
corrplot(all.cor, order = "hclust",
         tl.col = "black", tl.srt = 80,
         addCoef.col = "black",
         number.cex = 0.8,
         cl.cex = 1,
         tl.cex = 0.8)

library(corrplot)
library(RColorBrewer)

#library(psych)
#corPlot(data, cex =1.2, main="", 
#       cex.lab = 1.2,
#        cex.axis =1.2,
#       cex.main = 1.2,
#      cex.sub = 1.2)

library(psych)
cor.plot(all.cor,numbers=TRUE,colors=TRUE,
         n=51,main=NULL,labels=NULL,
         cex =1,
         cex.lab = 1,
         cex.axis =1, #right side level
         cex.main = 1,
         cex.sub = 1)



0 comments:

Post a Comment