|
| 1 | +# --- Potential for Conflict Index --- # |
| 2 | +# Vaske, J. J., Beaman, J., Barreto, H., & Shelby, L. B. (2010). |
| 3 | +# An Extension and Further Validation of the Potential for Conflict Index. |
| 4 | +# Leisure Sciences, 32(X), 240–254 |
| 5 | + |
| 6 | +############################ |
| 7 | +### >>> qd_pci1 |
| 8 | +############################ |
| 9 | + |
| 10 | +#' Potential conflict index (first variant) |
| 11 | +#' |
| 12 | +#' questionnaire data analysis: potential conflict index |
| 13 | +#' @param x vector with scores of the respondents |
| 14 | +#' @param scale_values vector with levels; default: -2:2 |
| 15 | +#' @param x_is_table if TRUE, x is table with the distribution of the scores |
| 16 | +#' |
| 17 | +#' @return PCI-score (potential for conflict index) |
| 18 | +#' @export |
| 19 | +#' @family plotting |
| 20 | +#' |
| 21 | +#' @examples |
| 22 | +#' \dontrun{ |
| 23 | +#' set.seed(201) |
| 24 | +#' Xv <- sample(-2:2, size = 100, replace = TRUE) #random responses |
| 25 | +#' Yv <- rep(c(-2,2),50) #most extreme difference |
| 26 | +#' Zv <- rep(2,100) #minimal difference |
| 27 | + |
| 28 | +#' #qd_pci1 |
| 29 | +#' qd_pci1(Xv, scale_values = -2:2, x_is_table = FALSE) # 0.4 |
| 30 | +#' qd_pci1(Yv, scale_values = -2:2, x_is_table = FALSE) # 1 |
| 31 | +#' qd_pci1(Zv, scale_values = -2:2, x_is_table = FALSE) # 0 |
| 32 | +#' } |
| 33 | +#' |
| 34 | +qd_pci1 <- function(x, scale_values = c(-2:2), |
| 35 | + x_is_table = FALSE){ |
| 36 | + |
| 37 | + ### ERROR CONTROL AND PREPARE DATA |
| 38 | + if (scale_values[1] != -scale_values[length(scale_values)]) |
| 39 | + stop("index should be symmetric") |
| 40 | + if (x_is_table) { |
| 41 | + if (length(x) != length(scale_values)) |
| 42 | + stop("table of x should contain countdata for every scale-value") |
| 43 | + } else { |
| 44 | + x <- table(factor(x, levels = scale_values)) |
| 45 | + } |
| 46 | + S <- NULL #To avoid the compilation NOTE |
| 47 | + |
| 48 | + ### PREP DATA |
| 49 | + countdata <- data.frame(N = as.numeric(x), |
| 50 | + X = abs(scale_values), |
| 51 | + S = sign(scale_values)) |
| 52 | + negatives <- subset(countdata, S == -1) |
| 53 | + positives <- subset(countdata, S == 1) |
| 54 | + neutrals <- subset(countdata, S == 0) |
| 55 | + |
| 56 | + #CALC DATA |
| 57 | + sum_Xa <- sum(positives$N * positives$X) |
| 58 | + sum_Xu <- sum(negatives$N * negatives$X) |
| 59 | + Xt <- sum_Xa + sum_Xu |
| 60 | + n <- sum(positives$N) + sum(negatives$N) + sum(neutrals$N) |
| 61 | + Z <- n * max(c(min(scale_values), max(scale_values))) |
| 62 | + |
| 63 | + #RETURN RESULT |
| 64 | + (1 - abs((sum_Xa / Xt) - (sum_Xu / Xt))) * Xt/Z |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +########################### |
| 71 | +### >>> qd_pci2 |
| 72 | +########################### |
| 73 | + |
| 74 | + |
| 75 | +#' Distance matrix for qd_pci2 |
| 76 | +#' |
| 77 | +#'Calculates distance matrix for the function qd_pci2 |
| 78 | +#' @param x vector with the scores of the respondents |
| 79 | +#' @param m m value in the formula (see details) |
| 80 | +#' @param p power value in the formula (see details) |
| 81 | +#' @details |
| 82 | +#' \deqn{Dp_{x,y} = (|r_{x} - r_{y}|) - (m-1))^{p}} |
| 83 | +#' \deqn{if sign(r_{x} \neq r_{y}) \\ |
| 84 | +#' else d_{x,y} = 0} |
| 85 | +#' Dp_x,y = (|r_x - r_y| - (m-1))^p |
| 86 | +#' @return single value containing pci index |
| 87 | +#' @examples |
| 88 | +#' \dontrun{ |
| 89 | +#' #'set.seed(201) |
| 90 | +#'Xv <- sample(-2:2, size = 100, replace = TRUE) #random responses |
| 91 | +#'qd_pci2(Xv, scale_values = -2:2, x_is_table = FALSE, m = 1, p = 1) # 0.37 |
| 92 | +#' } |
| 93 | +#' @export |
| 94 | +#' @family plotting |
| 95 | + |
| 96 | + |
| 97 | +qd_pci2_D <- function(x, m=1, p=1){ |
| 98 | + d <- matrix(nrow = length(x), ncol = length(x), data = NA) |
| 99 | + for (i in 1:nrow(d)) { |
| 100 | + for (j in 1:i) { |
| 101 | + if (abs(c(sign(x[i]) - sign(x[j]))) == 2) { |
| 102 | + d[i,j] <- d[j,i] <- (abs(x[i] - x[j]) - (m - 1)) ^ p |
| 103 | + } |
| 104 | + else { |
| 105 | + d[i,j] <- d[j,i] <- 0 |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + return(d) |
| 110 | +} |
| 111 | + |
| 112 | +###---------------- |
| 113 | + |
| 114 | +#' Potential conflict index (second variant) |
| 115 | +#' |
| 116 | +#' Calculates the potential conflict index based on the distance matrix between responses. |
| 117 | +#' |
| 118 | +#' @param x vector with scores of the respondents |
| 119 | +#' @param scale_values vector with levels; default: -2:2 |
| 120 | +#' @param x_is_table if TRUE, x is table with the distribution of the scores |
| 121 | +#' @param m correction; default: m = 1 |
| 122 | +#' @param p power; default: p = 1 |
| 123 | +#' @param print flag; if TRUE print results |
| 124 | +#' |
| 125 | +#' @return PCI-score (potential for conflict index) |
| 126 | +#' @export |
| 127 | +#' @family plotting |
| 128 | +#' |
| 129 | +#' @examples |
| 130 | +#' \dontrun{ |
| 131 | +#'set.seed(201) |
| 132 | +#'Xv <- sample(-2:2, size = 100, replace = TRUE) #random responses |
| 133 | +#'Yv <- rep(c(-2,2),50) #most extreme difference |
| 134 | +#'Zv <- rep(2,100) #minimal difference |
| 135 | +#' #qd_pci2 - using D2 (m=1) |
| 136 | +#'qd_pci2(Xv, scale_values = -2:2, x_is_table = FALSE, m = 1, p = 1) # 0.37 |
| 137 | +#'qd_pci2(Yv, scale_values = -2:2, x_is_table = FALSE, m = 1, p = 1) # 1 |
| 138 | +#'qd_pci2(Zv, scale_values = -2:2, x_is_table = FALSE, m = 1, p = 1) # 0 |
| 139 | + |
| 140 | +#qd_pci2 - using D1 (m=2) |
| 141 | +#'qd_pci2(Xv, scale_values = -2:2, x_is_table = FALSE, m = 2, p = 1) # 0.31 |
| 142 | +#'qd_pci2(Yv, scale_values = -2:2, x_is_table = FALSE, m = 2, p = 1) # 1 |
| 143 | +#'qd_pci2(Zv, scale_values = -2:2, x_is_table = FALSE, m = 2, p = 1) # 0 |
| 144 | +#' } |
| 145 | +qd_pci2 <- function(x, scale_values = c(-2:2), |
| 146 | + x_is_table = FALSE, m = 1, p = 1, print = FALSE){ |
| 147 | + |
| 148 | + ### ERROR CONTROL AND PREPARE DATA |
| 149 | + |
| 150 | + if (scale_values[1] != -scale_values[length(scale_values)]) |
| 151 | + stop("index should be symmetric") |
| 152 | + if (x_is_table) { |
| 153 | + if (length(x) != length(scale_values)) |
| 154 | + stop("table of x should contain countdata for every scale-value") |
| 155 | + } else { |
| 156 | + x <- table(factor(x, levels = scale_values)) |
| 157 | + } |
| 158 | + |
| 159 | + ### PREP DATA |
| 160 | + |
| 161 | + #Total N |
| 162 | + Ntot <- sum(x) |
| 163 | + |
| 164 | + #call distance function |
| 165 | + d <- qd_pci2_D(scale_values, m = m, p = p) |
| 166 | + |
| 167 | + #matrix with counts |
| 168 | + n <- matrix(nrow = length(x), ncol = length(x), data = rep(x, length(x))) |
| 169 | + |
| 170 | + #Actual Distance |
| 171 | + #n = nk, t(n) = nh |
| 172 | + #d is distance matrix between the scale_value levels |
| 173 | + #d * nk * nh accounts for number of elements in each scale_value level |
| 174 | + #rowsums(d*n*t(n)) calculates the deltax for each level |
| 175 | + #diag(d)*diag(n)^2 actual distance with itself is subtracted |
| 176 | + #sum(...) sums the results for each level |
| 177 | + |
| 178 | + weightedsum <- sum(rowSums(d * n * t(n)) - (diag(d) * diag(n) * diag(n))) |
| 179 | + |
| 180 | + #Maximum Possible Distance |
| 181 | + #dmax = max distance between 2 single elements |
| 182 | + #even N: multiply with Ntot^2 = max distance |
| 183 | + # if each element is at the extremes |
| 184 | + #odd N: multiply with Ntot^2 - 1 |
| 185 | + dmax <- max(d) |
| 186 | + |
| 187 | + delta <- dmax * (Ntot^2 - Ntot %% 2) / 2 |
| 188 | + |
| 189 | + #return the normalized sum |
| 190 | + if (print == TRUE) { |
| 191 | + cat("\nqd_pci2 (m =", m, ", p =", p, ", |
| 192 | + levels =", length(scale_values), ")\n") |
| 193 | + cat("------------------------------------\n") |
| 194 | + cat("Total actual distance:", weightedsum, "\n") |
| 195 | + cat("Maximum total distance:", delta, "\n") |
| 196 | + cat("Maximum distance:", dmax, "\n") |
| 197 | + cat("\nqd_pci2:", round(weightedsum / delta, 2),"\n") |
| 198 | + } |
| 199 | + |
| 200 | + return(invisible(weightedsum / delta)) |
| 201 | +} |
| 202 | + |
| 203 | + |
0 commit comments