Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFA nice path plots

Tags:

r

When I do an EFA from the package psych I can generate really nice path plots with correlations and loadings and such like this:

enter image description here

using the fa.diagram function with the following code:

library(psych)
covariances <- ability.cov$cov
correlations <- cov2cor(covariances)
fa.promax <- fa(correlations, nfactors=2, rotate="promax", fm="pa")
fa.diagram(fa.promax, simple=FALSE)

I tend to use CFA more than EFA and would like to be able to plot the model with correlations in the same way. I use the SEM package. I have tried the pathDiagram function but it only produces the code that I can then paste to GVedit from Graphviz. I do that and the correlations and between factor correlations aren't there. Additionally I don't like cutting and pasting to another program (though the help file from sem for pathDiagram reads: "To obtain graphics output directly, the dot program must be on the system search path."; I don't know what exactly to put in the path to make that happen and if it will even have the correlations anyway?)

I'd like to be able to make a path diagram similar to what fa.diagram from psych does for EFA. I'd like to be able to do this in R and not cut and paste. I don't care what package you use to do this but I think most people would agree the simpler the better and if it can be done within R it would give great freedom of the choice of graphics device.

Here is some sample code for a CFA model to work with:

dat3 <- read.table(url("http://dl.dropbox.com/u/61803503/Proj_2b.dat"), 
            header=T, strip.white = T, as.is=FALSE, 
            na.strings= c("999", "NA", " "))

NHSDA.cov <- cov(dat3)

ete.mod <- specifyModel() 
F1 -> item3, lam1, NA 
F1 -> item5, lam2, NA 
F1 -> item9, lam3, NA 
F1 -> item10, lam4, NA 
F2 -> item4, lam5, NA 
F2 -> item13, lam6, NA 
F2 -> item14, lam7, NA 
F2 -> item15, lam8, NA 
F3 -> item1, lam9, NA 
F3 -> item6, lam10, NA 
F3 -> item7, lam11, NA 
F3 -> item11, lam12, NA 
F3 -> item12, lam13, NA 
F4 -> item2, lam14, NA 
F4 -> item8, lam15, NA 
F4 -> item16, lam16, NA 
F4 -> item17, lam17, NA 
item1 <-> item1, e1, NA 
item2 <-> item2, e2, NA 
item3 <-> item3, e3, NA 
item4 <-> item4, e4, NA 
item5 <-> item5, e5, NA 
item6 <-> item6, e6, NA 
item7 <-> item7, e7, NA 
item8 <-> item8, e8, NA 
item9 <-> item9, e9, NA 
item10 <-> item10, e10, NA 
item11 <-> item11, e11, NA 
item12 <-> item12, e12, NA 
item13 <-> item13, e13, NA 
item14 <-> item14, e14, NA 
item15 <-> item15, e15, NA 
item16 <-> item16, e16, NA 
item17 <-> item17, e17, NA 
F1 <-> F1, NA, 1
F2 <-> F2, NA, 1
F3 <-> F3, NA, 1
F4 <-> F4, NA, 1
F2 <-> F1, rF1F2 #1
F3 <-> F1, rF1F3 #2
F4 <-> F1, rF1F4 #3
F3 <-> F2, rF2F3 #4
F4 <-> F2, rF2F4 #5
F4 <-> F3, rF3F4 #6

ete.sem <- sem(ete.mod, NHSDA.cov, nrow(dat3)) 
(ete.SUM <- summary(ete.sem))

pathDiagram(ete.sem) #the attempt thus far

Running R 2.14.2 on Win 7

like image 419
Tyler Rinker Avatar asked Jun 12 '26 22:06

Tyler Rinker


2 Answers

Try like this:

pathDiagram(
              model=ete.sem
            , file="PathDiagram"
            , max.rank=paste("item", 1:17, sep="")
            , ignore.double=FALSE
            , edge.labels="values"
            , size=c(8, 8)
            , node.font=c("Helvetica", 10)
            , edge.font=c("Helvetica", 10)
            , rank.direction="LR"
            , digits=3
            , standardize=TRUE
            , graphics.fmt="pdf"
            )

This will generate PathDiagram.pdf in your working directory. I don't know anyway to display the graph in R.

enter image description here

like image 164
MYaseen208 Avatar answered Jun 15 '26 15:06

MYaseen208


I just had the same problem. The application Graphviz needs to be installed, which creates then a PDF file out of the .dot file. This is also explained in the help file of the R package pathDiagram.

like image 24
Roman Avatar answered Jun 15 '26 15:06

Roman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!