pacman::p_load(lavaan, semPlot)
library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lavaan)
library(semPlot)
# Random Data
OBrienKaiser <- read_csv("C:/Users/autum/Documents/R/SurveyStats/OBrienKaiser.csv")
## New names:
## Rows: 16 Columns: 18
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (2): treatment, gender dbl (16): ...1, pre.1, pre.2, pre.3, pre.4, pre.5,
## post.1, post.2, post.3, p...
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
data <- OBrienKaiser
data <-
data %>%
rename("Att1" = "pre.1",
"Att2" = "pre.2",
"PBC1" = "pre.3",
"PBC2" = "pre.4",
"SN1" = "pre.5",
"SN2" = "post.1",
"Int1" = "post.2",
"Int2" = "post.3",
"Bhv1" = "post.4",
"Bhv2" = "post.5")
# This is the most basic form of TBP that the Other Lab had a designed script for
# I will also attach what they are hoping to model with this one
TPBModel <- '
# Measurement Model
Attitude =~ Att1 + Att2
PBC =~ PBC1 + PBC2
SubNorms=~ SN1 + SN2
Intent =~ Int1 + Int2
Behave=~ Bhv1 + Bhv2
# Structural Model
Intent ~ AttInt*Attitude + PBCInt*PBC + SNInt*SubNorms
Behave ~ IntentBehave*Intent
PBC ~~ SubNorms + Attitude
Attitude ~~ SubNorms
# Indirect Effects
AttitudeBehave := AttInt*IntentBehave
PBCBehave := PBCInt*IntentBehave
SNBehave := SNInt*IntentBehave
'
TPBSEM <- sem(TPBModel, data=data)
## Warning in lav_object_post_check(object): lavaan WARNING: some estimated ov
## variances are negative
## Warning in lav_object_post_check(object): lavaan WARNING: some estimated lv
## variances are negative
summary(TPBSEM)
## lavaan 0.6-12 ended normally after 91 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 27
##
## Number of observations 16
##
## Model Test User Model:
##
## Test statistic 64.749
## Degrees of freedom 28
## P-value (Chi-square) 0.000
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Expected
## Information saturated (h1) model Structured
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|)
## Attitude =~
## Att1 1.000
## Att2 1.117 0.110 10.150 0.000
## PBC =~
## PBC1 1.000
## PBC2 1.433 0.403 3.559 0.000
## SubNorms =~
## SN1 1.000
## SN2 2.198 1.086 2.024 0.043
## Intent =~
## Int1 1.000
## Int2 0.768 0.118 6.493 0.000
## Behave =~
## Bhv1 1.000
## Bhv2 1.073 0.226 4.750 0.000
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## Intent ~
## Attitud (AttI) 0.856 2.233 0.383 0.702
## PBC (PBCI) -1.946 2.575 -0.756 0.450
## SubNrms (SNIn) 3.691 3.560 1.037 0.300
## Behave ~
## Intent (IntB) 0.609 0.151 4.035 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## PBC ~~
## SubNorms 0.664 0.477 1.393 0.164
## Attitude ~~
## PBC 1.808 0.726 2.491 0.013
## SubNorms 0.993 0.637 1.560 0.119
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .Att1 0.257 0.121 2.127 0.033
## .Att2 0.072 0.102 0.698 0.485
## .PBC1 0.803 0.340 2.361 0.018
## .PBC2 2.108 0.839 2.513 0.012
## .SN1 3.003 1.076 2.790 0.005
## .SN2 1.520 1.007 1.510 0.131
## .Int1 -0.062 0.254 -0.243 0.808
## .Int2 1.104 0.415 2.662 0.008
## .Bhv1 0.147 0.545 0.269 0.788
## .Bhv2 1.459 0.810 1.800 0.072
## Attitude 2.270 0.892 2.544 0.011
## PBC 1.287 0.707 1.819 0.069
## SubNorms 0.681 0.775 0.879 0.380
## .Intent -1.216 2.911 -0.418 0.676
## .Behave 1.746 0.824 2.119 0.034
##
## Defined Parameters:
## Estimate Std.Err z-value P(>|z|)
## AttitudeBehave 0.521 1.366 0.381 0.703
## PBCBehave -1.184 1.596 -0.742 0.458
## SNBehave 2.247 2.240 1.003 0.316
semPaths(TPBSEM,
what = "paths",
whatLabels = "stand",
rotation = 2)
# This is what I would have done based on the other models we had created the past few weeks
TPB2 <- '
# Regressions
Attitude =~ Att1 + Att2
PBC =~ PBC1 + PBC2
Norms =~ SN1 + SN2
Intent =~ Int1 + Int2
Behave =~ Bhv1 + Bhv2
Intent ~ Attitude + PBC + Norms
Behave ~ Intent
# variance and covariance
Att1 ~~ Att1
Att1 ~~ Att2
Att2 ~~ Att2
PBC1 ~~ PBC1
PBC1 ~~ PBC2
PBC2 ~~ PBC2
SN1 ~~ SN1
SN1 ~~ SN2
SN2 ~~ SN2
Int1 ~~ Int1
Int1 ~~ Int2
Int2 ~~ Int2
Bhv1 ~~ Bhv1
Bhv1 ~~ Bhv2
Bhv2 ~~ Bhv2
'
TPBSEM2 <- sem(TPB2, data=data)
## Warning in lav_model_vcov(lavmodel = lavmodel, lavsamplestats = lavsamplestats, : lavaan WARNING:
## Could not compute standard errors! The information matrix could
## not be inverted. This may be a symptom that the model is not
## identified.
## Warning in lav_object_post_check(object): lavaan WARNING: some estimated lv
## variances are negative
summary(TPBSEM2)
## lavaan 0.6-12 ended normally after 88 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 32
##
## Number of observations 16
##
## Model Test User Model:
##
## Test statistic 64.137
## Degrees of freedom 23
## P-value (Chi-square) 0.000
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Expected
## Information saturated (h1) model Structured
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|)
## Attitude =~
## Att1 1.000
## Att2 1.120 NA
## PBC =~
## PBC1 1.000
## PBC2 1.411 NA
## Norms =~
## SN1 1.000
## SN2 2.019 NA
## Intent =~
## Int1 1.000
## Int2 0.754 NA
## Behave =~
## Bhv1 1.000
## Bhv2 1.162 NA
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## Intent ~
## Attitude 1.437 NA
## PBC 1.531 NA
## Norms -1.219 NA
## Behave ~
## Intent 0.658 NA
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## .Att1 ~~
## .Att2 2.000 NA
## .PBC1 ~~
## .PBC2 1.996 NA
## .SN1 ~~
## .SN2 0.639 NA
## .Int1 ~~
## .Int2 0.436 NA
## .Bhv1 ~~
## .Bhv2 0.918 NA
## Attitude ~~
## PBC 1.815 NA
## Norms 1.106 NA
## PBC ~~
## Norms 0.748 NA
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .Att1 2.049 NA
## .Att2 2.303 NA
## .PBC1 2.198 NA
## .PBC2 4.965 NA
## .SN1 3.259 NA
## .SN2 3.077 NA
## .Int1 0.423 NA
## .Int2 1.495 NA
## .Bhv1 1.221 NA
## .Bhv2 2.170 NA
## Attitude 0.478 NA
## PBC -0.108 NA
## Norms 0.425 NA
## .Intent 2.138 NA
## .Behave 0.547 NA
semPaths(TPBSEM2,
what = "paths",
whatLabels = "stand",
rotation = 2)
# IBCM Random Data Time
OBrienKaiser <- read_csv("C:/Users/autum/Documents/R/SurveyStats/OBrienKaiser.csv")
## New names:
## Rows: 16 Columns: 18
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (2): treatment, gender dbl (16): ...1, pre.1, pre.2, pre.3, pre.4, pre.5,
## post.1, post.2, post.3, p...
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
data2 <- OBrienKaiser
data2 <-
data2 %>%
rename("AM1" = "pre.1",
"AM2" = "pre.2",
"CM1" = "pre.3",
"CM2" = "pre.4",
"Int3" = "pre.5",
"Int4" = "post.1",
"Att3" = "post.2",
"Att4" = "post.3",
"SN3" = "post.4",
"SN4" = "post.5",
"PBC3" = "fup.1",
"PBC4" = "fup.2",
"Bhv3" = "fup.3",
"Bhv4" = "fup.4",
"Auto" = "fup.5")
# This one looks most like the TBP I used but it's a touch more complex
# I'll attach the photo of what it is supposed to put out
IBCMModel <- '
# Measures
T1AutonomousMotivation =~ AM1 + AM2
T1ControlledMotivation =~ CM1 + CM2
T1Intentions =~ Int3 + Int4
T1ExplicitAttitudes =~ Att3 + Att4
T1SubNorms =~ SN3 + SN4
T1PBC =~ PBC3 + PBC4
T2Behaviour =~ Bhv3 + Bhv4
Habits =~ Auto
# Direct Effects
T2Behaviour ~ IB*T1Intentions + Habits
T1Intentions ~ EI*T1ExplicitAttitudes + PBCI*T1PBC + SNI*T1SubNorms
T1ExplicitAttitudes ~ AME*T1AutonomousMotivation + CME*T1ControlledMotivation
T1PBC ~ AMPBC*T1AutonomousMotivation + CMPBC*T1ControlledMotivation
T1SubNorms ~ AMSN*T1AutonomousMotivation + CMSN*T1ControlledMotivation
# Indirect Effects
ExpIntBehav := EI*IB
PBCIntBehav := PBCI*IB
SNIntBehav := SNI*IB
AMExpIntBehav := EI*IB*AME
AMPBCIntBehav := PBCI*IB*AMPBC
AMSNIntBehav := SNI*IB*AMSN
CMExpIntBehav := EI*IB*CME
CMPBCIntBehav := PBCI*IB*CMPBC
CMSNIntBehav := SNI*IB*CMSN
CMBEHVRFULL := CMExpIntBehav + CMPBCIntBehav + CMSNIntBehav
AMBEHVRFULL := AMExpIntBehav + AMPBCIntBehav + AMSNIntBehav
# Covariances
T1ExplicitAttitudes ~~ T1SubNorms
T1ExplicitAttitudes ~~ T1PBC
T1PBC ~~ T1SubNorms
T1AutonomousMotivation ~~ T1ControlledMotivation
'
IBCMSem <- sem(IBCMModel, data=data2)
## Warning in lav_object_post_check(object): lavaan WARNING: some estimated ov
## variances are negative
## Warning in lav_object_post_check(object): lavaan WARNING: some estimated lv
## variances are negative
summary(IBCMSem)
## lavaan 0.6-12 ended normally after 193 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 46
##
## Number of observations 16
##
## Model Test User Model:
##
## Test statistic 207.073
## Degrees of freedom 74
## P-value (Chi-square) 0.000
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Expected
## Information saturated (h1) model Structured
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|)
## T1AutonomousMotivation =~
## AM1 1.000
## AM2 1.084 0.102 10.672 0.000
## T1ControlledMotivation =~
## CM1 1.000
## CM2 1.446 0.361 4.007 0.000
## T1Intentions =~
## Int3 1.000
## Int4 1.484 0.573 2.588 0.010
## T1ExplicitAttitudes =~
## Att3 1.000
## Att4 0.754 0.132 5.690 0.000
## T1SubNorms =~
## SN3 1.000
## SN4 1.299 0.215 6.056 0.000
## T1PBC =~
## PBC3 1.000
## PBC4 1.039 0.120 8.665 0.000
## T2Behaviour =~
## Bhv3 1.000
## Bhv4 0.822 0.138 5.966 0.000
## Habits =~
## Auto 1.000
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## T2Behaviour ~
## T1Intnt (IB) 1.875 0.660 2.842 0.004
## Habits -0.008 0.127 -0.066 0.947
## T1Intentions ~
## T1ExplA (EI) 0.071 0.081 0.876 0.381
## T1PBC (PBCI) 0.906 0.418 2.169 0.030
## T1SbNrm (SNI) -0.362 0.299 -1.210 0.226
## T1ExplicitAttitudes ~
## T1AtnmM (AME) -5.592 12.270 -0.456 0.649
## T1CntrM (CME) 8.309 15.712 0.529 0.597
## T1PBC ~
## T1AtnmM (AMPB) -7.851 15.970 -0.492 0.623
## T1CntrM (CMPB) 11.071 20.454 0.541 0.588
## T1SubNorms ~
## T1AtnmM (AMSN) -8.195 16.426 -0.499 0.618
## T1CntrM (CMSN) 11.337 21.044 0.539 0.590
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## .T1ExplicitAttitudes ~~
## .T1SubNorms 4.324 7.230 0.598 0.550
## .T1PBC 4.329 7.033 0.616 0.538
## .T1SubNorms ~~
## .T1PBC 5.451 9.352 0.583 0.560
## T1AutonomousMotivation ~~
## T1CntrlldMtvtn 1.828 0.727 2.513 0.012
## Habits 1.161 0.843 1.377 0.169
## T1ControlledMotivation ~~
## Habits 1.070 0.733 1.459 0.144
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .AM1 0.185 0.091 2.036 0.042
## .AM2 0.149 0.091 1.637 0.102
## .CM1 0.693 0.236 2.939 0.003
## .CM2 1.830 0.612 2.989 0.003
## .Int3 2.348 0.832 2.824 0.005
## .Int4 1.868 0.673 2.777 0.005
## .Att3 -0.157 0.494 -0.319 0.750
## .Att4 1.166 0.498 2.344 0.019
## .SN3 0.818 0.362 2.259 0.024
## .SN4 0.599 0.419 1.432 0.152
## .PBC3 0.232 0.106 2.188 0.029
## .PBC4 0.478 0.187 2.562 0.010
## .Bhv3 0.596 0.297 2.008 0.045
## .Bhv4 1.010 0.384 2.630 0.009
## .Auto 0.000
## T1AutonmsMtvtn 2.342 0.893 2.622 0.009
## T1CntrlldMtvtn 1.396 0.694 2.013 0.044
## .T1Intentions -0.078 0.093 -0.839 0.402
## .T1ExplctAtttds 5.603 5.747 0.975 0.330
## .T1SubNorms 5.886 9.743 0.604 0.546
## .T1PBC 5.409 9.108 0.594 0.553
## .T2Behaviour -0.082 0.299 -0.275 0.783
## Habits 4.121 1.457 2.828 0.005
##
## Defined Parameters:
## Estimate Std.Err z-value P(>|z|)
## ExpIntBehav 0.132 0.143 0.928 0.353
## PBCIntBehav 1.698 0.577 2.940 0.003
## SNIntBehav -0.678 0.512 -1.324 0.186
## AMExpIntBehav -0.740 1.807 -0.409 0.682
## AMPBCIntBehav -13.330 27.507 -0.485 0.628
## AMSNIntBehav 5.559 11.867 0.468 0.639
## CMExpIntBehav 1.099 2.390 0.460 0.646
## CMPBCIntBehav 18.796 35.322 0.532 0.595
## CMSNIntBehav -7.690 15.358 -0.501 0.617
## CMBEHVRFULL 12.205 22.657 0.539 0.590
## AMBEHVRFULL -8.511 17.684 -0.481 0.630
semPaths(IBCMSem,
what = "paths",
whatLabels = "stand",
rotation = 2)