1. Tải dữ liệu

library(pathdiagram)
## Loading required package: shape
# manifest() sets the graphical specifcations for a manifest variable
# latent() sets the graphical specifcations for a latent variable
# wall() opens a new plotting window to start drawing a path diagram
# draw() plots either a manifest or a latent variable
# arrow() plots an arrow between two specifed variables

2. Tạo biến con manifest()

biencon = list(
eggs = manifest("Eggs", x = 0.25, y = 0.8, width = 0.1, height = 0.08),
milk = manifest("Milk", x = 0.25, y = 0.65, width = 0.1, height = 0.08),
flour = manifest("Flour", x = 0.25, y = 0.5, width = 0.1, height = 0.08),
sugar = manifest("Sugar", x = 0.25, y = 0.35, width = 0.1, height = 0.08),
butter = manifest("Butter", x = 0.25, y = 0.2, width = 0.1, height = 0.08)
)

3. Tạo biến tiềm ẩn

pancakes = latent("Pancakes", x = 0.8, y = 0.65, rx = 0.08, ry = 0.06)
waffles = latent("Waffles", x = 0.8, y = 0.35, rx = 0.08, ry = 0.06)

4. Vẽ ra hình

# Mở ra khung tranh - bức tường
wall(xlim = c(0.1, 0.9), ylim = c(0.1, 0.9))
# vẽ biến tiềm ẩn
draw(pancakes)
draw(waffles)
# Vẽ biến con
for (i in 1:5) {
draw(biencon[[i]])
}

5. Vẽ hoàn chỉnh có mũi tên

wall(xlim = c(0.1, 0.9), ylim = c(0.1, 0.9))
draw(pancakes)
draw(waffles)

for (i in 1:5) {
draw(biencon[[i]])
}

for (i in 1:5) {
arrow(from = biencon[[i]], to = pancakes, start = "east", end = "west")
arrow(from = biencon[[i]], to = waffles, start = "east", end = "west")
}

6. Ví dụ 2

# define Attack block
attack = list(
att1 = manifest("Messi", x=0.15, y=0.9, width=0.09, height=0.08, fill="#d199a4"),
att2 = manifest("Xavi", x=0.15, y=0.75, width=0.09, height=0.08, fill="#d199a4"),
att3 = manifest("Iniesta", x=0.15, y=0.6, width=0.09, height=0.08, fill="#d199a4"))
ATTACK = latent("ATTACK", x=0.35, y=0.75, rx=0.08, ry=0.07, fill="#a12b43", font=1)
# define Defense block
defense = list(
def1 = manifest("Puyol", x=0.15, y=0.4, width=0.09, height=0.08, fill="#a0bee1"),
def2 = manifest("Pique", x=0.15, y=0.25, width=0.09, height=0.08, fill="#a0bee1"),
def3 = manifest("Abidal", x=0.15, y=0.1, width=0.09, height=0.08, fill="#a0bee1"))
DEFENSE = latent("DEFENSE", x=0.35, y=0.25, rx=0.08, ry=0.07, fill="#1e67ba", font=1)
# define Success block
success = list(
suc1 = manifest("2008-2009", x=0.85, y=0.65, width=0.14, height=0.08, fill="gold2"),
suc2 = manifest("2009-2010", x=0.85, y=0.5, width=0.14, height=0.08, fill="gold2"),
suc3 = manifest("2010-2011", x=0.85, y=0.35, width=0.14, height=0.08, fill="gold2"))
SUCCESS = latent("SUCCESS", x=0.65, y=0.5, rx=0.08, ry=0.07, fill="gold2", font=1)

# open plot window
wall(ylim=c(0.1, 0.9))
# draw latent variables
draw(ATTACK)
draw(DEFENSE)
draw(SUCCESS)
# draw manifest variables
for (i in 1:3) {
draw(attack[[i]])
arrow(from=attack[[i]], to=ATTACK, start="east", end="west", col="#d199a4")
draw(defense[[i]])
arrow(from=defense[[i]], to=DEFENSE, start="east", end="west", col="#a0bee1")
draw(success[[i]])
arrow(from=SUCCESS, to=success[[i]], start="east", end="west", col="gold1")
}

# arrows of inner model
arrow(from=ATTACK, to=SUCCESS, start="east", end="west", col="#d199a4")
arrow(from=DEFENSE, to=SUCCESS, start="east", end="west", col="#a0bee1")

7. Ví dụ 3

