Skip to contents

This function takes the results of list_model() and returns predictions for a new data set.

Usage

list_predict(
  model,
  newdata,
  weights_df = NULL,
  weights_var = NULL,
  hdi = 0.95,
  higher = 0.03,
  id = "response_id"
)

Arguments

model

Object – result of list_model().

newdata

A data frame with data to predict. Data needs to be dummy coded.

weights_df

Data frame containing id and weight. If not provided, the weights will be set to 1.

weights_var

Character name of column storing weights.

hdi

Numeric (1) in range (0, 1). Default 0.95.

higher

Numeric (1) in range (0, 1). Default 0.03.

id

Character (1). Variable name containing ids. Default "response_id".

Value

A list with the following three elements:

  • predicted overall means – a tibble containing predicted mean probabilities of public and private opinion, their difference and N of the new sample;

  • predicted_prob_by_group - A tibble with means of predicted probabilities (public, private and their difference), along with Ns, by demographic groups.

  • predicted_stat_by_group - A tibble with HDI, probability that the difference of private and public is higher than a threshold (default 0.03) and that the difference from Overall difference is higher or lower than the threshold.

Examples

library("dplyr") 
## Create a data frame with demographic variables
### We select 3 categorical variables as demographic breaks and one artificial
### numeric variable just for the purpose of demonstration.

demo_toy <- list_experiment_toy[1:300, ] %>%
  select("response_id", "d_party", "s_gender", "s_region") %>%
  mutate(num_var = rnorm(nrow(.), 3, 0.4))

## Create a data frame holding weights
  
demo_weights <- list_experiment_toy %>%
  select("response_id", "weights")

## Tidy the asserted data

list_data <- list_read_data_bulk(list_experiment_toy[1:300, ], "response_id")
list_data_asserted <- list_assert_data_bulk(
  list_data, id = "response_id", n_items = 4L, demo = demo_toy
)
#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ── Asserting data for `bundle_1` ────────────────────────────────────────────────────────────────────────────────────────────
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting Identifiers
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting groups
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting direct question
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting response question
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting demo variables
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Demo is data.frame with `response_id` column
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ✖ 21 respondents have at least one missing value in break variables - dropping them.
#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ── Asserting data for `bundle_2` ────────────────────────────────────────────────────────────────────────────────────────────
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting Identifiers
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting groups
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting direct question
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting response question
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ✖ 4 respondents from control group have response values that should not be available (other than integers from 0 to n_item) - dropping them.
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting demo variables
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Demo is data.frame with `response_id` column
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ✖ 21 respondents have at least one missing value in break variables - dropping them.
#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ── Asserting data for `bundle_3` ────────────────────────────────────────────────────────────────────────────────────────────
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting Identifiers
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting groups
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting direct question
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting response question
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Asserting demo variables
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ℹ Demo is data.frame with `response_id` column
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ✖ 21 respondents have at least one missing value in break variables - dropping them.
#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

list_data_tidy <- list_tidy_data_bulk(list_data_asserted, "response_id")

## Run Models

### Pass the tidied data to list_model or list_model_bulk() functions.

if (FALSE) { # \dontrun{
list_model_toy_1 <- list_model(
  list_data_tidy[[1]], seed = 666, iter = 200, chains = 1
)
list_model_toy_2 <- list_model(
  list_data_tidy[[2]], seed = 666, iter = 200, chains = 1
)
 list_model_toy_3 <- list_model(
  list_data_tidy[[3]], seed = 666, iter = 200, chains = 1
)
} # }

### After running model you are able to extract the statistics

### The list_model_bulk() function stores the results in models directory.
### To simulate it lets create models directory and save there models mockups.
### The file names needs to end with "bundle_X.rds", where X is the number of
### the bundle.

out_dir <- file.path(tempdir(), "g6tr.list")
if (!dir.exists(out_dir)) dir.create(out_dir, recursive = TRUE)

