-
Notifications
You must be signed in to change notification settings - Fork 0
/
sem_script.R
299 lines (246 loc) · 9.44 KB
/
sem_script.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#####################################################################################
#SETTING ENVIRONMENT
#####################################################################################
#Load packages needes for the analysis
lapply(c("ggplot2", "psych", "RCurl", "irr", "nortest", "moments","nFactors",
"psych","GPArotation", "gmodels","sqldf","gdata","sem"), library, character.only=T)
#####################################################################################
#IMPORTING DATA
#####################################################################################
#uploading data ------------------------------------------------------------------------
#Functions to pull the dara from the internet file
#see http://goo.gl/mQwxO on how to get this link
data<-read.csv("/Users/joaovissoci/Desktop/mergeRC1.csv",header=T)
###########################################################################################
#EXPLORATORY DATA ANALYSIS
###########################################################################################
#Exploratory Data Anlysis
#dim(data)
str (data) #Wll give you the classification of each variable
#head(data) #Will show you the first 6 observations of the dataset
#tail(data) #Will show you the ast 6 observations of the dataset
#names(data) #Will show you the names for all the variables
summary(data)#This comand will provide a whole set of descriptive results for each variables
#ad.test() # Anderson-Darling test for normality
#skewness() #Will provide skweness analysis
#kurtosis() - 3 #Will provide kurtosis analysis
#qplot() # histogram plot
#pwr.anova.test(k = , n = , f = , sig.level = , power = )#Power analysis for variance analysis
#boxplot() #will provide a boxplot for the variables to analysis potential outliers
###########################################################################################
#DATA MANAGEMENT
###########################################################################################
#Excluding ST1, MD6, SH5 due ot missing data
data<-with(data,data.frame(hindex,noOfDocs,IDPT,IM,MR,
AFR,BR,DR,GDP,HEP,HET))
summary(data)
data$hindex<-as.numeric(as.character(data$hindex))
data$noOfDocs<-as.numeric(as.character(data$noOfDocs))
data$IM<-as.numeric(as.character(data$IM))
data$MR<-as.numeric(as.character(data$MR))
data$AFR<-as.numeric(as.character(data$AFR))
data$BR<-as.numeric(as.character(data$BR))
data$DR<-as.numeric(as.character(data$DR))
data$GDP<-as.numeric(as.character(data$GDP))
data$HEP<-as.numeric(as.character(data$HEP))
data$HET<-as.numeric(as.character(data$HET))
str(data)
datanew<-na.omit(data)
str(datanew)
write.csv(data, "/Users/joaovissoci/Desktop/LODHealthAquamethData.csv")
#AI<-data.frame(data$AI1,data$AI2,data$AI3,data$AI4)
#dim(AI)
#SH<-data.frame(data$SH2,data$SH3,data$SH4)
#MD<-data.frame(data$MD1,data$MD2,data$MD3,data$MD4,data$MD5)
#EI<-data.frame(data$EI1,data$EI2)
#Health<-data.frame(data$H1,data$H2,data$H3)
#HI<-data.frame(SH,MD)
###########################################################################################
#TABLE 1: EFA
###########################################################################################
#Correlations
qgraph(cor(datanew,method="spearman"),cut=1,minimum=.20,layout="spring",
directed=FALSE,label.scale=FALSE,edge.labels=TRUE,label.cex = 0.80)
#efadata<-remove.vars(data,names=c("Country","Year","Year_1","X"))
#efadata<-na.omit(efadata)
#str(efadata)
##Eigen-Values and Scree plot
par(mfrow=c(2,2)) #Command to configure the plot area for the scree plot graph
ev <- eigen(cor(datanew)) # get eigenvalues - insert the data you want to calculate the scree plot for
ev # Show eigend values
ap <- parallel(subject=nrow(datanew),var=ncol(datanew),rep=100,cent=.05) #Calculate the acceleration factor
nS <- nScree(ev$values) #Set up the Scree Plot
plotnScree(nS) # Plot the ScreePlot Graph
#KMO
kmo = function(data){
library(MASS)
X <- cor(as.matrix(data))
iX <- ginv(X)
S2 <- diag(diag((iX^-1)))
AIS <- S2%*%iX%*%S2 # anti-image covariance matrix
IS <- X+AIS-2*S2 # image covariance matrix
Dai <- sqrt(diag(diag(AIS)))
IR <- ginv(Dai)%*%IS%*%ginv(Dai) # image correlation matrix
AIR <- ginv(Dai)%*%AIS%*%ginv(Dai) # anti-image correlation matrix
a <- apply((AIR - diag(diag(AIR)))^2, 2, sum)
AA <- sum(a)
b <- apply((X - diag(nrow(X)))^2, 2, sum)
BB <- sum(b)
MSA <- b/(b+a) # indiv. measures of sampling adequacy
AIR <- AIR-diag(nrow(AIR))+diag(MSA) # Examine the anti-image of the
# correlation matrix. That is the
# negative of the partial correlations,
# partialling out all other variables.
kmo <- BB/(AA+BB) # overall KMO statistic
# Reporting the conclusion
if (kmo >= 0.00 && kmo < 0.50){
test <- 'The KMO test yields a degree of common variance
unacceptable for FA.'
} else if (kmo >= 0.50 && kmo < 0.60){
test <- 'The KMO test yields a degree of common variance miserable.'
} else if (kmo >= 0.60 && kmo < 0.70){
test <- 'The KMO test yields a degree of common variance mediocre.'
} else if (kmo >= 0.70 && kmo < 0.80){
test <- 'The KMO test yields a degree of common variance middling.'
} else if (kmo >= 0.80 && kmo < 0.90){
test <- 'The KMO test yields a degree of common variance meritorious.'
} else {
test <- 'The KMO test yields a degree of common variance marvelous.'
}
ans <- list( overall = kmo,
report = test,
individual = MSA,
AIS = AIS,
AIR = AIR )
return(ans)
} # end of kmo()
kmo(datanew)
## EFA para o banco todo
health_efa<-with(datanew,data.frame(IDPT,IM,MR,
AFR,BR,HEP))
efa_HO<-fa(health_efa,1,rotate="promax")
###########################################################################################
#SEM MODELS
###########################################################################################
#SEM Model C - Working but poor fit indicators
datanew<-remove.vars(datanew,names=c("DR"))
#attach(semdata)
#FINAL MODEL
semmodel<- specifyModel()# Type these values that specify the model's relations (just use de Ctrl+R over each relation).
#Latent Variables
HealthOut->IDPT,efa14,NA
HealthOut->IM,efa11,NA
HealthOut->MR,efa12,NA
HealthOut->AFR,efa13,NA
HealthOut->BR,NA,1
HealthOut->HEP,efa16,NA
hindex->GDP,ob1,NA
HET->GDP,ob3,NA
hindex->HealthOut,lat12,NA
HET->HealthOut,lat14,NA
HealthOut<->HealthOut,laterro1,NA
GDP<->GDP,erro1,NA
hindex<->hindex,erro3,NA
noOfDocs<->noOfDocs,erro4,NA
HET<->HET,erro5,NA
MR<->AFR,erro12,NA
HET<->hindex,erro13,NA
#End of the model
# Insert de covariance matrix
var<-var(semdata)
cov<-cov(datanew)
cor<-cor(datanew)
#Running SEM model
sem <- sem::sem(semmodel,cor, N=781)
summary(sem,fit.indices=c("GFI", "AGFI", "RMSEA", "NFI",
"NNFI", "CFI", "RNI", "IFI", "SRMR", "AIC", "AICc"))
modIndices(sem)
qgraph(sem,cut = 0.8,gray=TRUE)
#SEM Model B - Working but poor fit indicators
semmodel<- specifyModel()# Type these values that specify the model's relations (just use de Ctrl+R over each relation).
#Latent Variables
HealthOut->IDPT,efa14,NA
HealthOut->IM,efa11,NA
HealthOut->MR,efa12,NA
HealthOut->AFR,efa13,NA
HealthOut->BR,NA,1
HealthOut->HEP,efa16,NA
hindex->GDP,ob1,NA
HET->GDP,ob3,NA
hindex->HealthOut,lat12,NA
HET->HealthOut,lat14,NA
HealthOut<->HealthOut,laterro1,NA
GDP<->GDP,erro1,NA
hindex<->hindex,erro3,NA
noOfDocs<->noOfDocs,erro4,NA
HET<->HET,erro5,NA
#End of the model
# Insert de covariance matrix
var<-var(semdata)
cov<-cov(datanew)
cor<-cor(datanew)
#Running SEM model
sem <- sem::sem(semmodel,cor, N=781)
summary(sem,fit.indices=c("GFI", "AGFI", "RMSEA", "NFI",
"NNFI", "CFI", "RNI", "IFI", "SRMR", "AIC", "AICc"))
modIndices(sem)
qgraph(sem,cut = 0.8,gray=TRUE)
#SEM Model A - Working but poor fit indicators
semmodel<- specifyModel()# Type these values that specify the model's relations (just use de Ctrl+R over each relation).
#Latent Variables
HealthOut->IDPT,efa14,NA
HealthOut->IM,efa11,NA
HealthOut->MR,efa12,NA
HealthOut->AFR,efa13,NA
HealthOut->BR,NA,1
HealthOut->HEP,efa16,NA
RD->hindex,NA,1
RD->HET,efa22,NA
RD->noOfDocs,efa23,NA
RD->GDP,ob1,NA
RD->HealthOut,lat14,NA
HealthOut<->HealthOut,laterro1,NA
GDP<->GDP,erro1,NA
RD<->RD,erro2,NA
hindex<->hindex,erro3,NA
noOfDocs<->noOfDocs,erro4,NA
HET<->HET,erro5,NA
#End of the model
# Insert de covariance matrix
var<-var(semdata)
cov<-cov(datanew)
cor<-cor(datanew)
#Running SEM model
sem <- sem::sem(semmodel,cor, N=781)
summary(sem,fit.indices=c("GFI", "AGFI", "RMSEA", "NFI",
"NNFI", "CFI", "RNI", "IFI", "SRMR", "AIC", "AICc"))
modIndices(sem)
qgraph(sem,cut = 0.8,gray=TRUE)
#SEM Model D - Working but poor fit indicators
semmodel<- specifyModel()# Type these values that specify the model's relations (just use de Ctrl+R over each relation).
#Latent Variables
HealthOut->IDPT,efa14,NA
HealthOut->IM,efa11,NA
HealthOut->MR,efa12,NA
HealthOut->AFR,efa13,NA
HealthOut->BR,NA,1
HealthOut->HEP,efa16,NA
GDP->hindex,ob1,NA
GDP->HET,ob3,NA
hindex->HealthOut,lat12,NA
HET->HealthOut,lat14,NA
HealthOut<->HealthOut,laterro1,NA
GDP<->GDP,erro1,NA
hindex<->hindex,erro3,NA
noOfDocs<->noOfDocs,erro4,NA
HET<->HET,erro5,NA
#End of the model
# Insert de covariance matrix
var<-var(semdata)
cov<-cov(datanew)
cor<-cor(datanew)
#Running SEM model
sem <- sem::sem(semmodel,cor, N=781)
summary(sem,fit.indices=c("GFI", "AGFI", "RMSEA", "NFI",
"NNFI", "CFI", "RNI", "IFI", "SRMR", "AIC", "AICc"))
modIndices(sem)