{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "title: 结构方程模型R语言实战教程\n", "date: 2024-02-28 12:44:03\n", "tags: [结构方程, R, mplus, amos]\n", "mathjax: true\n", "---\n", "\n", "SEMinR 为创建和估计结构方程模型 (SEM) 非常容易的使用方法。 SEMinR 集成了 SmartPLS 具有的偏最小二乘路径建模 (PLS-PM) 进行估计,也可以使用 LISREL 和 AMOS 的基于协方差的结构方程模型 (CBSEM) 进行估计, 还支持反应性测量模型的验证性因素分析 (CFA)。SEMinR 中 CBSEM 和 CFA 估计都使用 Lavaan 软件包。\n", "\n", "\n", "\n", "SEMinR 有如下特点:\n", "- 使用一种通俗易懂语言,用于在 R 中构建和估计结构方程模型\n", "- 可以使用基于方差的 PLS 估计和基于协方差的 SEM 估计来对潜变量结构进行建模 \n", "- 高级函数可快速指定交互作用、高阶结构 和结构路径\n", "\n", "SEMinR 使用自己的 PLS-PM 估计引擎,并与 Lavaan 软件包集成以进行 CBSEM/CFA 估计。 它还带来了其他软件包或软件中没有的一些方法。\n", "\n", "## 安装\n", "\n", "使用如下代码安装 SEMinR , 它是R的包, 所以你需要在R中运行如下代码:\n", "\n", "```R\n", "install.packages(\"seminr\")\n", "```\n", "\n", "注意seminr是使用R4.3.3编译的, 你的R版本不能低于这个版本.\n", "\n", "然后加载包:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "library(seminr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 数据\n", "\n", "你需要从任何数据文件中加载数据到数据框(data.frame)。 列名必须是测量项目的名称。 重要提示:避免在列名称中使用星号“*”(这些是为交互术语保留的)。\n", "\n", "我们的教程使用的是 seminr 自带的数据集 mobi, 可以使用data方法加载数据, 如下:\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
A data.frame: 6 × 24
CUEX1CUEX2CUEX3CUSA1CUSA2CUSA3CUSCOCUSL1CUSL2CUSL3IMAG5PERQ1PERQ2PERQ3PERQ4PERQ5PERQ6PERQ7PERV1PERV2
<int><int><int><int><int><int><int><int><int><int><int><int><int><int><int><int><int><int><int><int>
1 7 7 6 6 4 7 7 65 6 4 7 6 4 76 5 5 2 3
21010 91010 81010210 910 91010910101010
3 7 7 7 8 7 7 6 62 7 7 7 8 5 78 7 7 7 7
4 710 5101010 51041010 81010 84 5 8 5 5
5 8 71010 8 8 5103 8 910 9 8109 9 8 6 6
610 9 7 8 7 7 810310 9 910 9108 9 91010
\n" ], "text/latex": [ "A data.frame: 6 × 24\n", "\\begin{tabular}{r|lllllllllllllllllllll}\n", " & CUEX1 & CUEX2 & CUEX3 & CUSA1 & CUSA2 & CUSA3 & CUSCO & CUSL1 & CUSL2 & CUSL3 & ⋯ & IMAG5 & PERQ1 & PERQ2 & PERQ3 & PERQ4 & PERQ5 & PERQ6 & PERQ7 & PERV1 & PERV2\\\\\n", " & & & & & & & & & & & ⋯ & & & & & & & & & & \\\\\n", "\\hline\n", "\t1 & 7 & 7 & 6 & 6 & 4 & 7 & 7 & 6 & 5 & 6 & ⋯ & 4 & 7 & 6 & 4 & 7 & 6 & 5 & 5 & 2 & 3\\\\\n", "\t2 & 10 & 10 & 9 & 10 & 10 & 8 & 10 & 10 & 2 & 10 & ⋯ & 9 & 10 & 9 & 10 & 10 & 9 & 10 & 10 & 10 & 10\\\\\n", "\t3 & 7 & 7 & 7 & 8 & 7 & 7 & 6 & 6 & 2 & 7 & ⋯ & 7 & 7 & 8 & 5 & 7 & 8 & 7 & 7 & 7 & 7\\\\\n", "\t4 & 7 & 10 & 5 & 10 & 10 & 10 & 5 & 10 & 4 & 10 & ⋯ & 10 & 8 & 10 & 10 & 8 & 4 & 5 & 8 & 5 & 5\\\\\n", "\t5 & 8 & 7 & 10 & 10 & 8 & 8 & 5 & 10 & 3 & 8 & ⋯ & 9 & 10 & 9 & 8 & 10 & 9 & 9 & 8 & 6 & 6\\\\\n", "\t6 & 10 & 9 & 7 & 8 & 7 & 7 & 8 & 10 & 3 & 10 & ⋯ & 9 & 9 & 10 & 9 & 10 & 8 & 9 & 9 & 10 & 10\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.frame: 6 × 24\n", "\n", "| | CUEX1 <int> | CUEX2 <int> | CUEX3 <int> | CUSA1 <int> | CUSA2 <int> | CUSA3 <int> | CUSCO <int> | CUSL1 <int> | CUSL2 <int> | CUSL3 <int> | ⋯ ⋯ | IMAG5 <int> | PERQ1 <int> | PERQ2 <int> | PERQ3 <int> | PERQ4 <int> | PERQ5 <int> | PERQ6 <int> | PERQ7 <int> | PERV1 <int> | PERV2 <int> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| 1 | 7 | 7 | 6 | 6 | 4 | 7 | 7 | 6 | 5 | 6 | ⋯ | 4 | 7 | 6 | 4 | 7 | 6 | 5 | 5 | 2 | 3 |\n", "| 2 | 10 | 10 | 9 | 10 | 10 | 8 | 10 | 10 | 2 | 10 | ⋯ | 9 | 10 | 9 | 10 | 10 | 9 | 10 | 10 | 10 | 10 |\n", "| 3 | 7 | 7 | 7 | 8 | 7 | 7 | 6 | 6 | 2 | 7 | ⋯ | 7 | 7 | 8 | 5 | 7 | 8 | 7 | 7 | 7 | 7 |\n", "| 4 | 7 | 10 | 5 | 10 | 10 | 10 | 5 | 10 | 4 | 10 | ⋯ | 10 | 8 | 10 | 10 | 8 | 4 | 5 | 8 | 5 | 5 |\n", "| 5 | 8 | 7 | 10 | 10 | 8 | 8 | 5 | 10 | 3 | 8 | ⋯ | 9 | 10 | 9 | 8 | 10 | 9 | 9 | 8 | 6 | 6 |\n", "| 6 | 10 | 9 | 7 | 8 | 7 | 7 | 8 | 10 | 3 | 10 | ⋯ | 9 | 9 | 10 | 9 | 10 | 8 | 9 | 9 | 10 | 10 |\n", "\n" ], "text/plain": [ " CUEX1 CUEX2 CUEX3 CUSA1 CUSA2 CUSA3 CUSCO CUSL1 CUSL2 CUSL3 ⋯ IMAG5 PERQ1\n", "1 7 7 6 6 4 7 7 6 5 6 ⋯ 4 7 \n", "2 10 10 9 10 10 8 10 10 2 10 ⋯ 9 10 \n", "3 7 7 7 8 7 7 6 6 2 7 ⋯ 7 7 \n", "4 7 10 5 10 10 10 5 10 4 10 ⋯ 10 8 \n", "5 8 7 10 10 8 8 5 10 3 8 ⋯ 9 10 \n", "6 10 9 7 8 7 7 8 10 3 10 ⋯ 9 9 \n", " PERQ2 PERQ3 PERQ4 PERQ5 PERQ6 PERQ7 PERV1 PERV2\n", "1 6 4 7 6 5 5 2 3 \n", "2 9 10 10 9 10 10 10 10 \n", "3 8 5 7 8 7 7 7 7 \n", "4 10 10 8 4 5 8 5 5 \n", "5 9 8 10 9 9 8 6 6 \n", "6 10 9 10 8 9 9 10 10 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df=data('mobi')\n", "head(mobi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " mobi 数据框(也可在 semPLS R 软件包中找到) 该数据集来自适用于移动电话市场的欧洲客户满意度指数 (ECSI) 测量工具(Tenenhaus 等人,2005 年)。\n", "\n", " 下面是一些描述性统计:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "\n", "
  1. 250
  2. 24
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 250\n", "\\item 24\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 250\n", "2. 24\n", "\n", "\n" ], "text/plain": [ "[1] 250 24" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "dim(mobi) # 有250个样本, 24个变量" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/plain": [ " CUEX1 CUEX2 CUEX3 CUSA1 \n", " Min. : 1.00 Min. : 1.000 Min. : 1.000 Min. : 4.000 \n", " 1st Qu.: 7.00 1st Qu.: 7.000 1st Qu.: 6.000 1st Qu.: 7.000 \n", " Median : 8.00 Median : 8.000 Median : 8.000 Median : 8.000 \n", " Mean : 7.58 Mean : 7.532 Mean : 7.424 Mean : 7.988 \n", " 3rd Qu.: 8.00 3rd Qu.: 9.000 3rd Qu.: 9.000 3rd Qu.: 9.000 \n", " Max. :10.00 Max. :10.000 Max. :10.000 Max. :10.000 \n", " CUSA2 CUSA3 CUSCO CUSL1 \n", " Min. : 1.000 Min. : 1.000 Min. : 1.000 Min. : 1.000 \n", " 1st Qu.: 6.000 1st Qu.: 7.000 1st Qu.: 6.000 1st Qu.: 6.000 \n", " Median : 7.000 Median : 7.000 Median : 7.000 Median : 8.000 \n", " Mean : 7.128 Mean : 7.316 Mean : 7.068 Mean : 7.452 \n", " 3rd Qu.: 8.000 3rd Qu.: 8.000 3rd Qu.: 9.000 3rd Qu.:10.000 \n", " Max. :10.000 Max. :10.000 Max. :10.000 Max. :10.000 \n", " CUSL2 CUSL3 IMAG1 IMAG2 \n", " Min. : 1.000 Min. : 1.000 Min. : 1.00 Min. : 1.00 \n", " 1st Qu.: 3.000 1st Qu.: 7.000 1st Qu.: 7.00 1st Qu.: 7.00 \n", " Median : 4.000 Median : 8.000 Median : 8.00 Median : 8.00 \n", " Mean : 4.988 Mean : 7.668 Mean : 7.64 Mean : 7.78 \n", " 3rd Qu.: 6.750 3rd Qu.:10.000 3rd Qu.: 9.00 3rd Qu.: 9.00 \n", " Max. :10.000 Max. :10.000 Max. :10.00 Max. :10.00 \n", " IMAG3 IMAG4 IMAG5 PERQ1 \n", " Min. : 1.000 Min. : 1.000 Min. : 1.000 Min. : 2.000 \n", " 1st Qu.: 5.000 1st Qu.: 7.000 1st Qu.: 7.000 1st Qu.: 7.000 \n", " Median : 7.000 Median : 8.000 Median : 8.000 Median : 8.000 \n", " Mean : 6.744 Mean : 7.588 Mean : 7.932 Mean : 7.944 \n", " 3rd Qu.: 8.000 3rd Qu.: 9.000 3rd Qu.: 9.000 3rd Qu.: 9.000 \n", " Max. :10.000 Max. :10.000 Max. :10.000 Max. :10.000 \n", " PERQ2 PERQ3 PERQ4 PERQ5 \n", " Min. : 1.000 Min. : 1.0 Min. : 1.000 Min. : 3.000 \n", " 1st Qu.: 6.000 1st Qu.: 7.0 1st Qu.: 7.000 1st Qu.: 7.000 \n", " Median : 7.000 Median : 8.0 Median : 8.000 Median : 8.000 \n", " Mean : 7.192 Mean : 7.7 Mean : 7.916 Mean : 7.872 \n", " 3rd Qu.: 8.000 3rd Qu.: 9.0 3rd Qu.: 9.000 3rd Qu.: 9.000 \n", " Max. :10.000 Max. :10.0 Max. :10.000 Max. :10.000 \n", " PERQ6 PERQ7 PERV1 PERV2 \n", " Min. : 1.000 Min. : 1.000 Min. : 1.000 Min. : 1.000 \n", " 1st Qu.: 7.000 1st Qu.: 7.000 1st Qu.: 5.000 1st Qu.: 6.000 \n", " Median : 8.000 Median : 8.000 Median : 6.000 Median : 7.000 \n", " Mean : 7.776 Mean : 7.592 Mean : 6.156 Mean : 6.916 \n", " 3rd Qu.: 9.000 3rd Qu.: 9.000 3rd Qu.: 8.000 3rd Qu.: 8.000 \n", " Max. :10.000 Max. :10.000 Max. :10.000 Max. :10.000 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "summary(mobi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 测量模型的设定\n", "\n", "\"multi_items\"这个函数非常有用, 我么可以使用\"multi_items\"来生成连续变化的变量名, 例如:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "\n", "
  1. 'IMAG1'
  2. 'IMAG2'
  3. 'IMAG3'
  4. 'IMAG4'
  5. 'IMAG5'
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'IMAG1'\n", "\\item 'IMAG2'\n", "\\item 'IMAG3'\n", "\\item 'IMAG4'\n", "\\item 'IMAG5'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'IMAG1'\n", "2. 'IMAG2'\n", "3. 'IMAG3'\n", "4. 'IMAG4'\n", "5. 'IMAG5'\n", "\n", "\n" ], "text/plain": [ "[1] \"IMAG1\" \"IMAG2\" \"IMAG3\" \"IMAG4\" \"IMAG5\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "multi_items(\"IMAG\", 1:5)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "\n", "
  1. 'IMAG1'
  2. 'IMAG3'
  3. 'IMAG4'
  4. 'IMAG5'
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'IMAG1'\n", "\\item 'IMAG3'\n", "\\item 'IMAG4'\n", "\\item 'IMAG5'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'IMAG1'\n", "2. 'IMAG3'\n", "3. 'IMAG4'\n", "4. 'IMAG5'\n", "\n", "\n" ], "text/plain": [ "[1] \"IMAG1\" \"IMAG3\" \"IMAG4\" \"IMAG5\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 不连续的变量名\n", "\n", "multi_items(\"IMAG\", c(1, 3:5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"composite\"这个函数顾名思义, 就是生成一个\"构建\", 即形成性潜变量, 形成性只能在pls-sem中使用, 也就是说像AMOS这样的软件是不支持的, 例如:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "\n", "
  1. 'IMAG'
  2. 'IMAG1'
  3. 'A'
  4. 'IMAG'
  5. 'IMAG2'
  6. 'A'
  7. 'IMAG'
  8. 'IMAG3'
  9. 'A'
  10. 'IMAG'
  11. 'IMAG4'
  12. 'A'
  13. 'IMAG'
  14. 'IMAG5'
  15. 'A'
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'IMAG'\n", "\\item 'IMAG1'\n", "\\item 'A'\n", "\\item 'IMAG'\n", "\\item 'IMAG2'\n", "\\item 'A'\n", "\\item 'IMAG'\n", "\\item 'IMAG3'\n", "\\item 'A'\n", "\\item 'IMAG'\n", "\\item 'IMAG4'\n", "\\item 'A'\n", "\\item 'IMAG'\n", "\\item 'IMAG5'\n", "\\item 'A'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'IMAG'\n", "2. 'IMAG1'\n", "3. 'A'\n", "4. 'IMAG'\n", "5. 'IMAG2'\n", "6. 'A'\n", "7. 'IMAG'\n", "8. 'IMAG3'\n", "9. 'A'\n", "10. 'IMAG'\n", "11. 'IMAG4'\n", "12. 'A'\n", "13. 'IMAG'\n", "14. 'IMAG5'\n", "15. 'A'\n", "\n", "\n" ], "text/plain": [ " [1] \"IMAG\" \"IMAG1\" \"A\" \"IMAG\" \"IMAG2\" \"A\" \"IMAG\" \"IMAG3\" \"A\" \n", "[10] \"IMAG\" \"IMAG4\" \"A\" \"IMAG\" \"IMAG5\" \"A\" \n", "attr(,\"class\")\n", "[1] \"character\" \"construct\" \"composite\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 构建一个潜变量, 名为IMAG, 有五个题目, 分别是: 'IMAG1''IMAG2''IMAG3''IMAG4''IMAG5'\n", "composite(\"IMAG\", multi_items(\"IMAG\", 1:5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果你的潜变量是反应性的, 即由反应性指标构成, 那么我们需要使用 \"reflective\" 方法, 基于协方差矩阵的结构方程模型只能支持反应性的指标:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "\n", "
  1. 'IMAG'
  2. 'IMAG1'
  3. 'C'
  4. 'IMAG'
  5. 'IMAG2'
  6. 'C'
  7. 'IMAG'
  8. 'IMAG3'
  9. 'C'
  10. 'IMAG'
  11. 'IMAG4'
  12. 'C'
  13. 'IMAG'
  14. 'IMAG5'
  15. 'C'
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'IMAG'\n", "\\item 'IMAG1'\n", "\\item 'C'\n", "\\item 'IMAG'\n", "\\item 'IMAG2'\n", "\\item 'C'\n", "\\item 'IMAG'\n", "\\item 'IMAG3'\n", "\\item 'C'\n", "\\item 'IMAG'\n", "\\item 'IMAG4'\n", "\\item 'C'\n", "\\item 'IMAG'\n", "\\item 'IMAG5'\n", "\\item 'C'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'IMAG'\n", "2. 'IMAG1'\n", "3. 'C'\n", "4. 'IMAG'\n", "5. 'IMAG2'\n", "6. 'C'\n", "7. 'IMAG'\n", "8. 'IMAG3'\n", "9. 'C'\n", "10. 'IMAG'\n", "11. 'IMAG4'\n", "12. 'C'\n", "13. 'IMAG'\n", "14. 'IMAG5'\n", "15. 'C'\n", "\n", "\n" ], "text/plain": [ " [1] \"IMAG\" \"IMAG1\" \"C\" \"IMAG\" \"IMAG2\" \"C\" \"IMAG\" \"IMAG3\" \"C\" \n", "[10] \"IMAG\" \"IMAG4\" \"C\" \"IMAG\" \"IMAG5\" \"C\" \n", "attr(,\"class\")\n", "[1] \"character\" \"construct\" \"reflective\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "reflective(\"IMAG\", multi_items(\"IMAG\", 1:5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "定义测量模型使用\"constructs\", 例如:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
$composite
\n", "\t\t
\n", "
  1. 'Image'
  2. 'IMAG1'
  3. 'A'
  4. 'Image'
  5. 'IMAG2'
  6. 'A'
  7. 'Image'
  8. 'IMAG3'
  9. 'A'
  10. 'Image'
  11. 'IMAG4'
  12. 'A'
  13. 'Image'
  14. 'IMAG5'
  15. 'A'
\n", "
\n", "\t
$composite
\n", "\t\t
\n", "
  1. 'Expectation'
  2. 'CUEX1'
  3. 'A'
  4. 'Expectation'
  5. 'CUEX2'
  6. 'A'
  7. 'Expectation'
  8. 'CUEX3'
  9. 'A'
\n", "
\n", "\t
$composite
\n", "\t\t
\n", "
  1. 'Value'
  2. 'PERV1'
  3. 'A'
  4. 'Value'
  5. 'PERV2'
  6. 'A'
\n", "
\n", "\t
$composite
\n", "\t\t
\n", "
  1. 'Satisfaction'
  2. 'CUSA1'
  3. 'A'
  4. 'Satisfaction'
  5. 'CUSA2'
  6. 'A'
  7. 'Satisfaction'
  8. 'CUSA3'
  9. 'A'
\n", "
\n", "\t
$scaled_interaction
\n", "\t\t
structure(function (data, measurement_model, structural_model, \n",
       "    ints, ...) \n",
       "{\n",
       "    interaction_name <- paste(iv, moderator, sep = \"*\")\n",
       "    iv1_items <- measurement_model[measurement_model[, \"construct\"] == \n",
       "        iv, \"measurement\"]\n",
       "    iv2_items <- measurement_model[measurement_model[, \"construct\"] == \n",
       "        moderator, \"measurement\"]\n",
       "    iv1_data <- as.data.frame(scale(data[iv1_items]))\n",
       "    iv2_data <- as.data.frame(scale(data[iv2_items]))\n",
       "    multiples_list <- lapply(iv1_data, mult, iv2_data)\n",
       "    interaction_data <- do.call(\"cbind\", multiples_list)\n",
       "    colnames(interaction_data) <- as.vector(sapply(iv1_items, \n",
       "        name_items, iv2_items))\n",
       "    intxn_mm <- matrix(measure_interaction(interaction_name, \n",
       "        interaction_data, weights), ncol = 3, byrow = TRUE)\n",
       "    return(list(name = interaction_name, data = interaction_data, \n",
       "        mm = intxn_mm))\n",
       "}, class = c(\"function\", \"interaction\", \"scaled_interaction\"))
\n", "
\n" ], "text/latex": [ "\\begin{description}\n", "\\item[\\$composite] \\begin{enumerate*}\n", "\\item 'Image'\n", "\\item 'IMAG1'\n", "\\item 'A'\n", "\\item 'Image'\n", "\\item 'IMAG2'\n", "\\item 'A'\n", "\\item 'Image'\n", "\\item 'IMAG3'\n", "\\item 'A'\n", "\\item 'Image'\n", "\\item 'IMAG4'\n", "\\item 'A'\n", "\\item 'Image'\n", "\\item 'IMAG5'\n", "\\item 'A'\n", "\\end{enumerate*}\n", "\n", "\\item[\\$composite] \\begin{enumerate*}\n", "\\item 'Expectation'\n", "\\item 'CUEX1'\n", "\\item 'A'\n", "\\item 'Expectation'\n", "\\item 'CUEX2'\n", "\\item 'A'\n", "\\item 'Expectation'\n", "\\item 'CUEX3'\n", "\\item 'A'\n", "\\end{enumerate*}\n", "\n", "\\item[\\$composite] \\begin{enumerate*}\n", "\\item 'Value'\n", "\\item 'PERV1'\n", "\\item 'A'\n", "\\item 'Value'\n", "\\item 'PERV2'\n", "\\item 'A'\n", "\\end{enumerate*}\n", "\n", "\\item[\\$composite] \\begin{enumerate*}\n", "\\item 'Satisfaction'\n", "\\item 'CUSA1'\n", "\\item 'A'\n", "\\item 'Satisfaction'\n", "\\item 'CUSA2'\n", "\\item 'A'\n", "\\item 'Satisfaction'\n", "\\item 'CUSA3'\n", "\\item 'A'\n", "\\end{enumerate*}\n", "\n", "\\item[\\$scaled\\_interaction] \\begin{minted}{r}\n", "structure(function (data, measurement\\_model, structural\\_model, \n", " ints, ...) \n", "\\{\n", " interaction\\_name <- paste(iv, moderator, sep = \"*\")\n", " iv1\\_items <- measurement\\_model{[}measurement\\_model{[}, \"construct\"{]} == \n", " iv, \"measurement\"{]}\n", " iv2\\_items <- measurement\\_model{[}measurement\\_model{[}, \"construct\"{]} == \n", " moderator, \"measurement\"{]}\n", " iv1\\_data <- as.data.frame(scale(data{[}iv1\\_items{]}))\n", " iv2\\_data <- as.data.frame(scale(data{[}iv2\\_items{]}))\n", " multiples\\_list <- lapply(iv1\\_data, mult, iv2\\_data)\n", " interaction\\_data <- do.call(\"cbind\", multiples\\_list)\n", " colnames(interaction\\_data) <- as.vector(sapply(iv1\\_items, \n", " name\\_items, iv2\\_items))\n", " intxn\\_mm <- matrix(measure\\_interaction(interaction\\_name, \n", " interaction\\_data, weights), ncol = 3, byrow = TRUE)\n", " return(list(name = interaction\\_name, data = interaction\\_data, \n", " mm = intxn\\_mm))\n", "\\}, class = c(\"function\", \"interaction\", \"scaled\\_interaction\"))\n", "\\end{minted}\n", "\\end{description}\n" ], "text/markdown": [ "$composite\n", ": 1. 'Image'\n", "2. 'IMAG1'\n", "3. 'A'\n", "4. 'Image'\n", "5. 'IMAG2'\n", "6. 'A'\n", "7. 'Image'\n", "8. 'IMAG3'\n", "9. 'A'\n", "10. 'Image'\n", "11. 'IMAG4'\n", "12. 'A'\n", "13. 'Image'\n", "14. 'IMAG5'\n", "15. 'A'\n", "\n", "\n", "\n", "$composite\n", ": 1. 'Expectation'\n", "2. 'CUEX1'\n", "3. 'A'\n", "4. 'Expectation'\n", "5. 'CUEX2'\n", "6. 'A'\n", "7. 'Expectation'\n", "8. 'CUEX3'\n", "9. 'A'\n", "\n", "\n", "\n", "$composite\n", ": 1. 'Value'\n", "2. 'PERV1'\n", "3. 'A'\n", "4. 'Value'\n", "5. 'PERV2'\n", "6. 'A'\n", "\n", "\n", "\n", "$composite\n", ": 1. 'Satisfaction'\n", "2. 'CUSA1'\n", "3. 'A'\n", "4. 'Satisfaction'\n", "5. 'CUSA2'\n", "6. 'A'\n", "7. 'Satisfaction'\n", "8. 'CUSA3'\n", "9. 'A'\n", "\n", "\n", "\n", "$scaled_interaction\n", ": ```r\n", "structure(function (data, measurement_model, structural_model, \n", " ints, ...) \n", "{\n", " interaction_name <- paste(iv, moderator, sep = \"*\")\n", " iv1_items <- measurement_model[measurement_model[, \"construct\"] == \n", " iv, \"measurement\"]\n", " iv2_items <- measurement_model[measurement_model[, \"construct\"] == \n", " moderator, \"measurement\"]\n", " iv1_data <- as.data.frame(scale(data[iv1_items]))\n", " iv2_data <- as.data.frame(scale(data[iv2_items]))\n", " multiples_list <- lapply(iv1_data, mult, iv2_data)\n", " interaction_data <- do.call(\"cbind\", multiples_list)\n", " colnames(interaction_data) <- as.vector(sapply(iv1_items, \n", " name_items, iv2_items))\n", " intxn_mm <- matrix(measure_interaction(interaction_name, \n", " interaction_data, weights), ncol = 3, byrow = TRUE)\n", " return(list(name = interaction_name, data = interaction_data, \n", " mm = intxn_mm))\n", "}, class = c(\"function\", \"interaction\", \"scaled_interaction\"))\n", "```\n", "\n", "\n" ], "text/plain": [ "$composite\n", " [1] \"Image\" \"IMAG1\" \"A\" \"Image\" \"IMAG2\" \"A\" \"Image\" \"IMAG3\" \"A\" \n", "[10] \"Image\" \"IMAG4\" \"A\" \"Image\" \"IMAG5\" \"A\" \n", "attr(,\"class\")\n", "[1] \"character\" \"construct\" \"composite\"\n", "\n", "$composite\n", "[1] \"Expectation\" \"CUEX1\" \"A\" \"Expectation\" \"CUEX2\" \n", "[6] \"A\" \"Expectation\" \"CUEX3\" \"A\" \n", "attr(,\"class\")\n", "[1] \"character\" \"construct\" \"composite\"\n", "\n", "$composite\n", "[1] \"Value\" \"PERV1\" \"A\" \"Value\" \"PERV2\" \"A\" \n", "attr(,\"class\")\n", "[1] \"character\" \"construct\" \"composite\"\n", "\n", "$composite\n", "[1] \"Satisfaction\" \"CUSA1\" \"A\" \"Satisfaction\" \"CUSA2\" \n", "[6] \"A\" \"Satisfaction\" \"CUSA3\" \"A\" \n", "attr(,\"class\")\n", "[1] \"character\" \"construct\" \"composite\"\n", "\n", "$scaled_interaction\n", "function (data, measurement_model, structural_model, ints, ...) \n", "{\n", " interaction_name <- paste(iv, moderator, sep = \"*\")\n", " iv1_items <- measurement_model[measurement_model[, \"construct\"] == \n", " iv, \"measurement\"]\n", " iv2_items <- measurement_model[measurement_model[, \"construct\"] == \n", " moderator, \"measurement\"]\n", " iv1_data <- as.data.frame(scale(data[iv1_items]))\n", " iv2_data <- as.data.frame(scale(data[iv2_items]))\n", " multiples_list <- lapply(iv1_data, mult, iv2_data)\n", " interaction_data <- do.call(\"cbind\", multiples_list)\n", " colnames(interaction_data) <- as.vector(sapply(iv1_items, \n", " name_items, iv2_items))\n", " intxn_mm <- matrix(measure_interaction(interaction_name, \n", " interaction_data, weights), ncol = 3, byrow = TRUE)\n", " return(list(name = interaction_name, data = interaction_data, \n", " mm = intxn_mm))\n", "}\n", "\n", "\n", "attr(,\"class\")\n", "[1] \"function\" \"interaction\" \"scaled_interaction\"\n", "\n", "attr(,\"class\")\n", "[1] \"list\" \"measurement_model\" \"seminr_model\" " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "measurements <- constructs(\n", " composite(\"Image\", multi_items(\"IMAG\", 1:5)),\n", " composite(\"Expectation\", multi_items(\"CUEX\", 1:3)),\n", " composite(\"Value\", multi_items(\"PERV\", 1:2)),\n", " composite(\"Satisfaction\", multi_items(\"CUSA\", 1:3)),\n", " interaction_term(iv = \"Image\", moderator = \"Expectation\")\n", ")\n", "\n", "measurement\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "我们使用了interaction_term来定义交互项, 它是用来检验调节效应的, 下面我们定义结构模型:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 结构模型的设定\n", "\n", "\"paths\"就是路径的意思, 我们用这个方法可以定义一条或者多条路径, 它的from参数就是路径的起点, 它的to参数就是路径的终点, \"relationships\"函数用于将所有的路径组合起来构成结构模型:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "\n", "structure <- relationships(\n", " paths(from = c(\"Image\", \"Expectation\", \"Image*Expectation\"), to = \"Value\"),\n", " paths(from = \"Value\", to = \"Satisfaction\")\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 估计结果\n", "\n", "结构方程由测量模型和结构模型构成, 有了上面的模型, 我们可以计算结果了.\n", "\n", "假如我们想使用最小二乘法估计结构方程, 那么我们这样做:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Generating the seminr model\n", "\n", "All 250 observations are valid.\n", "\n" ] }, { "data": { "text/plain": [ "\n", "Results from package seminr (2.3.2)\n", "\n", "Path Coefficients:\n", " Value Satisfaction\n", "R^2 0.321 0.376\n", "AdjR^2 0.313 0.373\n", "Image 0.459 .\n", "Expectation 0.143 .\n", "Image*Expectation -0.143 .\n", "Value . 0.613\n", "\n", "Reliability:\n", " alpha rhoC AVE rhoA\n", "Image 0.723 0.817 0.476 0.753\n", "Expectation 0.452 0.727 0.474 0.466\n", "Image*Expectation 0.802 0.256 0.141 1.000\n", "Value 0.824 0.918 0.848 0.859\n", "Satisfaction 0.779 0.870 0.692 0.806\n", "\n", "Alpha, rhoC, and rhoA should exceed 0.7 while AVE should exceed 0.5\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pls_model <- estimate_pls(data = mobi, \n", " measurement_model = measurements, \n", " structural_model = structure)\n", "summary(pls_model)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Bootstrapping model using seminr...\n", "\n", "SEMinR Model successfully bootstrapped\n", "\n" ] }, { "data": { "text/plain": [ "\n", "Results from Bootstrap resamples: 1000\n", "\n", "Bootstrapped Structural Paths:\n", " Original Est. Bootstrap Mean Bootstrap SD T Stat.\n", "Image -> Value 0.459 0.436 0.069 6.611\n", "Expectation -> Value 0.143 0.149 0.074 1.924\n", "Image*Expectation -> Value -0.143 -0.015 0.178 -0.803\n", "Value -> Satisfaction 0.613 0.615 0.059 10.462\n", " 2.5% CI 97.5% CI\n", "Image -> Value 0.301 0.572\n", "Expectation -> Value -0.001 0.289\n", "Image*Expectation -> Value -0.250 0.251\n", "Value -> Satisfaction 0.485 0.714\n", "\n", "Bootstrapped Weights:\n", " Original Est. Bootstrap Mean Bootstrap SD\n", "IMAG1 -> Image 0.322 0.316 0.042\n", "IMAG2 -> Image 0.223 0.223 0.055\n", "IMAG3 -> Image 0.275 0.273 0.046\n", "IMAG4 -> Image 0.387 0.388 0.050\n", "IMAG5 -> Image 0.220 0.217 0.055\n", "CUEX1 -> Expectation 0.562 0.551 0.088\n", "CUEX2 -> Expectation 0.344 0.335 0.135\n", "CUEX3 -> Expectation 0.524 0.517 0.114\n", "PERV1 -> Value 0.476 0.477 0.020\n", "PERV2 -> Value 0.607 0.606 0.027\n", "CUSA1 -> Satisfaction 0.327 0.328 0.032\n", "CUSA2 -> Satisfaction 0.391 0.390 0.024\n", "CUSA3 -> Satisfaction 0.477 0.477 0.033\n", "IMAG1*CUEX1 -> Image*Expectation -0.262 0.077 0.180\n", "IMAG1*CUEX2 -> Image*Expectation -0.211 0.083 0.175\n", "IMAG1*CUEX3 -> Image*Expectation 0.088 0.085 0.157\n", "IMAG2*CUEX1 -> Image*Expectation 0.322 0.081 0.216\n", "IMAG2*CUEX2 -> Image*Expectation 0.199 0.043 0.182\n", "IMAG2*CUEX3 -> Image*Expectation 0.374 0.075 0.235\n", "IMAG3*CUEX1 -> Image*Expectation -0.030 0.081 0.160\n", "IMAG3*CUEX2 -> Image*Expectation -0.065 0.061 0.144\n", "IMAG3*CUEX3 -> Image*Expectation 0.396 0.087 0.218\n", "IMAG4*CUEX1 -> Image*Expectation -0.128 0.084 0.143\n", "IMAG4*CUEX2 -> Image*Expectation -0.038 0.079 0.139\n", "IMAG4*CUEX3 -> Image*Expectation 0.203 0.085 0.162\n", "IMAG5*CUEX1 -> Image*Expectation -0.264 0.010 0.207\n", "IMAG5*CUEX2 -> Image*Expectation 0.071 0.032 0.162\n", "IMAG5*CUEX3 -> Image*Expectation 0.200 0.047 0.193\n", " T Stat. 2.5% CI 97.5% CI\n", "IMAG1 -> Image 7.620 0.235 0.401\n", "IMAG2 -> Image 4.057 0.115 0.330\n", "IMAG3 -> Image 5.916 0.182 0.365\n", "IMAG4 -> Image 7.801 0.300 0.495\n", "IMAG5 -> Image 4.030 0.096 0.314\n", "CUEX1 -> Expectation 6.378 0.369 0.721\n", "CUEX2 -> Expectation 2.545 0.017 0.569\n", "CUEX3 -> Expectation 4.617 0.299 0.748\n", "PERV1 -> Value 24.180 0.435 0.512\n", "PERV2 -> Value 22.544 0.560 0.663\n", "CUSA1 -> Satisfaction 10.275 0.260 0.382\n", "CUSA2 -> Satisfaction 16.288 0.341 0.433\n", "CUSA3 -> Satisfaction 14.513 0.422 0.553\n", "IMAG1*CUEX1 -> Image*Expectation -1.457 -0.278 0.388\n", "IMAG1*CUEX2 -> Image*Expectation -1.211 -0.290 0.396\n", "IMAG1*CUEX3 -> Image*Expectation 0.561 -0.255 0.369\n", "IMAG2*CUEX1 -> Image*Expectation 1.489 -0.334 0.466\n", "IMAG2*CUEX2 -> Image*Expectation 1.091 -0.354 0.394\n", "IMAG2*CUEX3 -> Image*Expectation 1.588 -0.399 0.467\n", "IMAG3*CUEX1 -> Image*Expectation -0.187 -0.249 0.370\n", "IMAG3*CUEX2 -> Image*Expectation -0.448 -0.237 0.324\n", "IMAG3*CUEX3 -> Image*Expectation 1.819 -0.364 0.452\n", "IMAG4*CUEX1 -> Image*Expectation -0.893 -0.223 0.337\n", "IMAG4*CUEX2 -> Image*Expectation -0.274 -0.224 0.329\n", "IMAG4*CUEX3 -> Image*Expectation 1.247 -0.249 0.378\n", "IMAG5*CUEX1 -> Image*Expectation -1.271 -0.393 0.436\n", "IMAG5*CUEX2 -> Image*Expectation 0.437 -0.299 0.370\n", "IMAG5*CUEX3 -> Image*Expectation 1.038 -0.342 0.411\n", "\n", "Bootstrapped Loadings:\n", " Original Est. Bootstrap Mean Bootstrap SD\n", "IMAG1 -> Image 0.748 0.742 0.045\n", "IMAG2 -> Image 0.565 0.561 0.086\n", "IMAG3 -> Image 0.623 0.622 0.062\n", "IMAG4 -> Image 0.805 0.806 0.041\n", "IMAG5 -> Image 0.682 0.676 0.068\n", "CUEX1 -> Expectation 0.786 0.775 0.074\n", "CUEX2 -> Expectation 0.583 0.569 0.147\n", "CUEX3 -> Expectation 0.682 0.674 0.098\n", "PERV1 -> Value 0.901 0.900 0.020\n", "PERV2 -> Value 0.941 0.940 0.007\n", "CUSA1 -> Satisfaction 0.772 0.771 0.038\n", "CUSA2 -> Satisfaction 0.850 0.849 0.026\n", "CUSA3 -> Satisfaction 0.869 0.869 0.018\n", "IMAG1*CUEX1 -> Image*Expectation -0.134 0.452 0.388\n", "IMAG1*CUEX2 -> Image*Expectation -0.110 0.425 0.376\n", "IMAG1*CUEX3 -> Image*Expectation 0.449 0.385 0.354\n", "IMAG2*CUEX1 -> Image*Expectation 0.479 0.372 0.397\n", "IMAG2*CUEX2 -> Image*Expectation 0.156 0.290 0.339\n", "IMAG2*CUEX3 -> Image*Expectation 0.651 0.284 0.452\n", "IMAG3*CUEX1 -> Image*Expectation -0.137 0.340 0.333\n", "IMAG3*CUEX2 -> Image*Expectation -0.288 0.280 0.365\n", "IMAG3*CUEX3 -> Image*Expectation 0.661 0.285 0.423\n", "IMAG4*CUEX1 -> Image*Expectation -0.178 0.423 0.358\n", "IMAG4*CUEX2 -> Image*Expectation -0.113 0.367 0.356\n", "IMAG4*CUEX3 -> Image*Expectation 0.480 0.349 0.385\n", "IMAG5*CUEX1 -> Image*Expectation -0.316 0.251 0.327\n", "IMAG5*CUEX2 -> Image*Expectation -0.034 0.226 0.309\n", "IMAG5*CUEX3 -> Image*Expectation 0.538 0.234 0.353\n", " T Stat. 2.5% CI 97.5% CI\n", "IMAG1 -> Image 16.544 0.642 0.815\n", "IMAG2 -> Image 6.583 0.369 0.704\n", "IMAG3 -> Image 10.114 0.483 0.730\n", "IMAG4 -> Image 19.541 0.708 0.870\n", "IMAG5 -> Image 10.026 0.519 0.782\n", "CUEX1 -> Expectation 10.608 0.601 0.888\n", "CUEX2 -> Expectation 3.958 0.244 0.789\n", "CUEX3 -> Expectation 6.939 0.457 0.837\n", "PERV1 -> Value 44.041 0.854 0.932\n", "PERV2 -> Value 140.988 0.926 0.952\n", "CUSA1 -> Satisfaction 20.172 0.684 0.838\n", "CUSA2 -> Satisfaction 32.726 0.787 0.891\n", "CUSA3 -> Satisfaction 48.203 0.831 0.901\n", "IMAG1*CUEX1 -> Image*Expectation -0.345 -0.437 0.944\n", "IMAG1*CUEX2 -> Image*Expectation -0.291 -0.430 0.919\n", "IMAG1*CUEX3 -> Image*Expectation 1.269 -0.370 0.928\n", "IMAG2*CUEX1 -> Image*Expectation 1.208 -0.434 0.959\n", "IMAG2*CUEX2 -> Image*Expectation 0.459 -0.394 0.878\n", "IMAG2*CUEX3 -> Image*Expectation 1.440 -0.576 0.939\n", "IMAG3*CUEX1 -> Image*Expectation -0.411 -0.416 0.823\n", "IMAG3*CUEX2 -> Image*Expectation -0.789 -0.451 0.817\n", "IMAG3*CUEX3 -> Image*Expectation 1.563 -0.604 0.914\n", "IMAG4*CUEX1 -> Image*Expectation -0.496 -0.401 0.911\n", "IMAG4*CUEX2 -> Image*Expectation -0.319 -0.434 0.864\n", "IMAG4*CUEX3 -> Image*Expectation 1.247 -0.492 0.903\n", "IMAG5*CUEX1 -> Image*Expectation -0.966 -0.484 0.779\n", "IMAG5*CUEX2 -> Image*Expectation -0.111 -0.445 0.744\n", "IMAG5*CUEX3 -> Image*Expectation 1.522 -0.453 0.814\n", "\n", "Bootstrapped HTMT:\n", " Original Est. Bootstrap Mean Bootstrap SD\n", "Image -> Expectation 0.888 0.896 0.107\n", "Image -> Image*Expectation 0.211 0.336 0.084\n", "Image -> Value 0.652 0.651 0.067\n", "Image -> Satisfaction 0.910 0.913 0.036\n", "Expectation -> Image*Expectation 0.330 0.465 0.119\n", "Expectation -> Value 0.589 0.593 0.121\n", "Expectation -> Satisfaction 0.865 0.870 0.097\n", "Image*Expectation -> Value 0.115 0.206 0.049\n", "Image*Expectation -> Satisfaction 0.154 0.230 0.047\n", "Value -> Satisfaction 0.741 0.741 0.075\n", " 2.5% CI 97.5% CI\n", "Image -> Expectation 0.680 1.108\n", "Image -> Image*Expectation 0.204 0.527\n", "Image -> Value 0.515 0.771\n", "Image -> Satisfaction 0.838 0.979\n", "Expectation -> Image*Expectation 0.276 0.733\n", "Expectation -> Value 0.348 0.815\n", "Expectation -> Satisfaction 0.683 1.064\n", "Image*Expectation -> Value 0.121 0.308\n", "Image*Expectation -> Satisfaction 0.147 0.336\n", "Value -> Satisfaction 0.578 0.866\n", "\n", "Bootstrapped Total Paths:\n", " Original Est. Bootstrap Mean Bootstrap SD\n", "Image -> Value 0.459 0.436 0.069\n", "Image -> Satisfaction 0.281 0.269 0.055\n", "Expectation -> Value 0.143 0.149 0.074\n", "Expectation -> Satisfaction 0.087 0.092 0.047\n", "Image*Expectation -> Value -0.143 -0.015 0.178\n", "Image*Expectation -> Satisfaction -0.088 -0.010 0.110\n", "Value -> Satisfaction 0.613 0.615 0.059\n", " 2.5% CI 97.5% CI\n", "Image -> Value 0.301 0.572\n", "Image -> Satisfaction 0.161 0.376\n", "Expectation -> Value -0.001 0.289\n", "Expectation -> Satisfaction -0.001 0.186\n", "Image*Expectation -> Value -0.250 0.251\n", "Image*Expectation -> Satisfaction -0.165 0.155\n", "Value -> Satisfaction 0.485 0.714\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 使用bootstrap的方法来计算路径系数的显著性和置信区间\n", "boot_estimates <- bootstrap_model(pls_model, nboot = 1000, cores = 2)\n", "summary(boot_estimates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当然我们还可以使用这个模型进行CBSEM, 但是测量模型定义了很多形成性变量, 这些形成性的测量指标不可以在CBSEM中使用, 那么我们为了演示方便,\n", "我们将形成性指标改为反应性指标, 使用的是\"as.reflective\"方法. 基于协方差矩阵的结构方程需要先评估测量模型, 即先进行验证性因子分析:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Generating the seminr model for CFA\n", "\n" ] }, { "data": { "text/plain": [ "\n", "Results from package seminr (2.3.2)\n", "Estimation used package seminr (2.3.2)\n", "\n", " Fit metrics:\n", " npar fmin logl aic bic ntotal bic2 rmr \n", " 3.20e+01 2.36e-01 -5.95e+03 1.20e+04 1.21e+04 2.50e+02 1.20e+04 1.66e-01 \n", " srmr crmr gfi agfi pgfi mfi ecvi \n", " 5.09e-02 5.50e-02 9.31e-01 8.94e-01 6.04e-01 8.88e-01 7.29e-01 \n", " metric scaled robust\n", "cfi 9.44e-01 9.57e-01 0.9612\n", "tli 9.26e-01 9.43e-01 0.9487\n", "nnfi 9.26e-01 9.43e-01 0.9487\n", "rni 9.44e-01 9.57e-01 0.9612\n", "rmsea 6.33e-02 4.50e-02 0.0519\n", "rmsea.ci.lower 4.66e-02 2.71e-02 0.0275\n", "rmsea.ci.upper 7.99e-02 6.10e-02 0.0731\n", "rmsea.pvalue 9.12e-02 6.78e-01 0.4230\n", "rmsea.notclose.pvalue 4.88e-02 6.25e-05 0.0127\n", "chisq 1.18e+02 8.88e+01 NA\n", "df 5.90e+01 5.90e+01 NA\n", "pvalue 7.67e-06 7.24e-03 NA\n", "baseline.chisq 1.14e+03 7.73e+02 NA\n", "baseline.df 7.80e+01 7.80e+01 NA\n", "baseline.pvalue 0.00e+00 0.00e+00 NA\n", "rfi 8.63e-01 8.48e-01 NA\n", "nfi 8.96e-01 8.85e-01 NA\n", "pnfi 6.78e-01 6.69e-01 NA\n", "ifi 9.45e-01 9.58e-01 NA\n", "\n", " Loadings:\n", "$coefficients\n", " Image Expectation Value Satisfaction\n", "IMAG1 0.66 . . .\n", "IMAG2 0.50 . . .\n", "IMAG3 0.47 . . .\n", "IMAG4 0.69 . . .\n", "IMAG5 0.62 . . .\n", "CUEX1 . 0.52 . .\n", "CUEX2 . 0.53 . .\n", "CUEX3 . 0.38 . .\n", "PERV1 . . 0.74 .\n", "PERV2 . . 0.95 .\n", "CUSA1 . . . 0.68\n", "CUSA2 . . . 0.72\n", "CUSA3 . . . 0.79\n", "\n", "$significance\n", " Std Estimate SE t-Value 2.5% CI 97.5% CI\n", "Image -> IMAG1 0.66 0.055 0.0e+00 0.56 0.77\n", "Image -> IMAG2 0.50 0.068 2.1e-13 0.36 0.63\n", "Image -> IMAG3 0.47 0.067 2.7e-12 0.34 0.60\n", "Image -> IMAG4 0.69 0.063 0.0e+00 0.57 0.82\n", "Image -> IMAG5 0.62 0.047 0.0e+00 0.53 0.72\n", "Expectation -> CUEX1 0.52 0.090 4.7e-09 0.35 0.70\n", "Expectation -> CUEX2 0.53 0.106 6.3e-07 0.32 0.74\n", "Expectation -> CUEX3 0.38 0.078 1.1e-06 0.23 0.53\n", "Value -> PERV1 0.74 0.049 0.0e+00 0.64 0.83\n", "Value -> PERV2 0.95 0.034 0.0e+00 0.88 1.02\n", "Satisfaction -> CUSA1 0.68 0.049 0.0e+00 0.59 0.78\n", "Satisfaction -> CUSA2 0.72 0.043 0.0e+00 0.64 0.81\n", "Satisfaction -> CUSA3 0.79 0.036 0.0e+00 0.72 0.87\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cfa_model <- estimate_cfa(data = mobi, as.reflective(measurements))\n", "summary(cfa_model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "然后再进行结构方程模型的估计:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Generating the seminr model for CBSEM\n", "\n" ] }, { "data": { "text/plain": [ "\n", "Results from package seminr (2.3.2)\n", "Estimation used package seminr (2.3.2)\n", "\n", "Fit metrics:\n", " npar fmin logl aic bic ntotal bic2 \n", " 63.000 2.828 -11493.906 23113.811 23335.663 250.000 23135.948 \n", " rmr srmr crmr gfi agfi pgfi mfi \n", " 0.166 0.091 0.094 0.716 0.663 0.605 0.117 \n", " ecvi \n", " 6.161 \n", "\n", " metric scaled robust\n", "cfi 0.586 0.618 0.644\n", "tli 0.544 0.579 0.608\n", "nnfi 0.544 0.579 0.608\n", "rni 0.586 0.618 0.644\n", "rmsea 0.112 0.072 0.096\n", "rmsea.ci.lower 0.106 0.067 0.087\n", "rmsea.ci.upper 0.118 0.077 0.105\n", "rmsea.pvalue 0.000 0.000 0.000\n", "rmsea.notclose.pvalue 1.000 0.002 0.998\n", "chisq 1414.143 783.068 .\n", "df 343.000 343.000 .\n", "pvalue 0.000 0.000 .\n", "baseline.chisq 2964.484 1530.344 .\n", "baseline.df 378.000 378.000 .\n", "baseline.pvalue 0.000 0.000 .\n", "rfi 0.474 0.436 .\n", "nfi 0.523 0.488 .\n", "pnfi 0.475 0.443 .\n", "ifi 0.591 0.629 .\n", "\n", "Reliability:\n", " rhoC AVE\n", "Image 0.73 0.36\n", "Expectation 0.47 0.23\n", "Value 0.73 0.57\n", "Satisfaction 0.78 0.54\n", "\n", "Path Coefficients:\n", " Value Satisfaction\n", "R^2 0.70 0.84\n", "Image 0.79 .\n", "Expectation 0.06 .\n", "Image_x_Expectation -0.03 .\n", "Value . 0.92\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cbsem_model <- estimate_cbsem(data = mobi, as.reflective(measurements), structure)\n", "summary(cbsem_model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bootstrapping方法用于估计参数的显著性\n", "\n", "bootstrap_model 方法接受一个pls模型, 这个模型一般是 estimate_pls 方法返回的结果. 第二个参数是nboot, 代表抽样次数. 第三个参数 cores cpu核心数, 如果你想要高性能的计算, 可以多设定几个核心.\n", "\n", "我们用下面的代码:\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Bootstrapping model using seminr...\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "SEMinR Model successfully bootstrapped\n", "\n" ] } ], "source": [ "# 抽样1000次, 并且使用2个核心\n", "boot_seminr_model <- bootstrap_model(seminr_model = pls_model,\n", " nboot = 1000,\n", " cores = 2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "bootstrap_model 返回多种结果, 我们列在下方:\n", "\n", "- boot_seminr_model$boot_paths 路径系数的显著性检验\n", "- boot_seminr_model$boot_loadings 因子载荷\n", "- boot_seminr_model$boot_weights 权重(形成性指标与构念之间的关系叫权重)\n", "- boot_seminr_model$boot_HTMT HTMT的检验\n", "- boot_seminr_model$boot_total_paths 总效应的显著性检验\n", "- boot_seminr_model$paths_descriptives 描述性统计\n", "- boot_seminr_model$loadings_descriptives 载荷的均值和标准差\n", "- boot_seminr_model$weights_descriptives 权重的举止和标准差\n", "- boot_seminr_model$HTMT_descriptives \n", "- boot_seminr_model$total_paths_descriptives\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 汇报结果\n", "\n", "有很多种方法来获取你想要的结果, 最常用的就是使用summary方法, 它接受一个seminr_model模型, 例如estimate_pls返回的模型.\n", "对于结构方程模型, 它可以输出R方, 调整R方, 结构部分的路径系数, 构建效度, CR composite reliability (Dillon and Goldstein 1987), AVE (Fornell and Larcker 1981), and rhoA (Dijkstra and Henseler 2015).\n", "\n", "你还可以使用一个变量来代表summary的输出结果, 这个变量就可以任意调用你想要的结果: `model_summary <- summary(seminr_model)`\n", "\n", "有如下结果:\n", "\n", "- meta reports the metadata about the estimation technique and version\n", "- model_summary$iterations (PLS only) reports the number of iterations to converge on a stable model\n", "- model_summary$paths reports the matrix of path coefficients, R2, and adjusted R2total_effects reports the total effects of the structural model total_indirect_effects reports the total indirect effects of the structural model\n", "- model_summary$loadings reports the estimated loadings of the measurement model\n", "- model_summary$weights reports the estimated weights of the measurement model\n", "- model_summary$validity$vif_items reports the Variance Inflation Factor (VIF) for the measurement model\n", "- model_summary$validity$htmt reports the HTMT for the constructs\n", "- model_summary$validity$fl_criteria reports the fornell larcker criteria for the constructs\n", "- model_summary$validity$cross_loadings (PLS only) reports all possible loadings between contructs and items\n", "- model_summary$reliability reports composite reliability (rhoC), cronbachs alpha, average variance extracted (AVE), and rhoA\n", "- model_summary$composite_scores reports the construct scores of composites\n", "- model_summary$vif_antecedents report the Variance Inflation Factor (VIF) for the structural model\n", "- model_summary$fSquare reports the effect sizes (f2) for the structural model\n", "- model_summary$descriptives reports the descriptive statistics and correlations for both items and constructs\n", "- model_summary$fSquare reports the effect sizes (f2) for the structural modelit_criteria reports the AIC and BIC for the outcome constructs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "输出直接效应中介效应的方法:\n", "\n", "specific_effect_significance 接受一个bootstrap结果, 然后输出具体路径的直接和中介效应及其显著性.\n", "\n", "```R\n", "mobi_mm <- constructs(\n", "composite(\"Image\", multi_items(\"IMAG\", 1:5)),\n", "composite(\"Expectation\", multi_items(\"CUEX\", 1:3)),\n", "composite(\"Quality\", multi_items(\"PERQ\", 1:7)),\n", "composite(\"Value\", multi_items(\"PERV\", 1:2)),\n", "composite(\"Satisfaction\", multi_items(\"CUSA\", 1:3)),\n", "composite(\"Complaints\", single_item(\"CUSCO\")),\n", "composite(\"Loyalty\", multi_items(\"CUSL\", 1:3))\n", ")\n", "# Creating structural model\n", "mobi_sm <- relationships(\n", " paths(from = \"Image\", to = c(\"Expectation\", \"Satisfaction\", \"Loyalty\")),\n", " paths(from = \"Expectation\", to = c(\"Quality\", \"Value\", \"Satisfaction\")),\n", " paths(from = \"Quality\", to = c(\"Value\", \"Satisfaction\")),\n", " paths(from = \"Value\", to = c(\"Satisfaction\")),\n", " paths(from = \"Satisfaction\", to = c(\"Complaints\", \"Loyalty\")),\n", " paths(from = \"Complaints\", to = \"Loyalty\")\n", ")\n", "# Estimating the model\n", "mobi_pls <- estimate_pls(data = mobi,\n", " measurement_model = mobi_mm,\n", " structural_model = mobi_sm)\n", "#> Generating the seminr model\n", "#> All 250 observations are valid.\n", "# Load data, assemble model, and bootstrap\n", "boot_seminr_model <- bootstrap_model(seminr_model = mobi_pls,\n", " nboot = 50, cores = 2, seed = NULL)\n", "#> Bootstrapping model using seminr...\n", "#> SEMinR Model successfully bootstrapped\n", "\n", "# Calculate the 5% confidence interval for mediated path Image -> Expectation -> Satisfaction\n", "specific_effect_significance(boot_seminr_model = boot_seminr_model,\n", " from = \"Image\",\n", " through = c(\"Expectation\", \"Satisfaction\"),\n", " to = \"Complaints\",\n", " alpha = 0.05)\n", "#> Original Est. Bootstrap Mean Bootstrap SD T Stat. 2.5% CI \n", "#> 0.016670348 0.013801736 0.011054848 1.507967125 -0.004741531 \n", "#> 97.5% CI \n", "#> 0.037007585\n", "\n", "# Calculate the 10% confidence interval for direct path Image -> Satisfaction\n", "specific_effect_significance(boot_seminr_model = boot_seminr_model,\n", " from = \"Image\",\n", " to = \"Satisfaction\",\n", " alpha = 0.10)\n", "#> Original Est. Bootstrap Mean Bootstrap SD T Stat. 5% CI \n", "#> 0.17873950 0.17214888 0.04604578 3.88177842 0.10760500 \n", "#> 95% CI \n", "#> 0.26319886\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 描述性统计\n", "\n", "```R\n", "+ `model_summary$descriptives$statistics$items` 题目的描述统计\n", "+ `model_summary$descriptives$correlations$items` 题目的相关矩阵\n", "+ `model_summary$descriptives$statistics$constructs` 构念的描述性统计\n", "+ `model_summary$descriptives$correlations$constructs` 构念的相关矩阵\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 绘制模型图\n", "\n", "```R\n", "# generate a small model for creating the plot\n", "mobi_mm <- constructs(\n", " composite(\"Image\", multi_items(\"IMAG\", 1:3)),\n", " composite(\"Value\", multi_items(\"PERV\", 1:2)),\n", " higher_composite(\"Satisfaction\", dimensions = c(\"Image\",\"Value\"), method = two_stage),\n", " composite(\"Quality\", multi_items(\"PERQ\", 1:3), weights = mode_B),\n", " composite(\"Complaints\", single_item(\"CUSCO\")),\n", " reflective(\"Loyalty\", multi_items(\"CUSL\", 1:3))\n", ")\n", "mobi_sm <- relationships(\n", " paths(from = c(\"Quality\"), to = \"Satisfaction\"),\n", " paths(from = \"Satisfaction\", to = c(\"Complaints\", \"Loyalty\"))\n", ")\n", "pls_model <- estimate_pls(\n", " data = mobi,\n", " measurement_model = mobi_mm,\n", " structural_model = mobi_sm\n", ")\n", "#> Generating the seminr model\n", "#> Generating the seminr model\n", "#> All 250 observations are valid.\n", "#> All 250 observations are valid.\n", "boot_estimates <- bootstrap_model(pls_model, nboot = 100, cores = 1)\n", "#> Bootstrapping model using seminr...\n", "#> SEMinR Model successfully bootstrapped\n", "```\n", "\n", "当我们有了模型, 我们就可以绘制这个模型图了:\n", "\n", "```R\n", "plot(boot_estimates, title = \"Bootstrapped Model\")\n", "save_plot(\"myfigure.png\")\n", "```\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 参考文献 \n", "https://cran.r-project.org/web/packages/seminr/vignettes/SEMinR.html\n", "Yuan, K. H., & Bentler, P. M. (2000). Three likelihood-based methods for mean and covariance structure analysis with nonnormal missing data. Sociological methodology, 30(1), 165-200.\n", "Zhao, X., Lynch Jr, J. G., & Chen, Q. (2010). Reconsidering Baron and Kenny: Myths and truths about mediation analysis. Journal of consumer research, 37(2), 197-206." ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.3.3" } }, "nbformat": 4, "nbformat_minor": 2 }