Я пытаюсь построить результаты анализа пути, предложенного Дьюи и Лу (1959) в R, используя следующий код, который привел к созданию диаграммы, которая вышла за пределы области графика, и нижние стрелки были удалены. Диаграмма пути, полученная после запуска скрипта. Для этого анализа я использую диаграмму пакетов R и агролы.
require(agricolae)
require(Hmisc)
library(diagram)
data(wilt)
data(soil)
x<-soil[,c(2:23)]
y<-wilt[,14]
data <- cbind(y,x)
#Correlation of independant variables
cor.x <- rcorr(as.matrix(x))$r
#Correlation of dependant variable with all the independant variables
cor.y <- as.data.frame(t(subset(rcorr(as.matrix(cbind(y,x)))$r, select = c(y))))[,-1]
cor.y<-correlation(y,x)$correlation
cor.x<-correlation(x)$correlation
#Path Analysis
Path <- path.analysis(cor.x,cor.y)
#Direct Effects
diag(Path$Coeff)
#Residual Effects
Path$Residual
par(mar = c(0.5, 0.5, 0.5, 0.5)), mfrow = c(2, 2))
openplotmat()
# Get plot coordinates
elpos <- coordinates (c(2, length(cor.y)))
# adjust coordinates for Residual
elpos[2,1] <- abs((elpos[1,1]+elpos[1,2])/2)
#Specify Arrow positions
#1 Residual to Dependent
ft1 <- matrix(ncol = 2, byrow = TRUE, data = c(1, 2))
#2 Independent to 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 For cor.x
fromto_CU <- t(combn(seq((2+length(cor.y)))[3:(length(cor.y)+2)],2))
#4 For path distances
fromto_ST <- rbind(ft1,ft2)
# Plot Path distance arrows
nr <- nrow(fromto_ST)
arrpos <- matrix(ncol = 2, nrow = nr)
for (i in 1:nr)
arrpos[i, ] <- straightarrow (to = elpos[fromto_ST[i, 2], ],
from = elpos[fromto_ST[i, 1], ],lcol = "black",arr.col = "black",
lwd = 0.01, arr.pos = 0.5, arr.length = 0.15,arr.width=0.05)
#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.25)
}
# Plot correlation arrows direction 1
nr <- nrow(fromto_CU)
arrpos <- matrix(ncol = 2, nrow = nr)
for (i in 1:nr)
arrpos[i, ] <- curvedarrow (to = elpos[fromto_CU[i, 2], ],
from = elpos[fromto_CU[i, 1], ],lcol = "black",arr.col = "black",
lwd = 0.001, arr.pos = 0.25, arr.length = 0.15, ,arr.width=0.05, curve = 0.35)
# Plot correlation arrows - direction 2
nr <- nrow(fromto_CU)
arrpos <- matrix(ncol = 2, nrow = nr)
for (i in 1:nr)
arrpos[i, ] <- curvedarrow (to = elpos[fromto_CU[i, 1], ],
from = elpos[fromto_CU[i, 2], ],lcol = "black",arr.col = "black",
lwd = 0.001, arr.pos = 0.25, arr.length = 0.15, ,arr.width=0.05, curve = -0.35)
# Create combinations of cor.x for 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)
for (i in 1:nr)
arrpos[i, ] <- curvedarrow (to = elpos[fromto_CU[i, 1], ],
from = 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 = "tan1",
shadow.col = "grey", shadow.size = 0.005, cex = 1)
# Label Dependent
textrect (elpos[2,], 0.09, 0.03,lab = "Seed Yield/Plt", box.col = "light blue",
shadow.col = "grey", shadow.size = 0.005, cex = 0.5)
# Label independents
nr <- nrow(elpos)
for (i in 3:nr){
textrect (elpos[i,], 0.01, 0.03,lab = colnames(x)[i-2], box.col = "light green",
shadow.col = "grey", shadow.size = 0.005, cex = 0.5)
}
Как решить такую проблему? Кто-нибудь может мне помочь в предложении соответствующей модификации кода?