Skip to content

Commit

Permalink
Add plot method for osmasem2
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewlcheung committed Jul 27, 2024
1 parent f4f500d commit 8386ada
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 21 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: metaSEM
Type: Package
Title: Meta-Analysis using Structural Equation Modeling
Version: 1.4.4
Version: 1.4.5
Date: 2024-07-27
Depends: R (>= 3.4.0), OpenMx
Imports: Matrix, MASS, ellipse, graphics, stats, utils, mvtnorm, numDeriv, lavaan
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ S3method(plot, meta)
S3method(plot, character)
S3method(plot, wls)
S3method(plot, osmasem)
S3method(plot, osmasem2)
S3method(plot, mxRAMmodel)
# S3method(plot, osmasem3L)

Expand Down
77 changes: 61 additions & 16 deletions R/meta2semPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,41 @@ plot.osmasem <- function(x, manNames=NULL, latNames=NULL, labels=c("labels", "RA
if (!requireNamespace("semPlot", quietly=TRUE))
stop("\"semPlot\" package is required for this function.")

if (!is.element(class(x), c("osmasem", "osmasem3L")))
stop("\"x\" must be an object of either class \"osmasem\" or \"osmasem3L\".")
if (!is.element(class(x), c("osmasem", "osmasem2", "osmasem3L")))
stop("\"x\" must be an object of either class \"osmasem\", \"osmasem2\", or \"osmasem3L\".")

A <- x$mx.fit$Amatrix$result
S <- x$mx.fit$Smatrix$result
## S can be calculated when diag.constraints=FALSE
if (is.null(S)) {
if (is.element(class(x), "osmasem2")) {
A <- x$mx.fit$Amatrix$values
S <- x$mx.fit$Smatrix$values
} else {
## osmasem and osmasem3L: different from osmasem2 coz they replace error
## variances with other parameters
A <- x$mx.fit$Amatrix$result
S <- x$mx.fit$Smatrix$result
## S can be calculated when diag.constraints=FALSE
if (is.null(S)) {
S <- x$mx.fit$algebras$Smatrix$result
}
}

F <- x$mx.fit$Fmatrix$values
Id <- diag(nrow(S))

## Approximate sample covariance matrix
## The means in osmasem are correlation matrix
ObsCovs <- vec2symMat(apply(x$data$data[, x$labels$ylabels], 2, mean, na.rm=TRUE), diag=FALSE)
ImpCovs <- x$mx.fit$impliedR$result

if (is.null(manNames)) manNames <- x$labels$obslabels
if (is.element(class(x), "osmasem2")) {
ObsCovs <- vec2symMat(apply(x$data$data[, x$data$ylabels], 2, mean, na.rm=TRUE),
diag=!x$cor.analysis)
ImpCovs <- x$mx.fit$expSigma$result
if (is.null(manNames)) manNames <- x$data$obslabels
} else {
## A labels slot is required because osmasem allows subset.
## labels can be shorter than those in data$labels
ObsCovs <- vec2symMat(apply(x$data$data[, x$labels$ylabels], 2, mean,
na.rm=TRUE), diag=FALSE)
ImpCovs <- x$mx.fit$impliedR$result
if (is.null(manNames)) manNames <- x$labels$obslabels
}
## If manNames is still NULL
if (is.null(manNames)) manNames <- paste("X", seq(1, nrow(F), 1), sep="")

Expand All @@ -156,11 +172,28 @@ plot.osmasem <- function(x, manNames=NULL, latNames=NULL, labels=c("labels", "RA
}
}

out <- semPlot::ramModel(A=A, S=S, F=F, manNames=manNames, latNames=latNames, ObsCovs=ObsCovs,
ImpCovs=ImpCovs)

labels <- match.arg(labels)
if (labels=="labels") {
if (is.element(class(x), "osmasem2")) {
if (x$mean.analysis) {
## osmasem2 with mean structure
M <- x$mx.fit$Mmatrix$values
colnames(M) <- colnames(F)
out <- semPlot::ramModel(A=A, S=S, F=F, M=M, manNames=manNames,
latNames=latNames, ObsCovs=ObsCovs,
ImpCovs=ImpCovs)
} else {
## osmasem2 without mean structure
out <- semPlot::ramModel(A=A, S=S, F=F, manNames=manNames, latNames=latNames,
ObsCovs=ObsCovs, ImpCovs=ImpCovs)
}
} else {
## osmasem and osmasem3L
out <- semPlot::ramModel(A=A, S=S, F=F, manNames=manNames, latNames=latNames,
ObsCovs=ObsCovs, ImpCovs=ImpCovs)
}

if (is.element(class(x), c("osmasem", "osmasem3L"))) {
labels <- match.arg(labels)
if (labels=="labels") {
A.labels <- x$mx.fit$A0$labels
S.labels <- x$mx.fit$S0$labels
labels <- c(c(A.labels), c(S.labels))
Expand All @@ -170,7 +203,8 @@ plot.osmasem <- function(x, manNames=NULL, latNames=NULL, labels=c("labels", "RA
na.index <- is.na(out@Pars$label)
##out@Pars$label[na.index] <- sprintf("%.2f", out@Pars$est[na.index])
out@Pars$label[na.index] <- ""
}
}
}

invisible( semPlot::semPaths(out, what=what, nCharNodes=nCharNodes,
nCharEdges=nCharEdges, layout=match.arg(layout),
Expand All @@ -179,6 +213,17 @@ plot.osmasem <- function(x, manNames=NULL, latNames=NULL, labels=c("labels", "RA
weighted=weighted, ...) )
}

plot.osmasem2 <- function(x, manNames=NULL, latNames=NULL, labels=c("labels", "RAM"),
what="est", nCharNodes=0, nCharEdges=0,
layout=c("tree", "circle", "spring", "tree2", "circle2"),
sizeMan=8, sizeLat=8, edge.label.cex=1.3, color="white",
weighted=FALSE, ...) {
plot.osmasem(x, manNames=manNames, latNames=latNames, labels=labels,
what=what, nCharNodes=nCharNodes, nCharEdges=nCharEdges,
layout=layout, sizeMan=sizeMan, sizeLat=sizeLat,
edge.label.cex=edge.label.cex, color=color, weighted=weighted, ...)
}

## plot.osmasem3L <- function(x, manNames=NULL, latNames=NULL, labels=c("labels", "RAM"),
## what="est", nCharNodes=0, nCharEdges=0,
## layout=c("tree", "circle", "spring", "tree2", "circle2"),
Expand Down
4 changes: 2 additions & 2 deletions R/osmasem2.R
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,10 @@ osmasem2 <- function(model.name="osmasem2", RAM, data, cor.analysis=TRUE,
} else {
mx.fit <- mx.model
}

out <- list(mx.fit=mx.fit, mx.ind=mx.ind, mx.sat=mx.sat,
cor.analysis=cor.analysis, mean.analysis=mean.analysis,
n=sum(data$n), intervals.type=intervals.type)
n=sum(data$n), intervals.type=intervals.type, data=data)
class(out) <- "osmasem2"
out
}
Expand Down
2 changes: 1 addition & 1 deletion man/metaSEM-package.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
\tabular{ll}{
Package: \tab metaSEM\cr
Type: \tab Package\cr
Version: \tab 1.4.4\cr
Version: \tab 1.4.5\cr
Date: \tab 2024-07-27\cr
License: \tab GPL (>=2)\cr
LazyLoad: \tab yes\cr
Expand Down
2 changes: 1 addition & 1 deletion man/osmasem.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ modeling with moderating effects on SEM parameters. \emph{Psychological
Methods}, \bold{25} (4), 430-455. https://doi.org/10.1037/met0000245
}
\seealso{ \code{\link[metaSEM]{Cor2DataFrame}}, \code{\link[metaSEM]{create.vechsR}},
\code{\link[metaSEM]{create.Tau2}}, \code{\link[metaSEM]{create.V}}, \code{\link[metaSEM]{osmasem}}, \code{\link[metaSEM]{Nohe15}}
\code{\link[metaSEM]{create.Tau2}}, \code{\link[metaSEM]{create.V}}, \code{\link[metaSEM]{osmasem}}, \code{\link[metaSEM]{Nohe15}}, \code{\link[metaSEM]{issp05}}
}

% Add one or more standard keywords, see file 'KEYWORDS' in the
Expand Down
6 changes: 6 additions & 0 deletions man/plot.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
\alias{plot.character}
\alias{plot.wls}
\alias{plot.osmasem}
\alias{plot.osmasem2}
\alias{plot.mxRAMmodel}

\title{Plot methods for various objects
Expand Down Expand Up @@ -42,6 +43,11 @@
layout=c("tree", "circle", "spring", "tree2", "circle2"),
sizeMan=8, sizeLat=8, edge.label.cex=1.3, color="white",
weighted=FALSE, \dots)
\method{plot}{osmasem2}(x, manNames=NULL, latNames=NULL, labels=c("labels", "RAM"),
what="est", nCharNodes=0, nCharEdges=0,
layout=c("tree", "circle", "spring", "tree2", "circle2"),
sizeMan=8, sizeLat=8, edge.label.cex=1.3, color="white",
weighted=FALSE, \dots)
\method{plot}{mxRAMmodel}(x, manNames=NULL, latNames=NULL, labels=c("labels", "RAM"),
what="est", nCharNodes=0, nCharEdges=0,
layout=c("tree", "circle", "spring", "tree2", "circle2"),
Expand Down

0 comments on commit 8386ada

Please sign in to comment.