data("Adult")
## Example 1: Manual decoding
## Extract the item coding as a vector of item labels.
iLabels <- itemLabels(Adult)
head(iLabels)
## get undecoded list (itemIDs)
list <- LIST(Adult[1:5], decode = FALSE)
list
## decode itemIDs by replacing them with the appropriate item label
decode(list, itemLabels = iLabels)
## Example 2: Manually create an itemMatrix using iLabels as the common item coding
data <- list(
c("income=small", "age=Young"),
c("income=large", "age=Middle-aged")
)
# Option a: encode to match the item coding in Adult
iM <- encode(data, itemLabels = Adult)
iM
inspect(iM)
compatible(iM, Adult)
# Option b: coercion plus recode to make it compatible to Adult
# (note: the coding has 115 item columns after recode)
iM <- as(data, "itemMatrix")
iM
compatible(iM, Adult)
iM <- recode(iM, itemLabels = Adult)
iM
compatible(iM, Adult)
## Example 3: use recode to make itemMatrices compatible
## select first 100 transactions and all education-related items
sub <- Adult[1:100, itemInfo(Adult)$variables == "education"]
itemLabels(sub)
image(sub)
## After choosing only a subset of items (columns), the item coding is now
## no longer compatible with the Adult dataset
compatible(sub, Adult)
## recode to match Adult again
sub.recoded <- recode(sub, itemLabels = Adult)
image(sub.recoded)
## Example 4: manually create 2 new transaction for the Adult data set
## Note: check itemLabels(Adult) to see the available labels for items
twoTransactions <- as(
encode(list(
c("age=Young", "relationship=Unmarried"),
c("age=Senior")
), itemLabels = Adult),
"transactions"
)
twoTransactions
inspect(twoTransactions)
## the same using the transactions constructor function instead
twoTransactions <- transactions(
list(
c("age=Young", "relationship=Unmarried"),
c("age=Senior")
),
itemLabels = Adult
)
twoTransactions
inspect(twoTransactions)
## Example 5: Use a common item coding
# Creation of transactions separately will produce different item codings
trans1 <- transactions(
list(
c("age=Young", "relationship=Unmarried"),
c("age=Senior")
)
)
trans1
trans2 <- transactions(
list(
c("age=Middle-aged", "relationship=Married"),
c("relationship=Unmarried", "age=Young")
)
)
trans2
compatible(trans1, trans2)
# produce common item coding (all item labels in the two sets)
commonItemLabels <- union(itemLabels(trans1), itemLabels(trans2))
commonItemLabels
trans1 <- recode(trans1, itemLabels = commonItemLabels)
trans1
trans2 <- recode(trans2, itemLabels = commonItemLabels)
trans2
compatible(trans1, trans2)
## Example 6: manually create a rule using the item coding in Adult
## and calculate interest measures
aRule <- new("rules",
lhs = encode(list(c("age=Young", "relationship=Unmarried")),
itemLabels = Adult
),
rhs = encode(list(c("income=small")),
itemLabels = Adult
)
)
## shorter version using the rules constructor
aRule <- rules(
lhs = list(c("age=Young", "relationship=Unmarried")),
rhs = list(c("income=small")),
itemLabels = Adult
)
quality(aRule) <- interestMeasure(aRule,
measure = c("support", "confidence", "lift"), transactions = Adult
)
inspect(aRule)
Run the code above in your browser using DataLab