User Tools

Site Tools


r:path_analysis

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
r:path_analysis [2022/11/15 13:01] hkimscilr:path_analysis [2023/11/27 16:57] (current) – [Lavaan in R: explanation] hkimscil
Line 1: Line 1:
 ====== Path Analysis ====== ====== Path Analysis ======
-====== Path Analysis 2 ======+{{:r:pasted:20230529-234519.png}} 
 +====== Introduction ======
 {{youtube>UGIVPtFKwc0}} {{youtube>UGIVPtFKwc0}}
  
Line 110: Line 111:
 </code> </code>
  
 +----
 +<code>
 +# 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, data=df)
 +summary(fitmod5, fit.measures=TRUE, rsquare=TRUE)
 +</code>
 ====== Output ====== ====== Output ======
 <code> <code>
Line 439: Line 458:
  
 </code> </code>
 +===== specmod5 =====
 +<code>
 +> specmod5 <- "
 ++     # Directional relations (path)
 ++     intention ~ attitude + norms + control
 ++     behavior ~ intention + norms
 ++     # Covariances 
 ++     attitude ~~ norms + control
 ++     norms ~~ control    
 ++ "
 +> fitmod5 <- sem(specmod5, data=df)
 +> summary(fitmod5, fit.measures=TRUE, rsquare=TRUE)
 +lavaan 0.6-12 ended normally after 18 iterations
  
 +  Estimator                                         ML
 +  Optimization method                           NLMINB
 +  Number of model parameters                        13
 +
 +  Number of observations                           199
 +
 +Model Test User Model:
 +                                                      
 +  Test statistic                                 1.781
 +  Degrees of freedom                                 2
 +  P-value (Chi-square)                           0.410
 +
 +Model Test Baseline Model:
 +
 +  Test statistic                               182.295
 +  Degrees of freedom                                10
 +  P-value                                        0.000
 +
 +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)         2544.421
 +
 +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                             Standard
 +  Information                                 Expected
 +  Information saturated (h1) model          Structured
 +
 +Regressions:
 +                   Estimate  Std.Err  z-value  P(>|z|)
 +  intention ~                                         
 +    attitude          0.352    0.058    6.068    0.000
 +    norms             0.153    0.059    2.577    0.010
 +    control           0.275    0.058    4.740    0.000
 +  behavior ~                                          
 +    intention         0.443    0.068    6.525    0.000
 +    norms             0.034    0.068    0.493    0.622
 +
 +Covariances:
 +                   Estimate  Std.Err  z-value  P(>|z|)
 +  attitude ~~                                         
 +    norms             0.200    0.064    3.128    0.002
 +    control           0.334    0.070    4.748    0.000
 +  norms ~~                                            
 +    control           0.220    0.065    3.411    0.001
 +
 +Variances:
 +                   Estimate  Std.Err  z-value  P(>|z|)
 +   .intention         0.530    0.053    9.975    0.000
 +   .behavior          0.698    0.070    9.975    0.000
 +    attitude          0.928    0.093    9.975    0.000
 +    norms             0.830    0.083    9.975    0.000
 +    control           0.939    0.094    9.975    0.000
 +
 +R-Square:
 +                   Estimate
 +    intention         0.369
 +    behavior          0.199
 +
 +</code>
  
 ===== Lavaan in R: explanation ===== ===== Lavaan in R: explanation =====
Line 460: Line 573:
  
 <code> <code>
-processdata<-read.csv("path analysis dataN BinW.csv", header=TRUE, sep=",")+processdata<-read.csv("path analysis dataN BinW.csv", header=TRUE, sep=",") 
 +processdata<-read.csv("http://commres.net/wiki/_media/r/path_analysis_datan_binw.csv",  
 +                       header=TRUE, sep=",", fileEncoding="UTF-8-BOM")
 </code> </code>
  
