Project 2 by Cassidy Stauffer

A. Introduction

The United States’ economy has long been involved in the production of cotton. According to the USDA, the “United States is the world’s leading cotton exporter, providing approximately 35 percent of global cotton exports in recent years,” (U.S. Department of Agriculture, Economic Research Service, n.d.) an achievement the country has maintained almost since inception (National Cotton Council of America, n.d.). Although the US holds a large global stake in the cotton market, my research seeks to explore possible economic trends. The question driving my research is:

How have changes in global cotton prices affected U.S. cotton production over time?

I retreved my datasets directly from USDA and I used two in my research: Cotton Prices, and US Supply and Demand. These datasets are 7x65 and 16x68 respectively and include variables like marketing year, production (1,000 480-pound bales), export(1,000 480-pound bales), and price per pound.

Reference: U.S. Department of Agriculture, Economic Research Service. (n.d). Cotton and wool yearbook [Data set].

#import the libraries and data sets
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(dslabs)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.1     ✔ readr     2.2.0
## ✔ ggplot2   4.0.3     ✔ stringr   1.6.0
## ✔ lubridate 1.9.5     ✔ tibble    3.3.1
## ✔ purrr     1.2.2     ✔ tidyr     1.3.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)
## Warning: package 'readxl' was built under R version 4.6.1

B. Data Analysis

To address my question, my plan was to look at global cotton prices, US cotton production, and US cotton exports. I started by importing, reading, and cleaning all of the data I planned to use. I cleaned the data by dropping unnecessary rows, renaming columns, and extracting only the columns I needed. After I had cleaned both data frames, I merged them together by year.

I also needed to convert all of the columns that were strings into integers. When I created a scatter plot looking at global cotton prices and US production, I was issued a warning about NA values. I knew that the index_a_far_eastern column had NA values since there were several years where the data was unaccounted for. I removed the NA rows which narrowed down my scope of data to be between 2007/08 and 2024/25.

setwd("/Users/cassidystauffer/Documents/MCCC/Data 101/Week 10/project 2/All-cotton-yearbook-tables") #Set the working directory to the correct folder

cotton_prices <- read_xlsx("Cotton-Prices.xlsx", #Read the excel file
                           sheet = "Table14")
## New names:
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
cotton_prices
## # A tibble: 64 × 7
##    Table 14—Index of selected cotton price…¹ ...2  ...3  ...4  ...5  ...6  ...7 
##    <chr>                                     <chr> <chr> <chr> <chr> <chr> <chr>
##  1 <NA>                                      <NA>  Nort… <NA>  <NA>  Far … <NA> 
##  2 <NA>                                      <NA>  U.S.  <NA>  <NA>  U.S.  <NA> 
##  3 Marketing                                 A     Memp… U.S.  A     Memp… U.S. 
##  4 year                                      Inde… terr… CA/A… Inde… East… CA/A…
##  5 <NA>                                      <NA>  <NA>  <NA>  <NA>  <NA>  <NA> 
##  6 <NA>                                      Cent… <NA>  <NA>  <NA>  <NA>  <NA> 
##  7 1975/76                                   65.2… 71.41 NA    NA    NA    NA   
##  8 1976/77                                   81.75 82.47 83.05 NA    NA    NA   
##  9 1977/78                                   65.0… 65.25 66.52 NA    NA    NA   
## 10 1978/79                                   75.9… 75.9… 70.69 NA    NA    NA   
## # ℹ 54 more rows
## # ℹ abbreviated name:
## #   ¹​`Table 14—Index of selected cotton price quotation offerings, c.i.f. Northern Europe and c/f Far Eastern, annual, 1975/76–2024/25`
#clean up the table by getting rid of the last 4 columns

cotton_prices <- cotton_prices[-c(1,2,5:6,58:64), ] #get rid of rows