# Gryffindor block
gryff = list(
harry = manifest("Harry \nPotter", x=0.15, y=0.8, width=0.12, height=0.08,
cex=0.8, fill="#f2d22e", col="#7c4f87", family="serif"),
ron = manifest("Ron\nWeasley", x=0.15, y=0.7, width=0.12, height=0.08,
cex=0.8, fill="#f2d22e", col="#7c4f87", family="serif"),
hermione = manifest("Hermione\nGranger", x=0.15, y=0.6, width=0.12, height=0.08,
cex=0.8, fill="#f2d22e", col="#7c4f87", family="serif"),
albus = manifest("Albus\nDumbledore", x=0.15, y=0.5, width=0.12, height=0.08,
cex=0.8, fill="#f2d22e", col="#7c4f87", family="serif"),
neville = manifest("Neville\nLongbottom", x=0.15, y=0.4, width=0.12, height=0.08,
cex=0.8, fill="#f2d22e", col="#7c4f87", family="serif"),
sirius = manifest("Sirius\nBlack", x=0.15, y=0.3, width=0.12, height=0.08,
cex=0.8, fill="#f2d22e", col="#7c4f87", family="serif"),
rubeus = manifest("Rubeus\nHagrid", x=0.15, y=0.2, width=0.12, height=0.08,
cex=0.8, fill="#f2d22e", col="#7c4f87", family="serif"))
# Slytherin block
slyth = list(
tom = manifest("Tom\nRiddle", x=0.85, y=0.8, width=0.12, height=0.08,
cex=0.8, fill="gray70", col="#467d70", family="serif"),
severus = manifest("Severus\nSnape", x=0.85, y=0.7, width=0.12, height=0.08,
cex=0.8, fill="gray70", col="#467d70", family="serif"),
bella = manifest("Bellatrix\nLestrange", x=0.85, y=0.6, width=0.12, height=0.08,
cex=0.8, fill="gray70", col="#467d70", family="serif"),
regulus = manifest("Regulus\nBlack", x=0.85, y=0.5, width=0.12, height=0.08,
cex=0.8, fill="gray70", col="#467d70", family="serif"),
phineas = manifest("Phineas\nBlack", x=0.85, y=0.4, width=0.12, height=0.08,
cex=0.8, fill="gray70", col="#467d70", family="serif"),
draco = manifest("Draco\nMalfoy", x=0.85, y=0.3, width=0.12, height=0.08,
cex=0.8, fill="gray70", col="#467d70", family="serif"),
horace = manifest("Horace\nSlughorn", x=0.85, y=0.2, width=0.12, height=0.08,
cex=0.8, fill="gray70", col="#467d70", family="serif"))

# latent variables
gry = latent("Gryffindor", x=0.375, y=0.5, rx=0.07, ry=0.06, cex=0.85,
fill="#7c4f87", family="serif")
sly = latent("Slytherin", x=0.625, y=0.5, rx=0.07, ry=0.06, cex=0.85,
fill="#467d70", family="serif")

# open plot window
wall(xlim=c(0.1, 0.9), ylim=c(0.15, 0.85))
# draw variables
for (i in 1:7)
{
# arrows between each block and its latent
arrow(from=gryff[[i]], to=gry, start="east", end="west",
col="#b095b7", angle=5, lwd=1)
arrow(from=slyth[[i]], to=sly, start="west", end="east",
col="#90b1a9", angle=5, lwd=1)
# variables
draw(gryff[[i]])
draw(slyth[[i]])
draw(gry)
draw(sly)
# arrows between latent variables
arrow(from=gry, to=sly, start="east", end="west", col="#dddddd", angle=20)
arrow(from=sly, to=gry, start="west", end="east", col="#dddddd", angle=20)
}

7. Thử nghiệm

bien = list(
x1 = manifest("Tình Yêu\nLâu dài", x=0.15, y=0.8, width=0.2, height=0.15,cex=0.8, fill="#914e75", col="#e69138", family="serif", font=1),
x2 = manifest("Vật chất", x=0.15, y=0.6, width=0.2, height=0.15,cex=0.8, fill="#914e75", col="#e69138", family="serif", font=4),
x3 = manifest("Tiếng sét", x=0.15, y=0.4, width=0.2, height=0.15,cex=0.8, fill="#914e75", col="#e69138", family="serif", font=3),
x4 = manifest("Thoát ế", x=0.15, y=0.2, width=0.2, height=0.15,cex=0.8, fill="#914e75", col="#e69138", family="serif", font=2))

y = latent("Kết hôn", x=0.7, y=0.5, rx=0.12, ry=0.1, cex=0.85,fill="#e69138", family="serif")


wall(xlim=c(0.1, 0.9), ylim=c(0.15, 0.85))
for (i in 1:4){
draw(bien[[i]])
arrow(from=bien[[i]], to=y, start="east", end="west",col="#e69138", angle=5, lwd=2)
}
draw(y)