data("list_model_toy_1")
data("list_model_toy_2")
data("list_model_toy_3")
saveRDS(list_model_toy_1, file.path(out_dir, "list_model_bundle_1.rds")) 
saveRDS(list_model_toy_2, file.path(out_dir, "list_model_bundle_2.rds"))
saveRDS(list_model_toy_3, file.path(out_dir, "list_model_bundle_3.rds"))

list_data_tidy_new <- list_data_tidy
list_data_tidy_new$bundle_1$grid <- list_data_tidy_new$bundle_1$grid[1:50, ]
list_data_tidy_new$bundle_1$demo <- list_data_tidy_new$bundle_1$demo[1:50, ]

model_pred <- list_predict(list_model_toy_1, list_data_tidy_new$bundle_1)
#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ── Making List Experiment Predictions ──
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ✔ All predictors columns present in new dataset.
#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating private predictions

#> 
✔ Average private predictions ready! [4ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating public predictions

#> 
✔ Average public predictions ready! [8ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating delta predictions

#> 
✔ Average delta predictions ready! [7ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> Joining with `by = join_by(response_id, num_var)`
#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ── Calcualting Average group probabilities ──
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating public probabilities for: `d_party`

#> 
✔ Public probabilities for: `s_gender` ready! [5ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating public probabilities for: `s_gender`

#> 
✔ Public probabilities for: `s_region` ready! [10ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating public probabilities for: `s_region`

#> 
✔ Public probabilities for: `d_party` ready! [11ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating private probabilities for: `d_party`

#> 
✔ Private probabilities for: `s_gender` ready! [12ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating private probabilities for: `s_gender`

#> 
✔ Private probabilities for: `s_region` ready! [11ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating private probabilities for: `s_region`

#> 
✔ Private probabilities for: `d_party` ready! [11ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating delta probabilities for: `d_party`

#> 
✔ Delta probabilities for: `s_gender` ready! [11ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating delta probabilities for: `s_gender`

#> 
✔ Delta probabilities for: `s_region` ready! [12ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating delta probabilities for: `s_region`

#> 
✔ Delta probabilities for: `s_region` ready! [23ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m


# Additionally we can pass additional data frame with weights for new
# data set.

model_pred_weighted <- list_predict(
  model = list_model_toy_1, 
  newdata = list_data_tidy_new$bundle_1, 
  weights_df = demo_weights, 
  weights_var = "weights"
) 
#> 

#> ── Making List Experiment Predictions ──
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ✔ All predictors columns present in new dataset.
#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating private predictions

#> 
✔ Average private predictions ready! [4ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating public predictions

#> 
✔ Average public predictions ready! [8ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating delta predictions

#> 
✔ Average delta predictions ready! [7ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> Joining with `by = join_by(response_id, num_var)`
#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> ── Calcualting Average group probabilities ──
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating public probabilities for: `d_party`

#> 
✔ Public probabilities for: `s_gender` ready! [5ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating public probabilities for: `s_gender`

#> 
✔ Public probabilities for: `s_region` ready! [10ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating public probabilities for: `s_region`

#> 
✔ Public probabilities for: `d_party` ready! [10ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating private probabilities for: `d_party`

#> 
✔ Private probabilities for: `s_gender` ready! [10ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating private probabilities for: `s_gender`

#> 
✔ Private probabilities for: `s_region` ready! [10ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating private probabilities for: `s_region`

#> 
✔ Private probabilities for: `d_party` ready! [10ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating delta probabilities for: `d_party`

#> 
✔ Delta probabilities for: `s_gender` ready! [10ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating delta probabilities for: `s_gender`

#> 
✔ Delta probabilities for: `s_region` ready! [10ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m

#> 
ℹ Calculating delta probabilities for: `s_region`

#> 
✔ Delta probabilities for: `s_region` ready! [22ms]

#> 
#> Building packages ■■■■■■■■■■■■■■■■■■■■■             67% | ETA:  4m