collapse: A function to collapse questionnaire items before item...

Description Usage Arguments Details Value Examples

View source: R/collapse.R

Description

This function takes a vector (v) of response category data, looks for response categories that are sparse (e.g., with fewer that 5 responses in the data set). Starting with the least frequent responses, the function will merge them to an adjacent category. The function will merge the whichever adjacent response category has fewer responses; in the case of ties the function will follow a user-specified preference (right or left). The parameters "prefer" and "merge_first" may result in slightly different patterns of merging depending on the pattern of sparse response categories.

Usage

1
2
3
4
5
6
7
collapse(
  v,
  min_f = 5,
  prefer = c("left", "right"),
  merge_first = c("leftmost", "rightmost"),
  warn_me = TRUE
)

Arguments

v

A vector of questionnaire response data. These are assumed to be ordered numeric data. v should be passed to collapse() as numeric data rather than as factors or character data. collapse() will attempt to sort and merge whatever data passed to it though.

min_f

The minimum frequency desired for response category data. Any response categories that have a frequency count less than min_f will be merged with an adjacent response category. The default value is 5. If it is set to a higher number, then each category will have more data, but collapse() will be more likely to merge. A lower number will mean that collapse() is less likely to merge, but fewer observations will be in each resultant category.

prefer

In cases where a sparse response category has a left and right adjacent response category of equal frequency, does the user prefer to merge with the category on the left or on the right? This is only relevant for response categories in the middle (e.g., for 1, 2, 3, 4, 5, this is only relevant for 2, 3, and 4).

merge_first

collapse() begins by merging the least frequent response category. If there is more than one response category with the lowest frequency count, does the user prefer starting with the leftmost or rightmost one?

warn_me

collapse() will provide warnings about the collapsing, especially when it is important to check the results, or to tell the user if there is a potential problem with the data. The warnings can be turned off using this parameter.

Details

NA is not considered a response type. It is not recommended to recode NA into a response type for this function.

Value

A vector of merged response data. If the function is unable to collapse the data, then the original data are returned (usually with a warning if there is potentially a problem with the data).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## Not run: 

sparse_vector <- c(1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4, 5, 5)
collapse(sparse_vector)

table(sparse_vector)
table(collapse(sparse_vector))


## End(Not run)

jameswgriffith/jamie documentation built on Dec. 20, 2021, 9:03 p.m.