cotton_prices_clean <- cotton_prices %>% #rename columns
  rename(
    "year" = "Table 14—Index of selected cotton price quotation offerings, c.i.f. Northern Europe and c/f Far Eastern, annual, 1975/76–2024/25",
     "index_a_north_europe" = "...2",
     "index_a_far_eastern" = "...6")

cotton_prices_index_a <- cotton_prices_clean[, c("year", "index_a_north_europe", "index_a_far_eastern") ]

cotton_prices_index_a
## # A tibble: 53 × 3
##    year      index_a_north_europe index_a_far_eastern
##    <chr>     <chr>                <chr>              
##  1 Marketing A                    Memphis            
##  2 year      Index 1/             Eastern 4/         
##  3 1975/76   65.260000000000005   NA                 
##  4 1976/77   81.75                NA                 
##  5 1977/78   65.010000000000005   NA                 
##  6 1978/79   75.989999999999995   NA                 
##  7 1979/80   85.46                NA                 
##  8 1980/81   93.3                 NA                 
##  9 1981/82   73.760000000000005   NA                 
## 10 1982/83   76.650000000000006   NA                 
## # ℹ 43 more rows
setwd("/Users/cassidystauffer/Documents/MCCC/Data 101/Week 10/project 2/All-cotton-yearbook-tables")
#import the other dataset
us_supply_demand <- read_xlsx("US-Cotton-Supply-and-Demand.xlsx", #Read the excel file
                           sheet = "Table 1")
## New names:
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
## • `` -> `...13`
## • `` -> `...14`
## • `` -> `...15`
## • `` -> `...16`
us_supply_demand <- us_supply_demand[-c(1:6), ] #get rid of unnecessary rows

us_supply_demand_clean <- us_supply_demand %>% #rename columns
  rename(
    "year" = "Table 1—U.S. cotton supply and use, 1975/76–2025/26",
     "production" = "...7",
     "exports" = "...11")
#extract the year, production and export
us_production_export <- us_supply_demand_clean[,c("year", "production", "exports")]
#combine the data

df = merge(cotton_prices_index_a, us_production_export, by = "year")
df <- df %>%
  mutate(across(c("index_a_north_europe", "index_a_far_eastern", "production", "exports"),
                ~ .x %>%
                  str_remove_all("[,]")%>%
                  as.numeric))
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `across(...)`.
## Caused by warning in `index_a_north_europe %>% str_remove_all("[,]") %>% as.numeric`:
## ! NAs introduced by coercion
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
df
##       year index_a_north_europe index_a_far_eastern production exports
## 1  1975/76                65.26                  NA       8302    3311
## 2  1976/77                81.75                  NA      10581    4784
## 3  1977/78                65.01                  NA      14389    5484
## 4  1978/79                75.99                  NA      10856    6180
## 5  1979/80                85.46                  NA      14629    9229
## 6  1980/81                93.30                  NA      11122    5926
## 7  1981/82                73.76                  NA      15646    6567
## 8  1982/83                76.65                  NA      11963    5207
## 9  1983/84                87.61                  NA       7771    6786
## 10 1984/85                69.18                  NA      12982    6215
## 11 1985/86                48.90                  NA      13432    1960
## 12 1986/87                61.99                  NA       9731    6684
## 13 1987/88                72.26                  NA      14760    6582
## 14 1988/89                66.42                  NA      15411    6148
## 15 1989/90                82.34                  NA      12196    7694
## 16 1990/91                82.87                  NA      15505    7793
## 17 1991/92                62.90                  NA      17614    6646
## 18 1992/93                56.87                  NA      16218    5201
## 19 1993/94                70.75                  NA      16134    6862
## 20 1994/95                92.66                  NA      19662    9402
## 21 1995/96                85.61                  NA      17900    7675
## 22 1996/97                78.66                  NA      18942    6865
## 23 1997/98                72.11                  NA      18793    7500
## 24 1998/99                58.97                  NA      13918    4298
## 25 1999/00                52.85                  NA      16968    6750
## 26 2000/01                57.25                  NA      17188    6740
## 27 2001/02                41.88                  NA      20303   11000
## 28 2002/03                55.81                  NA      17209   11900
## 29 2003/04                69.24                  NA      18255   13758
## 30 2004/05                53.53                  NA      23251   14436
## 31 2005/06                57.10                  NA      23890   17673
## 32 2006/07                60.77                  NA      21588   12959
## 33 2007/08                74.40               74.37      19207   13634
## 34 2008/09                   NA               62.05      12825   13261
## 35 2009/10                   NA               81.56      12183   12037
## 36 2010/11                   NA              166.60      18102   14376
## 37 2011/12                   NA              104.45      15573   11714
## 38 2012/13                   NA               89.67      17314   13026
## 39 2013/14                   NA               94.54      12909   10530
## 40 2014/15                   NA               73.70      16319   11246
## 41 2015/16                   NA               75.35      12888    9153
## 42 2016/17                   NA               84.40      17170   14917
## 43 2017/18                   NA               90.11      20923   16281
## 44 2018/19                   NA               86.06      18367   14700
## 45 2019/20                   NA               72.39      19913   15300
## 46 2020/21                   NA               88.67      14608   16050
## 47 2021/22                   NA              135.51      17523   14050
## 48 2022/23                   NA              101.78      14468   12450
## 49 2023/24                   NA               94.71      12066   11750
## 50 2024/25                   NA               80.02      14413   11900
df %>% #find the mean for all numeric columns
  summarise(across(where(is.numeric), ~ mean(.x, na.rm = TRUE)))
