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_responseobject. Useas.character(resp)for text, andLLMR::tokens(resp)andLLMR::finish_reason(resp)for usage/metadata. - The legacy
json = TRUEargument was removed fromLLMR::call_llm()andLLMR::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 omitapi_keyinLLMR::llm_config(...)and LLMR will read it automatically.
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.
Question Analysis
# Analyze question patterns
questions <- result$focus_group$analyze_question_patterns()
print(questions$question_patterns)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
)Best Practices
- Always start with a clear research question
- Use appropriate number of participants (typically 5-8)
- Set realistic duration for the simulation
- Review and adjust agent profiles based on your research needs
- Use multiple analysis methods to get a comprehensive understanding
- 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.
