アイスタット
様「共分散構造分析」 Rで学習した記録です。
小杉孝司先生
「Lesson6.CFA,SEM,Multiple Group
SEM」も参考にさせていただきました。小杉先生は、SEMは因子分析と回帰分析を足し合わせたような方法とおっしゃっておられます。
と言えるかもしれません。
観測変数がk個あるとき、分散共分散行列の成分は\(\frac{k(k+1)}2\)個あります。モデルの求めるべきパラメータ数を\(m\)とすると、
   自由度:\(\frac{k(k+1)}2-m\)  (\(m\)はパラメータ数).
(パス図では、明らかに相関が0である場合以外は外生変数同士には相関をつける)相関を0にしたいときは0.5-12(べータ版)p13参照.
また、アイシュタット 様では、
   \(m=\)(矢印の個数)+(外生変数)+(誤差変動)+(潜在変数).
としています。
[注意]
lavaanではデフォルトでは、外生変数間に相関があることを前提としており、
   \(m=\)(矢印の個数)+(外生変数の相関)+(誤差変動)+(潜在変数).
    外生変数間に相関がない0*設定にすると.
   \(m=\)(矢印の個数)+(外生変数の分散)+(誤差変動)+(潜在変数).
これらはsummaryから読み取ることができます。
推定する母パラメーター
外生変数の分散と相関係数、パス係数、内生変数の誤差分散、潜在変数の分散と相関係数
dat <- read.csv("istat.csv", header = TRUE,row.names=1)
attach(dat)
勝数
##  [1] 67 59 57 55 54 52 51 51 45 45 45 45 42 38 37 36 36 30 28 27
model.RM <- '
  勝数 ~ 体重 + 食事量 + 稽古量
  稽古量 ~ 体重
  体重 ~ 食事量 '
