我使用此代码查找性别、年龄组之间的差异:
GeneralModel.fit = cfa(model = GeneralModel,
data=Invariance,
meanstructure = TRUE)
summary(GeneralModel.fit,
standardized=TRUE,
rsquare=TRUE,
fit.measure = TRUE)
parameterestimates(GeneralModel.fit, standardized=TRUE)
fitted(GeneralModel.fit)
residuals(GeneralModel.fit)
fitmeasures(GeneralModel.fit)
modificationindices(GeneralModel.fit)
semPaths(GeneralModel.fit,
whatLabels = "std",
layout = "tree")
Teenagers = subset(Invariance, Group == "0")
Teenagers.fit = cfa(GeneralModel,
data=Teenagers,
meanstructure=TRUE)
summary(Teenagers.fit,
standardized=TRUE,
rsquare=TRUE,
fit.measure=TRUE)
parameterestimates(Teenagers.fit, standardized = TRUE)
fitted(Teenagers.fit)
residuals(Teenagers.fit)
fitmeasures(Teenagers.fit)
semPaths(Teenagers.fit,
whatLabels = "std",
layout = "tree")
CollegesStudents = subset(Invariance, Group == "1")
CollegeStudents
CollegeStudents.fit = cfa(GeneralModel,
data=CollegeStudents,
meanstructure=TRUE)
summary(CollegeStudents.fit,
standardized=TRUE,
rsquare=TRUE,
fit.measure=TRUE)
parameterestimates(CollegeStudents.fit, standardized = TRUE)
fitted(CollegeStudents.fit)
residuals(CollegeStudents.fit)
fitmeasures(CollegeStudents.fit)
IM <- measurementInvariance(model=GeneralModel, data=Invariance, group="Group") 对于性别差异,我使用了同样的方法(当然,名字也不同)。
现在的问题是:
如何改变这一脚本,以找到不变的四组(青少年妇女,青少年男子,大学妇女,大学男子)?在创建子集时,是否必须使用代码Group1="Sex"和Group2="Age Group"来指定,例如:
Group2 == 0
Group1 == 0,Group1 == 0男性:Group1 == 1,Group2 == 0,
Group1 == 0, Group2 == 1
Group1 == 1, Group2 == 1?
F 218
我不知道我是否用这句话回答了我自己的问题,所以我想确认这是否错误,以及是否有更好的办法来做我想做的事。我提前感谢你。
发布于 2020-03-08 01:58:57
考虑在用户定义的方法中泛化您的进程,然后使用by运行传递给方法的各种级别的子集。由于某些子集可能不返回任何行,所以在tryCatch中包装方法可以在出错时安全地返回NULL (空结果):
find_diffs <- function(df) {
tryCatch({
model_fit = cfa(model = GeneralModel,
data = df
meanstructure = TRUE)
summary(model_fit,
standardized=TRUE,
rsquare=TRUE,
fit.measure = TRUE)
# NAMED LIST OF RESULTS
results <- list(estimates = parameterestimates(model_fit, standardized=TRUE),
fit = fitted(model_fit),
residuals = residuals(model_fit),
fitmeasures = fitmeasures(model_fit),
mod_indices = modificationindices(model_fit)
)
}, error = function(e) return(NULL)
)
}by NO
GeneralModel_fit <- find_diffs(Invariance)
# ALL RESULTS
GeneralModel_fit
# SELECT RESULTS
GeneralModel_fit$estimates
GeneralModel_fit$fit
GeneralModel_fit$residuals
...by ONE GROUP
sex_fit_list <- by(Invariance, Invariance$Group1, find_diffs)
# WOMEN
sex_fit_list$`0`
# MEN
sex_fit_list$`1`
age_fit_list <- by(Invariance, Invariance$Group2, find_diffs)
# TEENAGERS
age_fit_list$`0`
# COLLEGE STUDENTS
age_fit_list$`1`by 二组
sex_and_age_fit_list <- by(Invariance, Invariance[,c("Group1", "Group2")], find_diffs)
# TEEN GIRLS
sex_and_age_fit_list[[1]]
# COLLEGE WOMEN
sex_and_age_fit_list[[2]]
# TEEN BOYS
sex_and_age_fit_list[[3]]
# COLLEGE MEN
sex_and_age_fit_list[[4]]https://stackoverflow.com/questions/60570547
复制相似问题