semLibraryを使ってモデルを書いてみる

# Semライブラリーを使ったモデル分析
 - とりあえず今回モデル図をざっと描いてみた。
 - 注意点として、データの流れを意識して記述しないといけないことがわかった。
 - また、観測変数には何かしらかの矢印を伸ばさないとエラーが出る。

```{r}
library(sem)

#データ読み込み
dat <- read.csv(`ltest3.csv`, header=TRUE, fileEncoding = `CP932`)

#データの標準化
dat <- scale(dat)

#相関行列 <- とりあえずこっち使う
#オプションで欠損値を無視して県産、欠損値=NA
r <- cor(dat,use=`complete.obs`)

#共分散行列
cov <- cov(dat)

model01 <- specifyModel(endog.variances=FALSE,
text=`
time_6m -> time , a101, NA
time_3m -> time , a102, NA
time_1m -> time , a103, NA
bounce_6m -> bounce , a104, NA
bounce_3m -> bounce , a105, NA
bounce_1m -> bounce , a106, NA
time_1m <-> time_3m , m101 , NA
time_1m <-> time_6m , m102 , NA
time_3m <-> time_6m , m103 , NA
bounce_6m <-> bounce_3m , m104 , NA
bounce_6m <-> bounce_1m , m105 , NA
bounce_3m <-> bounce_1m , m106 , NA
bounce -> happiness , r101 , NA
time -> happiness , r102 , NA
happiness -> score_rank , r103 , NA
search -> rank, a1 , NA
release -> score_rank, a2, NA
rank <-> rank , e1 , NA
search <-> search, e2, NA
release <-> release, e3, NA
rank <- score_rank , r1 , NA
score_rank <-> score_rank, re1 ,NA
`)

model02 <- specifyModel(endog.variances=FALSE,
text=`
olink -> link_score, a201, NA
odomain -> link_score, a202, NA
olink <-> olink, e201, NA
odomain <-> odomain, e202, NA
link_score <-> link_score , e203 , NA
link_score -> score_rank , r201 , NA
search -> rank, a1 , NA
release -> score_rank, a2, NA
rank <-> rank , e1 , NA
search <-> search, e2, NA
release <-> release, e3, NA
rank <- score_rank , r1 , NA
score_rank <-> score_rank, re1 ,NA
`)

model03 <- specifyModel(endog.variances=FALSE,
text=`
main_link -> query_score , a301 , NA
sub_link -> query_score , a302 , NA
ctr_3m -> query_score , a303 , NA
ctr_1m -> query_score , a304 , NA
main -> query_score , a305 , NA
main_par -> query_score , a306 , NA
sub -> query_score , a307 , NA
sub_par -> query_score , a308 , NA
sub_to_title -> query_score , a309 , NA
sub_to_tag -> query_score , a310 , NA
main_link <-> sub_link , m301 , NA
ctr_3m <-> ctr_1m , m302 , NA
main <-> main_par , m303 , NA
sub <-> sub_par , m304 , NA
sub_to_title <-> sub_to_title, e301, NA
sub_to_tag <-> sub_to_tag, e302, NA
query_score -> score_rank, r301 , NA
query_score <-> query_score, e303 ,NA
search -> rank, a1 , NA
release -> score_rank, a2, NA
rank <-> rank , e1 , NA
search <-> search, e2, NA
release <-> release, e3, NA
rank <- score_rank , r1 , NA
score_rank <-> score_rank, re1 ,NA
`)

model04 <- specifyModel(endog.variances=FALSE,
text=`
original -> original_content , a401, NA
original_p1 -> original_content , a402, NA
original_content <-> original_content , e401 , NA
original <-> original_p1 , m401 , NA
quote_p1 -> quote_score , a403, NA
quote -> quote_score , a404, NA
url_p1 -> quote_score , a405, NA
url -> quote_score , a406, NA
domain_p1 -> quote_score , a407, NA
domain -> quote_score , a408, NA
quote_score <-> quote_score , e402 , NA
quote <-> quote, e403, NA
quote_p1 <-> quote_p1, e404, NA
url <-> url, e405, NA
url_p1 <-> url_p1, e406, NA
domain <-> domain, e407, NA
domain_p1 <-> domain_p1, e408, NA
quote <-> quote_p1 , m402 , NA
url <-> url_p1 , m403 , NA
domain <-> domain_p1 , m404 , NA
quote_score -> content_score, r401 , NA
original_content -> content_score, r402 , NA
content_score -> score_rank, r403 ,NA
keyword_p1 <- content_score , a409, NA
keyword <- content_score , a410, NA
page <- content_score , a411, NA
keyword_p1 <-> keyword_p1, e409, NA
keyword <-> keyword, e410, NA
page <-> page, e411, NA
search -> rank, a1 , NA
release -> score_rank, a2, NA
rank <-> rank , e1 , NA
search <-> search, e2, NA
release <-> release, e3, NA
rank <- score_rank , r1 , NA
score_rank <-> score_rank, re1 ,NA
`)

model05 <- specifyModel(endog.variances=FALSE,
text=`
keyword_cat_p1 -> quality_score , a501, NA
keyword_cat -> quality_score , a502, NA
original_par -> quality_score , a503, NA
pic_p1 -> quality_score , a504, NA
pic -> quality_score , a505, NA
mov_p1 -> quality_score , a506, NA
mov -> quality_score , a507, NA
original_par <-> original_par, e501, NA
keyword_cat <-> keyword_cat_p1 , m501 , NA
pic <-> pic_p1 , m502 , NA
mov <-> mov_p1 , m503 , NA
quality_score -> score_rank, r501 ,NA
quality_score <-> quality_score , e502 , NA
search -> rank, a1 , NA
release -> score_rank, a2, NA
rank <-> rank , e1 , NA
search <-> search, e2, NA
release <-> release, e3, NA
rank <- score_rank , r1 , NA
score_rank <-> score_rank, re1 ,NA
`)

クロスモデル  <- specifyModel(endog.variances=FALSE,
text=`
original -> original_par , e901, NA
original_p1 -> original_par , e902, NA
quote <-> original_par, e903, NA
quote_p1 <-> original_par, e904, NA
url <-> original_par, e905, NA
url_p1 <-> original_par, e906, NA
domain <-> original_par, e907, NA
domain_p1 <-> original_par, e908, NA
`)

#モデルの結合
結合モデル <- combineModels( クロスモデル , model01 , model02 , model03 , model04 , model05 )
#ダブリ削除
結合モデル <- removeRedundantPaths( 結合モデル )
結合リザルト <- sem(結合モデル , r , nrow(dat) )
std.coef( 結合リザルト )
pathDiagram( 結合リザルト , `dat`, ignore.double=FALSE, edge.labels=`values`, digits=3, node.font=c(``, 8))

summary( 結合リザルト )

```

![9d36336f-e4ff-92fb-49bc-c55fb1ca05b0.png](https://qiita-image-store.s3.amazonaws.com/0/127228/290537e9-5764-89fc-6209-4db0b790546c.png)

#今後の問題点
 - モデルは描けるようになったがsummaryが出力されない。計算できてない？モデルは出るのに？
エラー文...

```{r}
library(sem)
Error in summary.objectiveML(結合リザルト) :
  coefficient covariances cannot be computed
In addition: Warning message:
In vcov.sem(object, robust = robust, analytic = analytic.se) :
   singular Hessian: model is probably underidentified.
```