pkgdown/extra-head.html

Skip to contents

correlateScales() creates a dataframe of scale items representing correlated constructs, as one might find in a completed questionnaire.

Usage

correlateScales(dataframes, scalecors)

Arguments

dataframes

a list of 'k' dataframes to be rearranged and combined

scalecors

target correlation matrix - should be a symmetric \(k \times k\) positive-semi-definite matrix, where 'k' is the number of dataframes

Value

Returns a dataframe whose columns are taken from the starter dataframes and whose summated values are correlated according to a user-specified correlation matrix

Details

Correlated rating-scale items generally are summed or averaged to create a measure of an "unobservable", or "latent", construct. correlateScales() takes several such dataframes of rating-scale items and rearranges their rows so that the scales are correlated according to a predefined correlation matrix. Univariate statistics for each dataframe of rating-scale items do not change, but their correlations with rating-scale items in other dataframes do.

Examples


## three attitudes and a behavioural intention
n <- 32
lower <- 1
upper <- 5

### attitude #1
cor_1 <- makeCorrAlpha(items = 4, alpha = 0.90)
#> reached max iterations (1600) - best mean difference: 2.2e-05
means_1 <- c(2.5, 2.5, 3.0, 3.5)
sds_1 <- c(0.9, 1.0, 0.9, 1.0)

Att_1 <- makeScales(
  n = n, means = means_1, sds = sds_1,
  lowerbound = rep(lower, 4), upperbound = rep(upper, 4),
  items = 4,
  cormatrix = cor_1
)
#> Variable  1 :  item01  - 
#> reached maximum of 1024 iterations
#> Variable  2 :  item02  - 
#> best solution in 667 iterations
#> Variable  3 :  item03  - 
#> reached maximum of 1024 iterations
#> Variable  4 :  item04  - 
#> reached maximum of 1024 iterations
#> 
#> Arranging data to match correlations
#> 
#> Successfully generated correlated variables
#> 


### attitude #2
cor_2 <- makeCorrAlpha(items = 5, alpha = 0.85)
#> reached max iterations (2500) - best mean difference: 8.1e-05
#> Correlation matrix is not yet positive definite
#> Working on it
#> 
#> improved at swap - 1 (min eigenvalue: -0.017242)
#> improved at swap - 3 (min eigenvalue: -0.008469)
#> improved at swap - 4 (min eigenvalue: 0.025501)
#> positive definite at swap - 4
means_2 <- c(2.5, 2.5, 3.0, 3.0, 3.5)
sds_2 <- c(1.0, 1.0, 0.9, 1.0, 1.5)

Att_2 <- makeScales(
  n = n, means = means_2, sds = sds_2,
  lowerbound = rep(lower, 5), upperbound = rep(upper, 5),
  items = 5,
  cormatrix = cor_2
)
#> Variable  1 :  item01  - 
#> reached maximum of 1024 iterations
#> Variable  2 :  item02  - 
#> reached maximum of 1024 iterations
#> Variable  3 :  item03  - 
#> best solution in 808 iterations
#> Variable  4 :  item04  - 
#> reached maximum of 1024 iterations
#> Variable  5 :  item05  - 
#> reached maximum of 1024 iterations
#> 
#> Arranging data to match correlations
#> 
#> Successfully generated correlated variables
#> 


### attitude #3
cor_3 <- makeCorrAlpha(items = 6, alpha = 0.75)
#> correlation values consistent with desired alpha in 72 iterations
means_3 <- c(2.5, 2.5, 3.0, 3.0, 3.5, 3.5)
sds_3 <- c(1.0, 1.5, 1.0, 1.5, 1.0, 1.5)

Att_3 <- makeScales(
  n = n, means = means_3, sds = sds_3,
  lowerbound = rep(lower, 6), upperbound = rep(upper, 6),
  items = 6,
  cormatrix = cor_3
)
#> Variable  1 :  item01  - 
#> reached maximum of 1024 iterations
#> Variable  2 :  item02  - 
#> reached maximum of 1024 iterations
#> Variable  3 :  item03  - 
#> reached maximum of 1024 iterations
#> Variable  4 :  item04  - 
#> reached maximum of 1024 iterations
#> Variable  5 :  item05  - 
#> reached maximum of 1024 iterations
#> Variable  6 :  item06  - 
#> reached maximum of 1024 iterations
#> 
#> Arranging data to match correlations
#> 
#> Successfully generated correlated variables
#> 


### behavioural intention
intent <- lfast(n, mean = 3.0, sd = 3, lowerbound = 0, upperbound = 10) |>
  data.frame()
#> reached maximum of 1024 iterations
names(intent) <- "int"


### target scale correlation matrix
scale_cors <- matrix(
  c(
    1.0, 0.6, 0.5, 0.3,
    0.6, 1.0, 0.4, 0.2,
    0.5, 0.4, 1.0, 0.1,
    0.3, 0.2, 0.1, 1.0
  ),
  nrow = 4
)

data_frames <- list("A1" = Att_1, "A2" = Att_2, "A3" = Att_3, "Int" = intent)


### apply the function
my_correlated_scales <- correlateScales(
  dataframes = data_frames,
  scalecors = scale_cors
)
#> scalecors  is positive-definite
#> 
#> New dataframe successfully created
head(my_correlated_scales)
#>   A1_1 A1_2 A1_3 A1_4 A2_1 A2_2 A2_3 A2_4 A2_5     A3_1     A3_2     A3_3
#> 1 2.25 1.50 1.75 2.00  1.0  1.6  1.2  1.8  1.0 1.166667 1.000000 1.666667
#> 2 4.25 4.00 4.50 4.75  4.4  3.2  3.8  4.4  5.0 2.833333 2.666667 4.500000
#> 3 1.75 3.00 3.00 4.25  2.4  1.8  2.0  2.8  2.6 3.500000 1.166667 3.833333
#> 4 1.75 2.00 3.00 3.25  2.2  2.6  2.8  1.6  3.2 2.666667 4.166667 3.166667
#> 5 1.25 1.75 2.00 2.00  1.4  1.2  1.6  2.6  1.8 2.333333 3.000000 2.166667
#> 6 1.25 2.25 2.75 4.25  2.0  2.4  2.2  2.6  4.4 3.000000 4.666667 3.166667
#>       A3_4     A3_5     A3_6 Int_1
#> 1 1.000000 2.333333 1.333333     1
#> 2 5.000000 3.666667 5.000000     4
#> 3 4.666667 1.833333 5.000000     8
#> 4 5.000000 5.000000 3.666667     2
#> 5 3.000000 2.500000 1.000000     2
#> 6 2.166667 3.833333 3.166667     9