陈涛涛的博客分享 http://blog.sciencenet.cn/u/ttchen86

博文

利用R进行通径分析

已有 5597 次阅读 2020-4-27 14:59 |个人分类:R学习|系统分类:科研笔记| 通径分析, Path Analysis

library(tidyverse)

library(agricolae)

library(semPlot)

library(lavaanPlot)


data(haynes)

X <- haynes %>% dplyr::select(FL,   MI,   ME)

Y <- haynes$WI


# correlation analysis ---------------------------------------

corr.x <- correlation(X, X)

corr.y <- correlation(Y, X)


# Path Analysis -----------------------------------------------

result <- path.analysis(corr.x$correlation, corr.y$correlation)


## Direct effect path coefficients

DEffect <- diag(result$Coeff)


## Indirect effect path coefficients

diag(result$Coeff) <- 0

IEffect <- rowSums(result$Coeff)


# print results of path analysis -----------------------------------------------

result1 <- result$Coeff %>% as.data.frame %>%

  mutate(

    '总间接作用' = IEffect,

    '直接作用' = DEffect,

    '总作用' = corr.y$correlation %>% as.numeric()

  ) 

rownames(result1) <- DEffect %>% names

cat("\n\nDirect and indirect effect:\n========================\n" )

print(result1)

sprintf("\nResidual Effect^2 =  %2.3f\n========================\n",

        result$Residual) %>% cat


path_analysis.jpg




https://blog.sciencenet.cn/blog-3427939-1230425.html

上一篇:利用R进行时间序列Mann-Kendall突变检测
下一篇:利用R lavaan包进行通径分析并绘制路径图
收藏 IP: 210.47.175.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (1 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-23 05:34

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部