---
title: "Lab Assignment 4"
subtitle: "Text Data"
author: [Your name here]
date: today
format:
  html:
    theme: cosmo
    toc: true
    toc-location: left
    toc-depth: 2
    highlight-style: github
    code-copy: true
    embed-resources: true
    df-print: paged
    fig-align: center
    fig-width: 8
    fig-height: 6
execute:
  message: false
editor: visual
---

*Note: The header at the top of this file has a bunch of settings to make your rendered html look nice. Don't worry about it (just add your name)!*

## Instructions

Welcome to your fourth lab assignment! It's time to analyze some text on your own. Hoooooray! Please read the following instructions before you begin.

For your lab assignments, you may use AI to help you code. You will be required to explicitly fill out the **SPARCK** framework for each question. You may not use AI to write the non-coding parts of the framework, although you may consult it for assistance.

As you write code, think about what we've learned in class so far, and especially what we learned in the Text Data tutorial. You don't need to exclusively use functions we used in class, but they should serve as a reminder of the types of things you might wish to do. Except where noted, a version of everything you need to do can be found in our tutorials.

You should guide me through the answer the questions. This means you shouldn't just copy and paste code from AI and be done with it; you should intersperse code and plain text throughout so that you can clearly explain what your code is doing.

Remember that you must render your Quarto document into an html file. This html is what you should hand in on Canvas. If you encounter errors, use AI to help you troubleshoot!

**Make sure you put this Quarto file in your Week 6 folder, and then open the file from that folder so that R's working directory is your Week 6 folder.**

**S: Set up** (What is your end goal?)

**P: Pseudocode** (What are the steps?)

**A: AI** (What are your AI prompts?)

**R: Run** (Run it!)

**C: Check** (Did your code work?)

**K: Knowledge** (What does your code mean?)

### Graph-specific instructions

For all graphs you make, they should look good. This means they have clear labels (including a title and axis titles that a normal human can understand), pretty colors, and bars in a sensible order. Essentially, if you were giving a talk on this topic, your graph should look like it belongs in your presentation.

## Question 0: The data

For this homework, you will be exploring **National Science Foundation (NSF) grants that were terminated by the federal government in April 2025**. The NSF is a federal agency that funds scientific research. Many of these grants were targeted because they focused on diversity, equity, and inclusion (DEI). The dataset comes from TidyTuesday, a weekly project that shares datasets for people learning R. I have provided the dataset for you on our [course website](https://psyc1495f26.netlify.app/lab_tutorials). You will need to download the data and put it in your R Project folder. You can learn more about the dataset, including what every column means, [here](https://github.com/rfordatascience/tidytuesday/tree/main/data/2025/2025-05-06).

Each row is one terminated grant. The dataset has columns with text that can be analyzed (`project_title` and `abstract`) and a bunch of columns that give more information about the grant.

Load the packages you'll need, and load in the data. Be sure to take a look at it before you get started.

```{r}

```

## Question 1: Keywords

Choose a **word or set of words** that you think is interesting, and use `stringr` functions to find out how many grants mention it. Then make a graph that compares the **percentage** of grants that mention it across a group of your choice (i.e. directorates, or grants that were/weren't in the Cruz database).

Make sure your search catches the different ways that a word can be written. Think about capitalization, different forms of the same word ("diverse" and "diversity"), and different spellings ("underrepresented" and "under-represented"). You might need `|` or another regular expression.

**S:**

**P:**

**A:**

**R:**

```{r}

```

(You may insert as many code chunks as you'd like. You can do so by going to Insert \> Executable Cell \> R in the toolbar above.)

**C:**

**K:**

**Explain what your graph shows.**

## Question 2: Word frequency

Use `tidytext` to find the **most common words** in the grants, and make a graph that compares the most common words across **two or more groups** of your choice. You can analyze either the `abstract` or the `project_title` column.

Remove stop words before you count. You should also make your own list of **custom stop words**: words that are common in grant abstracts but don't tell you much about what makes the grants different. (Hint: Make a graph of the most common words first, and look at which words you'd want to remove.)

**S:**

**P:**

**A:**

**R:**

```{r}

```

(You may insert as many code chunks as you'd like. You can do so by going to Insert \> Executable Cell \> R in the toolbar above.)

**C:**

**K:**

**Explain what your graph shows. Which words did you add to your custom stop words?**

## Question 3: Sentiment

For this question, calculate a sentiment score for each grant, and make a graph that compares the average score across a group of your choice. Before you do, make a graph of the most common positive and negative words in the abstracts, and decide whether you need to remove any words from the dictionary. You can use "bing" (like we did in class), or try "afinn" or "nrc."

**S:**

**P:**

**A:**

**R:**

```{r}

```

(You may insert as many code chunks as you'd like. You can do so by going to Insert \> Executable Cell \> R in the toolbar above.)

**C:**

**K:**

**Explain what your graph shows.**
