plot - Plotting Dewey and Lu path analysis results as a Path diagram in R -
i trying plot outputs of path analysis suggested dewey , lu (1959) in r.
require(agricolae) require(hmisc) data(wilt) data(soil) x<-soil[,c(3,12,14,20)] y<-wilt[,14] info <- cbind(y,x) #correlation of independant variables cor.x <- rcorr(as.matrix(x))$r #correlation of dependant variable independant variables cor.y <- as.data.frame(t(subset(rcorr(as.matrix(cbind(y,x)))$r, select = c(y))))[,-1] #path analysis path <- path.analysis(cor.x,cor.y) #direct effects diag(path$coeff) #residual effects path$residual i want plot direct effects of independent variables dependent variable y , correlations among dependent variables follows along residual effect.
i have tried semplot , path.diagram {sem} , qgraph.lavaan, plot model only. pathdiagram not plot border labels (path coefficients , correlations). how in r?
this far got using `diagram package.
par(mar = c(1, 1, 1, 1)) openplotmat() # plot coordinates elpos <- coordinates (c(2, length(cor.y))) # adjust coordinates residual elpos[2,1] <- abs((elpos[1,1]+elpos[1,2])/2) #specify arrow positions #1 residual dependent ft1 <- matrix(ncol = 2, byrow = true, info = c(1, 2)) #2 independent dependent ft2 <- matrix(ncol=2, byrow = false, data= c(seq((2+length(cor.y)))[3:(length(cor.y)+2)], rep(2, length(cor.y)))) #3 cor.x fromto_cu <- t(combn(seq((2+length(cor.y)))[3:(length(cor.y)+2)],2)) #4 path distances fromto_st <- rbind(ft1,ft2) # plot path distance arrows nr <- nrow(fromto_st) arrpos <- matrix(ncol = 2, nrow = nr) (i in 1:nr) arrpos[i, ] <- straightarrow (to = elpos[fromto_st[i, 2], ], = elpos[fromto_st[i, 1], ], lwd = 2, arr.pos = 0.6, arr.length = 0.5) #label residual path distance arrow text(arrpos[1, 1], arrpos[1, 2] + 0.05, paste("p", "x", nrow(cor.x)+1," = ", round(path$residual, 2), sep=""), cex=0.6) #label path distance arrows nr <- nrow(arrpos) for(i in 2:nr){ text(arrpos[i, 1], arrpos[i, 2] + 0.05, paste("p", "x", i-1," = ", round(diag(path$coeff)[i-1], 2), sep=""), cex=0.6) } # plot correlation arrows direction 1 nr <- nrow(fromto_cu) arrpos <- matrix(ncol = 2, nrow = nr) (i in 1:nr) arrpos[i, ] <- curvedarrow (to = elpos[fromto_cu[i, 2], ], = elpos[fromto_cu[i, 1], ], lwd = 2, arr.pos = 0.8, arr.length = 0.5, curve = 0.35) # plot correlation arrows - direction 2 nr <- nrow(fromto_cu) arrpos <- matrix(ncol = 2, nrow = nr) (i in 1:nr) arrpos[i, ] <- curvedarrow (to = elpos[fromto_cu[i, 1], ], = elpos[fromto_cu[i, 2], ], lwd = 2, arr.pos = 0.8, arr.length = 0.5, curve = -0.35) # create combinations of cor.x labelling rxy in correlation arrows rcomb <- as.data.frame(t(combn(seq(nrow(cor.x)),2))) rcomb <- paste(rcomb$v1,rcomb$v2, sep="") # label correlation arrows nr <- nrow(fromto_cu) arrpos <- matrix(ncol = 2, nrow = nr) (i in 1:nr) arrpos[i, ] <- curvedarrow (to = elpos[fromto_cu[i, 1], ], = elpos[fromto_cu[i, 2], ], lwd = 2, arr.pos = 0.5, lcol = "transparent", arr.length = 0.5, curve = -0.35) nr <- nrow(arrpos) for(i in 1:nr){ text(arrpos[i, 1], arrpos[i, 2] + 0.05, paste("r", "x", rcomb[i]," = ", round(as.dist(cor.x)[i], 2), sep=""), cex=0.6) } # label residual textrect (elpos[1,], 0.09, 0.03,lab = "residual", box.col = "white", shadow.col = "grey", shadow.size = 0.005, cex = 1) # label dependent textrect (elpos[2,], 0.09, 0.03,lab = attributes(y)$class, box.col = "white", shadow.col = "grey", shadow.size = 0.005, cex = 1) # label independents nr <- nrow(elpos) (i in 3:nr){ textrect (elpos[i,], 0.09, 0.03,lab = colnames(x)[i-2], box.col = "white", shadow.col = "grey", shadow.size = 0.005, cex = 1) } i need help
1) plotting on horizontal layout in diagram, plot looks first one
2) plotting subscripts in arrow labels such px5, r12, r34 etc. combination of expression , paste in loops used returns indexing notation such , not indexed element.
you may find quicker in long run utilize tools text place text want on drawing. don't know parameters used define diagonal lines "p25 = -0.37" , similar labels, on assumption know each line's coordinates (endpoints, e.g.), utilize plotrix:radialtext .
r plot data-visualization diagram igraph
No comments:
Post a Comment