RでSEM(共分散構造分析/構造方程式モデリング)2

昨日はGraphVizが上手く動かなかったのでパス図の描画は割愛しましたが、EasyGraphVizで描画に成功したので記録しておきます。

library(sem)
勉強量a <- c( 5,  4,  4,  5,  4,  5,  5,  6,  4,  4,  3,  6,  5,  8,  5,
              5,  6,  4,  3,  4,  6,  4,  3,  3,  6,  3,  5,  5,  3,  3)
勉強量b <- c( 6,  4,  7,  5,  5,  7,  6,  7,  5,  5,  3,  5,  6,  8,  6,
              6,  5,  6,  4,  3,  5,  5,  3,  4,  7,  4,  6,  6,  4,  4)
期待a   <- c( 2,  5,  6,  5,  4,  3,  3,  5,  6,  5,  5,  5,  4,  6,  7,
              7,  6,  5,  5,  4,  4,  6,  5,  6,  4,  3,  7,  3,  6,  7)
期待b   <- c( 3,  6,  5,  4,  5,  2,  5,  5,  6,  4,  4,  6,  5,  5,  7,
              6,  6,  6,  4,  4,  5,  4,  4,  5,  5,  2,  5,  4,  7,  6)
自信a   <- c(36, 51, 62, 50, 60, 50, 45, 62, 48, 44, 59, 55, 57, 58, 67,
             58, 48, 47, 32, 25, 44, 45, 28, 36, 45, 35, 51, 54, 38, 60)
自信b   <- c(31, 45, 41, 28, 38, 34, 31, 56, 45, 35, 42, 51, 40, 54, 60,
             53, 45, 31, 23, 24, 38, 40, 33, 41, 39, 36, 43, 48, 26, 55)
dat2.cor <- cor(cbind(勉強量a, 勉強量b, 期待a, 期待b, 自信a, 自信b))
model21 <- specify.model()
  勉強量   -> 勉強量a, b01, 1
  勉強量   -> 勉強量b, b02, NA
  期待     -> 期待a,   b03, 1
  期待     -> 期待b,   b04, NA
  自信     -> 自信a,   b05, 1
  自信     -> 自信b,   b06, NA
  勉強量   -> 自信,    b07, NA
  期待     -> 自信,    b09, NA
  勉強量  <-> 勉強量,  NA,  1
  自信    <-> 自信,    NA,  1
  期待    <-> 期待,    NA,  1
  勉強量a <-> 勉強量a, e03, NA
  勉強量b <-> 勉強量b, e04, NA
  期待a   <-> 期待a,   e05, NA
  期待b   <-> 期待b,   e06, NA
  自信a   <-> 自信a,   e07, NA
  自信b   <-> 自信b,   e08, NA


dat21.sem <- sem(model21, dat2.cor, N=30)
path.diagram(dat21.sem, ignore.double=FALSE, edge.labels="values", digits=2,
             standardize=TRUE)
# digraph "dat21.sem" {
#   rankdir=LR;
#   size="8,8";
#   node [fontname="Helvetica" fontsize=14 shape=box];
#   edge [fontname="Helvetica" fontsize=10];
#   center=1;
#   "自信" [shape=ellipse]
#   "勉強量" [shape=ellipse]
#   "期待" [shape=ellipse]
#   "勉強量" -> "勉強量a" [label="0.81"];
#   "勉強量" -> "勉強量b" [label="0.92"];
#   "期待" -> "期待a" [label="0.87"];
#   "期待" -> "期待b" [label="0.76"];
#   "自信" -> "自信a" [label="0.88"];
#   "自信" -> "自信b" [label="0.87"];
#   "勉強量" -> "自信" [label="0.57"];
#   "期待" -> "自信" [label="0.58"];
#   "勉強量" -> "勉強量" [label="1" dir=both];
#   "自信" -> "自信" [label="0.34" dir=both];
#   "期待" -> "期待" [label="1" dir=both];
#   "勉強量a" -> "勉強量a" [label="0.35" dir=both];
#   "勉強量b" -> "勉強量b" [label="0.16" dir=both];
#   "期待a" -> "期待a" [label="0.24" dir=both];
#   "期待b" -> "期待b" [label="0.43" dir=both];
#   "自信a" -> "自信a" [label="0.23" dir=both];
#   "自信b" -> "自信b" [label="0.25" dir=both];
# }


このpath.diagram関数の出力をテキストエディタに貼り付けて保存し、そのファイルをEasyGraphVizに読み込ませれば出来上がりです。
ただ、そのままだとノードの位置などが意図通りにならないので、すこし手を加えます。タイトルを付けて、ノードの階層の指定を加えました。

digraph "dat21.sem" {
  rankdir=LR;
  size="8,8";
  node [fontname="Helvetica" fontsize=14 shape=box];
  edge [fontname="Helvetica" fontsize=10];
  center=1;
  "自信" [shape=ellipse]
  "勉強量" [shape=ellipse]
  "期待" [shape=ellipse]
  "勉強量" -> "勉強量a" [label="0.81"];
  "勉強量" -> "勉強量b" [label="0.92"];
  "期待" -> "期待a" [label="0.87"];
  "期待" -> "期待b" [label="0.76"];
  "自信" -> "自信a" [label="0.88"];
  "自信" -> "自信b" [label="0.87"];
  "勉強量" -> "自信" [label="0.57"];
  "期待" -> "自信" [label="0.58"];
  "勉強量" -> "勉強量" [label="1" dir=both];
  "自信" -> "自信" [label="0.34" dir=both];
  "期待" -> "期待" [label="1" dir=both];
  "勉強量a" -> "勉強量a" [label="0.35" dir=both];
  "勉強量b" -> "勉強量b" [label="0.16" dir=both];
  "期待a" -> "期待a" [label="0.24" dir=both];
  "期待b" -> "期待b" [label="0.43" dir=both];
  "自信a" -> "自信a" [label="0.23" dir=both];
  "自信b" -> "自信b" [label="0.25" dir=both];
  /* 以下が書き加えた部分 */
  graph [label = "【潜在変数間の因果関係】のパス図", labelloc = t];
  { rank = same; "勉強量" "期待"}
  { rank = min; "勉強量a" "勉強量b" "期待a" "期待b"}
  { rank = max; "自信a" "自信b"}
}


できた図はこんな感じ。