## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6,
                      fig.height = 2.8, dpi = 96)

## ----setup--------------------------------------------------------------------
library(FactorialSurvey)

## ----design-------------------------------------------------------------------
attrs <- list(
  main  = c("grilled fish", "teriyaki chicken", "tofu hamburger"),
  side  = c("no", "yes"),
  info  = c("no", "yes"),
  price = c(700, 900, 1100, 1300)
)

design <- fs_defficient(attrs, n_vignettes = 16, n_blocks = 2,
                        seed = 1, verbose = FALSE)
design

## ----efficiency---------------------------------------------------------------
fs_efficiency(design)

## ----assign-------------------------------------------------------------------
design <- fs_vignette_text(
  design,
  "A {{main}} lunch box, side dish: {{side}}, nutrition label: {{info}}, ",
  column = "part1"
)
schedule <- fs_assign(design, n_respondents = 200, seed = 1)
head(schedule[, c("respondent", "block", "position", "main", "price")])

## ----power--------------------------------------------------------------------
pw <- fs_power(
  design, n_respondents = c(50, 150),
  coef = c("(Intercept)" = 1, "mainteriyaki chicken" = 0.7,
           "maintofu hamburger" = 0.1, sideyes = 0.3, infoyes = 0.15,
           price = -0.0015),
  formula = ~ main + side + info + price, price = "price",
  n_sim = 50, seed = 1, verbose = FALSE
)
pw[pw$term %in% c("sideyes", "infoyes"), ]

## ----data---------------------------------------------------------------------
head(bento)

## ----fit----------------------------------------------------------------------
fit <- fs_betareg(rating ~ main + side + info + price, data = bento,
                  cluster = ~ respondent, compute_me = FALSE)
summary(fit)

## ----mwtp---------------------------------------------------------------------
w <- fs_mwtp(fit, price = "price")
w
plot(w)

## ----ewtp---------------------------------------------------------------------
boxes <- data.frame(
  main = c("grilled fish", "teriyaki chicken", "teriyaki chicken"),
  side = c(0, 0, 1),
  info = c(0, 0, 1),
  row.names = c("Fish, plain", "Chicken, plain", "Chicken, full")
)
e <- fs_ewtp(fit, boxes, price = "price")
e
plot(e) + ggplot2::labs(x = "Expected willingness to pay (yen)")

## ----logit--------------------------------------------------------------------
yesno <- fs_simulate_ratings(
  design, n_respondents = 300,
  coef = c("(Intercept)" = 2, "mainteriyaki chicken" = 0.7,
           "maintofu hamburger" = 0.1, sideyes = 0.3, infoyes = 0.1,
           price = -0.002),
  formula = ~ main + side + info + price, response = "binary",
  name = "buy", seed = 2
)
lfit <- fs_logit(buy ~ main + side + info + price, data = yesno,
                 cluster = ~ respondent, compute_me = FALSE)
fs_mwtp(lfit, price = "price")

