Skip to contents

Introduction

The FocusGroup package provides tools for simulating and analyzing focus group conversations. This vignette demonstrates how to use the package to create, run, and analyze focus group simulations. The package uses FGAgents as the agents who participate in a focus group study; these agents can be based on various LLM models potentially from different providers; see LLMR documentation for which providers have built-in support and how to add new ones. One FGAgent is always the moderator.

LLMR 0.6.0 compatibility notes

  • Generative calls return an llmr_response object. Use as.character(resp) for text, and LLMR::tokens(resp) and LLMR::finish_reason(resp) for usage/metadata.
  • The legacy json = TRUE argument was removed from LLMR::call_llm() and LLMR::call_llm_robust(). You do not need to set it; FocusGroup handles responses correctly without it.
  • Recommended: set your provider API key in an environment variable (e.g., OPENAI_API_KEY). You can omit api_key in LLMR::llm_config(...) and LLMR will read it automatically.

Installation

# Install from GitHub
remotes::install_github("asanaei/FocusGroup")

Running a Simple Focus Group

Let’s start by running a focus group with 5 participants using the high-level wrapper. Note that you need to set your OpenAI API key in the environment variable OPENAI_API_KEY:

# Set your OpenAI API key (or set it in your environment)
Sys.setenv(OPENAI_API_KEY = "your-api-key-here")

# Run a focus group with 5 participants
result <- run_focus_group(
  topic = "Impact of Social Media on Mental Health",
  participants = 5
)

# View the focus group configuration
print(result$focus_group)

Viewing the Conversation

# View the conversation
head(result$conversation)

Analyzing the Results

The package provides several analysis functions to help understand the conversation:

Basic Analysis

# Get basic statistics
stats <- analyze_focus_group(result)
print(stats$basic_stats)

Text Analysis

Several analyses are methods on the FocusGroup object itself, reached through result$focus_group.

# Readability scores per speaker
readability <- result$focus_group$analyze_readability()
print(readability)

# Response patterns and interaction behaviors
patterns <- result$focus_group$analyze_response_patterns()
print(patterns$response_metrics)

Question Analysis

# Analyze question patterns
questions <- result$focus_group$analyze_question_patterns()
print(questions$question_patterns)

Visualization

The FocusGroup object provides several plotting methods:

# Participation timeline
participation_plot <- result$focus_group$plot_participation_timeline()
print(participation_plot)

# Readability by speaker (a table, not a plot)
print(result$focus_group$analyze_readability())

Advanced Usage

Custom Agent Profiles and Manual Construction

You can create custom agent profiles and manually construct a FocusGroup object:

# Set your OpenAI API key (or set it in your environment)
Sys.setenv(OPENAI_API_KEY = "your-api-key-here")

# Create diverse agents. `demographics` is a data frame with one row per
# participant.
agents <- create_diverse_agents(
  n_participants = 3,
  demographics = data.frame(
    age = c(25, 35, 45),
    gender = c("male", "female", "non-binary"),
    education = c("high school", "bachelor's", "master's"),
    stringsAsFactors = FALSE
  )
)

# Name the agents by their IDs
agents_named <- setNames(agents, sapply(agents, function(a) a$id))

# Find moderator ID (create_diverse_agents adds a moderator with ID "MOD")
moderator_id <- "MOD"

# Create a conversation flow object
flow_obj <- DesireBasedFlow$new(agents_named, moderator_id)

# Create the FocusGroup object
fg <- FocusGroup$new(
  topic = "Impact of Social Media on Mental Health",
  purpose = "To explore perspectives and experiences related to social media.",
  agents = agents_named,
  moderator_id = moderator_id,
  turn_taking_flow = flow_obj
)

Custom Analysis

You can perform custom analysis using the conversation data:

# Extract conversation data
conv_data <- result$conversation

# Custom analysis example
word_counts <- sapply(conv_data$text, function(x) length(strsplit(x, "\\s+")[[1]]))
summary(word_counts)

Best Practices

  1. Always start with a clear research question
  2. Use appropriate number of participants (typically 5-8)
  3. Set realistic duration for the simulation
  4. Review and adjust agent profiles based on your research needs
  5. Use multiple analysis methods to get a comprehensive understanding
  6. Remember to set your API key in the environment variable like OPENAI_API_KEY

Conclusion

The FocusGroup package provides a powerful toolkit for simulating and analyzing focus group conversations. By following this vignette, you should be able to:

  • Create and configure focus groups
  • Run simulations with different parameters
  • Analyze conversations using various methods
  • Visualize results effectively
  • Customize the analysis to your specific needs

For more information, please refer to the package documentation and examples.