library(cbcTools)
# Create profiles for examples
profiles <- cbc_profiles(
price = c(1, 1.5, 2, 2.5, 3),
type = c('Fuji', 'Gala', 'Honeycrisp'),
freshness = c('Poor', 'Average', 'Excellent')
)
# Example 1: Simple fixed priors
priors_fixed <- cbc_priors(
profiles = profiles,
price = -0.25, # Negative = prefer lower prices
type = c(0.5, 1.0), # "Fuji" is reference level
freshness = c(0.6, 1.2) # "Poor" reference level
)
# Example 2: Named categorical priors (more explicit)
priors_named <- cbc_priors(
profiles = profiles,
price = -0.25,
type = c("Gala" = 0.5, "Honeycrisp" = 1.0), # "Fuji" is reference
freshness = c("Average" = 0.6, "Excellent" = 1.2) # "Poor" is reference
)
# Example 3: Random parameters - normal distributions for "price" and "freshness"
priors_random <- cbc_priors(
profiles = profiles,
price = rand_spec(
dist = "n",
mean = -0.25,
sd = 0.1
),
type = c(0.5, 1.0),
freshness = rand_spec(
dist = "n",
mean = c(0.6, 1.2),
sd = c(0.1, 0.1)
)
)
# Example 4: Correlated random parameters
priors_correlated <- cbc_priors(
profiles = profiles,
price = rand_spec(
dist = "n",
mean = -0.1,
sd = 0.05,
correlations = list(
cor_spec(
with = "type",
with_level = "Honeycrisp",
value = 0.3
)
)
),
type = rand_spec(
dist = "n",
mean = c("Gala" = 0.1, "Honeycrisp" = 0.2),
sd = c("Gala" = 0.05, "Honeycrisp" = 0.1)
),
freshness = c(0.1, 0.2)
)
# Example 5: With interaction terms
priors_interactions <- cbc_priors(
profiles = profiles,
price = -0.25,
type = c("Fuji" = 0.5, "Honeycrisp" = 1.0),
freshness = c("Average" = 0.6, "Excellent" = 1.2),
interactions = list(
# Price sensitivity varies by apple type
int_spec(
between = c("price", "type"),
with_level = "Fuji",
value = 0.1
),
int_spec(
between = c("price", "type"),
with_level = "Honeycrisp",
value = 0.2
),
# Type preferences vary by freshness
int_spec(
between = c("type", "freshness"),
level = "Honeycrisp",
with_level = "Excellent",
value = 0.3
)
)
)
# Example 6: Including no-choice option
priors_nochoice_fixed <- cbc_priors(
profiles = profiles,
price = -0.25,
type = c(0.5, 1.0),
freshness = c(0.6, 1.2),
no_choice = -0.5 # Negative values make no-choice less attractive
)
# Example 7: Random no-choice
priors_nochoice_random <- cbc_priors(
profiles = profiles,
price = -0.25,
type = c(0.5, 1.0),
freshness = c(0.6, 1.2),
no_choice = rand_spec(dist = "n", mean = -0.5, sd = 0.2)
)
# View the priors
priors_fixed
priors_random
Run the code above in your browser using DataLab