##   index_a_north_europe index_a_far_eastern production exports
## 1             69.39727            91.99667    15677.6  9651.8
library(ggplot2)
ggplot(data = df, aes(x = index_a_far_eastern, y = production)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE) + 
  labs(
    title = "Global Price vs. US Production of Cotton",
    x = "Global Price",
    y = "US Production",
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 32 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 32 rows containing missing values or values outside the scale range
## (`geom_point()`).

sum(is.na(df$index_a_far_eastern)) #total na values in index_a_far_eastern column
## [1] 32
df %>% #filter out na values
  filter(!is.na(index_a_far_eastern))
##       year index_a_north_europe index_a_far_eastern production exports
## 1  2007/08                 74.4               74.37      19207   13634
## 2  2008/09                   NA               62.05      12825   13261
## 3  2009/10                   NA               81.56      12183   12037
## 4  2010/11                   NA              166.60      18102   14376
## 5  2011/12                   NA              104.45      15573   11714
## 6  2012/13                   NA               89.67      17314   13026
## 7  2013/14                   NA               94.54      12909   10530
## 8  2014/15                   NA               73.70      16319   11246
## 9  2015/16                   NA               75.35      12888    9153
## 10 2016/17                   NA               84.40      17170   14917
## 11 2017/18                   NA               90.11      20923   16281
## 12 2018/19                   NA               86.06      18367   14700
## 13 2019/20                   NA               72.39      19913   15300
## 14 2020/21                   NA               88.67      14608   16050
## 15 2021/22                   NA              135.51      17523   14050
## 16 2022/23                   NA              101.78      14468   12450
## 17 2023/24                   NA               94.71      12066   11750
## 18 2024/25                   NA               80.02      14413   11900
#global price vs us production
ggplot(data = df, aes(x = index_a_far_eastern, y = production)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE) + 
  labs(
    title = "Global Price vs. US Production of Cotton",
    x = "Global Cotton Price, cents per pound",
    y = "US Cotton Production",
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 32 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 32 rows containing missing values or values outside the scale range
## (`geom_point()`).

#global price vs us exports
ggplot(data = df, aes(x = index_a_far_eastern, y = exports)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE) + 
  labs(
    title = "Global Price vs. US Production of Cotton",
    x = "Global Cotton Price, cents per pound",
    y = "US Cotton Exports",
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 32 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 32 rows containing missing values or values outside the scale range
## (`geom_point()`).

df %>% #find the max for all numeric columns
  summarise(across(where(is.numeric), ~ max(.x, na.rm = TRUE)))
##   index_a_north_europe index_a_far_eastern production exports
## 1                 93.3               166.6      23890   17673
df %>% #find the min for all numeric columns
  summarise(across(where(is.numeric), ~ min(.x, na.rm = TRUE)))
##   index_a_north_europe index_a_far_eastern production exports
## 1                41.88               62.05       7771    1960

C. Statistical Analysis

H0 = There is no relationship between global cotton prices and U.S. production over time. H μ = There is relationship between global cotton prices and U.S. production over time.

I chose the t-test because it seemed to be the best test to use given the options for this assignment. To perform this test though, I needed to make 2 groups so that I could find the difference in means. Therefore, I sorted used the median global cost and compared each year. After runnning the two sample t-test, the p-value is determined to be .987, which is significantly greater than .05. Consequently, I fail to reject the null hypothesis. There is no statistical evidence that average US Cotton Production is impacted by Global high or low price years. The difference in means in 22.33 and the confidence level is 95%.

#categorize the data by low price and high price years
#below global median price is considered low, above median global price = high
df_test <- df %>%
  filter(
    !is.na(index_a_far_eastern),
    !is.na(production)
  ) %>%
  mutate(
    price_group = ifelse(
      index_a_far_eastern < median(index_a_far_eastern),
      "Low",
      "High"
    )
  )
df_test %>%
  group_by(price_group) %>%
  summarise(
    mean_production = mean(production), #avg production for each type of year
    sd_production = sd(production), #standard deviation of production for each type of year
    observations = n()
  )
## # A tibble: 2 × 4
##   price_group mean_production sd_production observations
##   <chr>                 <dbl>         <dbl>        <int>
## 1 High                 15943.         2788.            9
## 2 Low                  15921.         2947.            9
#run the t-test
t.test(production ~ price_group, data = df_test)
## 
##  Welch Two Sample t-test
## 
## data:  production by price_group
## t = 0.016517, df = 15.951, p-value = 0.987
## alternative hypothesis: true difference in means between group High and group Low is not equal to 0
## 95 percent confidence interval:
##  -2844.776  2889.443
## sample estimates:
## mean in group High  mean in group Low 
##           15942.89           15920.56
#repeat for the exports just because
df_exports <- df %>%
  filter(
    !is.na(index_a_far_eastern),
    !is.na(exports)
  ) %>%
  mutate(
    price_group = ifelse(
      index_a_far_eastern < median(index_a_far_eastern),
      "Low",
      "High"
    )
  )
#t-test for exports

t.test(exports ~ price_group, data = df_exports)
## 
##  Welch Two Sample t-test
## 
## data:  exports by price_group
## t = 0.48153, df = 15.998, p-value = 0.6367
## alternative hypothesis: true difference in means between group High and group Low is not equal to 0
## 95 percent confidence interval:
##  -1542.093  2448.538
## sample estimates:
## mean in group High  mean in group Low 
##           13358.56           12905.33

Similarly, here the results suggest that there is no statistical evidence that US Cotton Exports are impacted by global prices. The p-value is .6367 and therefore I would fail to reejct the null hypothesis again. The difference in means is 453.23 with a 95% confidence interval.

D. Conclusion

In conclusion, the results of my t-tests indicate that there is no statistical significance. Given the data and my research question, I believe a linear or multiple linear regression test would have been a better way to understand the relationship between global price and productions/exports in the US.

E. References

National Cotton Council of America. (n.d.). The importance of cotton. https://www.cotton.org/pubs/cottoncounts/story/importance.cfm

U.S. Department of Agriculture, Economic Research Service. (n.d.). Cotton sector at a glance. https://www.ers.usda.gov/topics/crops/cotton-and-wool/cotton-sector-at-a-glance

U.S. Department of Agriculture, Economic Research Service. (n.d). Cotton and wool yearbook [Data set].