-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomp_reliability.R
More file actions
144 lines (140 loc) · 5.73 KB
/
comp_reliability.R
File metadata and controls
144 lines (140 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#' Function to calculate Composite reliability
#'
#' This function needs a lavaan object with a model fit to work. It calculates Composite reliability for CFA models.
#' There are two formulas inside the function and they run depending on the fit characteristics.
#' The composite reliability or factor rho coefficient is the ratio of explained variance over total variance.
#' With no error correlations the CR is just calculated as previously defined.
#' A different formula is needed for when indicators share at least one error covariance.
#' In this case the total variance is calculated by adding the sum of the unstandarized error variance multiplied by 2.
#'
#' @name comp_reliability
#' @seealso Kline, R. (2016). Principles and Practice of Structural Equation Modeling. Fourth Edition. Guilford press. NY.
#' @seealso Raykov, T. (2004). Behavioral scale realiability and measurement invariance evaluation using latent variable modeling. Behavior therapy, 35, 299-331.
#' @param x lavaan object: The name of the model fit that was calculated from the specified CFA model with the lavaan package.
#' @return The output is a data.frame specifying the latent factor in the first column and the CR in the second column.
#' @importFrom dplyr "%>%"
#' @importFrom dplyr filter
#' @importFrom dplyr select
#' @importFrom dplyr left_join
#' @importFrom dplyr if_else
#' @importFrom lavaan parameterEstimates
#' @importFrom lavaan inspect
#' @importFrom rlang .data
#' @author Juan Carlos Saravia
#' @examples
#' #'set.seed(123456)
#'data <- data.frame(replicate(10,sample(1:5,1000,rep=TRUE)))
#'
#'m2 <- 'f=~ X1 + X2 + X3+X4+X5+X6+X7+X8+X9+X10'
#'fit2 <- lavaan::cfa(m2, data = data)
#'lavaan::summary(fit2, fit.measures = TRUE,
#' standardized = TRUE)
#'comp_reliability(fit2)
#'
#' @export
require(lavaan)
require(dplyr)
comp_reliability <- function(x) {
# Creating sum of beta loadings per factor
x1 <- data.frame(inspect(x,what="est")$lambda)
sum_loadings <- data.frame(apply(x1, 2, sum)^2)
colnames(sum_loadings) <- "Sum_beta_loadings"
sum_loadings$lhs <- rownames(sum_loadings)
# Extracting latent factor variance
v <- parameterEstimates(x)
v <- v[,1:4]
v$variance <- if_else(v$lhs == v$rhs,1,0)
v <- v %>%
filter(.data$op == "~~",
.data$variance == "1") %>%
select(.data$lhs, .data$est)
# Extracting distribution of items per factor
z<- parameterEstimates(x)
z <- z[,1:3]
z <- z %>%
filter(.data$op == "=~") %>%
select(-.data$op)
z <- left_join(z,v, by = "lhs")
# Creating sum of error variance per factor
y <- data.frame(inspect(x,what="est")$theta)
y$max <- apply(y,2,max)
y <- y %>%
select(max)
y$rhs <- rownames(y)
yz <- left_join(z, y ,by = "rhs")
yz_sum <- yz %>%
dplyr::group_by(.data$lhs) %>%
dplyr::summarise(sum_error = sum(max),
Item_number = dplyr::n(),
variance_latent = mean(.data$est))
CR <- left_join(yz_sum,sum_loadings, by = "lhs")
# Amount of latent variables
var <- inspect(x)$psi
var <- ncol(var)
# Specify if there are error correlations
er <- parameterEstimates(x)
er <- er[,1:3]
er$covariance <- if_else(er$lhs != er$rhs,1,0)
er <- er %>%
filter(.data$op == "~~",
.data$covariance == "1")
eval <- dim(er)[1] == var
if (eval == TRUE) {
CR$composite_reliability <-(CR$Sum_beta_loadings*CR$variance_latent) / (CR$Sum_beta_loadings*CR$variance_latent + CR$sum_error)
} else {
CR$composite_reliability_ec <-(CR$Sum_beta_loadings*CR$variance_latent) / (CR$Sum_beta_loadings*CR$variance_latent + CR$sum_error + 2*CR$sum_error)
}
CR <- CR[,c(1,6)]
CR
}
#' Function to calculate Average variance extracted (AVE)
#'
#' This function needs a lavaan object with a model fit to work. It calculates the Average variance extracted for CFA models.
#' It is the average of the squared standardized pattern coefficients for indicators that depend on the same factor but are specified to measure no other factors.
#'
#' @name avar_extracted
#' @seealso Kline, R. (2016). Principles and Practice of Structural Equation Modeling. Fourth Edition. Guilford press. NY.
#' @seealso Raykov, T. (2004). Behavioral scale realiability and measurement invariance evaluation using latent variable modeling. Behavior therapy, 35, 299-331.
#' @param x lavaan object: The name of the model fit that was calculated from the specified CFA model in the lavaan package.
#' @return The output is a data.frame specifying the latent factor in the first column and the AVE in the second column.
#' @importFrom dplyr "%>%"
#' @importFrom dplyr filter
#' @importFrom dplyr select
#' @importFrom rlang .data
#' @author Juan Carlos Saravia
#' @examples
#'#' #'set.seed(123456)
#'data <- data.frame(replicate(10,sample(1:5,1000,rep=TRUE)))
#'
#'m2 <- 'f=~ X1 + X2 + X3+X4+X5+X6+X7+X8+X9+X10'
#'fit2 <- lavaan::cfa(m2, data = data)
#'lavaan::summary(fit2, fit.measures = TRUE,
#' standardized = TRUE)
#'avar_extracted(fit2)
#'
#'
# \donttest{ave(fit)}
#' @export
avar_extracted <- function(x) {
# Extracting distribution of items per factor
z<- lavaan::parameterEstimates(x)
z <- z[,1:3]
z <- z %>%
filter(.data$op == "=~") %>%
select(-.data$op)
# Creating sum of error variance per factor
y <- data.frame(lavaan::inspect(x,what="std")$theta)
y$max <- apply(y,2,max)
y <- y %>%
select(max)
y$rhs <- rownames(y)
yz <- dplyr::left_join(z, y ,by = "rhs")
yz_sum <- yz %>%
dplyr::group_by(.data$lhs) %>%
dplyr::summarise(sum_error = sum(max),
Item_number = dplyr::n())
yz_sum$AVE <- yz_sum$sum_error/yz_sum$Item_number
yz_sum <- yz_sum %>%
select(.data$lhs, .data$AVE)
yz_sum
}