library(readxl) install.packages("metaSEM") library("metaSEM") install.packages("OpenMx") library("OpenMx") #------------------------------------------------------------------------------------------------------------------- #get sample size practisesamplesize <- read_excel("C:/Users/JingJing/Desktop/samplesize for practise2.xlsx") samplesize=practisesamplesize$`sample size` samplesize #input correlation metrics #if cannot load the data, try with read table function to see the problem, usually the data format thing my.vec1=read.table(file="C:/Users/JingJing/Desktop/practise2.csv",sep=",",header=T,fill=TRUE) nvar=6 varname=c("par","indiriver","commdriver","instrumentdriver","indiout","hostout") labels=list(varname,varname) my.vec <- list() for (i in 1:nrow(my.vec1)){ my.vec[[i]] <- vec2symMat(as.matrix(my.vec1[i,1:15]), diag = FALSE) dimnames(my.vec[[i]]) <- labels } head(my.vec) # put NA on diagonal if variable is missing for (i in 1:length(my.vec)){ for (j in 1:nrow(my.vec[[i]])){ if (sum(is.na(my.vec[[i]][j,]))==nvar-1) {my.vec[[i]][j,j] <- NA} }} my.vec[1] #take care of missing correlation # put NA on diag for var with least present correlations for (i in 1:length(my.vec)){ for (j in 1:nrow(my.vec[[i]])){ for (k in 1:nvar){ if (is.na(my.vec[[i]][j,k])==TRUE &is.na(my.vec[[i]][j,j])!=TRUE &is.na(my.vec[[i]][k,k])!=TRUE){ if(sum(is.na(my.vec[[i]])[j,])>sum(is.na(my.vec[[i]])[k,])) {my.vec[[i]][k,k] <- NA} if(sum(is.na(my.vec[[i]])[j,])<=sum(is.na(my.vec[[i]])[k,])) {my.vec[[i]][j,j] <- NA} }}}} # try fix efect stage 1-assume homogeneity across study stage1fixed <- tssem1(Cov=my.vec, n=samplesize, method="FEM") #got returned errors that the matrix are not positively definite install.packages("Matrix") library("Matrix") my.vec[c(133,135,136,137,139)] nearPD(my.vec[133])