Software integration

Francisco Cardozo

Eric Brown

Agenda

  • Multitrait-Multimethod Matrix
  • Data set
  • Analysis
  • R and Mplus

Prevention of alcohol use

We usually measure risk factors by asking students in the schools, but because of Covid, it was impossible.

We wanted to ask while they were at homes, but it was extremely complicated because of biases and logistics.

What happens if we ask parents?

Is the parent evaluation of the student’s risk a good approximation of the student’s risk?

What is the correlation between students’ and parents’ perceptions of risk?

Multitrait-Multimethod Matrix in R

The data set contains:

  • Risk and protective factors
  • Parent’s and students’ perspective

Community Disorganization (CRCDO)

How much do each of the following statements describe your neighborhood?

Var_name Child Parent 4 3 2 1
NHDELIN Crime Crime YES! Yes No NO!
NHVENTA Drugs Drugs YES! Yes No NO!
NHFIGHT Fights Fights YES! Yes No NO!
NHEMPTY Lots of empty or abandoned buildings Lots of empty or abandoned buildings YES! Yes No NO!
NHGRAF Lots of graffiti Lots of graffiti YES! Yes No NO!

Perceived Risks of Drug Use (PRPRD)

How much do you think people risk harming themselves (physically or in other ways) if they…

Var_name Child Parent 4 3 2 1
HMCIG …smoke one or more packs of cigarettes per day? …smoke one or more packs of cigarettes per day? None Small Moderate Big
HMMARO …try marijuana once or twice? …try marijuana once or twice? None Small Moderate Big
HMMARR …smoke marijuana regularly? …smoke marijuana regularly? None Small Moderate Big
HMALC …take one or two drinks of an alcoholic beverage (beer, wine, or liquor) nearly every day? …take one or two drinks of an alcoholic beverage (beer, wine, or liquor) nearly every day? None Small Moderate Big

Poor Family Management (FRPFM)

In your family…

Var_name Child Parent 1 2 3 4
FAMRULE The rules in my family are clear. The rules in my family are clear. YES! Yes No NO!
HMWORK My parents ask if I've gotten my homework done. I frequently ask my children if they have finished their homework. YES! Yes No NO!
PARKNOW When I am not at home, one of my parents knows where I am and who I am with. When my children are not at home, one of the adults knows where they are and who they are with. YES! Yes No NO!
CMHOME Would your parents know if you did not come home on time? I would realize if my children do not arrive on time at home. YES! Yes No NO!
CLRRULE My family has clear rules about alcohol and drug use. I would realize if my children do not arrive on time at home. YES! Yes No NO!
CATCHAL If you drank some beer or wine or hard liquor (for example, vodka, whiskey or gin) without your parent's permission, would you be caught by your parents? If my son/daughter drinks beer, brandy, rum, wine, cocktails, or any other liquor without permission, I would realize it YES! Yes No NO!
CATCHSK If you skipped school, would you be caught by your parents? If my children skip school, I would realize it. YES! Yes No NO!

Multitrait-Multimethod Matrix

Multimethod= Parent and Child

Multitrait = Community Disorganization, Poor Family Management, and Perceived Risks of Drug Use

Plot your data

Descriptives

Cronbach’s Alpha

Community Disorganization = 0.75

Perceived Risk of Drug Use = 0.662

Poor Family Management = 0.818

Cronbach’s Alpha Community Disorganization (CRCDO)

Parent, student

[1] 0.7383984
[1] 0.7579803

Cronbach’s Alpha Perceived Risks of Drug Use (PRPRD)

Parent, student

[1] 0.6401554
[1] 0.618836

Cronbach’s Alpha Poor Family Management (FRPFM)

Parent, student

[1] 0.8343457
[1] 0.8081604

MultiTrait MultiMethod Matrix

CRCDO= Community Disorganization, PRPRD= Perceived Risks of Drug Use, FRPFM= Poor Family Management

MultiTrait MultiMethod Matrix

CRCDO= Community Disorganization, PRPRD= Perceived Risks of Drug Use, FRPFM= Poor Family Management

“All software are wrong, but some are useful”

R and Mplus

R

  • Integrate your data and text
  • Lot of tools for data manipulation
  • Packages

Mplus

  • Highly structured syntax
  • Complete outputs
  • Better for structural models?

Data set in long format

You have ID column, and the participants (unit of analysis) appear multiple times in different rows.


ID Member Risk Factor Item Answer Label
1 Parent RF1 Item1 2 no
1 Parent RF1 Item2 3 yes
1 Student RF1 Item1 2 no
1 Student RF1 Item2 1 YES!
1 Parent RF2 Item1 4 NO!
1 Parent RF2 Item2 2 no
1 Student RF2 Item1 3 yes
1 Student RF2 Item2 2 no
2 Parent RF1 Item1 4 NO!
2 Parent RF1 Item2 2 yes
2 Student RF1 Item1 3 no
2 Student RF1 Item2 1 YES!

Data set in wide format

You still have the ID column, but the participants (unit of analysis) only appear once.


ID Parent_RF1_item1 Parent_RF1_item2 Student_RF1_Item1 Student_RF1_Item2
1 1 3 2 1
2 4 2 3 1

Long and wide format

Click

Click to go to the lab. Practice

Mplus

Mplus automation

A package:

  • Run Mplus scripts from R.
  • Convert data into Mplus format.
  • Generate Mplus scripts
  • Read Mplus outputs into R
  • Plot model results

MplusAutomation

# 1. Install to save the package in your computer. 
# You only need to do this once
install.packages("MplusAutomation") 

# 2. Call the package to activate the functions saved in the package.
library(MplusAutomation) 


MplusAutomation

Before you start pivoting your data, begin by…


library(readxl) # To read excel files in R
library(tidyverse) # To activate the "pivot" functions
library(MplusAutomation) # To activate the function "prepareMplusData"

dyads <- read_xlsx("./replace_this_with_the_name_of_your_data_file.xlsx")

MplusAutomation

# Pivot wider your data
Mplus_dyads <- 
  dyads |> 
  group_by(ID, MEMBER, FACTORNAME) |>
  summarise(MEAN= mean(ANSWER)) |> 
  pivot_wider(names_from = c(MEMBER, FACTORNAME),
              values_from = MEAN) 

# Define missing data value
Mplus_dyads[is.na(Mplus_dyads)] <- 999

# Export your data to Mplus
prepareMplusData(Mplus_dyads, "Mplus_dyads.dat")

MplusAutomation

# Pivot wider your data
Mplus_dyads <- 
  dyads |> 
  group_by(ID, MEMBER, FACTORNAME) |>
  summarise(MEAN= mean(ANSWER)) |> 
  pivot_wider(names_from = c(MEMBER, FACTORNAME),
              values_from = MEAN) 

# Define missing data value
Mplus_dyads[is.na(Mplus_dyads)] <- 999

# Export your data to Mplus
prepareMplusData(Mplus_dyads, "Mplus_dyads.dat")

MplusAutomation

# Pivot wider your data
Mplus_dyads <- 
  dyads |> 
  group_by(ID, MEMBER, FACTORNAME) |>
  summarise(MEAN= mean(ANSWER)) |> 
  pivot_wider(names_from = c(MEMBER, FACTORNAME),
              values_from = MEAN) 

# Define missing data value
Mplus_dyads[is.na(Mplus_dyads)] <- 999

# Export your data to Mplus
prepareMplusData(Mplus_dyads, "Mplus_dyads.dat")

MplusAutomation

CFA in Lavaan


-----
Factor: ID 
Conversion:
                level number
 01-1002-022-20102021      1
 01-1002-023-20102021      2
 01-1002-024-20102021      3
 01-1002-025-20102021      4
 01-1002-026-20102021      5
 01-1002-027-20102021      6
 01-1002-028-21102021      7
 01-1002-046-20102021      8
 01-1003-029-21102021      9
 01-1003-030-21102021     10
 01-1003-031-21102021     11
 01-1003-032-21102021     12
 01-1003-033-21102021     13
 01-1003-034-21102021     14
 01-1003-035-21102021     15
 01-1003-036-21102021     16
 01-1003-037-21102021     17
 01-1003-038-21102021     18
 01-1003-039-21102021     19
 01-1003-040-21102021     20
 01-1003-041-21102021     21
 01-1003-042-21102021     22
 01-1003-043-21102021     23
 01-1102-044-20102021     24
 01-1102-045-20102021     25
  01-606-001-20102021     26
  01-606-002-20102021     27
  01-606-003-21102021     28
  01-606-004-21102021     29
  01-606-005-21102021     30
  01-705-006-19102021     31
  01-705-007-19102021     32
  01-705-008-19102021     33
  01-705-009-19102021     34
  01-804-010-19102021     35
  01-804-011-19102022     36
  01-804-012-19102022     37
  01-805-013-19102021     38
  01-805-014-19102021     39
  01-805-015-19102021     40
  01-905-016-19102021     41
  01-905-017-19102021     42
  01-905-018-19102021     43
  01-905-019-19102021     44
  01-905-020-19102021     45
  01-905-021-19102021     46
  02-101-062-14092021     47
  02-101-190-08112021     48
  02-101-191-08112021     49
  02-101-192-08112021     50
  02-102-051-14092021     51
  02-102-052-14092021     52
  02-102-053-14092021     53
  02-102-054-14092021     54
  02-102-055-14092021     55
  02-102-185-08112021     56
  02-102-186-08112021     57
  02-102-187-08112021     58
  02-102-188-08112021     59
  02-102-189-09112021     60
  02-111-047-14092021     61
  02-930-048-14092021     62
  02-930-049-14092021     63
  02-930-050-14092021     64
  02-930-058-14092021     65
  02-930-060-14092021     66
  02-932-135-14092021     67
  02-932-193-08112021     68
  02-932-194-08112021     69
  03-101-061-21092021     70
  03-101-063-21092021     71
  03-101-096-21092021     72
  03-102-075-23092021     73
  03-102-076-23092021     74
  03-102-077-23092021     75
  03-102-078-23092021     76
  03-102-155-17112021     77
  03-111-071-22092021     78
  03-111-072-22092021     79
  03-111-073-22092021     80
  03-111-074-22092021     81
  03-112-183-03112021     82
  03-112-184-03112021     83
  03-610-056-21092021     84
  03-610-057-21092021     85
  03-610-059-21092021     86
  03-610-097-21092021     87
  03-622-178-03112021     88
  03-622-179-03112021     89
  03-622-180-03112021     90
  03-622-181-03112021     91
  03-622-182-03112021     92
  03-710-066-22092021     93
  03-720-070-22092021     94
  03-720-079-23092021     95
  03-720-080-29092021     96
  03-721-168-02112021     97
  03-810-069-22092021     98
   03-811-167-2112021     99
  03-812-175-02112021    100
  03-820-067-22092021    101
  03-820-068-22092021    102
  03-821-158-02112021    103
  03-821-159-02112021    104
  03-821-164-02112021    105
  03-821-165-02112021    106
  03-821-166-02112021    107
  03-910-064-22092021    108
  03-910-065-22092021    109
  04-101-081-04102021    110
  04-101-082-04102021    111
  04-101-083-04102021    112
  04-101-084-04102021    113
  04-101-086-04102021    114
  04-101-087-04102021    115
  04-101-088-04102021    116
  04-101-089-06102021    117
  04-101-090-04102021    118
  04-101-103-05102021    119
  04-101-104-05102021    120
  04-101-106-06102021    121
  04-101-107-06102021    122
  04-101-108-06102021    123
  04-101-109-06102021    124
  04-101-110-06102021    125
  04-101-111-06102021    126
  04-101-112-06102021    127
  04-102-085-04102021    128
  04-102-091-04102021    129
  04-102-092-04102021    130
  04-102-105-06102021    131
  04-102-113-05102021    132
  04-111-093-05102021    133
  04-111-094-05102021    134
  04-111-095-05102021    135
  04-111-098-05102021    136
  04-111-114-19102021    137
  04-111-115-19102021    138
  04-111-116-19102021    139
  04-111-117-19102021    140
  04-111-118-19102021    141
  04-111-119-19102021    142
  04-111-120-19102021    143
  04-111-126-20102021    144
  04-111-127-20102021    145
  04-111-177-02112021    146
  04-112-099-05102021    147
  04-112-100-05102021    148
  04-112-101-05102021    149
  04-112-102-05102021    150
  04-112-121-19102021    151
  04-112-122-19102021    152
  04-112-123-19102021    153
  04-112-124-19102021    154
  04-112-125-19102021    155
  04-112-132-19102021    156
  04-732-176-02112021    157
  05-101-136-25102021    158
  05-101-137-25102021    159
  05-101-139-25102021    160
  05-101-140-25102021    161
  05-102-141-25102021    162
  05-102-142-25102021    163
  05-102-143-25102021    164
  05-102-144-25102021    165
  05-102-145-25102021    166
  05-102-146-25102021    167
  05-102-147-25102021    168
  05-711-131-27102021    169
  05-711-148-27102021    170
  05-711-149-27102021    171
  05-711-150-27102021    172
  05-711-151-27102021    173
  05-711-152-27102021    174
  05-711-153-27102021    175
  05-711-169-22112021    176
  05-711-170-22112021    177
  05-711-171-22112021    178
  05-711-172-22112021    179
  05-711-173-22112021    180
  05-811-128-27102021    181
  05-811-129-27102021    182
  05-811-130-27102021    183
  05-811-174-22112021    184
  05-812-133-22112021    185
  05-812-134-22112021    186
  05-821-138-25102021    187
  05-911-154-27102021    188
   05-911-156-7102021    189
  05-911-157-27102021    190
  06-102-160-26102021    191
  06-102-161-26102021    192
  06-931-162-26102021    193
  06-931-163-26102021    194
-----


-----
Factor: MEMBER 
Conversion:
 level number
     P      1
     S      2
-----

TITLE: Your title goes here
DATA: FILE = "Mplus_CFA.dat";
VARIABLE: 
NAMES = ID MEMBER SAFENH NHDELIN NHVENTA NHFIGHT NHEMPTY NHGRAF FAMRULE HMWORK
     PARKNOW CMHOME CLRRULE CATCHAL CATCHSK HMCIG HMMARO HMMARR HMALC; 
MISSING=.;

CFA Lavaan

Complete sample

Parents

Students