#Making mean composites of variables
#The package is multicon, the function is composite.
#In each line, you indicate the data frame (cfa3) and which columns from that data frame
#you want to combine into your composite (I picked 3, 2 and 5 for the first composite).
#As an interesting side note, If you select R, and list variables in that, it will automatically
#reverse-code/reflect any questionnaire items that need to be reflected! The rel=TRUE tells you the
#alpha-reliability of your parcels. That’s not needed, but I just thought it was a cool thing to add.
#Nomiss=0.8 is an interesting thing…it gives the function a rule of when NOT to calculate the composite.
#Right now it is set for when the participant is missing 80% of the items (that’s the default).
#Finally tr=0 means all observations are included (there is no trimming).
#If you set some other value, it will trim extreme values … no need for that here.

#Another nice thing…it is common when making scales out of items to first z-score all the items
#(so they have equal weight in the composite – this is called a unit-weighted composite). If you say
#Zitems=TRUE, it will automatically z-score your items! I like this package/function very much!


setwd("H:/data/User/Classes/multiv_13/Class11")
cfa3<-read_sav("Class11_practice_2014.sav")

#install.packages("multicon")
library(multicon)
## Warning: package 'multicon' was built under R version 4.0.3
## Loading required package: psych
## 
## Attaching package: 'psych'
## The following object is masked from 'package:lavaan':
##
## cor2cov
## Loading required package: abind
## Loading required package: foreach
cfa3$min1_1<-composite(cfa3[,c(3,2,5)], R = NULL, Zitems = FALSE, maxScore = NULL, rel=TRUE, nomiss = 0.0, tr = 0)
##  raw_alpha std.alpha   G6(smc) average_r      S/N        ase     mean        sd
## 0.9041657 0.9073821 0.8674076 0.7655709 9.797049 0.01626116 2.593333 0.7691878
## median_r
## 0.7662618
cfa3$min1_2<-composite(cfa3[,c(1,7,8)], R = NULL, Zitems = FALSE, maxScore = NULL, rel=TRUE, nomiss = 0.0, tr = 0)
##  raw_alpha std.alpha   G6(smc) average_r     S/N        ase     mean        sd
## 0.8930538 0.916196 0.8828574 0.7846777 10.9326 0.01492642 2.576667 0.8038278
## median_r
## 0.7843725
cfa3$min1_3<-composite(cfa3[,c(4,9,6)], R = NULL, Zitems = FALSE, maxScore = NULL, rel=TRUE, nomiss = 0.0, tr = 0)
##  raw_alpha std.alpha   G6(smc) average_r      S/N        ase mean        sd
## 0.9147466 0.9147589 0.8813565 0.7815232 10.73143 0.01476809 2.55 0.7175451
## median_r
## 0.7704699
cfa3$min2_1<-composite(cfa3[,c(11,17,15)], R = NULL, Zitems = FALSE, maxScore = NULL, rel=TRUE, nomiss = 0.0, tr = 0)
##  raw_alpha std.alpha   G6(smc) average_r      S/N        ase mean        sd
## 0.9084222 0.9102627 0.8738289 0.7717528 10.14364 0.01587451 2.53 0.7462514
## median_r
## 0.7765564
cfa3$min2_2<-composite(cfa3[,c(10,14,12)], R = NULL, Zitems = FALSE, maxScore = NULL, rel=TRUE, nomiss = 0.0, tr = 0)
##  raw_alpha std.alpha   G6(smc) average_r      S/N        ase mean        sd
## 0.9029924 0.9236745 0.8923545 0.8013479 12.10178 0.01528541 2.43 0.8372568
## median_r
## 0.7974945
cfa3$min2_3<-composite(cfa3[,c(16,18,13)], R = NULL, Zitems = FALSE, maxScore = NULL, rel=TRUE, nomiss = 0.0, tr = 0)
##  raw_alpha std.alpha   G6(smc) average_r      S/N        ase     mean        sd
## 0.9092927 0.911986 0.8865125 0.7754799 10.36183 0.01613778 2.506667 0.6708288
## median_r
## 0.7370215