Blog
R and Python Together: Refactoring and Prompt Engineering A Previous Case Study, Using the Perplexity API
I wrote a post last year looking at how to employ tools in LangChain to have GPT-3.5 Turbo access information on the web, outside of its training data.
The purpose of the present post is to revisit this post, improving the poor performance I saw there through refactoring and prompt engineering.
Background
The motivating example is again using large language models (LLMs) to help me calculate features for my Oscar model. Specifically: How many films did the director of a Best Picture nominee direct before the nominated film?
Rethinking How I Do Supervised Topic Modeling, Using ModernBERT and GPT-4o mini
I wrote a post in July 2023 describing my process for building a supervised text classification pipeline. In short, the process first involves reading the text, writing a thematic content coding guide, and having humans label text. Then, I define a variety of ways to pre-process text (e.g., word vs. word-and-bigram tokenizing, stemming vs. not, stop words vs. not, filtering on the number of times a word had to appear in the corpus) in a workflowset. Then, I run these different pre-processors through different standard models: elastic net, XGBoost, random forest, etc. Each class of text has its own model, so I would run this pipeline five times if there were five topics in the text. Importantly, this is not natural language processing (NLP), as it was a bag-of-words approach.
The idea was to leverage the domain knowledge of the experts on my team through content coding, and then scaling it up using a machine learning pipeline. In the post, I bemoaned how most of the “NLP” or “AI-driven” tools I had tested did not do very well. The tools I was thinking of were all web-based, point-and-click applications that I had tried out since about 2018, and they usually were unsupervised.
We are in a wildly different environment now when it comes to analyzing text than we were even a few years ago. I am revisiting that post to explore alternate routes to classifying text. I will use the same data as I did in that post: 720 Letterboxd reviews of Wes Anderson’s film Asteroid City. There is only one code: Did the review discuss Wes Anderson’s unique visual style (1) or not (0)? I hand-labeled all of these on one afternoon to give me a supervised dataset to play with.
Archive
-
2026
- Jan 12, 2026 Predicting Best Picture at the 2026 Academy Awards Jan 12, 2026
-
2024
- Dec 30, 2024 R and Python Together: Refactoring and Prompt Engineering A Previous Case Study, Using the Perplexity API Dec 30, 2024
- Dec 21, 2024 Rethinking How I Do Supervised Topic Modeling, Using ModernBERT and GPT-4o mini Dec 21, 2024
- Dec 1, 2024 Predicting Best Picture at the 2025 Academy Awards Dec 1, 2024
- Apr 14, 2024 R and Python Together: A Second Case Study Using LangChain's LLM Tools Apr 14, 2024
- Mar 24, 2024 Using R and Python Together, Seamlessly: A Case Study Using OpenAI's GPT Models Mar 24, 2024
- Mar 3, 2024 Modeling the Oscar for Best Picture (and Some Insights About XGBoost) Mar 3, 2024
-
2023
- Jul 20, 2023 Supervised Topic Modeling for Short Texts: My Workflow and A Worked Example Jul 20, 2023
-
2022
- Oct 1, 2022 Probabilistic Photograph Manipulation with ggplot2 and imager Oct 1, 2022
- May 6, 2022 Color-Swapping Film Palettes in R with imager, ggplot2, and kmeans May 6, 2022
-
2021
- Sep 21, 2021 Why and How to Model Conditional Variance, with an Application to My Letterboxd Data Sep 21, 2021
- Apr 29, 2021 Exploring the Star Wars "Prequel Renaissance" Using tidymodels and workflowsets Apr 29, 2021
-
2020
- May 17, 2020 Quickly Making an R Shiny Bingo App May 17, 2020
- Apr 16, 2020 Examining Aphex Twin's Eclectic Discography With the Spotify API and Generalized Variance Apr 16, 2020
- Apr 5, 2020 A Function for Calculating Tidy Summaries of Multiple t-tests Apr 5, 2020
- Mar 20, 2020 Introducing bwsTools: A Package for Case 1 Best-Worst Scaling (MaxDiff) Designs Mar 20, 2020
-
2019
- Jul 13, 2019 Simulating Data in R: Examples in Writing Modular Code Jul 13, 2019
- May 30, 2019 Star Wars Fandom Survey, Part 5: Importance of Movie Characteristics May 30, 2019
- May 30, 2019 Star Wars Fandom Survey, Part 4: Age and Nostalgia May 30, 2019
- May 30, 2019 Star Wars Fandom Survey, Part 3: Sexism and Political Attitudes May 30, 2019
- May 30, 2019 Star Wars Fandom Survey, Part 2: The Three Major Types of Star Wars Fans May 30, 2019
- May 30, 2019 Star Wars Fandom Survey, Part 1: Methods, Demographics, Validity Checks May 30, 2019
- Apr 28, 2019 How I Put Logos on ggplot2 Figures Apr 28, 2019
- Apr 9, 2019 Confidence Interval Coverage in Weighted Surveys: A Simulation Study Apr 9, 2019
- Jan 23, 2019 Using Word Similarity Graphs to Explore Themes in Text: A Tutorial Jan 23, 2019
-
2018
- Nov 25, 2018 How Do We Judge a Probabilistic Model? Or, How Did FiveThirtyEight Do in Forecasting the 2018 Midterms? Nov 25, 2018
- Oct 29, 2018 In Support of Open Seeding in the NBA, Pt. 2 Oct 29, 2018
- Oct 14, 2018 Explicitly Optimizing on Causal Effects via the Causal Random Forest: A Practical Introduction and Tutorial Oct 14, 2018
- Aug 20, 2018 How Big Should the Control Group Be in a Randomized Field Experiment? Aug 20, 2018
- Apr 14, 2018 On Calculating Power for Interactions in 2 x 2 Factorial Designs Apr 14, 2018
- Jan 5, 2018 The Force is Too Strong with This One? Sexism, Star Wars, and Female Heroes Jan 5, 2018
- Jan 1, 2018 "Ode to Viceroy": Mac DeMarco's Influence on Interest in Viceroy Cigarettes Jan 1, 2018
-
2017
- Dec 6, 2017 A Monte Carlo Study on Methods for Handling Class Imbalance in Machine Learning Dec 6, 2017
- Nov 28, 2017 Using Beta Regression to Better Model Norms in Political Psychology Nov 28, 2017
- Sep 24, 2017 In Support of Open Seeding in the NBA Sep 24, 2017
- Sep 22, 2017 The Importance of Blocks for NBA Defenses, Over Time Sep 22, 2017
- Jul 13, 2017 Analyzing Rudy Gay Trades Using the CausalImpact Package Jul 13, 2017
- Jun 6, 2017 Quantifying "Low-Brow" and "High-Brow" Films Jun 6, 2017
- May 20, 2017 Sentiment Analysis of Kanye West's Discography May 20, 2017
- Apr 18, 2017 Text Mining Kanye's Vocabulary Apr 18, 2017
- Mar 18, 2017 Political Targeting Using k-means Clustering Mar 18, 2017
- Mar 2, 2017 Predicting All-NBA Teams Mar 2, 2017