Line 518: Line 633:
   * Step 2: Use 'lavaan' function to run analysis. Here, I will be saving  the results in an R object called 'fit' (arbitrarily named). Inside  the parenthesis are arguments separated by commas. The first argument contains the name of the object containing the model syntax (see above). The object is named 'model' (again, arbitrarily named above). Next, we have the 'data' argument. This identifies the object (i.e., data frame)  containing the raw data.    * Step 2: Use 'lavaan' function to run analysis. Here, I will be saving  the results in an R object called 'fit' (arbitrarily named). Inside  the parenthesis are arguments separated by commas. The first argument contains the name of the object containing the model syntax (see above). The object is named 'model' (again, arbitrarily named above). Next, we have the 'data' argument. This identifies the object (i.e., data frame)  containing the raw data. 
 <code> <code>
-fit<-lavaan(model,data=processdata)+fit<-lavaan(model, data=processdata)
 </code> </code>
   * The 'summary' function can be used to obtain various fit measures and the parameter estimates for the model   * The 'summary' function can be used to obtain various fit measures and the parameter estimates for the model
 <code> <code>
-summary(fit,fit.measures=TRUE)+summary(fit, fit.measures=TRUE)
 </code> </code>
   * To obtain standardized estimates, use the 'standardized' argument (setting it to TRUE) when using the 'summary' function. You will need to interpret the Std.all column in the output, as it will provide standardized estimates for all measured variables in the model.   * To obtain standardized estimates, use the 'standardized' argument (setting it to TRUE) when using the 'summary' function. You will need to interpret the Std.all column in the output, as it will provide standardized estimates for all measured variables in the model.
 <code> <code>
-summary(fit,fit.measures=TRUE,standardized=TRUE,rsquare=TRUE)+summary(fit, fit.measures=TRUE, standardized=TRUE, rsquare=TRUE)
 </code> </code>
  
Line 576: Line 691:
   interest~~anxiety'   interest~~anxiety'
  
-  fit<-lavaan(model,data=processdata,auto.var=TRUE) +  fit<-lavaan(model, data=processdata, auto.var=TRUE) 
-  summary(fit,fit.measures=TRUE,standardized=TRUE,rsquare=TRUE)+  summary(fit, fit.measures=TRUE, standardized=TRUE, rsquare=TRUE)
 </code> </code>
  
Line 636: Line 751:
 CODING  CODING 
 <code> <code>
-processdata <- read.csv("http://commres.net/wiki/_media/r/path_analysis_datan_binw.csv")+processdata<-read.csv("http://commres.net/wiki/_media/r/path_analysis_datan_binw.csv",  
 +                       header=TRUE, sep=",", fileEncoding="UTF-8-BOM")
 str(processdata) str(processdata)
 library(lavaan) library(lavaan)
Line 656: Line 772:
     interest~~anxiety      interest~~anxiety 
 ' '
-fit <- lavaan(modeldata=processdata) +fit <- lavaan(modeldata=processdata) 
-fit <- sem(modeldata=processdata)+fit <- sem(modeldata=processdata)
  
 summary(fit, fit.measures=TRUE) summary(fit, fit.measures=TRUE)
Line 799: Line 915:
 see [[https://www.rensvandeschoot.com/tutorials/lme4/|lme4 tutorial]] see [[https://www.rensvandeschoot.com/tutorials/lme4/|lme4 tutorial]]
  
 +===== Exercise =====
 +Using mtcars in R
 +<code>
 +?mtcars
 +mtcars
 +str(mtcars)
 +df <- mtcars
 +</code>
 +
 +<code>
 +# model specfication
 +model <-'
 +  mpg ~ hp + gear + cyl + disp + carb + am + wt
 +  hp ~ cyl + disp + carb
 +'
 +# model fit
 +fit <- cfa(model, data = mtcars)
 +summary(fit, fit.measures = TRUE, standardized=T, rsquare=T)
 +semPaths(fit, 'std', layout = 'circle')
 +</code>
  
r/path_analysis.1668484896.txt.gz · Last modified: 2022/11/15 13:01 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki