I have two dependent (endogenous) variables that are continuous and normalized.
Among the explanatory (exogenous) variables, there are both continuous variables and factors (recoded as dummy variables (0/1)). In addition, observations (individual nestlings) are clustered in another variable (nest ID, coded as 1,2,3,… and treated as a continuous variable). There may be 1, 2 or 3 nestlings per nest, and thus, samples are not completely independent.
The dataset is complete and I wonder what estimator should I use. I tried with ML and MLR.
Results are apparently correct. However, I’m not
familiar with R, and I still have many doubts about the syntax, etc…
so, I would appreciate very much if you could take a look to the script (below) to check if I made any important mistake.
Any advice and/or suggestion will be most wellcome.
Thank you very much in advance
#Installing packages
install.packages("lavaan", dependencies = TRUE)
library(lavaan)
library (survey)
install.packages("lavaan.survey")
library (lavaan.survey)
#calling dataset
data<-read.table("cut_GLM.txt",header=TRUE, sep="\t")
#inspecting dataset
str(data)
#coding categorical factors as dummy (0/1) variables
data$dSEX<-model.matrix(~as.factor(data$SEX))[,-1]
data$dfosterF<-model.matrix(~as.factor(data$fosterF))[,-1]
data$dfosterM<-model.matrix(~as.factor(data$fosterM))[,-1]
data$dbiolF<-model.matrix(~as.factor(data$biolF))[,-1]
data$dbiolM<-model.matrix(~as.factor(data$biolM))[,-1]
data$dMC1R<-model.matrix(~as.factor(data$MC1R))[,-1]
# centering and rescaling continuous explanatory variables
data$sage<-scale (data$age)
data$sweight<-scale (data$weight)
data$sbrood<-scale (data$brood)
#Initial SEM model with direct and indirect effects
model1 <- "
PHA~ dMC1R + sweight
IHA~ dMC1R + sweight + PHA
sweight~ dfosterF + sbrood + sage
"
#PA1 path analysis without cluster.
PA1 <- lavaan(model1, data=data,auto.var=TRUE,std.ov = TRUE, estimator="ML")
summary(PA1)
#group= defining the cluster
group<- svydesign(ids=~nest, prob=~1, data=data)
#path análisis with cluster
survey.fit <- lavaan.survey(lavaan.fit=PA1, survey.design=group)
summary(survey.fit)
# parameter estimates, SE, z-values, standarized parameter values
parameterEstimates (survey.fit)
# model-implied (fitted) covariance matrix (and mean vector) of a fitted model
fitted (survey.fit)
#residuals of a fitted model
resid (fit.survey, type = "standarized")
#estimated covariance matrix of the parameter estimates
vcov(survey.fit)
AIC(survey.fit)
BIC(survey.fit)