r:path_analysis
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| r:path_analysis [2022/11/15 13:01] – hkimscil | r:path_analysis [2024/11/04 10:28] (current) – [Introduction] hkimscil | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Path Analysis ====== | ====== Path Analysis ====== | ||
| - | ======  | + | {{: | 
| + | ======  | ||
| {{youtube> | {{youtube> | ||
| Line 15: | Line 16: | ||
| * The number of unique (non-redundent) source of information | * The number of unique (non-redundent) source of information | ||
| * $p(p+1)/2$ | * $p(p+1)/2$ | ||
| - |      | + |    | 
| - | * Just-identified (df = 0) | + | * Just-identified (df = 0) | 
| - | * Model can be estimated, but cannot be assessed | + | * Model can be estimated, but cannot be assessed | 
| - | * Over-identified (df > 0) | + | * Over-identified (df > 0) | 
| - | * Model can be estimated and assessed | + | * Model can be estimated and assessed | 
| - | * Under-identified (df < 0) | + | * Under-identified (df < 0) | 
| - | * Model cannot be either estimated or assessed | + | * Model cannot be either estimated or assessed | 
| * Exogenous and | * Exogenous and | ||
| Line 110: | Line 111: | ||
| </ | </ | ||
| + | ---- | ||
| + | < | ||
| + | # my own | ||
| + | # pbt model | ||
| + | specmod5 <- ' | ||
| + | # Directional relations (path) | ||
| + | intention ~ a*attitude + b*norms + c*control | ||
| + |     behavior ~ d*intention  | ||
| + |     # Covariances  | ||
| + | attitude ~~ norms + control | ||
| + |     norms ~~ control  | ||
| + | ad := a*d | ||
| + | bd := b*d | ||
| + | cd := c*d | ||
| + | ' | ||
| + | fitmod5 <- sem(specmod5, | ||
| + | summary(fitmod5, | ||
| + | </ | ||
| ====== Output ====== | ====== Output ====== | ||
| < | < | ||
| Line 439: | Line 458: | ||
| </ | </ | ||
| + | ===== specmod5 ===== | ||
| + | < | ||
| + | > specmod5 <- " | ||
| + | + # Directional relations (path) | ||
| + | +      | ||
| + | +      | ||
| + | +     # Covariances  | ||
| + | +      | ||
| + | +     norms ~~ control  | ||
| + | + " | ||
| + | > fitmod5 <- sem(specmod5, | ||
| + | > summary(fitmod5, | ||
| + | lavaan 0.6-12 ended normally after 18 iterations | ||
| + |   Estimator  | ||
| + |   Optimization method  | ||
| + |   Number of model parameters  | ||
| - | ===== Lavaan in R: explanation ===== | + |   Number of observations  | 
| + | Model Test User Model: | ||
| + |                                                        | ||
| + |   Test statistic  | ||
| + |   Degrees of freedom  | ||
| + |   P-value (Chi-square)  | ||
| + | |||
| + | Model Test Baseline Model: | ||
| + | |||
| + |   Test statistic  | ||
| + |   Degrees of freedom  | ||
| + |   P-value  | ||
| + | |||
| + | User Model versus Baseline Model: | ||
| + | |||
| + | Comparative Fit Index (CFI) 1.000 | ||
| + | Tucker-Lewis Index (TLI) 1.006 | ||
| + | |||
| + | Loglikelihood and Information Criteria: | ||
| + | |||
| + | Loglikelihood user model (H0) -1258.396 | ||
| + | Loglikelihood unrestricted model (H1) -1257.506 | ||
| + |                                                        | ||
| + | Akaike (AIC) 2542.792 | ||
| + | Bayesian (BIC) 2585.605 | ||
| + |   Sample-size adjusted Bayesian (BIC)          | ||
| + | |||
| + | Root Mean Square Error of Approximation: | ||
| + | |||
| + | RMSEA 0.000 | ||
| + | 90 Percent confidence interval - lower 0.000 | ||
| + | 90 Percent confidence interval - upper 0.136 | ||
| + | P-value RMSEA <= 0.05 0.569 | ||
| + | |||
| + | Standardized Root Mean Square Residual: | ||
| + | |||
| + | SRMR 0.018 | ||
| + | |||
| + | Parameter Estimates: | ||
| + | |||
| + |   Standard errors  | ||
| + |   Information  | ||
| + | Information saturated (h1) model Structured | ||
| + | |||
| + | Regressions: | ||
| + |                     | ||
| + | intention ~ | ||
| + |     attitude  | ||
| + |     norms              | ||
| + |     control  | ||
| + |   behavior ~                                           | ||
| + |     intention  | ||
| + |     norms              | ||
| + | |||
| + | Covariances: | ||
| + |                     | ||
| + | attitude ~~ | ||
| + |     norms              | ||
| + |     control  | ||
| + |   norms ~~                                             | ||
| + |     control  | ||
| + | |||
| + | Variances: | ||
| + |                     | ||
| + |     | ||
| + |     | ||
| + |     attitude  | ||
| + |     norms              | ||
| + |     control  | ||
| + | |||
| + | R-Square: | ||
| + |                     | ||
| + |     intention  | ||
| + |     behavior  | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Lavaan in R: explanation ===== | ||
| {{youtube> | {{youtube> | ||
| Path analysis in R with Lavaan (introduction) | Path analysis in R with Lavaan (introduction) | ||
| Line 460: | Line 572: | ||
| < | < | ||
| - | processdata< | + | # processdata< | 
| + | processdata< | ||
| + |                         | ||
| </ | </ | ||
| Line 518: | Line 632: | ||
|   * Step 2: Use ' |   * Step 2: Use ' | ||
| < | < | ||
| - | fit< | + | fit< | 
| </ | </ | ||
|   * The ' |   * The ' | ||
| < | < | ||
| - | summary(fit, | + | summary(fit, | 
| </ | </ | ||
|   * To obtain standardized estimates, use the ' |   * To obtain standardized estimates, use the ' | ||
| < | < | ||
| - | summary(fit, | + | summary(fit, | 
| </ | </ | ||
| Line 544: | Line 658: | ||
| </ | </ | ||
|   * Note: Modification indices represent the expected decrease in model chi-square after freeing a given parameter (Schumacker & Lomax, 2004). The EPC is an estimate of the model parameter itself. A MI value of 3.84 or greater may be considered " |   * Note: Modification indices represent the expected decrease in model chi-square after freeing a given parameter (Schumacker & Lomax, 2004). The EPC is an estimate of the model parameter itself. A MI value of 3.84 or greater may be considered " | ||
| + | |||
| + | output  | ||
| + | |||
| + | < | ||
| + | > # install.packages(" | ||
| + | > | ||
| + | > # processdata< | ||
| + | > processdata< | ||
| + | +                        header=TRUE, | ||
| + | > | ||
| + | > str(processdata) | ||
| + | ' | ||
| + | $ id : int 1 2 3 4 5 6 7 8 9 10 ... | ||
| + | $ ses : int 1 0 0 1 1 1 0 0 1 1 ... | ||
| + | $ genderid: int 1 0 1 1 1 1 0 0 0 0 ... | ||
| + | $ perfgoal: num 29.5 29.5 30.4 33.5 28.7 ... | ||
| + | $ achieve : num 6.12 1.62 4.5 2.38 5.12 ... | ||
| + | $ mastery : num 5.71 1.43 1.29 2.29 4.57 ... | ||
| + | $ interest: num 6 4 2 4 5.5 4 4 5 4.5 4 ... | ||
| + | $ anxiety : num 1.67 6.33 3.67 3.67 3.67 ... | ||
| + | $ pgoal_MS: int 0 0 1 1 0 1 0 1 0 0 ... | ||
| + | > library(lavaan) | ||
| + | > | ||
| + | > # model specification | ||
| + | > model <- ' | ||
| + | + # equation where interest is predicted by ses | ||
| + | + # & mastery and performance goals | ||
| + | +    | ||
| + | + | ||
| + | + # equation where achieve is predicted by | ||
| + | + # interest and anxiety | ||
| + | +    | ||
| + | + | ||
| + | +   # equation where anxiety is predicted  | ||
| + | + # by mastery and performance goals | ||
| + | +    | ||
| + | + | ||
| + | + # estimating the variances of | ||
| + | +   # the exogenous variables (ses, mastery, | ||
| + | +    | ||
| + | +    | ||
| + | + ses ~~ ses | ||
| + | + | ||
| + | +   # estimtating the covariances of the exogenous  | ||
| + | +   # variables (ses, mastery, | ||
| + | +    | ||
| + | +    | ||
| + | + | ||
| + | +   # estimating the residual variances  | ||
| + | + # for endogenous variables (interest, anxiety, achieve) | ||
| + | +    | ||
| + | +    | ||
| + | +    | ||
| + | + | ||
| + | +   # estimating the covariance of residuals  | ||
| + | + # for interest and anxiety | ||
| + | +    | ||
| + | > | ||
| + | > fit< | ||
| + | > summary(fit, | ||
| + | lavaan 0.6.16 ended normally after 27 iterations | ||
| + | |||
| + |   Estimator  | ||
| + |   Optimization method  | ||
| + |   Number of model parameters  | ||
| + | |||
| + |   Number of observations  | ||
| + | |||
| + | Model Test User Model: | ||
| + |                                                        | ||
| + |   Test statistic  | ||
| + |   Degrees of freedom  | ||
| + |   P-value (Chi-square)  | ||
| + | |||
| + | Model Test Baseline Model: | ||
| + | |||
| + |   Test statistic  | ||
| + |   Degrees of freedom  | ||
| + |   P-value  | ||
| + | |||
| + | User Model versus Baseline Model: | ||
| + | |||
| + | Comparative Fit Index (CFI) 0.860 | ||
| + | Tucker-Lewis Index (TLI) 0.300 | ||
| + | |||
| + | Loglikelihood and Information Criteria: | ||
| + | |||
| + | Loglikelihood user model (H0) -1391.274 | ||
| + | Loglikelihood unrestricted model (H1) -1376.659 | ||
| + |                                                        | ||
| + | Akaike (AIC) 2818.548 | ||
| + | Bayesian (BIC) 2871.498 | ||
| + |   Sample-size adjusted Bayesian (SABIC)  | ||
| + | |||
| + | Root Mean Square Error of Approximation: | ||
| + | |||
| + | RMSEA 0.250 | ||
| + | 90 Percent confidence interval - lower 0.172 | ||
| + | 90 Percent confidence interval - upper 0.336 | ||
| + | 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.074 | ||
| + | |||
| + | Parameter Estimates: | ||
| + | |||
| + |   Standard errors  | ||
| + |   Information  | ||
| + | Information saturated (h1) model Structured | ||
| + | |||
| + | Regressions: | ||
| + |                     | ||
| + |   interest ~                                           | ||
| + |     mastery  | ||
| + |     perfgoal  | ||
| + |     ses                | ||
| + | achieve ~ | ||
| + |     anxiety  | ||
| + |     interest  | ||
| + |     mastery  | ||
| + | anxiety ~ | ||
| + |     perfgoal  | ||
| + |     mastery  | ||
| + | |||
| + | Covariances: | ||
| + |                     | ||
| + |   mastery ~~                                           | ||
| + |     perfgoal  | ||
| + |     ses                | ||
| + | perfgoal ~~ | ||
| + |     ses              -0.226  | ||
| + |   | ||
| + |     | ||
| + | |||
| + | Variances: | ||
| + |                     | ||
| + |     mastery  | ||
| + |     perfgoal  | ||
| + |     ses                | ||
| + |     | ||
| + |     | ||
| + |     | ||
| + | |||
| + | > summary(fit, | ||
| + | lavaan 0.6.16 ended normally after 27 iterations | ||
| + | |||
| + |   Estimator  | ||
| + |   Optimization method  | ||
| + |   Number of model parameters  | ||
| + | |||
| + |   Number of observations  | ||
| + | |||
| + | Model Test User Model: | ||
| + |                                                        | ||
| + |   Test statistic  | ||
| + |   Degrees of freedom  | ||
| + |   P-value (Chi-square)  | ||
| + | |||
| + | Model Test Baseline Model: | ||
| + | |||
| + |   Test statistic  | ||
| + |   Degrees of freedom  | ||
| + |   P-value  | ||
| + | |||
| + | User Model versus Baseline Model: | ||
| + | |||
| + | Comparative Fit Index (CFI) 0.860 | ||
| + | Tucker-Lewis Index (TLI) 0.300 | ||
| + | |||
| + | Loglikelihood and Information Criteria: | ||
| + | |||
| + | Loglikelihood user model (H0) -1391.274 | ||
| + | Loglikelihood unrestricted model (H1) -1376.659 | ||
| + |                                                        | ||
| + | Akaike (AIC) 2818.548 | ||
| + | Bayesian (BIC) 2871.498 | ||
| + |   Sample-size adjusted Bayesian (SABIC)  | ||
| + | |||
| + | Root Mean Square Error of Approximation: | ||
| + | |||
| + | RMSEA 0.250 | ||
| + | 90 Percent confidence interval - lower 0.172 | ||
| + | 90 Percent confidence interval - upper 0.336 | ||
| + | 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.074 | ||
| + | |||
| + | Parameter Estimates: | ||
| + | |||
| + |   Standard errors  | ||
| + |   Information  | ||
| + | Information saturated (h1) model Structured | ||
| + | |||
| + | Regressions: | ||
| + |                     | ||
| + |   interest ~                                                             | ||
| + |     mastery  | ||
| + |     perfgoal  | ||
| + |     ses                | ||
| + | achieve ~ | ||
| + |     anxiety  | ||
| + |     interest  | ||
| + |     mastery  | ||
| + | anxiety ~ | ||
| + |     perfgoal  | ||
| + |     mastery  | ||
| + | |||
| + | Covariances: | ||
| + |                     | ||
| + |   mastery ~~                                                             | ||
| + |     perfgoal  | ||
| + |     ses                | ||
| + | perfgoal ~~ | ||
| + |     ses              -0.226  | ||
| + |   | ||
| + |     | ||
| + | |||
| + | Variances: | ||
| + |                     | ||
| + |     mastery  | ||
| + |     perfgoal  | ||
| + |     ses                | ||
| + |     | ||
| + |     | ||
| + |     | ||
| + | |||
| + | R-Square: | ||
| + |                     | ||
| + |     interest  | ||
| + |     anxiety  | ||
| + |     achieve  | ||
| + | |||
| + | > | ||
| + | > parameterEstimates(fit) | ||
| + | lhs op rhs est se z pvalue ci.lower ci.upper | ||
| + | 1  interest  | ||
| + | 2  interest  | ||
| + | 3  interest  | ||
| + | 4    | ||
| + | 5    | ||
| + | 6    | ||
| + | 7    | ||
| + | 8    | ||
| + | 9    | ||
| + | 10 perfgoal ~~ perfgoal  | ||
| + | 11 ses ~~ ses 0.249 0.030 8.367 0.000 0.191 0.308 | ||
| + | 12  mastery ~~ perfgoal -0.935 0.361 -2.590  | ||
| + | 13 mastery ~~ ses 0.170 0.061 2.805 0.005 0.051 0.288 | ||
| + | 14 perfgoal ~~      ses -0.226 0.128 -1.768  | ||
| + | 15 interest ~~ interest  | ||
| + | 16  anxiety ~~  anxiety  | ||
| + | 17  achieve ~~  achieve  | ||
| + | 18 interest ~~  anxiety  | ||
| + | > fitMeasures(fit) | ||
| + |                   | ||
| + |                 | ||
| + |                     | ||
| + |                 3.000                  | ||
| + |           baseline.df  | ||
| + |                 | ||
| + |                   tli                  nnfi                    | ||
| + |                 0.300                  | ||
| + |                   nfi                  pnfi                    | ||
| + |                 0.856                  | ||
| + |                   rni                  logl      | ||
| + |                 0.860              | ||
| + |                   aic                    | ||
| + |               | ||
| + |                   | ||
| + |               | ||
| + |         | ||
| + |                 0.336                  | ||
| + |         | ||
| + |                 0.050                  | ||
| + |                   rmr            rmr_nomean  | ||
| + |                 0.122                  | ||
| + |           | ||
| + |                 0.074                  | ||
| + |           crmr_nomean  | ||
| + |                 0.088                  | ||
| + |                 cn_05                  | ||
| + |                 | ||
| + |                   | ||
| + |                 0.587                  | ||
| + |                   | ||
| + | 0.466 | ||
| + | > modificationIndices(fit) | ||
| + |         lhs op      rhs      | ||
| + | 19 interest ~~  achieve 25.396 -2.899  | ||
| + | 23  achieve ~~  anxiety  | ||
| + | 24  achieve ~~  mastery 22.476 -1.743  | ||
| + | 25  achieve ~~ perfgoal  | ||
| + | 26  achieve ~~      ses 20.541  | ||
| + | 27  anxiety ~~  mastery  | ||
| + | 28  anxiety ~~ perfgoal  | ||
| + | 29  anxiety ~~      ses  0.921 -0.061  | ||
| + | 30 interest  | ||
| + | 32  achieve  | ||
| + | 33  achieve  | ||
| + | 34  anxiety  | ||
| + | 35  anxiety  | ||
| + | 36  anxiety  | ||
| + | 37  mastery  | ||
| + | 38  mastery  | ||
| + | 39  mastery  | ||
| + | 43 perfgoal  | ||
| + | 44 perfgoal  | ||
| + | 47      ses  ~ interest  | ||
| + | 48      ses  ~  achieve 20.964  | ||
| + | 49      ses  ~  anxiety  | ||
| + | > | ||
| + | > | ||
| + | </ | ||
| + | |||
| + | |||
| ----------------------------- | ----------------------------- | ||
| Line 576: | Line 1010: | ||
|   interest~~anxiety' |   interest~~anxiety' | ||
| - |   fit< | + |   fit< | 
| - |   summary(fit, | + |   summary(fit, | 
| </ | </ | ||
| Line 636: | Line 1070: | ||
| CODING  | CODING  | ||
| < | < | ||
| - | processdata <- read.csv(" | + | processdata< | 
| + |                         | ||
| str(processdata) | str(processdata) | ||
| library(lavaan) | library(lavaan) | ||
| Line 656: | Line 1091: | ||
|     interest~~anxiety  |     interest~~anxiety  | ||
| ' | ' | ||
| - | fit <- lavaan(model. data=processdata) | + | fit <- lavaan(model, data=processdata) | 
| - | fit <- sem(model. data=processdata) | + | fit <- sem(model, data=processdata) | 
| summary(fit, | summary(fit, | ||
| Line 799: | Line 1234: | ||
| see [[https:// | see [[https:// | ||
| + | ===== Exercise ===== | ||
| + | Using mtcars in R | ||
| + | < | ||
| + | ?mtcars | ||
| + | mtcars | ||
| + | str(mtcars) | ||
| + | df <- mtcars | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | # model specfication | ||
| + | model <-' | ||
| + | mpg ~ hp + gear + cyl + disp + carb + am + wt | ||
| + | hp ~ cyl + disp + carb | ||
| + | ' | ||
| + | # model fit | ||
| + | fit <- cfa(model, data = mtcars) | ||
| + | summary(fit, | ||
| + | semPaths(fit, | ||
| + | </ | ||
r/path_analysis.1668484896.txt.gz · Last modified:  by hkimscil
                
                