## Setting up item names for fake data
G_names <- c(paste0('GP', 1:7),
paste0('GS', 1:7),
paste0('GE', 1:6),
paste0('GF', 1:7))
AC_names1 <- c('ES1', 'ES2', 'ES3', 'ES4', 'ES5', 'ES6', 'ES7', 'ES8', 'ES9', 'ES10',
'An9', 'O2', 'C5', 'An10', 'Tax1', 'ES11', 'ES12', 'ES13', 'BRM1')
AC_names2 <- c('ES1', 'ES2', 'ES3', 'ES4', 'ES5', 'ES6', 'ES7', 'ES8', 'ES9', 'ES10',
'An9', 'O2', 'C5', 'An10', 'Tax1', 'ES11', 'ES12', 'ES13', 'BRM1',
'GP1', 'GP2', 'GE4', 'GF5')
AC_names3 <- c('B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9')
AC_names <- c(AC_names1, AC_names2, AC_names3)
itemNames <- c(G_names, AC_names)
## Generating random item responses for 8 fake respondents
set.seed(6375309)
exampleDat <- t(replicate(8, sample(0:4, size = length(itemNames), replace = TRUE)))
## Making half of respondents missing about 10% of items,
## half missing about 50%.
miss10 <- t(replicate(4, sample(c(0, 9), prob = c(0.9, 0.1),
size = length(itemNames), replace = TRUE)))
miss50 <- t(replicate(4, sample(c(0, 9), prob = c(0.5, 0.5),
size = length(itemNames), replace = TRUE)))
missMtx <- rbind(miss10, miss50)
## Using 9 as the code for missing responses
exampleDat[missMtx == 9] <- 9
exampleDat <- as.data.frame(cbind(ID = paste0('ID', 1:8),
as.data.frame(exampleDat)))
names(exampleDat) <- c('ID', itemNames)
## Returns data frame with scale scores and with original items untouched
scoredDat <- scoreFACT_ES_BCS(exampleDat)
names(scoredDat)
scoredDat
## Returns data frame with scale scores, with the appropriate items
## reverse scored, and with item values of 8 and 9 replaced with NA.
## Also illustrates the effect of setting keepNvalid = TRUE.
scoredDat <- scoreFACT_ES_BCS(exampleDat, updateItems = TRUE, keepNvalid = TRUE)
names(scoredDat)
## Descriptives of scored scales
summary(scoredDat[, c('PWB', 'SWB', 'EWB', 'FWB', 'FACTG',
'ESS_19', 'ESS_23', 'BCS', 'FACT_ES_TOTAL', 'FACT_B_TOI')])
Run the code above in your browser using DataLab