パス係数5と内生変数3の誤差の個数は8.
外生変数は1個である。
- Comparative Fit Index (CFI).
- Tucker-Lewis Index (TLI)
- Akaike (AIC).
- Bayesian (BIC).
- Sample-size adjusted Bayesian (SABIC).
- RMSEA (Root Mean Square Error of Approximation ).
- SRMR
fit.RM <- sem(model.RM, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.RM, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.RM, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         8
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.874
##   Degrees of freedom                                 1
##   P-value (Chi-square)                           0.350
## 
## Model Test Baseline Model:
## 
##   Test statistic                                41.271
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.021
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -149.044
##   Loglikelihood unrestricted model (H1)       -148.607
##                                                       
##   Akaike (AIC)                                 314.089
##   Bayesian (BIC)                               322.055
##   Sample-size adjusted Bayesian (SABIC)        297.393
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.576
##   P-value H_0: RMSEA <= 0.050                    0.362
##   P-value H_0: RMSEA >= 0.080                    0.620
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.037
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.311    0.343    0.907    0.364    0.311    0.258
##     食事量          2.825    3.864    0.731    0.465    2.825    0.199
##     稽古量          5.729    2.581    2.220    0.026    5.729    0.396
##   稽古量 ~                                                            
##     体重            0.038    0.017    2.303    0.021    0.038    0.458
##   体重 ~                                                              
##     食事量          9.562    1.534    6.235    0.000    9.562    0.813
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           55.534   17.561    3.162    0.002   55.534    0.502
##    .稽古量          0.417    0.132    3.162    0.002    0.417    0.790
##    .体重           25.755    8.144    3.162    0.002   25.755    0.340
## 
## R-Square:
##                  Estimate
##     勝数            0.498
##     稽古量          0.210
##     体重            0.660
複数の回帰分析を行っていることになる。
round(cor(dat),3)
##         勝数 稽古量 食事量  体重
## 勝数   1.000  0.604  0.593 0.595
## 稽古量 0.604  1.000  0.479 0.458
## 食事量 0.593  0.479  1.000 0.813
## 体重   0.595  0.458  0.813 1.000
cor(食事量,勝数)=0.479・0.4+0.81・0.26+0.2
(0.479は食事と稽古の相関。パスがなくても相関はある)
cor(体重,勝数)=0.46・0.4+0.81・0.2+0.26=0.606
cor(体重,稽古量)=0.46・0.4+0.81・0.2+0.26=0.606.
間接効果=Σ(単回帰係数・偏回帰係数),逆向きの偏回帰係数は0
RMSEAはGFIの逆で0.1未満なら良いモデル
model.1 <- '
  勝数 ~ 体重 + 食事量 + 稽古量
  稽古量 ~~ 0*食事量
  稽古量 ~~ 0*体重
  体重 ~~ 0*食事量 '
fit.1 <- sem(model.1, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.1, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.1, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 12 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         7
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                27.170
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                                41.271
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.315
##   Tucker-Lewis Index (TLI)                      -0.371
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -184.547
##   Loglikelihood unrestricted model (H1)       -170.962
##                                                       
##   Akaike (AIC)                                 383.095
##   Bayesian (BIC)                               390.065
##   Sample-size adjusted Bayesian (SABIC)        368.486
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.635
##   90 Percent confidence interval - lower         0.430
##   90 Percent confidence interval - upper         0.863
##   P-value H_0: RMSEA <= 0.050                    0.000
##   P-value H_0: RMSEA >= 0.080                    1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.384
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.311    0.191    1.625    0.104    0.311    0.294
##     食事量          2.825    2.252    1.255    0.210    2.825    0.227
##     稽古量          5.729    2.294    2.497    0.013    5.729    0.453
## 
## Covariances:
##                 Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   食事量 ~~                                                          
##     稽古量         0.000                               0.000    0.000
##   体重 ~~                                                            
##     稽古量         0.000                               0.000    0.000
##     食事量         0.000                               0.000    0.000
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           55.534   17.561    3.162    0.002   55.534    0.657
##     体重           75.810   23.973    3.162    0.002   75.810    1.000
##     食事量          0.547    0.173    3.162    0.002    0.547    1.000
##     稽古量          0.528    0.167    3.162    0.002    0.528    1.000
## 
## R-Square:
##                  Estimate
##     勝数            0.343
矢印=3 、誤差=1、外生変数=3.
m=7 よって、自由度は 3.
(相関=3 にすれば Degrees of freedom = 0)
(標準化解は少しずれる).
model.11 <- '
  勝数 ~ 体重 + 食事量 + 稽古量'
fit.11 <- sem(model.11, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.11, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.11, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         4
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                                14.101
##   Degrees of freedom                                 3
##   P-value                                        0.003
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)                -68.549
##   Loglikelihood unrestricted model (H1)        -68.549
##                                                       
##   Akaike (AIC)                                 145.097
##   Bayesian (BIC)                               149.080
##   Sample-size adjusted Bayesian (SABIC)        136.749
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value H_0: RMSEA <= 0.050                       NA
##   P-value H_0: RMSEA >= 0.080                       NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.311    0.331    0.938    0.348    0.311    0.255
##     食事量          2.825    3.949    0.715    0.474    2.825    0.197
##     稽古量          5.729    2.638    2.172    0.030    5.729    0.393
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           55.534   17.561    3.162    0.002   55.534    0.494
## 
## R-Square:
##                  Estimate
##     勝数            0.506
model.2 <- '
  勝数 ~ 体重 + 稽古量
  稽古量 ~ 食事量
  体重 ~~ 0*食事量 '
fit.2 <- sem(model.2, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.2, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.2, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 11 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         7
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                22.459
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                                41.271
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.448
##   Tucker-Lewis Index (TLI)                      -0.103
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -182.192
##   Loglikelihood unrestricted model (H1)       -170.962
##                                                       
##   Akaike (AIC)                                 378.383
##   Bayesian (BIC)                               385.354
##   Sample-size adjusted Bayesian (SABIC)        363.774
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.569
##   90 Percent confidence interval - lower         0.364
##   90 Percent confidence interval - upper         0.800
##   P-value H_0: RMSEA <= 0.050                    0.000
##   P-value H_0: RMSEA >= 0.080                    1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.334
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.491    0.194    2.534    0.011    0.491    0.439
##     稽古量          6.120    2.323    2.634    0.008    6.120    0.456
##   稽古量 ~                                                            
##     食事量          0.470    0.193    2.441    0.015    0.470    0.479
## 
## Covariances:
##                 Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   体重 ~~                                                            
##     食事量         0.000                               0.000    0.000
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           56.955   18.011    3.162    0.002   56.955    0.600
##    .稽古量          0.406    0.129    3.162    0.002    0.406    0.770
##     体重           75.810   23.973    3.162    0.002   75.810    1.000
##     食事量          0.547    0.173    3.162    0.002    0.547    1.000
## 
## R-Square:
##                  Estimate
##     勝数            0.400
##     稽古量          0.230
矢印=3 、誤差=2、外生変数=2.
m=7 よって、自由度は 3.
外生変数の相関を入れれば.
m=8 よって、自由度は 2.
model.21 <- '
  勝数 ~ 体重 + 稽古量
  稽古量 ~ 食事量 '
fit.21 <- sem(model.21, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.21, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.21, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.867
##   Degrees of freedom                                 2
##   P-value (Chi-square)                           0.648
## 
## Model Test Baseline Model:
## 
##   Test statistic                                19.679
##   Degrees of freedom                                 5
##   P-value                                        0.001
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.193
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)                -88.176
##   Loglikelihood unrestricted model (H1)        -87.742
##                                                       
##   Akaike (AIC)                                 186.352
##   Bayesian (BIC)                               191.330
##   Sample-size adjusted Bayesian (SABIC)        175.917
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.347
##   P-value H_0: RMSEA <= 0.050                    0.662
##   P-value H_0: RMSEA >= 0.080                    0.317
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.033
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.491    0.210    2.334    0.020    0.491    0.408
##     稽古量          6.120    2.523    2.426    0.015    6.120    0.424
##   稽古量 ~                                                            
##     食事量          0.470    0.193    2.441    0.015    0.470    0.479
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           56.955   18.011    3.162    0.002   56.955    0.519
##    .稽古量          0.406    0.129    3.162    0.002    0.406    0.770
## 
## R-Square:
##                  Estimate
##     勝数            0.481
##     稽古量          0.230
model.3 <- '
  勝数 ~ 体重 + 稽古量
  体重 ~ 食事量
  稽古量 ~~ 0*食事量 '
fit.3 <- sem(model.3, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.3, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.3, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 10 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         7
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 6.083
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.108
## 
## Model Test Baseline Model:
## 
##   Test statistic                                41.271
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.913
##   Tucker-Lewis Index (TLI)                       0.825
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -174.004
##   Loglikelihood unrestricted model (H1)       -170.962
##                                                       
##   Akaike (AIC)                                 362.008
##   Bayesian (BIC)                               368.978
##   Sample-size adjusted Bayesian (SABIC)        347.399
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.227
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.488
##   P-value H_0: RMSEA <= 0.050                    0.122
##   P-value H_0: RMSEA >= 0.080                    0.855
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.246
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.491    0.194    2.534    0.011    0.491    0.439
##     稽古量          6.120    2.323    2.634    0.008    6.120    0.456
##   体重 ~                                                              
##     食事量          9.562    1.534    6.235    0.000    9.562    0.813
## 
## Covariances:
##                 Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   稽古量 ~~                                                          
##     食事量         0.000                               0.000    0.000
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           56.955   18.011    3.162    0.002   56.955    0.600
##    .体重           25.755    8.144    3.162    0.002   25.755    0.340
##     稽古量          0.527    0.167    3.162    0.002    0.527    1.000
##     食事量          0.548    0.173    3.162    0.002    0.548    1.000
## 
## R-Square:
##                  Estimate
##     勝数            0.400
##     体重            0.660
矢印=3 、誤差=2、外生変数=2.
m=7 よって、自由度は 3.
外生変数間に 相関があるとすれば、
m=8 よって、自由度は 2となる.
model.3 <- '
  勝数 ~ 体重 + 稽古量
  体重 ~ 食事量 '
fit.3 <- sem(model.3, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.3, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.3, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.867
##   Degrees of freedom                                 2
##   P-value (Chi-square)                           0.648
## 
## Model Test Baseline Model:
## 
##   Test statistic                                36.055
##   Degrees of freedom                                 5
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.091
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -129.666
##   Loglikelihood unrestricted model (H1)       -129.233
##                                                       
##   Akaike (AIC)                                 269.333
##   Bayesian (BIC)                               274.311
##   Sample-size adjusted Bayesian (SABIC)        258.898
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.347
##   P-value H_0: RMSEA <= 0.050                    0.662
##   P-value H_0: RMSEA >= 0.080                    0.317
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.033
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.491    0.210    2.334    0.020    0.491    0.408
##     稽古量          6.120    2.523    2.426    0.015    6.120    0.424
##   体重 ~                                                              
##     食事量          9.562    1.534    6.235    0.000    9.562    0.813
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           56.955   18.011    3.162    0.002   56.955    0.519
##    .体重           25.755    8.144    3.162    0.002   25.755    0.340
## 
## R-Square:
##                  Estimate
##     勝数            0.481
##     体重            0.660
model.4 <- '
  勝数 ~ 体重 + 稽古量+ 食事量
  体重 ~ 食事量
  稽古量 ~~ 0*食事量 '
fit.4 <- sem(model.4, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.4, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.4, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 10 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         8
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 5.578
##   Degrees of freedom                                 2
##   P-value (Chi-square)                           0.061
## 
## Model Test Baseline Model:
## 
##   Test statistic                                41.271
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.899
##   Tucker-Lewis Index (TLI)                       0.696
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -173.751
##   Loglikelihood unrestricted model (H1)       -170.962
##                                                       
##   Akaike (AIC)                                 363.502
##   Bayesian (BIC)                               371.468
##   Sample-size adjusted Bayesian (SABIC)        346.806
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.299
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.607
##   P-value H_0: RMSEA <= 0.050                    0.070
##   P-value H_0: RMSEA >= 0.080                    0.916
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.241
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.311    0.328    0.947    0.344    0.311    0.280
##     稽古量          5.729    2.294    2.497    0.013    5.729    0.430
##     食事量          2.825    3.864    0.731    0.465    2.825    0.216
##   体重 ~                                                              
##     食事量          9.562    1.534    6.235    0.000    9.562    0.813
## 
## Covariances:
##                 Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   稽古量 ~~                                                          
##     食事量         0.000                               0.000    0.000
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           55.534   17.561    3.162    0.002   55.534    0.592
##    .体重           25.755    8.144    3.162    0.002   25.755    0.340
##     稽古量          0.527    0.167    3.162    0.002    0.527    1.000
##     食事量          0.548    0.173    3.162    0.002    0.548    1.000
## 
## R-Square:
##                  Estimate
##     勝数            0.408
##     体重            0.660
矢印=4 、誤差=2、外生変数=2.
m=8 よって、自由度は 2.
外生変数間に 相関があるとすれば、
m=9 よって、自由度は1 となる.
model.41 <- '
  勝数 ~ 体重 + 稽古量+ 食事量
  体重 ~ 食事量 '
fit.41 <- sem(model.41, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.41, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.41, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         6
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.361
##   Degrees of freedom                                 1
##   P-value (Chi-square)                           0.548
## 
## Model Test Baseline Model:
## 
##   Test statistic                                36.055
##   Degrees of freedom                                 5
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.103
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -129.414
##   Loglikelihood unrestricted model (H1)       -129.233
##                                                       
##   Akaike (AIC)                                 270.827
##   Bayesian (BIC)                               276.802
##   Sample-size adjusted Bayesian (SABIC)        258.305
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.497
##   P-value H_0: RMSEA <= 0.050                    0.558
##   P-value H_0: RMSEA >= 0.080                    0.427
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.024
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.311    0.328    0.947    0.344    0.311    0.257
##     稽古量          5.729    2.614    2.192    0.028    5.729    0.395
##     食事量          2.825    4.055    0.697    0.486    2.825    0.199
##   体重 ~                                                              
##     食事量          9.562    1.534    6.235    0.000    9.562    0.813
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           55.534   17.561    3.162    0.002   55.534    0.501
##    .体重           25.755    8.144    3.162    0.002   25.755    0.340
## 
## R-Square:
##                  Estimate
##     勝数            0.499
##     体重            0.660
model.5 <- '
  勝数 ~ 体重 + 稽古量
  体重 ~ 食事量
  体重 ~ 稽古量
  稽古量 ~~ 0*食事量 '
fit.5 <- sem(model.5, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.5, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.5, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 10 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         8
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 5.722
##   Degrees of freedom                                 2
##   P-value (Chi-square)                           0.057
## 
## Model Test Baseline Model:
## 
##   Test statistic                                41.271
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.894
##   Tucker-Lewis Index (TLI)                       0.683
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -173.823
##   Loglikelihood unrestricted model (H1)       -170.962
##                                                       
##   Akaike (AIC)                                 363.647
##   Bayesian (BIC)                               371.612
##   Sample-size adjusted Bayesian (SABIC)        346.950
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.305
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.612
##   P-value H_0: RMSEA <= 0.050                    0.065
##   P-value H_0: RMSEA >= 0.080                    0.921
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.229
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.491    0.201    2.439    0.015    0.491    0.419
##     稽古量          6.120    2.333    2.623    0.009    6.120    0.451
##   体重 ~                                                              
##     食事量          9.061    1.520    5.962    0.000    9.061    0.797
##     稽古量          1.065    1.548    0.688    0.492    1.065    0.092
## 
## Covariances:
##                 Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   稽古量 ~~                                                          
##     食事量         0.000                               0.000    0.000
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           56.955   18.011    3.162    0.002   56.955    0.586
##    .体重           25.294    7.999    3.162    0.002   25.294    0.357
##     稽古量          0.527    0.167    3.162    0.002    0.527    1.000
##     食事量          0.548    0.173    3.162    0.002    0.548    1.000
## 
## R-Square:
##                  Estimate
##     勝数            0.414
##     体重            0.643
矢印=3 、誤差=2、外生変数=2.
m=7 よって、自由度は 3.
外生変数間に 相関があるとすれば、
m=8 よって、自由度は 2となる.
model.7 <- '
  勝数 ~ 体重 + 食事量 + 稽古量
  体重 ~ 稽古量
  体重 ~ 食事量
  食事量 ~ 0*稽古量'
fit.7 <- sem(model.7, data = dat, estimator = "ML")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit.7, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
summary(fit.7, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6.15 ended normally after 6 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         8
## 
##   Number of observations                            20
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 5.217
##   Degrees of freedom                                 1
##   P-value (Chi-square)                           0.022
## 
## Model Test Baseline Model:
## 
##   Test statistic                                41.271
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.880
##   Tucker-Lewis Index (TLI)                       0.283
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -151.588
##   Loglikelihood unrestricted model (H1)       -148.980
##                                                       
##   Akaike (AIC)                                 319.176
##   Bayesian (BIC)                               327.142
##   Sample-size adjusted Bayesian (SABIC)        302.480
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.459
##   90 Percent confidence interval - lower         0.139
##   90 Percent confidence interval - upper         0.879
##   P-value H_0: RMSEA <= 0.050                    0.026
##   P-value H_0: RMSEA >= 0.080                    0.969
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.224
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   勝数 ~                                                              
##     体重            0.311    0.331    0.938    0.348    0.311    0.269
##     食事量          2.825    3.753    0.753    0.452    2.825    0.215
##     稽古量          5.729    2.321    2.468    0.014    5.729    0.427
##   体重 ~                                                              
##     稽古量          1.065    1.548    0.688    0.492    1.065    0.092
##     食事量          9.061    1.520    5.962    0.000    9.061    0.797
##   食事量 ~                                                            
##     稽古量          0.000                               0.000    0.000
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .勝数           55.534   17.561    3.162    0.002   55.534    0.586
##    .体重           25.294    7.999    3.162    0.002   25.294    0.357
##    .食事量          0.547    0.173    3.162    0.002    0.547    1.000
## 
## R-Square:
##                  Estimate
##     勝数            0.414
##     体重            0.643
##     食事量          0.000
適合度の指標 GFI、AGFI、RMSEA、カイ2乗値 ・GFIは重回帰分析における決定係数(R2)、 ・AGFIは自由度修正済み決定係数 ・RMSEAはGFIの逆で0.1未満なら良いモデル p値が0.05以上は母集団においてパス図は適用できると判断します。 シリウス先生の心理統計学 http://daas.la.coocan.jp/sem/fitting_and_testing.htm ・NCP(Estimated Non-centrality Parameter) NCP = カイ自乗値 - 自由度 0に近ければ近いほど、そのモデルがよく適合している ・F0(Population Discrepancy Function Value) F0 = NCP / (n - 1) 0に近ければ近いほど、そのモデルがよく適合している ・RMSEA(Root Mean Square Error of Approximation) RMSEA = sqrt(F0 / 自由度) RMSEA < 0.05 ⇒ よいモデルであると判断.
dat <- read.csv("istat2.csv", header = TRUE,row.names=1)
attach(dat)
head(dat)
| 品切れ | 新鮮 | 味 | 時間 | image | 態度 | layout | 品揃え | coffe | 総合 | 
|---|---|---|---|---|---|---|---|---|---|
| 3 | 1 | 2 | 4 | 5 | 2 | 2 | 1 | 2 | 2 | 
| 2 | 4 | 4 | 3 | 3 | 3 | 3 | 5 | 4 | 5 | 
| 2 | 3 | 2 | 2 | 1 | 4 | 4 | 2 | 1 | 3 | 
| 3 | 5 | 5 | 4 | 1 | 2 | 1 | 4 | 5 | 4 | 
| 3 | 4 | 4 | 3 | 3 | 2 | 3 | 5 | 3 | 4 | 
| 4 | 3 | 3 | 2 | 3 | 3 | 3 | 3 | 4 | 4 | 
str(dat)
## 'data.frame':    20 obs. of  10 variables:
##  $ 品切れ: int  3 2 2 3 3 4 4 5 3 1 ...
##  $ 新鮮  : int  1 4 3 5 4 3 2 1 3 3 ...
##  $ 味    : int  2 4 2 5 4 3 2 1 4 3 ...
##  $ 時間  : int  4 3 2 4 3 2 5 4 5 1 ...
##  $ image : int  5 3 1 1 3 3 3 4 3 2 ...
##  $ 態度  : int  2 3 4 2 2 3 3 3 2 3 ...
##  $ layout: int  2 3 4 1 3 3 3 4 3 2 ...
##  $ 品揃え: int  1 5 2 4 5 3 1 3 4 2 ...
##  $ coffe : int  2 4 1 5 3 4 3 4 3 2 ...
##  $ 総合  : int  2 5 3 4 4 4 4 2 4 1 ...
model <- '
  # measurement model(測定方程式)
    商品サービス =~ 味 + 新鮮 + 品揃え
    システム =~ 品切れ + 時間
    店舗 =~ layout + image
    従業員 =~ 態度
    
  # regressions
    総合 ~ 商品サービス + システム + 店舗 + 従業員
    
  # residual correlations
    商品サービス ~~ 0*システム
    商品サービス ~~ 0*店舗
    商品サービス ~~ 0*従業員
    店舗 ~~ 0*システム
    従業員 ~~ 0*システム
    従業員 ~~ 0*店舗
    
'
fit <- sem(model=model, data=dat, estimator="ML")
## Warning in lavaan::lavaan(model = model, data = dat, estimator = "ML", model.type = "sem", : lavaan WARNING:
##     the optimizer warns that a solution has NOT been found!
summary(fit, standardized=TRUE) # 標準化
## lavaan 0.6.15 did NOT end normally after 790 iterations
## ** WARNING ** Estimates below are most likely unreliable
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        20
## 
##   Number of observations                            20
## 
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate   Std.Err  z-value  P(>|z|)   Std.lv   Std.all 
##   商品サービス =~                                                          
##     味                 1.000                                1.142     1.002
##     新鮮               0.805       NA                       0.920     0.877
##     品揃え             0.766       NA                       0.875     0.673
##   システム =~                                                              
##     品切れ             1.000                                1.211     0.929
##     時間               0.579       NA                       0.702     0.550
##   店舗 =~                                                                  
##     layout             1.000                               78.116    70.811
##     image              0.000       NA                       0.013     0.009
##   従業員 =~                                                                
##     態度               1.000                                0.640     1.000
## 
## Regressions:
##                  Estimate   Std.Err  z-value  P(>|z|)   Std.lv   Std.all 
##   総合 ~                                                                 
##     商品サービス     0.824       NA                       0.941     0.646
##     システム         0.698       NA                       0.845     0.580
##     店舗             0.000       NA                       0.002     0.001
##     従業員           0.350       NA                       0.224     0.154
## 
## Covariances:
##                   Estimate   Std.Err  z-value  P(>|z|)   Std.lv   Std.all 
##   商品サービス ~~                                                         
##     システム          0.000                                0.000     0.000
##     店舗              0.000                                0.000     0.000
##     従業員            0.000                                0.000     0.000
##   システム ~~                                                             
##     店舗              0.000                                0.000     0.000
##     従業員            0.000                                0.000     0.000
##   店舗 ~~                                                                 
##     従業員            0.000                                0.000     0.000
## 
## Variances:
##                    Estimate   Std.Err  z-value  P(>|z|)   Std.lv   Std.all 
##    .味                -0.005       NA                      -0.005    -0.004
##    .新鮮               0.254       NA                       0.254     0.231
##    .品揃え             0.925       NA                       0.925     0.547
##    .品切れ             0.233       NA                       0.233     0.137
##    .時間               1.135       NA                       1.135     0.697
##    .layout         -6100.830       NA                   -6100.830 -5013.185
##    .image              1.947       NA                       1.947     1.000
##    .態度               0.000                                0.000     0.000
##    .総合               0.473       NA                       0.473     0.223
##     商品サービス       1.305       NA                       1.000     1.000
##     システム           1.467       NA                       1.000     1.000
##     店舗            6102.047       NA                       1.000     1.000
##     従業員             0.410       NA                       1.000     1.000
summary(fit, fit.measure=TRUE, standardized=TRUE)
## Warning in lav_object_summary(object = object, header = header, fit.measures = fit.measures, : lavaan WARNING: fit measures not available if model did not converge
## lavaan 0.6.15 did NOT end normally after 790 iterations
## ** WARNING ** Estimates below are most likely unreliable
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        20
## 
##   Number of observations                            20
## 
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate   Std.Err  z-value  P(>|z|)   Std.lv   Std.all 
##   商品サービス =~                                                          
##     味                 1.000                                1.142     1.002
##     新鮮               0.805       NA                       0.920     0.877
##     品揃え             0.766       NA                       0.875     0.673
##   システム =~                                                              
##     品切れ             1.000                                1.211     0.929
##     時間               0.579       NA                       0.702     0.550
##   店舗 =~                                                                  
##     layout             1.000                               78.116    70.811
##     image              0.000       NA                       0.013     0.009
##   従業員 =~                                                                
##     態度               1.000                                0.640     1.000
## 
## Regressions:
##                  Estimate   Std.Err  z-value  P(>|z|)   Std.lv   Std.all 
##   総合 ~                                                                 
##     商品サービス     0.824       NA                       0.941     0.646
##     システム         0.698       NA                       0.845     0.580
##     店舗             0.000       NA                       0.002     0.001
##     従業員           0.350       NA                       0.224     0.154
## 
## Covariances:
##                   Estimate   Std.Err  z-value  P(>|z|)   Std.lv   Std.all 
##   商品サービス ~~                                                         
##     システム          0.000                                0.000     0.000
##     店舗              0.000                                0.000     0.000
##     従業員            0.000                                0.000     0.000
##   システム ~~                                                             
##     店舗              0.000                                0.000     0.000
##     従業員            0.000                                0.000     0.000
##   店舗 ~~                                                                 
##     従業員            0.000                                0.000     0.000
## 
## Variances:
##                    Estimate   Std.Err  z-value  P(>|z|)   Std.lv   Std.all 
##    .味                -0.005       NA                      -0.005    -0.004
##    .新鮮               0.254       NA                       0.254     0.231
##    .品揃え             0.925       NA                       0.925     0.547
##    .品切れ             0.233       NA                       0.233     0.137
##    .時間               1.135       NA                       1.135     0.697
##    .layout         -6100.830       NA                   -6100.830 -5013.185
##    .image              1.947       NA                       1.947     1.000
##    .態度               0.000                                0.000     0.000
##    .総合               0.473       NA                       0.473     0.223
##     商品サービス       1.305       NA                       1.000     1.000
##     システム           1.467       NA                       1.000     1.000
##     店舗            6102.047       NA                       1.000     1.000
##     従業員             0.410       NA                       1.000     1.000
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
https://bi.biopapyrus.jp/rnaseq/analysis/clutering/hclust.html https://www.marketechlabo.com/r-distance-clustering/
library("tidyverse")
library("lavaan")
library("semPlot")
dat <- read.csv("istat3.csv", header = TRUE,row.names=1)
#str(dat)
attach(dat)
dat.d <- as.dist(1-cor(dat[,-1]))
dat.hc <- hclust(dat.d, method = "ward.D2")
par(family = "HiraKakuProN-W3") #日本語フォントの指定
plot(dat.hc, hang = -1)
rect.hclust(dat.hc, k = 3)
model <- '
  # measurement model(測定方程式)
    家庭環境 =~ 収入 + しつけ
    知的能力 =~ 暗記力 + 計算力 + 読解力
    学習方法 =~ 学習時間 + 繰返し + 計画的
    
  # regressions
    合格有無 ~ 家庭環境 + 知的能力 + 学習方法
    知的能力 ~ 家庭環境
    知的能力 ~ 学習方法
    
  # residual correlations
    学習方法 ~~ 0*家庭環境
'
fit <- sem(model=model, data=dat, estimator="ML")
## Warning in lav_object_post_check(object): lavaan WARNING: some estimated ov
## variances are negative
summary(fit, standardized=TRUE) # 標準化
## lavaan 0.6.15 ended normally after 66 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        22
## 
##   Number of observations                            30
## 
## Model Test User Model:
##                                                       
##   Test statistic                                30.333
##   Degrees of freedom                                23
##   P-value (Chi-square)                           0.140
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   家庭環境 =~                                                         
##     収入            1.000                               0.256    0.517
##     しつけ          1.997    1.034    1.931    0.054    0.512    1.026
##   知的能力 =~                                                         
##     暗記力          1.000                               0.170    0.341
##     計算力          1.024    0.736    1.391    0.164    0.174    0.349
##     読解力          2.868    1.681    1.706    0.088    0.487    0.977
##   学習方法 =~                                                         
##     学習時間        1.000                               0.389    0.794
##     繰返し          1.025    0.241    4.252    0.000    0.398    0.804
##     計画的          0.737    0.244    3.017    0.003    0.286    0.574
## 
## Regressions:
##                Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   合格有無 ~                                                        
##     家庭環境      0.662    0.258    2.570    0.010    0.170    0.344
##     知的能力      1.520    0.833    1.825    0.068    0.258    0.524
##     学習方法      0.663    0.174    3.804    0.000    0.258    0.523
##   知的能力 ~                                                        
##     家庭環境      0.040    0.120    0.334    0.738    0.061    0.061
##     学習方法      0.114    0.112    1.016    0.310    0.260    0.260
## 
## Covariances:
##                Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   家庭環境 ~~                                                       
##     学習方法      0.000                               0.000    0.000
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .収入            0.180    0.054    3.336    0.001    0.180    0.733
##    .しつけ         -0.013    0.109   -0.118    0.906   -0.013   -0.052
##    .暗記力          0.220    0.058    3.819    0.000    0.220    0.884
##    .計算力          0.219    0.057    3.814    0.000    0.219    0.878
##    .読解力          0.011    0.071    0.161    0.872    0.011    0.046
##    .学習時間        0.089    0.034    2.630    0.009    0.089    0.370
##    .繰返し          0.087    0.034    2.536    0.011    0.087    0.353
##    .計画的          0.167    0.047    3.532    0.000    0.167    0.670
##    .合格有無        0.041    0.028    1.479    0.139    0.041    0.170
##     家庭環境        0.066    0.051    1.284    0.199    1.000    1.000
##    .知的能力        0.027    0.029    0.918    0.358    0.929    0.929
##     学習方法        0.151    0.063    2.412    0.016    1.000    1.000
summary(fit, fit.measure=TRUE, standardized=TRUE)
## lavaan 0.6.15 ended normally after 66 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        22
## 
##   Number of observations                            30
## 
## Model Test User Model:
##                                                       
##   Test statistic                                30.333
##   Degrees of freedom                                23
##   P-value (Chi-square)                           0.140
## 
## Model Test Baseline Model:
## 
##   Test statistic                               118.064
##   Degrees of freedom                                36
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.911
##   Tucker-Lewis Index (TLI)                       0.860
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -150.345
##   Loglikelihood unrestricted model (H1)       -135.178
##                                                       
##   Akaike (AIC)                                 344.690
##   Bayesian (BIC)                               375.516
##   Sample-size adjusted Bayesian (SABIC)        307.019
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.103
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.193
##   P-value H_0: RMSEA <= 0.050                    0.207
##   P-value H_0: RMSEA >= 0.080                    0.673
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.104
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   家庭環境 =~                                                         
##     収入            1.000                               0.256    0.517
##     しつけ          1.997    1.034    1.931    0.054    0.512    1.026
##   知的能力 =~                                                         
##     暗記力          1.000                               0.170    0.341
##     計算力          1.024    0.736    1.391    0.164    0.174    0.349
##     読解力          2.868    1.681    1.706    0.088    0.487    0.977
##   学習方法 =~                                                         
##     学習時間        1.000                               0.389    0.794
##     繰返し          1.025    0.241    4.252    0.000    0.398    0.804
##     計画的          0.737    0.244    3.017    0.003    0.286    0.574
## 
## Regressions:
##                Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   合格有無 ~                                                        
##     家庭環境      0.662    0.258    2.570    0.010    0.170    0.344
##     知的能力      1.520    0.833    1.825    0.068    0.258    0.524
##     学習方法      0.663    0.174    3.804    0.000    0.258    0.523
##   知的能力 ~                                                        
##     家庭環境      0.040    0.120    0.334    0.738    0.061    0.061
##     学習方法      0.114    0.112    1.016    0.310    0.260    0.260
## 
## Covariances:
##                Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   家庭環境 ~~                                                       
##     学習方法      0.000                               0.000    0.000
## 
## Variances:
##                  Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .収入            0.180    0.054    3.336    0.001    0.180    0.733
##    .しつけ         -0.013    0.109   -0.118    0.906   -0.013   -0.052
##    .暗記力          0.220    0.058    3.819    0.000    0.220    0.884
##    .計算力          0.219    0.057    3.814    0.000    0.219    0.878
##    .読解力          0.011    0.071    0.161    0.872    0.011    0.046
##    .学習時間        0.089    0.034    2.630    0.009    0.089    0.370
##    .繰返し          0.087    0.034    2.536    0.011    0.087    0.353
##    .計画的          0.167    0.047    3.532    0.000    0.167    0.670
##    .合格有無        0.041    0.028    1.479    0.139    0.041    0.170
##     家庭環境        0.066    0.051    1.284    0.199    1.000    1.000
##    .知的能力        0.027    0.029    0.918    0.358    0.929    0.929
##     学習方法        0.151    0.063    2.412    0.016    1.000    1.000
par(family = "HiraKakuProN-W3") #日本語フォントの指定
semPaths(fit, what = "stand", style = "lisrel", layout = "tree", rotation = 2, nCharNodes = 0, nCharEdges = 0, fade = FALSE, edge.width = 0.2, label.scale = FALSE, label.cex = 1.2, theme = 'gray', asize = 6.0, node.width = 2.0, curve = 2.0)
library("tidyverse")
library("lavaan")
library("semPlot")
dat <- read.csv("istat3.csv", header = TRUE,row.names=1)
#str(dat)
attach(dat)
## The following objects are masked from dat (pos = 3):
## 
##     しつけ, 収入, 合格有無, 学習時間, 暗記力, 繰返し, 計画的, 計算力,
##     読解力
#dat.d <- proxy::dist(dat, method="correlation")各サンプル間の距離
dat.d <- as.dist(1-cor(dat))
dat.d
##           合格有無      収入    しつけ    計算力    読解力    暗記力    計画的
## 収入     0.8144796                                                            
## しつけ   0.6044854 0.4696509                                                  
## 計算力   0.7393199 0.8741544 0.9375000                                        
## 読解力   0.3348163 1.0089890 0.9375000 0.6696429                              
## 暗記力   0.8561765 1.1258456 0.9285714 0.6607143 0.6607143                    
## 計画的   0.7393199 1.2786580 1.3392857 0.9375000 0.9375000 1.0625000          
## 繰返し   0.5429864 0.9502262 1.0089890 1.0089890 0.8741544 0.8561765 0.4696509
## 学習時間 0.3409180 0.8901530 0.8090593 1.0818317 0.6726732 1.0545545 0.6726732
##             繰返し
## 収入              
## しつけ            
## 計算力            
## 読解力            
## 暗記力            
## 計画的            
## 繰返し            
## 学習時間 0.3409180
#dat.hc <- hclust(dat.d, method = "ward.D2")
#par(family = "HiraKakuProN-W3") #日本語フォントの指定
#plot(dat.hc, hang = -1)
#rect.hclust(dat.hc, k = 3)
library(tidyverse)
library(lavaan)
library(semPlot)
create_Model <- function() {
  # 変数名 ∼ 依存変数
  # + 依存変数の合成
  return ("
    # 潜在変数の定義
      ind60 =~ x1 + x2 + x3
      dem60 =~ y1 + y2 + y3 + y4
      dem65 =~ y5 + y6 + y7 + y8
    # 回帰
      dem60 ~ ind60
      dem65 ~ ind60 + dem60
    # 残差共分散
      y1 ~~ y5
      y2 ~~ y4 + y6
      y3 ~~ y7
      y4 ~~ y8
      y6 ~~ y8
  ")
}
model <- create_Model()
res_sem <- cfa(model, data= PoliticalDemocracy)
summary(res_sem, standardized = TRUE)
## lavaan 0.6.15 ended normally after 68 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        31
## 
##   Number of observations                            75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                38.125
##   Degrees of freedom                                35
##   P-value (Chi-square)                           0.329
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   ind60 =~                                                              
##     x1                1.000                               0.670    0.920
##     x2                2.180    0.139   15.742    0.000    1.460    0.973
##     x3                1.819    0.152   11.967    0.000    1.218    0.872
##   dem60 =~                                                              
##     y1                1.000                               2.223    0.850
##     y2                1.257    0.182    6.889    0.000    2.794    0.717
##     y3                1.058    0.151    6.987    0.000    2.351    0.722
##     y4                1.265    0.145    8.722    0.000    2.812    0.846
##   dem65 =~                                                              
##     y5                1.000                               2.103    0.808
##     y6                1.186    0.169    7.024    0.000    2.493    0.746
##     y7                1.280    0.160    8.002    0.000    2.691    0.824
##     y8                1.266    0.158    8.007    0.000    2.662    0.828
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   dem60 ~                                                               
##     ind60             1.483    0.399    3.715    0.000    0.447    0.447
##   dem65 ~                                                               
##     ind60             0.572    0.221    2.586    0.010    0.182    0.182
##     dem60             0.837    0.098    8.514    0.000    0.885    0.885
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .y1 ~~                                                                 
##    .y5                0.624    0.358    1.741    0.082    0.624    0.296
##  .y2 ~~                                                                 
##    .y4                1.313    0.702    1.871    0.061    1.313    0.273
##    .y6                2.153    0.734    2.934    0.003    2.153    0.356
##  .y3 ~~                                                                 
##    .y7                0.795    0.608    1.308    0.191    0.795    0.191
##  .y4 ~~                                                                 
##    .y8                0.348    0.442    0.787    0.431    0.348    0.109
##  .y6 ~~                                                                 
##    .y8                1.356    0.568    2.386    0.017    1.356    0.338
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x1                0.082    0.019    4.184    0.000    0.082    0.154
##    .x2                0.120    0.070    1.718    0.086    0.120    0.053
##    .x3                0.467    0.090    5.177    0.000    0.467    0.239
##    .y1                1.891    0.444    4.256    0.000    1.891    0.277
##    .y2                7.373    1.374    5.366    0.000    7.373    0.486
##    .y3                5.067    0.952    5.324    0.000    5.067    0.478
##    .y4                3.148    0.739    4.261    0.000    3.148    0.285
##    .y5                2.351    0.480    4.895    0.000    2.351    0.347
##    .y6                4.954    0.914    5.419    0.000    4.954    0.443
##    .y7                3.431    0.713    4.814    0.000    3.431    0.322
##    .y8                3.254    0.695    4.685    0.000    3.254    0.315
##     ind60             0.448    0.087    5.173    0.000    1.000    1.000
##    .dem60             3.956    0.921    4.295    0.000    0.800    0.800
##    .dem65             0.172    0.215    0.803    0.422    0.039    0.039
fitMeasures(res_sem)
##                  npar                  fmin                 chisq 
##                31.000                 0.254                38.125 
##                    df                pvalue        baseline.chisq 
##                35.000                 0.329               730.654 
##           baseline.df       baseline.pvalue                   cfi 
##                55.000                 0.000                 0.995 
##                   tli                  nnfi                   rfi 
##                 0.993                 0.993                 0.918 
##                   nfi                  pnfi                   ifi 
##                 0.948                 0.603                 0.996 
##                   rni                  logl     unrestricted.logl 
##                 0.995             -1547.791             -1528.728 
##                   aic                   bic                ntotal 
##              3157.582              3229.424                75.000 
##                  bic2                 rmsea        rmsea.ci.lower 
##              3131.720                 0.035                 0.000 
##        rmsea.ci.upper        rmsea.ci.level          rmsea.pvalue 
##                 0.092                 0.900                 0.611 
##        rmsea.close.h0 rmsea.notclose.pvalue     rmsea.notclose.h0 
##                 0.050                 0.114                 0.080 
##                   rmr            rmr_nomean                  srmr 
##                 0.276                 0.276                 0.044 
##          srmr_bentler   srmr_bentler_nomean                  crmr 
##                 0.044                 0.044                 0.049 
##           crmr_nomean            srmr_mplus     srmr_mplus_nomean 
##                 0.049                 0.045                 0.045 
##                 cn_05                 cn_01                   gfi 
##                98.970               113.803                 0.923 
##                  agfi                  pgfi                   mfi 
##                 0.854                 0.489                 0.979 
##                  ecvi 
##                 1.335
semPaths(res_sem, "model", "est", intercepts = FALSE)
str(PoliticalDemocracy)
## 'data.frame':    75 obs. of  11 variables:
##  $ y1: num  2.5 1.25 7.5 8.9 10 7.5 7.5 7.5 2.5 10 ...
##  $ y2: num  0 0 8.8 8.8 3.33 ...
##  $ y3: num  3.33 3.33 10 10 10 ...
##  $ y4: num  0 0 9.2 9.2 6.67 ...
##  $ y5: num  1.25 6.25 8.75 8.91 7.5 ...
##  $ y6: num  0 1.1 8.09 8.13 3.33 ...
##  $ y7: num  3.73 6.67 10 10 10 ...
##  $ y8: num  3.333 0.737 8.212 4.615 6.667 ...
##  $ x1: num  4.44 5.38 5.96 6.29 5.86 ...
##  $ x2: num  3.64 5.06 6.26 7.57 6.82 ...
##  $ x3: num  2.56 3.57 5.22 6.27 4.57 ...
head(PoliticalDemocracy)
| y1 | y2 | y3 | y4 | y5 | y6 | y7 | y8 | x1 | x2 | x3 | 
|---|---|---|---|---|---|---|---|---|---|---|
| 2.50 | 0.000000 | 3.333333 | 0.000000 | 1.250000 | 0.000000 | 3.726360 | 3.333333 | 4.442651 | 3.637586 | 2.557615 | 
| 1.25 | 0.000000 | 3.333333 | 0.000000 | 6.250000 | 1.100000 | 6.666666 | 0.736999 | 5.384495 | 5.062595 | 3.568079 | 
| 7.50 | 8.800000 | 9.999998 | 9.199991 | 8.750000 | 8.094061 | 9.999998 | 8.211809 | 5.961005 | 6.255750 | 5.224433 | 
| 8.90 | 8.800000 | 9.999998 | 9.199991 | 8.907948 | 8.127979 | 9.999998 | 4.615086 | 6.285998 | 7.567863 | 6.267495 | 
| 10.00 | 3.333333 | 9.999998 | 6.666666 | 7.500000 | 3.333333 | 9.999998 | 6.666666 | 5.863631 | 6.818924 | 4.573679 | 
| 7.50 | 3.333333 | 6.666666 | 6.666666 | 6.250000 | 1.100000 | 6.666666 | 0.368500 | 5.533389 | 5.135798 | 3.892270 |