2024 ManureDB NBB Dissertation Stats

Author

Nancy Bohl Bormann

Published

June 17, 2024

ManureDB

Manure data as of February 19, 2024 from ManureDB, the U.S. nationwide manure database, used for Nancy Bohl Bormann’s 2024 PhD dissertation.

Code
#page formatting is the code above
#setting the theme for plots
theme<-theme(legend.position = "bottom",
        legend.text = element_text(size = 8),
        legend.title = element_blank(),
        legend.box.background = element_rect(colour = "black"),
                                             #plot.title = 
                         #element_text (hjust =0.5, face = "bold")),
                                            
        axis.text.x = element_text(size = 8, vjust = 0.5),
        axis.title.x = element_text(margin = margin(t=5)),
        axis.text.y = element_text(size = 7.5),
        axis.title.y = element_text(margin = margin (r=5)),
        panel.grid.minor = element_blank(),
        strip.text.x = element_text(size = 8),
        strip.background = element_rect(colour = "black", fill=NA, linewidth = 0.5),
        axis.text.x.top = element_blank(),
        axis.ticks.x.top = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA, linewidth=0.25))

Rename column names

Code
# 2. Rename column names
colnames(df)[52]<-"Total_N" 
colnames(df)[41]<-"Phosphorus" 
colnames(df)[42]<-"Potassium" 
colnames(df)[16]<-"Ammonium-N" 
colnames(df)[53]<-"Total_Solids"
df[sapply(df, is.infinite)] <- NA  #Replace Inf Values with NA
#View(df)

Pulling data from certain years (2012-2022)

Code
#Filter data to work with from years 2012-2022
df <- data.frame(df)
# Define the range of years
start_year <- 2012
end_year <- 2022
# Filter for the range of years using indexing & removing "Undisclosed Region")
MDB_year_data <- df %>%
  filter(Year.Analyzed >= start_year & Year.Analyzed <= end_year & Region != "Undisclosed Region")
#write.csv(MDB_year_data, "D:/My Drive/Statistics/FACT-Manure-Database/data/MDB_year_data.csv")
#View(MDB_year_data)
#detach("package:MASS", unload = TRUE) only needed when attached earlier.

# Select columns to work with
MDB_select<-select(MDB_year_data,c(1,2,3,4,6,7,11,14,52,16,41,42,53))
#write.csv(MDB_select, "MDB_select.csv")
#View(MDB_select)

Percent of samples with metadata for each column

Code
# Define the write_clip function
write_clip <- function(data) {
  # Create a data frame with column names and percentages
  df <- data.frame(Column = names(data), Percentage = data)
  # Write to clipboard
  write.table(df, "clipboard", sep="\t", row.names=FALSE, col.names=TRUE)
}

# Replace "Unknown" values and blanks with NA
MDB_year_data_count <- MDB_year_data
MDB_year_data_count[MDB_year_data_count == "Unknown" | MDB_year_data_count == ""] <- NA

# Calculate the percentage of non-NA values in each column
percent_non_na <- colMeans(!is.na(MDB_year_data_count)) * 100

# Print the percentages
print(percent_non_na)
                                                                      ManureDB.Sample.ID 
                                                                            1.000000e+02 
                                                          Animal.or.Other.Amendment.Type 
                                                                            1.000000e+02 
                                                           Animal.Type.Combined.Category 
                                                                            1.000000e+02 
                                                                           Year.Analyzed 
                                                                            1.000000e+02 
                                                                                   State 
                                                                            7.422968e+01 
                                                                                  Region 
                                                                            1.000000e+02 
                                                                             Manure.Type 
                                                                            6.662918e+01 
                                                                        Manure.Treatment 
                                                                            3.072788e+00 
                                                                                Agitated 
                                                                            1.863504e-01 
                                                                            Bedding.Type 
                                                                            7.342101e+00 
                                                                            Storage.Type 
                                                                            4.520154e+01 
                                                                       Length.of.Storage 
                                                                            4.208170e+00 
                                                                      Application.Method 
                                                                            3.932685e+01 
                                                                    Moisture.Designation 
                                                                            9.942633e+01 
                                             Aluminum.Combined...As.Received..Wet..Basis 
                                                                            3.304862e+01 
                                                                              Ammonium.N 
                                                                            3.420554e+01 
                                              Arsenic.Combined...As.Received..Wet..Basis 
                                                                            6.167375e-02 
                                                  Ash.Combined...As.Received..Wet..Basis 
                                                                            2.951302e+00 
                                               Barium.Combined...As.Received..Wet..Basis 
                                                                            3.349523e-02 
                                                Boron.Combined...As.Received..Wet..Basis 
                                                                            4.326945e+01 
                                              Cadmium.Combined...As.Received..Wet..Basis 
                                                                            1.826287e-01 
                                              Calcium.Combined...As.Received..Wet..Basis 
                                                                            5.838670e+01 
                   Calcium.Carbonate.Equivalent..CCE..Combined...As.Received..Wet..Basis 
                                                                            2.219723e-01 
                              Carbon.Nitrogen.Ratio.Combined.C.N.As.Received..Wet..Basis 
                                                                            2.976024e+00 
Cation.Ratio.of.Soil.Structural.Stability..CROSS..Combined.Ratio.As.Received..Wet..Basis 
                                                                            1.379684e-01 
                                             Chloride.Combined...As.Received..Wet..Basis 
                                                                            4.907317e-01 
                                             Chromium.Combined...As.Received..Wet..Basis 
                                                                            6.842596e-01 
                                               Cobalt.Combined...As.Received..Wet..Basis 
                                                                            5.236952e-02 
                                               Copper.Combined...As.Received..Wet..Basis 
                                                                            5.772982e+01 
                           Electrical.Conductivity.Combined.dS.m.As.Received..Wet..Basis 
                                                                            2.277410e+00 
                                                 Iron.Combined...As.Received..Wet..Basis 
                                                                            5.315134e+01 
                                                 Lead.Combined...As.Received..Wet..Basis 
                                                                            1.717295e-01 
                                            Magnesium.Combined...As.Received..Wet..Basis 
                                                                            5.826149e+01 
                                            Manganese.Combined...As.Received..Wet..Basis 
                                                                            5.795871e+01 
                                             Moisture.Combined...As.Received..Wet..Basis 
                                                                            5.452252e+01 
                                           Molybdenum.Combined...As.Received..Wet..Basis 
                                                                            4.431472e-01 
                                               Nickel.Combined...As.Received..Wet..Basis 
                                                                            7.746436e-01 
                                            Nitrate.N.Combined...As.Received..Wet..Basis 
                                                                            3.071459e+00 
                                       Organic.Matter.Combined...As.Received..Wet..Basis 
                                                                            9.894384e-01 
                                                pH.Combined.none.As.Received..Wet..Basis 
                                                                            3.645211e+01 
                                                                              Phosphorus 
                                                                            9.909483e+01 
                                                                               Potassium 
                                                                            9.905070e+01 
                                             Selenium.Combined...As.Received..Wet..Basis 
                                                                            3.881193e-02 
                                              Silicon.Combined...As.Received..Wet..Basis 
                                                                            3.349523e-02 
                                               Sodium.Combined...As.Received..Wet..Basis 
                                                                            5.822693e+01 
                    Sodium.Absorption.Ratio..SAR..Combined.Ratio.As.Received..Wet..Basis 
                                                                            1.379684e-01 
                                            Strontium.Combined...As.Received..Wet..Basis 
                                                                            3.349523e-02 
                                            Sulfate.S.Combined...As.Received..Wet..Basis 
                                                                            1.254742e-01 
                                               Sulfur.Combined...As.Received..Wet..Basis 
                                                                            6.718824e+01 
                                         Total.Carbon.Combined...As.Received..Wet..Basis 
                                                                            3.163438e-01 
                               Total.Dissolved.Solids.Combined...As.Received..Wet..Basis 
                                                                            3.639283e-01 
                                                                                 Total_N 
                                                                            9.859320e+01 
                                                                            Total_Solids 
                                                                            5.838989e+01 
                            Total.Suspended.Solids.Combined.mg.L.As.Received..Wet..Basis 
                                                                            6.911713e-03 
                                      Volatile.Solids.Combined...As.Received..Wet..Basis 
                                                                            1.150003e+00 
                         Water.Extractable.Phosphorus.Combined...As.Received..Wet..Basis 
                                                                            1.988447e-01 
                                                 Zinc.Combined...As.Received..Wet..Basis 
                                                                            5.814187e+01 
Code
# Write percentages to CSV file
#write.csv(percent_non_na, "percent_non_na.csv")

# Copy percentages to clipboard
#write_clip(percent_non_na)

Liquid + Slurry “Pumpable” Data

(<10% total solids, likely to move this manure hydraulically)

Animal Type Counts Table and Graph

Code
MDB_pump<-subset(MDB_select, Moisture.Designation=="Liquid"| Moisture.Designation=="Slurry")
#View(MDB_pump)
MDBLiquidCounts<-ggplot(MDB_pump, aes(y=Animal.or.Other.Amendment.Type))+geom_bar()+theme()
MDBLiquidCounts

Code
MDB_pump %>% count(Animal.or.Other.Amendment.Type, sort=TRUE)
   Animal.or.Other.Amendment.Type      n
1                           Swine 185758
2                           Dairy  57256
3          Swine - Farrow to Wean   9584
4                            Beef   5358
5                          Cattle   4671
6                Swine - Finisher   2773
7                         Poultry   1064
8                 Swine - Nursery   1047
9                 Dairy - Heifers    803
10                           Duck    664
11       Swine - Farrow to Feeder    637
12                           Veal    455
13       Swine - Feeder to Finish    242
14                   Other Animal    210
15                        Chicken    140
16                   Dairy - Calf    128
17          Dairy - Lactating Cow     80
18                Chicken - Layer     79
19                Dairy - Dry Cow     56
20          Other - Feed Leachate     56
21             Other - Truck Wash     56
22         Swine - Wean to Finish     52
23                   Other - Whey     33
24           Other - Spoiled Feed     32
25  Other - Milkhouse Rinse Water     26
26                         Turkey     26
27                  Dairy - Steer     19
28                      Biosolids     13
29               Chicken - Pullet     12
30                           Fish      9
31                           Goat      8
32              Swine - Gestation      6
33                          Sheep      5
34              Chicken - Broiler      4
35                          Horse      4
36                         Alpaca      3
37        Dairy - Calf and Heifer      2
38                    Beef - Calf      1
39              Chicken - Breeder      1
40                           Worm      1
Code
#write_clip(MDB_pump %>% count(Animal.or.Other.Amendment.Type, sort=TRUE)) #this copies to the clipboard ready to paste into excel

Solid + Semi-Solid “Stackable” Data

(>10% total solids, likely move this manure mechanically)

Animal Type Counts Table and Graph

Code
MDB_stack<-subset(MDB_select, Moisture.Designation=="Solid"| Moisture.Designation=="Semi-solid")
#View(MN_solid)
MDBSolidCounts<-ggplot(MDB_stack, aes(y=Animal.or.Other.Amendment.Type))+geom_bar()+theme
MDBSolidCounts

Code
MDB_stack %>% count(Animal.or.Other.Amendment.Type, sort=TRUE)
   Animal.or.Other.Amendment.Type     n
1                           Dairy 23722
2               Chicken - Broiler 18626
3                         Poultry 17523
4                            Beef 10013
5                           Swine  6738
6                          Turkey  5640
7                          Cattle  4240
8                 Chicken - Layer  3604
9               Chicken - Breeder  1814
10                Dairy - Heifers  1759
11                          Horse  1359
12                   Other Animal  1251
13               Chicken - Pullet  1123
14                   Dairy - Calf  1005
15                        Chicken   673
16              Swine - Mortality   618
17                           Duck   609
18                Dairy - Dry Cow   459
19                      Biosolids   417
20            Poultry - Mortality   286
21          Dairy - Lactating Cow   202
22                           Worm   145
23               Swine - Finisher   141
24                          Sheep   132
25           Other - Spoiled Feed   101
26                   Turkey - Hen    87
27                           Goat    49
28       Swine - Feeder to Finish    46
29                  Dairy - Steer    37
30        Dairy - Calf and Heifer    30
31                    Beef - Calf    28
32                          Quail    24
33             Other - Truck Wash    23
34         Swine - Farrow to Wean    21
35                          Mixed    17
36                 Beef - Stocker    14
37                           Veal    13
38       Swine - Farrow to Feeder    12
39                         Alpaca     9
40                Swine - Nursery     7
41                Beef - Breeding     6
42                          Egret     6
43         Swine - Wean to Finish     6
44  Other - Milkhouse Rinse Water     2
45                   Other - Whey     2
46                           Fish     1
47              Swine - Gestation     1
Code
#write_clip(MDB_stack %>% count(Animal.or.Other.Amendment.Type, sort=TRUE)) #this copies to the clipboard ready to paste into excel

Liquid + slurry manure - pivot Longer, sort for beef, dairy, swine

Code
#Sort for beef, dairy, swine
MDB_pump3<-subset(MDB_pump, Animal.Type.Combined.Category=="Beef"| Animal.Type.Combined.Category=="Dairy"|Animal.Type.Combined.Category=="Swine")
#View(MDB_pump3)
MDB_pump3pl<-MDB_pump3 %>%
  mutate(across(contains("Total_N|Amm_N|P2O5|K2O"), \(x)  as.numeric(x))) %>%  #x is an anonymous placeholder(function) for those columns that contain "Total_N|Amm_N|P2O5|K2O"
pivot_longer(cols = contains("Total_N"), names_to = NULL, values_to = "Total_N") %>% 
  pivot_longer(cols = contains("Ammonium.N"), names_to = NULL, values_to = "Amm_N") %>%
  pivot_longer(cols = contains("Phosphorus"), names_to = NULL, values_to = "P2O5") %>% 
  pivot_longer(cols = contains("Potassium"), names_to = NULL, values_to = "K2O")
#View(MDB_pump3pl)
#write.csv(MDB_pump3pl, "MDB_pump3pl.csv")
MDB_pump3pl_clean<-MDB_pump3pl %>%
  pivot_longer(
    cols = c(Total_N, Amm_N, P2O5, K2O),
    names_to = "Variable", values_to = "Value"
  ) 
#View(MDB_pump3pl_clean)
#write.csv(MDB_pump3pl_clean, "MDB_pump3pl_clean.csv")

Liquid Manure Overview Table (%)

(likely hydraulically-moved “pumpable” manure)

Code
df_nest = MDB_pump3pl_clean %>% 
  group_by(Animal.Type.Combined.Category) %>% 
  nest()


MDB_pump_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1,  na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25,  na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75,  na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }
df_by_Animal_Type = df_nest %>% 
 mutate(summ = purrr::map(data, \(x) MDB_pump_long_summary(x)))%>%
 select(c(Animal.Type.Combined.Category, summ))%>%
 unnest(cols = c(summ))
#df_by_Animal_Type = df_nest %>% 
 # mutate(summ = purrr::map(data, ~MN_liquid_long_summary(.))) %>%
  #select(c(Animal.or.Other.Amendment.Type, summ)) %>%
  #unnest(cols = c(summ))

df_by_Animal_Type %>%   gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Beef
Total_N 0.2400 0.3410 142.0 0.4380 0.537 123.0 0 8.20e-03 0.02000 0.620 1.300 2.780 1.550 1.860 5358
Amm_N 0.0390 0.0578 148.0 0.1220 0.150 123.0 0 1.00e-04 0.00400 0.230 0.360 0.816 1.150 0.439 3888
P2O5 0.1300 0.1790 138.0 0.3610 0.536 148.0 0 8.00e-03 0.02000 0.400 1.180 7.460 2.440 10.000 5355
K2O 0.2300 0.2240 97.2 0.4530 0.597 132.0 0 4.10e-02 0.09000 0.435 1.460 5.900 2.140 4.940 5348
Dairy
Total_N 0.2050 0.0964 47.1 0.2070 0.795 385.0 0 4.00e-02 0.13000 0.262 0.320 183.000 217.000 49800.000 56541
Amm_N 0.0900 0.0741 82.4 0.0883 0.108 122.0 0 1.66e-05 0.02460 0.130 0.164 10.000 43.400 3440.000 31146
P2O5 0.0804 0.0528 65.7 0.1030 0.695 676.0 0 1.43e-02 0.04260 0.114 0.150 103.000 129.000 17700.000 57833
K2O 0.2060 0.0826 40.2 0.2390 1.270 530.0 0 7.18e-02 0.14500 0.260 0.330 182.000 128.000 17400.000 57907
Swine
Total_N 0.0517 0.0537 104.0 0.2120 0.264 125.0 0 1.55e-02 0.02850 0.390 0.650 11.900 1.920 28.300 199767
Amm_N 0.2800 0.1840 65.7 0.2790 0.174 62.5 0 2.30e-02 0.15000 0.400 0.500 1.350 0.303 -0.121 38579
P2O5 0.0133 0.0119 89.6 0.1030 0.180 174.0 0 5.45e-03 0.00791 0.179 0.328 17.200 12.700 658.000 200079
K2O 0.1000 0.0837 83.7 0.1710 0.158 92.1 0 3.54e-02 0.05850 0.266 0.430 4.120 1.500 7.030 200069

Liquid Manure Overview Table (lbs/1000 gal)

(likely hydraulically-moved “pumpable” manure)

Code
#Convert Units from % to lbs/1000
multiply_by_83pt4<- MDB_pump3pl_clean %>% mutate( Value = Value * 83.4)
#print(multiply_by_83pt4) #View(multiply_by_83pt4)
df_nest = multiply_by_83pt4 %>% 
  group_by(Animal.Type.Combined.Category) %>% 
  nest()

MDB_pump_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90,na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_pump_long_summary(x)))|>
  select(c(Animal.Type.Combined.Category, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Beef
Total_N 20.00 28.400 142.0 36.50 44.80 123.0 0 0.68400 1.670 51.7 109.0 232.0 1.550 1.860 5358
Amm_N 3.25 4.820 148.0 10.10 12.50 123.0 0 0.00834 0.334 19.2 30.0 68.1 1.150 0.439 3888
P2O5 10.80 15.000 138.0 30.10 44.70 148.0 0 0.66700 1.670 33.4 98.5 622.0 2.440 10.000 5355
K2O 19.20 18.700 97.2 37.80 49.80 132.0 0 3.42000 7.510 36.3 121.0 492.0 2.140 4.940 5348
Dairy
Total_N 17.10 8.040 47.1 17.20 66.30 385.0 0 3.34000 10.800 21.9 26.7 15300.0 217.000 49800.000 56541
Amm_N 7.51 6.180 82.4 7.37 8.98 122.0 0 0.00138 2.050 10.8 13.7 834.0 43.400 3440.000 31146
P2O5 6.71 4.410 65.7 8.57 57.90 676.0 0 1.19000 3.550 9.5 12.5 8620.0 129.000 17700.000 57833
K2O 17.20 6.890 40.2 19.90 106.00 530.0 0 5.99000 12.100 21.7 27.5 15100.0 128.000 17400.000 57907
Swine
Total_N 4.32 4.480 104.0 17.70 22.00 125.0 0 1.30000 2.380 32.5 54.2 995.0 1.920 28.300 199767
Amm_N 23.40 15.300 65.7 23.30 14.60 62.5 0 1.92000 12.500 33.4 41.7 112.0 0.303 -0.121 38579
P2O5 1.11 0.996 89.6 8.62 15.00 174.0 0 0.45400 0.660 14.9 27.4 1430.0 12.700 658.000 200079
K2O 8.34 6.980 83.7 14.30 13.20 92.1 0 2.95000 4.880 22.2 35.9 344.0 1.500 7.030 200069

Solid + semi-solid manure - Pivot Longer, sort for beef, dairy, chicken - broiler, chicken-layer, turkey

Code
#Sort for beef, dairy, beef, dairy, chicken - broiler, chicken-layer, turkey 
MDB_stack5<-subset(MDB_stack, Animal.Type.Combined.Category=="Beef"| Animal.Type.Combined.Category=="Dairy"|Animal.or.Other.Amendment.Type=="Chicken - Broiler"|Animal.or.Other.Amendment.Type=="Chicken - Layer"|Animal.or.Other.Amendment.Type=="Turkey")
                
#View(MDB_stack5)

#Combine beef types into beef, and dairy types into dairy
MDB_stack5fix <- MDB_stack5 %>%
  mutate(Animal.or.Other.Amendment.Type = ifelse(Animal.Type.Combined.Category == "Beef", "Beef", Animal.or.Other.Amendment.Type)) %>%
   mutate(Animal.or.Other.Amendment.Type = ifelse(Animal.Type.Combined.Category == "Dairy", "Dairy", Animal.or.Other.Amendment.Type)) 

#View(MDB_stack5fix)
#write.csv(MDB_stack5fix, "MDB_stack5fix.csv")
MDB_stack5pl<-MDB_stack5fix %>%
  mutate(across(contains("Total_N|Amm_N|P2O5|K2O"), \(x)  as.numeric(x))) %>%  #x is an anonymous placeholder(function) for those columns that contain "Total_N"|"Amm_N"|"P2O5"|"K2O"
pivot_longer(cols = contains("Total_N"), names_to = NULL, values_to = "Total_N") %>% 
  pivot_longer(cols = contains("Ammonium.N"), names_to = NULL, values_to = "Amm_N") %>%
  pivot_longer(cols = contains("Phosphorus"), names_to = NULL, values_to = "P2O5") %>% 
  pivot_longer(cols = contains("Potassium"), names_to = NULL, values_to = "K2O")

#View(MDB_stack5pl)
MDB_stack5pl_clean<-MDB_stack5pl %>%
  pivot_longer(
    cols = c(Total_N, Amm_N, P2O5, K2O),
    names_to = "Variable", values_to = "Value"
  ) 
#View(MDB_stack5pl_clean)

df_nest = MDB_stack5pl_clean%>% 
  group_by(Animal.or.Other.Amendment.Type) %>% 
  nest()

Solid Manure Overview Table (%)

(likely mechanically-moved “stackable” manure)

Code
df_nest = MDB_stack5pl_clean %>% 
  group_by(Animal.or.Other.Amendment.Type) %>% 
  nest()

MDB_stack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_stack_long_summary(x)))|>
  select(c(Animal.or.Other.Amendment.Type, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Beef
Total_N 0.7300 0.2820 38.6 0.8060 0.4030 50.0 0.00000 4.10e-01 0.5590 0.941 1.270 4.42 2.010 7.240 10023
Amm_N 0.0700 0.0900 129.0 0.1300 0.1530 118.0 0.00000 1.00e-04 0.0172 0.190 0.390 0.84 1.480 1.500 5427
P2O5 0.4250 0.2300 54.3 0.5360 0.4290 80.1 0.00000 2.01e-01 0.2910 0.640 0.980 11.80 5.660 86.900 10026
K2O 0.6490 0.3430 52.9 0.7680 0.5490 71.5 0.00000 3.10e-01 0.4400 0.939 1.360 19.40 6.060 142.000 10026
Chicken - Broiler
Total_N 2.8700 0.5660 19.7 2.8000 0.6710 23.9 0.03000 1.95e+00 2.4500 3.220 3.560 7.97 -0.463 1.450 18604
Amm_N 0.4080 0.1680 41.2 0.4170 0.1870 44.7 0.00000 1.96e-01 0.2970 0.524 0.638 2.63 1.210 9.160 6973
P2O5 2.5000 0.7060 28.3 2.5500 0.7960 31.3 0.01030 1.64e+00 2.0400 2.990 3.500 24.50 1.860 34.500 18609
K2O 2.8300 0.6500 23.0 2.7900 0.8150 29.2 0.00465 1.84e+00 2.3900 3.260 3.670 28.30 1.960 61.400 18609
Chicken - Layer
Total_N 2.1600 0.9260 42.9 2.2000 0.9550 43.4 0.01500 1.02e+00 1.5600 2.800 3.420 7.32 0.479 0.908 3526
Amm_N 0.2610 0.1700 65.0 0.3100 0.2320 74.8 0.00000 7.86e-02 0.1600 0.402 0.585 1.90 1.880 5.770 1965
P2O5 2.6900 1.1300 42.1 2.7900 1.3100 46.9 0.00000 1.32e+00 2.0000 3.530 4.280 21.00 2.510 28.400 3591
K2O 2.0100 0.7960 39.6 2.0100 1.0100 50.2 0.00000 8.76e-01 1.4400 2.520 3.040 24.70 5.630 108.000 3591
Dairy
Total_N 0.4260 0.2030 47.7 0.5310 0.3900 73.5 0.00000 2.30e-01 0.3110 0.624 0.921 9.13 3.570 29.300 26099
Amm_N 0.0725 0.0853 118.0 0.0906 0.0958 106.0 0.00000 1.54e-05 0.0193 0.138 0.195 1.94 4.230 49.000 15006
P2O5 0.1900 0.1150 60.3 0.3040 1.0900 357.0 0.00000 9.49e-02 0.1300 0.320 0.578 108.00 80.400 7280.000 26590
K2O 0.3620 0.2260 62.3 0.5470 1.8300 334.0 0.00000 1.61e-01 0.2430 0.610 0.996 180.00 81.900 7410.000 26504
Turkey
Total_N 2.5000 0.7480 29.9 2.4900 0.8260 33.1 0.00000 1.43e+00 1.9900 3.000 3.500 9.00 0.229 1.310 5631
Amm_N 0.5400 0.2250 41.7 0.5570 0.2560 45.9 0.00000 2.50e-01 0.3940 0.697 0.884 2.00 0.661 1.450 3526
P2O5 2.5300 0.8820 34.8 2.5500 0.9340 36.6 0.00000 1.43e+00 1.9100 3.100 3.660 13.30 1.080 8.020 5634
K2O 2.0600 0.6520 31.7 2.0200 0.7530 37.3 0.00000 1.13e+00 1.5800 2.470 2.800 16.80 2.460 39.600 5634

Solid Manure Overview Table (lbs/ton)

(likely mechanically-moved “stackable” manure)

Code
#Convert Units from % to lbs/ton
multiply_by_20<- MDB_stack5pl_clean %>%
 mutate(
    Value = Value * 20)

# View the result
#print(multiply_by_20)
#View(multiply_by_20)
df_nest = multiply_by_20 %>% 
  group_by(Animal.or.Other.Amendment.Type) %>% 
  nest()

MDB_stack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>% 
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x)MDB_stack_long_summary(x)))|>
  select(c(Animal.or.Other.Amendment.Type, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Beef
Total_N 14.60 5.63 38.6 16.10 8.06 50.0 0.0000 8.20e+00 11.200 18.80 25.40 88.4 2.010 7.240 10023
Amm_N 1.40 1.80 129.0 2.61 3.07 118.0 0.0000 2.00e-03 0.344 3.80 7.80 16.8 1.480 1.500 5427
P2O5 8.49 4.61 54.3 10.70 8.59 80.1 0.0000 4.02e+00 5.810 12.80 19.60 237.0 5.660 86.900 10026
K2O 13.00 6.86 52.9 15.40 11.00 71.5 0.0000 6.20e+00 8.800 18.80 27.20 387.0 6.060 142.000 10026
Chicken - Broiler
Total_N 57.40 11.30 19.7 56.00 13.40 23.9 0.6000 3.90e+01 49.000 64.40 71.10 159.0 -0.463 1.450 18604
Amm_N 8.16 3.36 41.2 8.35 3.73 44.7 0.0000 3.92e+00 5.940 10.50 12.80 52.6 1.210 9.160 6973
P2O5 49.90 14.10 28.3 50.90 15.90 31.3 0.2070 3.28e+01 40.700 59.80 69.90 491.0 1.860 34.500 18609
K2O 56.50 13.00 23.0 55.80 16.30 29.2 0.0931 3.68e+01 47.700 65.20 73.40 567.0 1.960 61.400 18609
Chicken - Layer
Total_N 43.10 18.50 42.9 44.00 19.10 43.4 0.3000 2.04e+01 31.100 55.90 68.40 146.0 0.479 0.908 3526
Amm_N 5.23 3.40 65.0 6.20 4.64 74.8 0.0000 1.57e+00 3.190 8.04 11.70 38.0 1.880 5.770 1965
P2O5 53.90 22.70 42.1 55.90 26.20 46.9 0.0000 2.63e+01 40.000 70.50 85.50 421.0 2.510 28.400 3591
K2O 40.20 15.90 39.6 40.30 20.20 50.2 0.0000 1.75e+01 28.900 50.40 60.70 493.0 5.630 108.000 3591
Dairy
Total_N 8.53 4.07 47.7 10.60 7.81 73.5 0.0000 4.60e+00 6.220 12.50 18.40 183.0 3.570 29.300 26099
Amm_N 1.45 1.71 118.0 1.81 1.92 106.0 0.0000 3.07e-04 0.386 2.76 3.89 38.8 4.230 49.000 15006
P2O5 3.80 2.29 60.3 6.07 21.70 357.0 0.0000 1.90e+00 2.600 6.40 11.60 2160.0 80.400 7280.000 26590
K2O 7.24 4.51 62.3 10.90 36.50 334.0 0.0000 3.21e+00 4.850 12.20 19.90 3610.0 81.900 7410.000 26504
Turkey
Total_N 50.00 15.00 29.9 49.90 16.50 33.1 0.0000 2.86e+01 39.700 60.00 70.00 180.0 0.229 1.310 5631
Amm_N 10.80 4.51 41.7 11.10 5.12 45.9 0.0000 5.01e+00 7.880 13.90 17.70 40.0 0.661 1.450 3526
P2O5 50.60 17.60 34.8 51.10 18.70 36.6 0.0000 2.85e+01 38.300 62.10 73.20 267.0 1.080 8.020 5634
K2O 41.20 13.00 31.7 40.40 15.10 37.3 0.0000 2.26e+01 31.600 49.50 56.00 336.0 2.460 39.600 5634

Beef Solid

Beef Solid Manure by Region Overview Table (%)

Code
MDB_beefstack<-filter(MDB_stack5pl_clean, Animal.or.Other.Amendment.Type=="Beef") 
#View(MDB_beefstack)

df_nest = MDB_beefstack %>% 
  group_by(Region) %>% 
  nest()

MDB_beefstack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_beefstack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Northeast
Total_N 0.5740 0.2590 45.1 0.6560 0.3790 57.8 0.133000 3.08e-01 0.41800 0.7790 1.050 2.880 2.240 7.310 753
Amm_N 0.0275 0.0335 122.0 0.0454 0.0517 114.0 0.000292 3.84e-03 0.00839 0.0662 0.111 0.416 2.240 7.670 735
P2O5 0.2980 0.1460 49.0 0.3550 0.2330 65.7 0.000000 1.43e-01 0.21100 0.4190 0.626 1.800 2.270 7.500 747
K2O 0.6300 0.4720 74.9 0.7790 0.6350 81.5 0.000000 1.70e-01 0.35800 1.0000 1.480 5.840 2.310 9.260 747
Midwest
Total_N 0.7400 0.2670 36.1 0.8120 0.3790 46.7 0.000000 4.30e-01 0.58000 0.9500 1.250 4.420 1.980 7.290 8162
Amm_N 0.1000 0.1330 133.0 0.1520 0.1610 105.0 0.000000 9.33e-06 0.02000 0.2300 0.421 0.840 1.180 0.603 3982
P2O5 0.4300 0.2220 51.7 0.5350 0.4230 78.9 0.000000 2.10e-01 0.30000 0.6400 0.950 11.800 6.590 111.000 8148
K2O 0.6300 0.3110 49.4 0.7310 0.4450 60.9 0.000000 3.30e-01 0.45000 0.9000 1.260 6.640 2.420 12.400 8148
Southwest
Total_N 0.8200 0.4370 53.3 1.0400 0.6300 60.4 0.420000 4.75e-01 0.56000 1.5800 1.880 2.300 0.748 -1.110 14
Amm_N NA NA NA NA NA NA NA NA NA NA NA NA NA NA 0
P2O5 0.5380 0.3060 56.8 0.7150 0.6570 91.9 0.183000 2.29e-01 0.33200 0.7100 1.340 2.660 1.830 2.670 14
K2O 1.1800 0.7740 65.8 1.3000 0.9190 70.8 0.132000 4.25e-01 0.63900 1.6100 2.170 3.720 1.120 0.900 14
Southeast
Total_N 0.6680 0.3260 48.8 0.8050 0.5050 62.7 0.000000 3.51e-01 0.49400 0.9650 1.430 4.380 2.080 7.080 757
Amm_N 0.0203 0.0198 97.6 0.0327 0.0505 154.0 0.000000 2.78e-03 0.00879 0.0362 0.071 0.534 5.770 45.500 475
P2O5 0.4400 0.2700 61.4 0.5750 0.4790 83.3 0.000000 1.95e-01 0.28600 0.6990 1.060 4.380 2.980 13.300 781
K2O 0.7470 0.5340 71.5 0.9780 1.0400 106.0 0.000000 2.07e-01 0.43300 1.2200 1.940 19.400 8.170 127.000 781
Northern Plains
Total_N 0.8350 0.4480 53.6 0.9920 0.5880 59.3 0.020000 4.21e-01 0.60600 1.2400 1.690 3.450 1.620 3.370 262
Amm_N 0.1900 0.1780 93.6 0.2420 0.1920 79.0 0.001000 3.94e-02 0.09150 0.3870 0.550 0.780 0.766 -0.470 190
P2O5 0.8480 0.6230 73.4 0.8630 0.5430 62.9 0.084900 2.49e-01 0.38200 1.1800 1.630 2.730 0.803 0.243 261
K2O 1.0000 0.5770 57.6 1.1000 0.6450 58.6 0.094000 3.98e-01 0.62400 1.3900 1.900 3.370 1.150 1.540 261
Pacific Northwest
Total_N 0.8270 0.4790 58.0 1.1300 0.7020 61.8 0.477000 5.02e-01 0.61100 1.6800 2.250 2.490 0.823 -0.953 17
Amm_N 0.0530 0.0700 132.0 0.1270 0.1770 139.0 0.004300 5.68e-03 0.00750 0.1700 0.344 0.620 1.520 1.300 17
P2O5 0.6200 0.5700 91.9 0.8590 0.6820 79.4 0.194000 2.30e-01 0.25500 1.4600 1.650 2.410 0.717 -0.826 17
K2O 1.7300 1.2400 71.9 1.6600 0.8360 50.2 0.632000 6.69e-01 0.88400 2.4500 2.670 3.000 0.125 -1.640 17
Southern Plains
Total_N 0.9280 0.6810 73.5 0.9610 0.5720 59.5 0.201000 2.42e-01 0.45500 1.3600 1.660 2.300 0.400 -0.829 58
Amm_N 0.1100 0.1040 94.3 0.1290 0.1090 84.7 0.000333 1.00e-03 0.04880 0.1800 0.233 0.440 1.040 0.856 28
P2O5 0.8770 0.5580 63.6 0.8710 0.6280 72.1 0.082400 1.37e-01 0.40800 1.0900 1.440 2.730 1.130 1.380 58
K2O 1.2400 1.1000 88.3 1.1000 0.7570 68.9 0.003600 1.72e-01 0.37700 1.6800 2.170 2.580 0.216 -1.250 58

Beef Solid Manure by Region Overview Table (lbs/ton)

Code
#Convert Units from % to lbs/ton
multiply_by_20<- MDB_beefstack %>%
 mutate(
    Value = Value * 20)

# View the result
#print(multiply_by_20)
#View(multiply_by_20)
df_nest = multiply_by_20 %>% 
  group_by(Region) %>% 
  nest()

MDB_beefstack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>% 
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x)MDB_beefstack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Northeast
Total_N 11.500 5.180 45.1 13.100 7.58 57.8 2.67000 6.16e+00 8.350 15.600 21.10 57.70 2.240 7.310 753
Amm_N 0.550 0.671 122.0 0.907 1.03 114.0 0.00584 7.67e-02 0.168 1.320 2.22 8.31 2.240 7.670 735
P2O5 5.950 2.920 49.0 7.090 4.66 65.7 0.00000 2.85e+00 4.230 8.370 12.50 36.10 2.270 7.500 747
K2O 12.600 9.430 74.9 15.600 12.70 81.5 0.00000 3.41e+00 7.150 20.000 29.60 117.00 2.310 9.260 747
Midwest
Total_N 14.800 5.340 36.1 16.200 7.58 46.7 0.00000 8.60e+00 11.600 19.000 25.00 88.40 1.980 7.290 8162
Amm_N 2.000 2.670 133.0 3.050 3.21 105.0 0.00000 1.87e-04 0.400 4.600 8.42 16.80 1.180 0.603 3982
P2O5 8.600 4.450 51.7 10.700 8.45 78.9 0.00000 4.20e+00 6.000 12.800 19.00 237.00 6.590 111.000 8148
K2O 12.600 6.230 49.4 14.600 8.90 60.9 0.00000 6.60e+00 9.000 18.000 25.20 133.00 2.420 12.400 8148
Southwest
Total_N 16.400 8.750 53.3 20.900 12.60 60.4 8.40000 9.50e+00 11.200 31.600 37.70 46.00 0.748 -1.110 14
Amm_N NA NA NA NA NA NA NA NA NA NA NA NA NA NA 0
P2O5 10.800 6.110 56.8 14.300 13.10 91.9 3.66000 4.58e+00 6.640 14.200 26.70 53.10 1.830 2.670 14
K2O 23.500 15.500 65.8 26.000 18.40 70.8 2.64000 8.50e+00 12.800 32.100 43.30 74.40 1.120 0.900 14
Southeast
Total_N 13.400 6.510 48.8 16.100 10.10 62.7 0.00000 7.02e+00 9.880 19.300 28.60 87.70 2.080 7.080 757
Amm_N 0.406 0.396 97.6 0.654 1.01 154.0 0.00000 5.55e-02 0.176 0.725 1.42 10.70 5.770 45.500 475
P2O5 8.790 5.400 61.4 11.500 9.58 83.3 0.00000 3.91e+00 5.720 14.000 21.20 87.60 2.980 13.300 781
K2O 14.900 10.700 71.5 19.600 20.80 106.0 0.00000 4.14e+00 8.670 24.500 38.80 387.00 8.170 127.000 781
Northern Plains
Total_N 16.700 8.950 53.6 19.800 11.80 59.3 0.40000 8.43e+00 12.100 24.800 33.80 69.10 1.620 3.370 262
Amm_N 3.800 3.560 93.6 4.850 3.83 79.0 0.02000 7.88e-01 1.830 7.750 11.00 15.60 0.766 -0.470 190
P2O5 17.000 12.500 73.4 17.300 10.90 62.9 1.70000 4.98e+00 7.650 23.500 32.70 54.70 0.803 0.243 261
K2O 20.000 11.500 57.6 22.000 12.90 58.6 1.88000 7.96e+00 12.500 27.900 38.00 67.40 1.150 1.540 261
Pacific Northwest
Total_N 16.500 9.590 58.0 22.700 14.00 61.8 9.54000 1.00e+01 12.200 33.600 45.00 49.80 0.823 -0.953 17
Amm_N 1.060 1.400 132.0 2.540 3.53 139.0 0.08600 1.14e-01 0.150 3.400 6.88 12.40 1.520 1.300 17
P2O5 12.400 11.400 91.9 17.200 13.60 79.4 3.88000 4.60e+00 5.110 29.200 32.90 48.10 0.717 -0.826 17
K2O 34.600 24.800 71.9 33.300 16.70 50.2 12.60000 1.34e+01 17.700 49.000 53.40 60.10 0.125 -1.640 17
Southern Plains
Total_N 18.600 13.600 73.5 19.200 11.40 59.5 4.02000 4.84e+00 9.100 27.200 33.20 46.10 0.400 -0.829 58
Amm_N 2.200 2.080 94.3 2.570 2.18 84.7 0.00666 2.00e-02 0.975 3.600 4.66 8.80 1.040 0.856 28
P2O5 17.500 11.200 63.6 17.400 12.60 72.1 1.65000 2.74e+00 8.160 21.800 28.70 54.60 1.130 1.380 58
K2O 24.900 22.000 88.3 22.000 15.10 68.9 0.07200 3.45e+00 7.540 33.500 43.30 51.50 0.216 -1.250 58

Sort out for beef while still having separate NPK columns

Code
# Sort out for beef while still having separate NPK columns
MDB_beefstackNPK <- filter(MDB_stack5fix, Animal.or.Other.Amendment.Type == "Beef")
#View(MDB_beefstackNPK)

Check for normality using histogram, Q-Q plots, Jarque-Bera test for original total N beef solid data

Code
# Group data by "Year_Analyzed" and "Region", and perform Jarque-Bera test for each group
# Filter out rows with missing values in the Total_N column
cleaned_data <- MDB_beefstackNPK %>%
  filter(!is.na(Total_N))
#write.csv(cleaned_data, "D:/My Drive/Statistics/FACT-Manure-Database/data/cleaned_data.csv")
jarque_bera_results <- cleaned_data %>%
  group_by(Year.Analyzed, Region) %>%
  summarize(jb_test = list(jarque.bera.test(Total_N))) %>%
  mutate(p_value = map_dbl(jb_test, ~ .x$p.value))

# Print the results
#print(jarque_bera_results)
#View(jarque_bera_results) #not normal distribution for majority

# Q-Q plot for original data
qqnorm(cleaned_data$Total_N)
qqline(cleaned_data$Total_N, col = 2)  # Add a reference line

Code
#Histogram
hist(cleaned_data$Total_N)

Beef Solid Total N log and square root transformation & check for normality using Jarque-Bera test

Code
# Log transformation with base 10 on Total_N column
cleaned_data$log_Total_N <- log(cleaned_data$Total_N, base = 10)
# Square root transformation of Total_N column
cleaned_data$sqrt_Total_N <- sqrt(cleaned_data$Total_N)

#View(cleaned_data)
#write.csv(cleaned_data, "D:/My Drive/Statistics/FACT-Manure-Database/data/cleaned_data.csv")
# Visualize the original and transformed data
par(mfrow = c(1, 2))
hist(cleaned_data$Total_N, main = "Original Total_N", xlab = "Value")
hist(cleaned_data$log_Total_N, main = "Log Transformed Total_N", xlab = "Log Value")

Code
hist(cleaned_data$sqrt_Total_N, main = "Sq root Transformed Total_N", xlab = "Sq root Value")

# Q-Q plot for log-transformed data
qqnorm(cleaned_data$log_Total_N[is.finite(cleaned_data$log_Total_N)])
qqline(cleaned_data$log_Total_N, col = 2)  # Add a reference line

Code
# Q-Q plot for sqrt-transformed data
qqnorm(cleaned_data$sqrt_Total_N)
qqline(cleaned_data$sqrt_Total_N, col = 2)  # Add a reference line


# Perform Jarque-Bera test for log data grouped by Year.Analyzed and Region
jarque_bera_results_log <- cleaned_data %>%
  group_by(Year.Analyzed, Region) %>%
  summarize(jb_test = list(jarque.bera.test(log_Total_N))) %>%
  mutate(p_value = map_dbl(jb_test, ~ .x$p.value))
# Print the results
#print(jarque_bera_results)
#View(jarque_bera_results_log)

# Perform Jarque-Bera test for sqare root data grouped by Year.Analyzed and Region
jarque_bera_results_sqrt <- cleaned_data %>%
  group_by(Year.Analyzed, Region) %>%
  summarize(jb_test = list(jarque.bera.test(sqrt_Total_N))) %>%
  mutate(p_value = map_dbl(jb_test, ~ .x$p.value))
# Print the results
#print(jarque_bera_results)
#View(jarque_bera_results_sqrt)

Table of samples by region by year for Beef solid manure

Code
# Create a table of number of samples for each region per year
sample_table <- table(MDB_beefstackNPK$Region, MDB_beefstackNPK$Year.Analyzed)

# Print the sample table
print(sample_table) #NE and SE have samples all years
                   
                    2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
  Midwest            132  724  798  760  771  819  733  770  795  947  914
  Northeast           76   79   81   54   66   74   70   69   63   55   78
  Northern Plains      0    0    0    0   10   19   16    9   14  134   60
  Pacific Northwest    0    0    0    0    0    0    0   11    0    6    0
  Southeast           90   65   94   76   71   71   63   70   68   57   57
  Southern Plains      0    0    0    0    0    0    0    0    0   46   12
  Southwest            0    0   10    4    0    0    0    0    0    0    0

Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Beef Solid)

Code
# Filter data for selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")
filtered_data <- MDB_beefstackNPK[MDB_beefstackNPK$Region %in% selected_regions, ]

# Initialize an empty list to store cleaned data for each nutrient
cleaned_data_list <- list()


# Loop over each nutrient
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Remove missing values for the current nutrient
  cleaned_data <- filtered_data[!is.na(filtered_data[[nutrient]]), ]
  cleaned_data_list[[nutrient]] <- cleaned_data
}

# Create a Function for Wilcoxon Test and Correction
perform_wilcoxon_test <- function(region1, region2, year, nutrient, data) {
    # Extract data for the given regions and year
    region1_data <- cleaned_data[cleaned_data$Region == region1 & cleaned_data$Year.Analyzed == year, nutrient]
    region2_data <- cleaned_data[cleaned_data$Region == region2 & cleaned_data$Year.Analyzed == year, nutrient]
    
    # Perform Wilcoxon rank sum test
    wilcox_result <- wilcox.test(region1_data, region2_data)
    
    # Apply Benjamini-Hochberg correction
    adjusted_p_value <- p.adjust(wilcox_result$p.value, method = "BH")
    
    # Return results
    return(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))
}

# Iterate Over Years, Regions, and Nutrients
years <- unique(cleaned_data$Year.Analyzed)
regions <- unique(cleaned_data$Region)
nutrients <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Initialize empty vectors
all_p_values <- c()
comparison_results <- list()
comparison_names <- c()

# Loop over years, regions, and nutrients, perform tests, and gather p-values
index <- 1
for (year in years) {
    for (nutrient in nutrients) {
        for (i in 1:(length(regions)-1)) {
            for (j in (i+1):length(regions)) {
                region1 <- regions[i]
                region2 <- regions[j]
                
                # Perform Wilcoxon test and correction
                results <- perform_wilcoxon_test(region1, region2, year, nutrient)
                
                # Store p-value
                all_p_values <- c(all_p_values, results$wilcox_result$p.value)
                
                # Store comparison name
                comparison_names <- c(comparison_names, paste(region1, region2, year, nutrient, sep = "_"))
                
                # Store results
                comparison_results[[index]] <- results
                
                index <- index + 1
            }
        }
    }
}

# Sort comparison names based on year
sorted_indices <- order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))
sorted_comparison_names <- comparison_names[sorted_indices]

# Apply Benjamini-Hochberg Procedure
adjusted_p_values <- p.adjust(all_p_values, method = "BH")

# Print results in chronological order
for (name in sorted_comparison_names) {
    index <- match(name, comparison_names)
    year <- as.numeric(unlist(strsplit(name, "_"))[3])
    nutrient <- unlist(strsplit(name, "_"))[4]
    
    # Extract Wilcoxon test results
    result <- comparison_results[[index]]$wilcox_result
    
    # Print results with adjusted p-values
    cat("Comparison:", name, "\n")
    cat("Year:", year, "\n")
    cat("Nutrient:", nutrient, "\n")
    cat("Wilcoxon Test Statistic:", result$statistic, "\n")
    cat("Wilcoxon Test P-value:", result$p.value, "\n")
    cat("Adjusted p-value:", adjusted_p_values[index], "\n")
    cat("\n")
}
Comparison: Northeast_Midwest_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 3505 
Wilcoxon Test P-value: 0.0003018019 
Adjusted p-value: 0.0009501788 

Comparison: Northeast_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 2234 
Wilcoxon Test P-value: 0.001069128 
Adjusted p-value: 0.002713939 

Comparison: Midwest_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 5660 
Wilcoxon Test P-value: 0.796449 
Adjusted p-value: 0.821338 

Comparison: Northeast_Midwest_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 698 
Wilcoxon Test P-value: 7.404964e-16 
Adjusted p-value: 3.574805e-14 

Comparison: Northeast_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1738 
Wilcoxon Test P-value: 0.6911428 
Adjusted p-value: 0.7417143 

Comparison: Midwest_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 3289 
Wilcoxon Test P-value: 8.124556e-16 
Adjusted p-value: 3.574805e-14 

Comparison: Northeast_Midwest_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 3642 
Wilcoxon Test P-value: 0.001016043 
Adjusted p-value: 0.002629759 

Comparison: Northeast_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2204 
Wilcoxon Test P-value: 8.159545e-05 
Adjusted p-value: 0.0002917686 

Comparison: Midwest_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 5192 
Wilcoxon Test P-value: 0.1116191 
Adjusted p-value: 0.1601491 

Comparison: Northeast_Midwest_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 4244 
Wilcoxon Test P-value: 0.06493033 
Adjusted p-value: 0.09630117 

Comparison: Northeast_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2622 
Wilcoxon Test P-value: 0.00974219 
Adjusted p-value: 0.01919357 

Comparison: Midwest_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 4927 
Wilcoxon Test P-value: 0.0311672 
Adjusted p-value: 0.05142588 

Comparison: Northeast_Midwest_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 17303 
Wilcoxon Test P-value: 8.550394e-09 
Adjusted p-value: 8.304375e-08 

Comparison: Northeast_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 1975 
Wilcoxon Test P-value: 0.02489387 
Adjusted p-value: 0.04323671 

Comparison: Midwest_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 27496 
Wilcoxon Test P-value: 0.01238219 
Adjusted p-value: 0.02334927 

Comparison: Northeast_Midwest_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 6998 
Wilcoxon Test P-value: 7.984013e-05 
Adjusted p-value: 0.0002917686 

Comparison: Northeast_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1565 
Wilcoxon Test P-value: 0.4759928 
Adjusted p-value: 0.5511495 

Comparison: Midwest_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 7015.5 
Wilcoxon Test P-value: 0.001663709 
Adjusted p-value: 0.004066843 

Comparison: Northeast_Midwest_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 14969 
Wilcoxon Test P-value: 3.620658e-12 
Adjusted p-value: 7.965447e-11 

Comparison: Northeast_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1700 
Wilcoxon Test P-value: 0.0005002714 
Adjusted p-value: 0.001405017 

Comparison: Midwest_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 26850 
Wilcoxon Test P-value: 0.05651707 
Adjusted p-value: 0.08575004 

Comparison: Northeast_Midwest_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 23462 
Wilcoxon Test P-value: 0.009140988 
Adjusted p-value: 0.01828198 

Comparison: Northeast_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2027 
Wilcoxon Test P-value: 0.03016975 
Adjusted p-value: 0.05041021 

Comparison: Midwest_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 22047 
Wilcoxon Test P-value: 0.4094161 
Adjusted p-value: 0.478256 

Comparison: Northeast_Midwest_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 17745 
Wilcoxon Test P-value: 9.411929e-07 
Adjusted p-value: 5.916069e-06 

Comparison: Northeast_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 2345 
Wilcoxon Test P-value: 0.01103402 
Adjusted p-value: 0.02110857 

Comparison: Midwest_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 38121 
Wilcoxon Test P-value: 0.2549848 
Adjusted p-value: 0.3205523 

Comparison: Northeast_Midwest_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 6039 
Wilcoxon Test P-value: 1.029191e-06 
Adjusted p-value: 6.175144e-06 

Comparison: Northeast_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1857 
Wilcoxon Test P-value: 0.672087 
Adjusted p-value: 0.7331858 

Comparison: Midwest_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 11831 
Wilcoxon Test P-value: 1.762884e-06 
Adjusted p-value: 1.011742e-05 

Comparison: Northeast_Midwest_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 20963 
Wilcoxon Test P-value: 0.0009980365 
Adjusted p-value: 0.002629759 

Comparison: Northeast_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2256 
Wilcoxon Test P-value: 0.0009216039 
Adjusted p-value: 0.002482688 

Comparison: Midwest_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 34175 
Wilcoxon Test P-value: 0.1586158 
Adjusted p-value: 0.2180967 

Comparison: Northeast_Midwest_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 27208.5 
Wilcoxon Test P-value: 0.8718141 
Adjusted p-value: 0.8852266 

Comparison: Northeast_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2558 
Wilcoxon Test P-value: 0.02150052 
Adjusted p-value: 0.03887765 

Comparison: Midwest_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 28995 
Wilcoxon Test P-value: 0.0003154775 
Adjusted p-value: 0.0009501788 

Comparison: Northeast_Midwest_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 14683.5 
Wilcoxon Test P-value: 0.0004725723 
Adjusted p-value: 0.001356077 

Comparison: Northeast_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 1928 
Wilcoxon Test P-value: 0.6450134 
Adjusted p-value: 0.709973 

Comparison: Midwest_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 34098 
Wilcoxon Test P-value: 0.004967757 
Adjusted p-value: 0.0107499 

Comparison: Northeast_Midwest_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 7531.5 
Wilcoxon Test P-value: 0.006499069 
Adjusted p-value: 0.0138132 

Comparison: Northeast_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1384 
Wilcoxon Test P-value: 0.16256 
Adjusted p-value: 0.2212157 

Comparison: Midwest_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 10840 
Wilcoxon Test P-value: 9.040119e-05 
Adjusted p-value: 0.0003140252 

Comparison: Northeast_Midwest_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 13441 
Wilcoxon Test P-value: 2.233207e-05 
Adjusted p-value: 9.509137e-05 

Comparison: Northeast_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1492 
Wilcoxon Test P-value: 0.008209497 
Adjusted p-value: 0.01667159 

Comparison: Midwest_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 29692 
Wilcoxon Test P-value: 0.6859694 
Adjusted p-value: 0.7417143 

Comparison: Northeast_Midwest_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 23131 
Wilcoxon Test P-value: 0.1178888 
Adjusted p-value: 0.1655459 

Comparison: Northeast_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2256 
Wilcoxon Test P-value: 0.3363382 
Adjusted p-value: 0.4036058 

Comparison: Midwest_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 28114 
Wilcoxon Test P-value: 0.7029078 
Adjusted p-value: 0.7482567 

Comparison: Northeast_Midwest_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 21578.5 
Wilcoxon Test P-value: 0.04339108 
Adjusted p-value: 0.06984906 

Comparison: Northeast_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 2072 
Wilcoxon Test P-value: 0.3010761 
Adjusted p-value: 0.3646059 

Comparison: Midwest_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 27208 
Wilcoxon Test P-value: 0.8802182 
Adjusted p-value: 0.8869374 

Comparison: Northeast_Midwest_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 10708 
Wilcoxon Test P-value: 0.02894824 
Adjusted p-value: 0.04898933 

Comparison: Northeast_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1917 
Wilcoxon Test P-value: 0.0003050977 
Adjusted p-value: 0.0009501788 

Comparison: Midwest_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 11434 
Wilcoxon Test P-value: 5.76504e-06 
Adjusted p-value: 2.624087e-05 

Comparison: Northeast_Midwest_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 16482 
Wilcoxon Test P-value: 2.240188e-06 
Adjusted p-value: 1.232103e-05 

Comparison: Northeast_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1495 
Wilcoxon Test P-value: 0.000261411 
Adjusted p-value: 0.0008626564 

Comparison: Midwest_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 26399 
Wilcoxon Test P-value: 0.64543 
Adjusted p-value: 0.709973 

Comparison: Northeast_Midwest_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 27956 
Wilcoxon Test P-value: 0.1702741 
Adjusted p-value: 0.2270322 

Comparison: Northeast_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2091 
Wilcoxon Test P-value: 0.2786319 
Adjusted p-value: 0.3469756 

Comparison: Midwest_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 21704 
Wilcoxon Test P-value: 0.004231624 
Adjusted p-value: 0.009309573 

Comparison: Northeast_Midwest_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 24288 
Wilcoxon Test P-value: 0.006592662 
Adjusted p-value: 0.0138132 

Comparison: Northeast_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 1925 
Wilcoxon Test P-value: 0.02230509 
Adjusted p-value: 0.03925696 

Comparison: Midwest_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 24342 
Wilcoxon Test P-value: 0.1566063 
Adjusted p-value: 0.2176003 

Comparison: Northeast_Midwest_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 13453.5 
Wilcoxon Test P-value: 0.04765912 
Adjusted p-value: 0.07579523 

Comparison: Northeast_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2163 
Wilcoxon Test P-value: 0.0006996987 
Adjusted p-value: 0.001924171 

Comparison: Midwest_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 12848 
Wilcoxon Test P-value: 2.861671e-05 
Adjusted p-value: 0.0001144668 

Comparison: Northeast_Midwest_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 20078.5 
Wilcoxon Test P-value: 2.396909e-06 
Adjusted p-value: 1.265568e-05 

Comparison: Northeast_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1604 
Wilcoxon Test P-value: 8.178362e-05 
Adjusted p-value: 0.0002917686 

Comparison: Midwest_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 24514 
Wilcoxon Test P-value: 0.05810112 
Adjusted p-value: 0.08715169 

Comparison: Northeast_Midwest_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 32239 
Wilcoxon Test P-value: 0.2890863 
Adjusted p-value: 0.3533277 

Comparison: Northeast_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2242 
Wilcoxon Test P-value: 0.1648383 
Adjusted p-value: 0.2220271 

Comparison: Midwest_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 22143 
Wilcoxon Test P-value: 0.002245922 
Adjusted p-value: 0.005201082 

Comparison: Northeast_Midwest_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 18198.5 
Wilcoxon Test P-value: 6.878696e-05 
Adjusted p-value: 0.0002670553 

Comparison: Northeast_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 1269 
Wilcoxon Test P-value: 0.000169341 
Adjusted p-value: 0.0005731542 

Comparison: Midwest_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 18284 
Wilcoxon Test P-value: 0.0535498 
Adjusted p-value: 0.08315969 

Comparison: Northeast_Midwest_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 8843 
Wilcoxon Test P-value: 0.0003167263 
Adjusted p-value: 0.0009501788 

Comparison: Northeast_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1707 
Wilcoxon Test P-value: 0.1174179 
Adjusted p-value: 0.1655459 

Comparison: Midwest_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 10347 
Wilcoxon Test P-value: 2.707346e-05 
Adjusted p-value: 0.000111678 

Comparison: Northeast_Midwest_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 17320 
Wilcoxon Test P-value: 8.340413e-06 
Adjusted p-value: 3.669782e-05 

Comparison: Northeast_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1063 
Wilcoxon Test P-value: 2.690533e-07 
Adjusted p-value: 1.973058e-06 

Comparison: Midwest_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 17492 
Wilcoxon Test P-value: 0.001607268 
Adjusted p-value: 0.004003006 

Comparison: Northeast_Midwest_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 27868 
Wilcoxon Test P-value: 0.2095209 
Adjusted p-value: 0.2685122 

Comparison: Northeast_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1516 
Wilcoxon Test P-value: 0.001918422 
Adjusted p-value: 0.004521995 

Comparison: Midwest_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 12895 
Wilcoxon Test P-value: 7.04401e-09 
Adjusted p-value: 7.748411e-08 

Comparison: Northeast_Midwest_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 16044 
Wilcoxon Test P-value: 5.195836e-08 
Adjusted p-value: 4.034414e-07 

Comparison: Northeast_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 1872 
Wilcoxon Test P-value: 0.02229214 
Adjusted p-value: 0.03925696 

Comparison: Midwest_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 30360 
Wilcoxon Test P-value: 0.07597217 
Adjusted p-value: 0.1114259 

Comparison: Northeast_Midwest_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 6586 
Wilcoxon Test P-value: 4.207884e-09 
Adjusted p-value: 5.049461e-08 

Comparison: Northeast_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1509 
Wilcoxon Test P-value: 0.6204056 
Adjusted p-value: 0.6962533 

Comparison: Midwest_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 11268 
Wilcoxon Test P-value: 2.709693e-08 
Adjusted p-value: 2.384529e-07 

Comparison: Northeast_Midwest_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 16021 
Wilcoxon Test P-value: 4.855481e-08 
Adjusted p-value: 4.005772e-07 

Comparison: Northeast_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1568 
Wilcoxon Test P-value: 0.0003624755 
Adjusted p-value: 0.001063262 

Comparison: Midwest_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 28063 
Wilcoxon Test P-value: 0.5543959 
Adjusted p-value: 0.6363501 

Comparison: Northeast_Midwest_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 25909 
Wilcoxon Test P-value: 0.7471288 
Adjusted p-value: 0.7827063 

Comparison: Northeast_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2370 
Wilcoxon Test P-value: 0.8512985 
Adjusted p-value: 0.8710961 

Comparison: Midwest_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 27115 
Wilcoxon Test P-value: 0.9181391 
Adjusted p-value: 0.9181391 

Comparison: Northeast_Midwest_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 16382 
Wilcoxon Test P-value: 4.789466e-06 
Adjusted p-value: 2.341516e-05 

Comparison: Northeast_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 1812 
Wilcoxon Test P-value: 0.209236 
Adjusted p-value: 0.2685122 

Comparison: Midwest_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 32319 
Wilcoxon Test P-value: 0.001726326 
Adjusted p-value: 0.004143182 

Comparison: Northeast_Midwest_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 6004 
Wilcoxon Test P-value: 3.489084e-10 
Adjusted p-value: 5.117324e-09 

Comparison: Northeast_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1556 
Wilcoxon Test P-value: 0.007411661 
Adjusted p-value: 0.01528655 

Comparison: Midwest_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 13551 
Wilcoxon Test P-value: 1.228645e-11 
Adjusted p-value: 2.316873e-10 

Comparison: Northeast_Midwest_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 16239 
Wilcoxon Test P-value: 3.32749e-06 
Adjusted p-value: 1.689341e-05 

Comparison: Northeast_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1700 
Wilcoxon Test P-value: 0.04197083 
Adjusted p-value: 0.06839691 

Comparison: Midwest_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 30849 
Wilcoxon Test P-value: 0.05291318 
Adjusted p-value: 0.08314928 

Comparison: Northeast_Midwest_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 26772 
Wilcoxon Test P-value: 0.3611507 
Adjusted p-value: 0.4294765 

Comparison: Northeast_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2260 
Wilcoxon Test P-value: 0.5883186 
Adjusted p-value: 0.6694659 

Comparison: Midwest_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 26326 
Wilcoxon Test P-value: 0.7213934 
Adjusted p-value: 0.7617914 

Comparison: Northeast_Midwest_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 14257 
Wilcoxon Test P-value: 5.230114e-06 
Adjusted p-value: 2.465625e-05 

Comparison: Northeast_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 1122 
Wilcoxon Test P-value: 0.08251698 
Adjusted p-value: 0.119695 

Comparison: Midwest_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 31715 
Wilcoxon Test P-value: 0.02625866 
Adjusted p-value: 0.04501484 

Comparison: Northeast_Midwest_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 4672 
Wilcoxon Test P-value: 1.811567e-13 
Adjusted p-value: 4.782537e-12 

Comparison: Northeast_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 838 
Wilcoxon Test P-value: 0.7839166 
Adjusted p-value: 0.8147795 

Comparison: Midwest_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 13776.5 
Wilcoxon Test P-value: 8.807671e-09 
Adjusted p-value: 8.304375e-08 

Comparison: Northeast_Midwest_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 13499 
Wilcoxon Test P-value: 7.754613e-07 
Adjusted p-value: 5.118045e-06 

Comparison: Northeast_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 926 
Wilcoxon Test P-value: 0.002898996 
Adjusted p-value: 0.006597715 

Comparison: Midwest_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 28824 
Wilcoxon Test P-value: 0.3883497 
Adjusted p-value: 0.4576978 

Comparison: Northeast_Midwest_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 20653 
Wilcoxon Test P-value: 0.1943833 
Adjusted p-value: 0.2542953 

Comparison: Northeast_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1227 
Wilcoxon Test P-value: 0.2842132 
Adjusted p-value: 0.3506182 

Comparison: Midwest_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 25942 
Wilcoxon Test P-value: 0.6224082 
Adjusted p-value: 0.6962533 

Comparison: Northeast_Midwest_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 21121 
Wilcoxon Test P-value: 2.228657e-09 
Adjusted p-value: 2.941828e-08 

Comparison: Northeast_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 1606 
Wilcoxon Test P-value: 0.01388027 
Adjusted p-value: 0.02580557 

Comparison: Midwest_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 31012 
Wilcoxon Test P-value: 0.003553034 
Adjusted p-value: 0.00794916 

Comparison: Northeast_Midwest_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 8598.5 
Wilcoxon Test P-value: 8.690041e-17 
Adjusted p-value: 1.147085e-14 

Comparison: Northeast_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2007 
Wilcoxon Test P-value: 0.05472772 
Adjusted p-value: 0.08400069 

Comparison: Midwest_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 19865 
Wilcoxon Test P-value: 6.102811e-15 
Adjusted p-value: 2.013928e-13 

Comparison: Northeast_Midwest_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 19730 
Wilcoxon Test P-value: 5.635671e-11 
Adjusted p-value: 9.298857e-10 

Comparison: Northeast_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1081 
Wilcoxon Test P-value: 3.671411e-07 
Adjusted p-value: 2.550664e-06 

Comparison: Midwest_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 23384 
Wilcoxon Test P-value: 0.1945745 
Adjusted p-value: 0.2542953 

Comparison: Northeast_Midwest_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 32655.5 
Wilcoxon Test P-value: 0.2182953 
Adjusted p-value: 0.2770671 

Comparison: Northeast_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1692 
Wilcoxon Test P-value: 0.01811221 
Adjusted p-value: 0.03320572 

Comparison: Midwest_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 20809 
Wilcoxon Test P-value: 0.01075146 
Adjusted p-value: 0.02087048 

Four Analytes All Years Comparison Mann-Whitney comparison for not normal distribution with Benjamini-Hochberg correction LOOP [just comparing regions, for all years] (Beef Solid)

Code
# Filter data for selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")
filtered_data <- MDB_beefstackNPK[MDB_beefstackNPK$Region %in% selected_regions, ]

# Initialize an empty list to store cleaned data for each nutrient
cleaned_data_list <- list()

# Loop over each nutrient
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Remove missing values for the current nutrient
  cleaned_data <- filtered_data[!is.na(filtered_data[[nutrient]]), ]
  cleaned_data_list[[nutrient]] <- cleaned_data
}

# Create a Function for Wilcoxon Test and Correction
perform_wilcoxon_test <- function(region1, region2, nutrient, data) {
  # Perform Wilcoxon rank sum test
  wilcox_result <- wilcox.test(data[data$Region == region1, nutrient], data[data$Region == region2, nutrient])
  
  # Apply Benjamini-Hochberg correction
  adjusted_p_value <- p.adjust(wilcox_result$p.value, method = "BH")
  
  # Return results
  return(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))
}

# Initialize empty vectors
all_p_values <- c()
comparison_results <- list()
comparison_names <- c()

# Loop over regions, nutrients, perform tests, and gather p-values
for (region1 in selected_regions) {
  for (region2 in selected_regions) {
    if (region1 != region2) {
      for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
        # Perform Wilcoxon test and correction
        results <- perform_wilcoxon_test(region1, region2, nutrient, cleaned_data_list[[nutrient]])
        
        # Store p-value
        all_p_values <- c(all_p_values, results$wilcox_result$p.value)
        
        # Store comparison name
        comparison_names <- c(comparison_names, paste(region1, region2, nutrient, sep = "_"))
        
        # Store results
        comparison_results[[length(comparison_results) + 1]] <- results
      }
    }
  }
}

# Apply Benjamini-Hochberg Procedure
adjusted_p_values <- p.adjust(all_p_values, method = "BH")

# Print results
for (i in seq_along(comparison_names)) {
  name_parts <- strsplit(comparison_names[i], "_")[[1]]
  region1 <- name_parts[1]
  region2 <- name_parts[2]
  nutrient <- name_parts[3]
  
  # Extract Wilcoxon test results
  result <- comparison_results[[i]]$wilcox_result
  
  # Print results with adjusted p-values
  cat("Comparison:", comparison_names[i], "\n")
  cat("Region 1:", region1, "\n")
  cat("Region 2:", region2, "\n")
  cat("Nutrient:", nutrient, "\n")
  cat("Wilcoxon Test Statistic:", result$statistic, "\n")
  cat("Wilcoxon Test P-value:", result$p.value, "\n")
  cat("Adjusted p-value:", adjusted_p_values[i], "\n")
  cat("\n")
}
Comparison: Midwest_Northeast_Total_N 
Region 1: Midwest 
Region 2: Northeast 
Nutrient: Total 
Wilcoxon Test Statistic: 4058739 
Wilcoxon Test P-value: 3.356785e-48 
Adjusted p-value: 1.007035e-47 

Comparison: Midwest_Northeast_Ammonium.N 
Region 1: Midwest 
Region 2: Northeast 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2031229 
Wilcoxon Test P-value: 6.242749e-63 
Adjusted p-value: 2.4971e-62 

Comparison: Midwest_Northeast_Phosphorus 
Region 1: Midwest 
Region 2: Northeast 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 4181152 
Wilcoxon Test P-value: 2.263612e-64 
Adjusted p-value: 1.358167e-63 

Comparison: Midwest_Northeast_Potassium 
Region 1: Midwest 
Region 2: Northeast 
Nutrient: Potassium 
Wilcoxon Test Statistic: 3119052 
Wilcoxon Test P-value: 0.2592915 
Adjusted p-value: 0.2828635 

Comparison: Midwest_Southeast_Total_N 
Region 1: Midwest 
Region 2: Southeast 
Nutrient: Total 
Wilcoxon Test Statistic: 3394215 
Wilcoxon Test P-value: 6.822312e-06 
Adjusted p-value: 9.096415e-06 

Comparison: Midwest_Southeast_Ammonium.N 
Region 1: Midwest 
Region 2: Southeast 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1408893 
Wilcoxon Test P-value: 2.067603e-68 
Adjusted p-value: 2.481123e-67 

Comparison: Midwest_Southeast_Phosphorus 
Region 1: Midwest 
Region 2: Southeast 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 3137598 
Wilcoxon Test P-value: 0.5207 
Adjusted p-value: 0.5207 

Comparison: Midwest_Southeast_Potassium 
Region 1: Midwest 
Region 2: Southeast 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2760293 
Wilcoxon Test P-value: 9.052868e-10 
Adjusted p-value: 1.55192e-09 

Comparison: Northeast_Midwest_Total_N 
Region 1: Northeast 
Region 2: Midwest 
Nutrient: Total 
Wilcoxon Test Statistic: 2087248 
Wilcoxon Test P-value: 3.356785e-48 
Adjusted p-value: 1.007035e-47 

Comparison: Northeast_Midwest_Ammonium.N 
Region 1: Northeast 
Region 2: Midwest 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 895541.5 
Wilcoxon Test P-value: 6.242749e-63 
Adjusted p-value: 2.4971e-62 

Comparison: Northeast_Midwest_Phosphorus 
Region 1: Northeast 
Region 2: Midwest 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1905405 
Wilcoxon Test P-value: 2.263612e-64 
Adjusted p-value: 1.358167e-63 

Comparison: Northeast_Midwest_Potassium 
Region 1: Northeast 
Region 2: Midwest 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2967505 
Wilcoxon Test P-value: 0.2592915 
Adjusted p-value: 0.2828635 

Comparison: Northeast_Southeast_Total_N 
Region 1: Northeast 
Region 2: Southeast 
Nutrient: Total 
Wilcoxon Test Statistic: 228877.5 
Wilcoxon Test P-value: 3.457631e-11 
Adjusted p-value: 6.915262e-11 

Comparison: Northeast_Southeast_Ammonium.N 
Region 1: Northeast 
Region 2: Southeast 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 199644.5 
Wilcoxon Test P-value: 2.383197e-05 
Adjusted p-value: 2.859836e-05 

Comparison: Northeast_Southeast_Phosphorus 
Region 1: Northeast 
Region 2: Southeast 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 186940 
Wilcoxon Test P-value: 5.672693e-34 
Adjusted p-value: 1.361446e-33 

Comparison: Northeast_Southeast_Potassium 
Region 1: Northeast 
Region 2: Southeast 
Nutrient: Potassium 
Wilcoxon Test Statistic: 252340 
Wilcoxon Test P-value: 4.982825e-06 
Adjusted p-value: 7.474238e-06 

Comparison: Southeast_Midwest_Total_N 
Region 1: Southeast 
Region 2: Midwest 
Nutrient: Total 
Wilcoxon Test Statistic: 2784420 
Wilcoxon Test P-value: 6.822312e-06 
Adjusted p-value: 9.096415e-06 

Comparison: Southeast_Midwest_Ammonium.N 
Region 1: Southeast 
Region 2: Midwest 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 482557.5 
Wilcoxon Test P-value: 2.067603e-68 
Adjusted p-value: 2.481123e-67 

Comparison: Southeast_Midwest_Phosphorus 
Region 1: Southeast 
Region 2: Midwest 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 3225990 
Wilcoxon Test P-value: 0.5207 
Adjusted p-value: 0.5207 

Comparison: Southeast_Midwest_Potassium 
Region 1: Southeast 
Region 2: Midwest 
Nutrient: Potassium 
Wilcoxon Test Statistic: 3603296 
Wilcoxon Test P-value: 9.052868e-10 
Adjusted p-value: 1.55192e-09 

Comparison: Southeast_Northeast_Total_N 
Region 1: Southeast 
Region 2: Northeast 
Nutrient: Total 
Wilcoxon Test Statistic: 341143.5 
Wilcoxon Test P-value: 3.457631e-11 
Adjusted p-value: 6.915262e-11 

Comparison: Southeast_Northeast_Ammonium.N 
Region 1: Southeast 
Region 2: Northeast 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 149480.5 
Wilcoxon Test P-value: 2.383197e-05 
Adjusted p-value: 2.859836e-05 

Comparison: Southeast_Northeast_Phosphorus 
Region 1: Southeast 
Region 2: Northeast 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 396467 
Wilcoxon Test P-value: 5.672693e-34 
Adjusted p-value: 1.361446e-33 

Comparison: Southeast_Northeast_Potassium 
Region 1: Southeast 
Region 2: Northeast 
Nutrient: Potassium 
Wilcoxon Test Statistic: 331067 
Wilcoxon Test P-value: 4.982825e-06 
Adjusted p-value: 7.474238e-06 

Mann-Kendall trend test 4 analytes combined regions (Beef Solid)

Code
# Load required library
library(Kendall)

# Function to perform Mann-Kendall trend test for a given analyte across all selected regions
perform_mk_test_combined <- function(data, analyte, selected_regions) {
  # Subset data for the selected regions and analyte
  filtered_data <- data[data$Region %in% selected_regions, ]
  cleaned_data <- filtered_data[!is.na(filtered_data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk))
}

# List of selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")

# List of analytes
analytes <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Perform Mann-Kendall trend test for each analyte combined across selected regions
for (analyte in analytes) {
  cat("Analyzing", analyte, "for all regions combined:\n")
  print(perform_mk_test_combined(MDB_beefstackNPK, analyte, selected_regions))
  cat("\n")
}
Analyzing Total_N for all regions combined:
Score =  7 , Var(Score) = 163
denominator =  53.99074
tau = 0.13, 2-sided pvalue =0.63839
NULL

Analyzing Ammonium.N for all regions combined:
Score =  31 , Var(Score) = 163
denominator =  53.99074
tau = 0.574, 2-sided pvalue =0.018784
NULL

Analyzing Phosphorus for all regions combined:
Score =  5 , Var(Score) = 163
denominator =  53.99074
tau = 0.0926, 2-sided pvalue =0.75405
NULL

Analyzing Potassium for all regions combined:
Score =  3 , Var(Score) = 165
denominator =  55
tau = 0.0545, 2-sided pvalue =0.87627
NULL

Mann-Kendall 4 Analytes trend test by region Beef Solid

Code
# List of selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")

# List of analytes
analytes <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Function to perform Mann-Kendall trend test
perform_mk_test <- function(data, analyte) {
  # Filter out NA values in the analyte column
  cleaned_data <- data[!is.na(data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk))
}

# Iterate over selected regions and analytes
for (region in selected_regions) {
  for (analyte in analytes) {
    # Subset data for the current region
    region_data <- MDB_beefstackNPK[MDB_beefstackNPK$Region == region, ]
    
    # Print information about the current region and analyte
    cat("Analyzing", analyte, "in", region, "region:\n")
    
    # Perform Mann-Kendall trend test
    result_summary <- perform_mk_test(region_data, analyte)
    
    # Print summary of the Mann-Kendall test result
    print(result_summary)
    cat("\n")
  }
}
Analyzing Total_N in Midwest region:
Score =  -2 , Var(Score) = 161.3333
denominator =  53.47897
tau = -0.0374, 2-sided pvalue =0.93725
NULL

Analyzing Ammonium.N in Midwest region:
Score =  5 , Var(Score) = 163
denominator =  53.99074
tau = 0.0926, 2-sided pvalue =0.75405
NULL

Analyzing Phosphorus in Midwest region:
Score =  5 , Var(Score) = 161
denominator =  52.96225
tau = 0.0944, 2-sided pvalue =0.75258
NULL

Analyzing Potassium in Midwest region:
Score =  6 , Var(Score) = 164
denominator =  54.4977
tau = 0.11, 2-sided pvalue =0.69622
NULL

Analyzing Total_N in Northeast region:
Score =  -7 , Var(Score) = 165
denominator =  55
tau = -0.127, 2-sided pvalue =0.64043
NULL

Analyzing Ammonium.N in Northeast region:
Score =  -9 , Var(Score) = 165
denominator =  55
tau = -0.164, 2-sided pvalue =0.53342
NULL

Analyzing Phosphorus in Northeast region:
Score =  -17 , Var(Score) = 165
denominator =  55
tau = -0.309, 2-sided pvalue =0.21291
NULL

Analyzing Potassium in Northeast region:
Score =  9 , Var(Score) = 165
denominator =  55
tau = 0.164, 2-sided pvalue =0.53342
NULL

Analyzing Total_N in Southeast region:
Score =  -7 , Var(Score) = 165
denominator =  55
tau = -0.127, 2-sided pvalue =0.64043
NULL

Analyzing Ammonium.N in Southeast region:
Score =  -27 , Var(Score) = 165
denominator =  55
tau = -0.491, 2-sided pvalue =0.04296
NULL

Analyzing Phosphorus in Southeast region:
Score =  7 , Var(Score) = 165
denominator =  55
tau = 0.127, 2-sided pvalue =0.64043
NULL

Analyzing Potassium in Southeast region:
Score =  5 , Var(Score) = 165
denominator =  55
tau = 0.0909, 2-sided pvalue =0.7555
NULL

Four Analytes Line + IQR graph with counts + trend arrow (Beef Solid)

Code
# Filter out rows with non-missing Total_N values and for specific regions
MDB_beefstackNPK_filtered <- MDB_beefstackNPK[!is.na(MDB_beefstackNPK$Total_N) & 
                                              MDB_beefstackNPK$Region %in% c("Midwest", "Northeast", "Southeast"), ]
custom_theme <- theme_minimal() +
  theme(
    text = element_text(family = "Times New Roman", size = 11),
    axis.text.x = element_text(angle = 45, hjust = 1)
  ) 
# Create separate plots for each nutrient type
plots <- list()

# Initialize variables to store the minimum and maximum values of lower and upper quartiles
min_lower_quantile <- Inf
max_upper_quantile <- -Inf

# Function to calculate counts for each nutrient
calculate_counts <- function(data, nutrient) {
  counts <- data %>%
    filter(!is.na(.data[[nutrient]]) & Year.Analyzed >= 2012 & Year.Analyzed <= 2022) %>%
    group_by(Region) %>%
    summarise(total_samples = n())
  return(counts)
}

for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Filter data for the current nutrient type
  filtered_data <- MDB_beefstackNPK_filtered[!is.na(MDB_beefstackNPK_filtered[[nutrient]]), ]
  
  # Calculate median and IQR for each combination of Year.Analyzed and Region
  summary_stats <- filtered_data %>%
    group_by(Year.Analyzed, Region) %>%
    summarise(median_value = median(.data[[nutrient]]),
              lower_quantile = quantile(.data[[nutrient]], 0.25),
              upper_quantile = quantile(.data[[nutrient]], 0.75)) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  
  # Update minimum and maximum values of lower and upper quartiles
  min_lower_quantile <- min(min_lower_quantile, min(summary_stats$lower_quantile))
  max_upper_quantile <- max(max_upper_quantile, max(summary_stats$upper_quantile))
  
  # Determine a suitable step size for the y-axis
  y_range <- max_upper_quantile - min_lower_quantile
  num_breaks <- max(4, floor(y_range * 10))  # Ensure at least four breaks
  step_size <- y_range / num_breaks
  
  # Calculate counts for the current nutrient
  counts <- calculate_counts(filtered_data, nutrient)
  
  # Create line graph with median for each year and region, with connecting lines
  plot <- ggplot(summary_stats, aes(x = factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +
    geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha = 0.3, position = "identity") +  # Add shaded area for IQR
    geom_line(aes(linetype = Region)) +  # Add line graph with connecting lines
    geom_point() +  # Add points for each data point
    labs(x = "Year Analyzed", 
         y = switch(nutrient,
                    Total_N = "Total N (%)",
                    Ammonium.N = expression(paste("NH"[4], "-N (%)")),
                    Phosphorus = expression(paste("P"[2], "O"[5], " (%)")),
                    Potassium = expression(paste("K"[2], "O (%)"))
         ), 
         title = NULL) +  # Remove titles for all graphs
    custom_theme +  # Apply custom theme with font settings
    scale_color_manual(values = c("Midwest" = "blue", "Northeast" = "darkgreen", "Southeast" = "red")) +  # Define colors for regions
    scale_fill_manual(values = c("Midwest" = "lightblue", "Northeast" = "lightgreen", "Southeast" = "lightpink")) +  # Define fill colors for IQR
    guides(fill = FALSE) +  # Remove legend for IQR fill colors
    scale_y_continuous(limits = c(0, 1.5), breaks = seq(0, 1.5, by = 0.25)) +  # Specify breaks for y-axis labels and ensure continuous scale
    geom_text(data = counts, aes(label = ifelse(Region == "Southeast" & nutrient == "Ammonium.N", paste("* n =", total_samples), paste("n =", total_samples)), 
                                 x = "2021", y = 1.5 - (0.15 * seq_along(total_samples)), color = Region), 
              hjust = 1, vjust = 0, size = 3)  # Add text annotations for counts
  
  # Add the arrow only for the Ammonium.N graph
  if (nutrient == "Ammonium.N") {
    plot <- plot + annotate("segment", x = "2022", y = 1.15, xend = "2022", yend = 1, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "red")
  }
  
  # Add the plot to the list
  plots[[nutrient]] <- plot
}

# Arrange the plots together using patchwork
beefstack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] + plot_layout(guides = "collect")

# Print the final plot
print(beefstack_final_plot)

Chicken - Broiler

Chicken - Broiler Solid Manure by Region Overview Table (%)

Code
MDB_broilerstack<-filter(MDB_stack5pl_clean, Animal.or.Other.Amendment.Type=="Chicken - Broiler") 


df_nest = MDB_broilerstack %>% 
  group_by(Region) %>% 
  nest()

MDB_broilerstack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_broilerstack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Southeast
Total_N 2.870 0.563 19.6 2.810 0.667 23.8 4.53e-02 1.950 2.450 3.220 3.560 7.97 -0.430 1.3800 17846
Amm_N 0.411 0.163 39.6 0.420 0.180 42.8 0.00e+00 0.206 0.304 0.523 0.636 2.63 0.853 5.6400 6238
P2O5 2.510 0.699 27.9 2.560 0.792 30.9 2.91e-02 1.660 2.060 3.000 3.510 24.50 1.980 36.6000 17851
K2O 2.840 0.640 22.6 2.800 0.811 28.9 4.65e-03 1.870 2.410 3.270 3.680 28.30 2.110 65.2000 17851
Midwest
Total_N 2.620 0.474 18.1 2.540 0.635 25.0 3.00e-02 1.840 2.300 2.940 3.200 4.28 -1.280 3.0100 128
Amm_N 0.510 0.148 29.1 0.544 0.341 62.8 3.27e-05 0.308 0.390 0.596 0.722 2.61 3.940 19.2000 109
P2O5 2.460 0.636 25.9 2.420 0.737 30.5 4.80e-01 1.610 1.980 2.830 3.450 4.40 -0.107 0.3600 128
K2O 2.800 0.808 28.8 2.730 0.860 31.4 3.20e-01 1.570 2.240 3.330 3.760 4.24 -0.605 0.0709 128
Northeast
Total_N 2.780 0.639 22.9 2.730 0.751 27.5 7.00e-02 1.930 2.370 3.220 3.570 4.92 -0.984 2.1400 630
Amm_N 0.343 0.207 60.5 0.373 0.204 54.8 2.39e-03 0.135 0.223 0.506 0.636 1.31 0.739 0.7580 626
P2O5 2.050 0.771 37.5 2.090 0.799 38.2 1.03e-02 1.190 1.600 2.690 3.120 4.24 -0.166 0.1430 630
K2O 2.400 0.760 31.6 2.340 0.806 34.4 6.23e-02 1.340 1.890 2.910 3.300 4.78 -0.572 0.4680 630

Chicken - Broiler Solid Manure by Region Overview Table (lbs/ton)

Code
#Convert Units from % to lbs/ton
multiply_by_20<- MDB_broilerstack %>%
 mutate(
    Value = Value * 20)

# View the result
#print(multiply_by_20)
#View(multiply_by_20)
df_nest = multiply_by_20 %>% 
  group_by(Region) %>% 
  nest()

MDB_broilerstack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_broilerstack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Southeast
Total_N 57.40 11.30 19.6 56.10 13.30 23.8 0.907000 39.00 49.10 64.4 71.2 159.0 -0.430 1.3800 17846
Amm_N 8.22 3.26 39.6 8.40 3.59 42.8 0.000000 4.12 6.07 10.5 12.7 52.6 0.853 5.6400 6238
P2O5 50.10 14.00 27.9 51.30 15.80 30.9 0.582000 33.30 41.20 60.1 70.2 491.0 1.980 36.6000 17851
K2O 56.70 12.80 22.6 56.10 16.20 28.9 0.093100 37.30 48.10 65.4 73.6 567.0 2.110 65.2000 17851
Midwest
Total_N 52.40 9.49 18.1 50.80 12.70 25.0 0.600000 36.80 46.00 58.8 64.1 85.6 -1.280 3.0100 128
Amm_N 10.20 2.97 29.1 10.90 6.83 62.8 0.000655 6.15 7.80 11.9 14.4 52.2 3.940 19.2000 109
P2O5 49.20 12.70 25.9 48.40 14.70 30.5 9.600000 32.20 39.50 56.6 68.9 88.0 -0.107 0.3600 128
K2O 56.10 16.20 28.8 54.70 17.20 31.4 6.400000 31.40 44.70 66.6 75.1 84.8 -0.605 0.0709 128
Northeast
Total_N 55.70 12.80 22.9 54.70 15.00 27.5 1.400000 38.60 47.30 64.4 71.4 98.3 -0.984 2.1400 630
Amm_N 6.85 4.15 60.5 7.45 4.08 54.8 0.047800 2.70 4.46 10.1 12.7 26.3 0.739 0.7580 626
P2O5 41.10 15.40 37.5 41.90 16.00 38.2 0.207000 23.80 32.10 53.7 62.3 84.8 -0.166 0.1430 630
K2O 48.00 15.20 31.6 46.80 16.10 34.4 1.250000 26.80 37.70 58.2 66.1 95.7 -0.572 0.4680 630

Table of samples by region by year for Chicken - Broiler solid manure

Code
# Create a table of number of samples for each region per year
MDB_broilerstackNPK <- filter(MDB_stack5fix, Animal.or.Other.Amendment.Type == "Chicken - Broiler")
sample_table <- table(MDB_broilerstackNPK$Region, MDB_broilerstackNPK$Year.Analyzed)

# Print the sample table
print(sample_table) #NE and SE have samples all years
           
            2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
  Midwest      5    0    0    0    7    9   15   21   24   19   28
  Northeast   19   21   64   84   56  142   45   36   57   46   60
  Southeast 2462 2154 1528 1789 1623 1595 1505 1472 1276 1236 1228

Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Chicken - Broiler solid)

Code
# Load required library
library(Kendall)

# List of selected regions
selected_regions <- c("Northeast", "Southeast")

# List of analytes
analytes <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Function to perform Mann-Kendall trend test for a given analyte across all selected regions
perform_mk_test_combined <- function(data, analyte, selected_regions) {
  # Subset data for the selected regions and analyte
  filtered_data <- data[data$Region %in% selected_regions, ]
  cleaned_data <- filtered_data[!is.na(filtered_data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk_combined <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk_combined))
}

# Perform Mann-Kendall trend test for each analyte combined across selected regions
for (analyte in analytes) {
  cat("Analyzing", analyte, "for all regions combined:\n")
  print(perform_mk_test_combined(MDB_broilerstackNPK, analyte, selected_regions))
  cat("\n")
}
Analyzing Total_N for all regions combined:
Score =  -11 , Var(Score) = 165
denominator =  55
tau = -0.2, 2-sided pvalue =0.43627
NULL

Analyzing Ammonium.N for all regions combined:
Score =  -35 , Var(Score) = 165
denominator =  55
tau = -0.636, 2-sided pvalue =0.0081234
NULL

Analyzing Phosphorus for all regions combined:
Score =  -13 , Var(Score) = 165
denominator =  55
tau = -0.236, 2-sided pvalue =0.3502
NULL

Analyzing Potassium for all regions combined:
Score =  41 , Var(Score) = 165
denominator =  55
tau = 0.745, 2-sided pvalue =0.0018457
NULL
Code
# Mann-Kendall trend test by region four 4 analytes

# Function to perform Mann-Kendall trend test
perform_mk_test <- function(data, analyte) {
  # Filter out NA values in the analyte column
  cleaned_data <- data[!is.na(data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk))
}

# Iterate over selected regions and analytes
for (region in selected_regions) {
  for (analyte in analytes) {
    # Subset data for the current region
    region_data <- MDB_broilerstackNPK[MDB_broilerstackNPK$Region == region, ]
    
    # Print information about the current region and analyte
    cat("Analyzing", analyte, "in", region, "region:\n")
    
    # Perform Mann-Kendall trend test
    result_summary <- perform_mk_test(region_data, analyte)
    
    # Print summary of the Mann-Kendall test result
    print(result_summary)
    cat("\n")
  }
}
Analyzing Total_N in Northeast region:
Score =  11 , Var(Score) = 165
denominator =  55
tau = 0.2, 2-sided pvalue =0.43627
NULL

Analyzing Ammonium.N in Northeast region:
Score =  -25 , Var(Score) = 165
denominator =  55
tau = -0.455, 2-sided pvalue =0.061707
NULL

Analyzing Phosphorus in Northeast region:
Score =  15 , Var(Score) = 165
denominator =  55
tau = 0.273, 2-sided pvalue =0.27576
NULL

Analyzing Potassium in Northeast region:
Score =  23 , Var(Score) = 165
denominator =  55
tau = 0.418, 2-sided pvalue =0.086768
NULL

Analyzing Total_N in Southeast region:
Score =  -23 , Var(Score) = 165
denominator =  55
tau = -0.418, 2-sided pvalue =0.086768
NULL

Analyzing Ammonium.N in Southeast region:
Score =  -31 , Var(Score) = 165
denominator =  55
tau = -0.564, 2-sided pvalue =0.019517
NULL

Analyzing Phosphorus in Southeast region:
Score =  -19 , Var(Score) = 165
denominator =  55
tau = -0.345, 2-sided pvalue =0.16112
NULL

Analyzing Potassium in Southeast region:
Score =  37 , Var(Score) = 165
denominator =  55
tau = 0.673, 2-sided pvalue =0.0050693
NULL

Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Chicken - Broiler solid)

Code
# Filter data for selected regions
selected_regions <- c("Northeast", "Southeast")
filtered_data <- MDB_broilerstackNPK[MDB_broilerstackNPK$Region %in% selected_regions, ]

# Initialize an empty list to store cleaned data for each nutrient
cleaned_data_list <- list()

# Loop over each nutrient
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Remove missing values for the current nutrient
  cleaned_data <- filtered_data[!is.na(filtered_data[[nutrient]]), ]
  cleaned_data_list[[nutrient]] <- cleaned_data
}

# Create a Function for Wilcoxon Test and Correction
perform_wilcoxon_test <- function(region1, region2, year, nutrient, data) {
    # Extract data for the given regions, year, and nutrient
    region1_data <- data[data$Region == region1 & data$Year.Analyzed == year, nutrient]
    region2_data <- data[data$Region == region2 & data$Year.Analyzed == year, nutrient]
    
    # Perform Wilcoxon rank sum test
    wilcox_result <- wilcox.test(region1_data, region2_data)
    
    # Apply Benjamini-Hochberg correction
    adjusted_p_value <- p.adjust(wilcox_result$p.value, method = "BH")
    
    # Return results
    return(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))
}

# Iterate Over Years, Regions, and Nutrients
years <- unique(filtered_data$Year.Analyzed)
regions <- unique(filtered_data$Region)
nutrients <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Initialize empty vectors
all_p_values <- c()
comparison_results <- list()
comparison_names <- c()

# Loop over years, regions, and nutrients, perform tests, and gather p-values
index <- 1
for (year in years) {
    for (nutrient in nutrients) {
        for (i in 1:(length(regions)-1)) {
            for (j in (i+1):length(regions)) {
                region1 <- regions[i]
                region2 <- regions[j]
                
                # Perform Wilcoxon test and correction
                results <- perform_wilcoxon_test(region1, region2, year, nutrient, cleaned_data_list[[nutrient]])
                
                # Store p-value
                all_p_values <- c(all_p_values, results$wilcox_result$p.value)
                
                # Store comparison name
                comparison_names <- c(comparison_names, paste(region1, region2, year, nutrient, sep = "_"))
                
                # Store results
                comparison_results[[index]] <- results
                
                index <- index + 1
            }
        }
    }
}

# Sort comparison names based on year
sorted_indices <- order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))
sorted_comparison_names <- comparison_names[sorted_indices]

# Apply Benjamini-Hochberg Procedure
adjusted_p_values <- p.adjust(all_p_values, method = "BH")

# Print results in chronological order
for (name in sorted_comparison_names) {
    index <- match(name, comparison_names)
    year <- as.numeric(unlist(strsplit(name, "_"))[3])
    nutrient <- unlist(strsplit(name, "_"))[4]
    
    # Extract Wilcoxon test results
    result <- comparison_results[[index]]$wilcox_result
    
    # Print results with adjusted p-values
    cat("Comparison:", name, "\n")
    cat("Year:", year, "\n")
    cat("Nutrient:", nutrient, "\n")
    cat("Wilcoxon Test Statistic:", result$statistic, "\n")
    cat("Wilcoxon Test P-value:", result$p.value, "\n")
    cat("Adjusted p-value:", adjusted_p_values[index], "\n")
    cat("\n")
}
Comparison: Southeast_Northeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 23058 
Wilcoxon Test P-value: 0.9177721 
Adjusted p-value: 0.9177721 

Comparison: Southeast_Northeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 4896 
Wilcoxon Test P-value: 0.1615552 
Adjusted p-value: 0.2090714 

Comparison: Southeast_Northeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 17576 
Wilcoxon Test P-value: 0.06198993 
Adjusted p-value: 0.09091857 

Comparison: Southeast_Northeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 27939 
Wilcoxon Test P-value: 0.1425744 
Adjusted p-value: 0.1900992 

Comparison: Southeast_Northeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 25458 
Wilcoxon Test P-value: 0.3172815 
Adjusted p-value: 0.3988682 

Comparison: Southeast_Northeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 7092 
Wilcoxon Test P-value: 0.3493619 
Adjusted p-value: 0.4269979 

Comparison: Southeast_Northeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 24865 
Wilcoxon Test P-value: 0.4278888 
Adjusted p-value: 0.5088407 

Comparison: Southeast_Northeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 29478 
Wilcoxon Test P-value: 0.01617289 
Adjusted p-value: 0.02846428 

Comparison: Southeast_Northeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 56634 
Wilcoxon Test P-value: 0.03094642 
Adjusted p-value: 0.0486301 

Comparison: Southeast_Northeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 28099 
Wilcoxon Test P-value: 1.216229e-08 
Adjusted p-value: 4.864918e-08 

Comparison: Southeast_Northeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 73062 
Wilcoxon Test P-value: 1.815159e-11 
Adjusted p-value: 9.983372e-11 

Comparison: Southeast_Northeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 62840 
Wilcoxon Test P-value: 0.0001039004 
Adjusted p-value: 0.0002406114 

Comparison: Southeast_Northeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 116199 
Wilcoxon Test P-value: 1.854813e-17 
Adjusted p-value: 1.632235e-16 

Comparison: Southeast_Northeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 37077 
Wilcoxon Test P-value: 3.254062e-12 
Adjusted p-value: 2.04541e-11 

Comparison: Southeast_Northeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 132995 
Wilcoxon Test P-value: 7.063484e-33 
Adjusted p-value: 3.107933e-31 

Comparison: Southeast_Northeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 127478 
Wilcoxon Test P-value: 3.28991e-27 
Adjusted p-value: 4.825201e-26 

Comparison: Southeast_Northeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 43051.5 
Wilcoxon Test P-value: 0.5072518 
Adjusted p-value: 0.5722841 

Comparison: Southeast_Northeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 10212 
Wilcoxon Test P-value: 1.523772e-08 
Adjusted p-value: 5.587162e-08 

Comparison: Southeast_Northeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 61432 
Wilcoxon Test P-value: 7.039787e-06 
Adjusted p-value: 1.720837e-05 

Comparison: Southeast_Northeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 68556 
Wilcoxon Test P-value: 8.536835e-11 
Adjusted p-value: 4.173564e-10 

Comparison: Southeast_Northeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 124937 
Wilcoxon Test P-value: 0.03488868 
Adjusted p-value: 0.05293455 

Comparison: Southeast_Northeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 31383 
Wilcoxon Test P-value: 1.042037e-06 
Adjusted p-value: 3.056642e-06 

Comparison: Southeast_Northeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 173019 
Wilcoxon Test P-value: 7.667619e-26 
Adjusted p-value: 8.434381e-25 

Comparison: Southeast_Northeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 179298 
Wilcoxon Test P-value: 3.657969e-31 
Adjusted p-value: 8.047531e-30 

Comparison: Southeast_Northeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 29073 
Wilcoxon Test P-value: 0.1112931 
Adjusted p-value: 0.1579643 

Comparison: Southeast_Northeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 18010 
Wilcoxon Test P-value: 9.68709e-09 
Adjusted p-value: 4.26232e-08 

Comparison: Southeast_Northeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 35491 
Wilcoxon Test P-value: 0.5712532 
Adjusted p-value: 0.6130522 

Comparison: Southeast_Northeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 41596 
Wilcoxon Test P-value: 0.00847912 
Adjusted p-value: 0.01622093 

Comparison: Southeast_Northeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 14813 
Wilcoxon Test P-value: 6.48831e-06 
Adjusted p-value: 1.679327e-05 

Comparison: Southeast_Northeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 11567 
Wilcoxon Test P-value: 0.1323543 
Adjusted p-value: 0.1819872 

Comparison: Southeast_Northeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 26806 
Wilcoxon Test P-value: 0.8821972 
Adjusted p-value: 0.9027134 

Comparison: Southeast_Northeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 28210 
Wilcoxon Test P-value: 0.4879743 
Adjusted p-value: 0.5650229 

Comparison: Southeast_Northeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 43023 
Wilcoxon Test P-value: 0.0186328 
Adjusted p-value: 0.03153242 

Comparison: Southeast_Northeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 17965 
Wilcoxon Test P-value: 5.98391e-06 
Adjusted p-value: 1.645575e-05 

Comparison: Southeast_Northeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 50816 
Wilcoxon Test P-value: 3.477186e-07 
Adjusted p-value: 1.09283e-06 

Comparison: Southeast_Northeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 44411 
Wilcoxon Test P-value: 0.00449292 
Adjusted p-value: 0.008985841 

Comparison: Southeast_Northeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 15734.5 
Wilcoxon Test P-value: 2.704815e-07 
Adjusted p-value: 9.154758e-07 

Comparison: Southeast_Northeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 10508 
Wilcoxon Test P-value: 0.556905 
Adjusted p-value: 0.6125956 

Comparison: Southeast_Northeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 29156 
Wilcoxon Test P-value: 0.7606414 
Adjusted p-value: 0.7968624 

Comparison: Southeast_Northeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 34128 
Wilcoxon Test P-value: 0.02018754 
Adjusted p-value: 0.03289821 

Comparison: Southeast_Northeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 26169.5 
Wilcoxon Test P-value: 0.0001489899 
Adjusted p-value: 0.0003277779 

Comparison: Southeast_Northeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 24463 
Wilcoxon Test P-value: 6.248208e-17 
Adjusted p-value: 4.58202e-16 

Comparison: Southeast_Northeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 27281 
Wilcoxon Test P-value: 0.0006796641 
Adjusted p-value: 0.001424058 

Comparison: Southeast_Northeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 43997 
Wilcoxon Test P-value: 0.01096408 
Adjusted p-value: 0.02010082 

Chicken - Broiler 4 Analyte Median + IQR over time with counts + arrows

Code
# Filter out rows with non-missing Total_N values and for specific regions
MDB_broilerstackNPK_filtered <- MDB_broilerstackNPK[!is.na(MDB_broilerstackNPK$Total_N) & 
                                                     MDB_broilerstackNPK$Region %in% c("Northeast", "Southeast"), ]
# Define custom theme with font settings and axis adjustments
custom_theme <- theme_minimal() +
  theme(
    text = element_text(family = "Times New Roman", size = 11),
    axis.text.x = element_text(angle = 45, hjust = 1))
# Create separate plots for each nutrient type
plots <- list()

# Initialize variables to store the minimum and maximum values of lower and upper quartiles
min_lower_quantile <- Inf
max_upper_quantile <- -Inf

# Function to calculate counts for each nutrient
calculate_counts <- function(data, nutrient) {
  counts <- data %>%
    filter(!is.na(.data[[nutrient]]) & Year.Analyzed >= 2012 & Year.Analyzed <= 2022) %>%
    group_by(Region) %>%
    summarise(total_samples = n())
  return(counts)
}

for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Filter data for the current nutrient type
  filtered_data <- MDB_broilerstackNPK_filtered[!is.na(MDB_broilerstackNPK_filtered[[nutrient]]), ]
  
  # Calculate median and IQR for each combination of Year.Analyzed and Region
  summary_stats <- filtered_data %>%
    group_by(Year.Analyzed, Region) %>%
    summarise(median_value = median(.data[[nutrient]]),
              lower_quantile = quantile(.data[[nutrient]], 0.25),
              upper_quantile = quantile(.data[[nutrient]], 0.75)) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  
  # Update minimum and maximum values of lower and upper quartiles
  min_lower_quantile <- min(min_lower_quantile, min(summary_stats$lower_quantile))
  max_upper_quantile <- max(max_upper_quantile, max(summary_stats$upper_quantile))
  
  # Determine a suitable step size for the y-axis
  y_range <- max_upper_quantile - min_lower_quantile
  num_breaks <- max(4, floor(y_range * 10))  # Ensure at least four breaks
  step_size <- y_range / num_breaks
  
  # Calculate counts for the current nutrient
  counts <- calculate_counts(filtered_data, nutrient)
  
  # Create line graph with median for each year and region, with connecting lines
  plot <- ggplot(summary_stats, aes(x = factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +
    geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha = 0.3, pattern = 1) +  # Add cross-hatch pattern for IQR
    geom_line(aes(linetype = Region)) +  # Add line graph with connecting lines
    geom_point() +  # Add points for each data point
    labs(x = "Year Analyzed", 
         y = switch(nutrient,
                    Total_N = "Total N (%)",
                    Ammonium.N = expression(paste("NH"[4], "-N (%)")),
                    Phosphorus = expression(paste("P"[2], "O"[5], " (%)")),
                    Potassium = expression(paste("K"[2], "O (%)"))
         ), 
         title = NULL) +  # Remove titles for all graphs
    custom_theme +  # Apply custom theme with font settings and white background
    scale_color_manual(values = c("Northeast" = "darkgreen", "Southeast" = "red")) +  # Define colors for regions
    scale_fill_manual(values = c("Northeast" = "lightgreen", "Southeast" = "lightpink")) +  # Define fill colors for IQR
    guides(fill = FALSE) +  # Remove legend for IQR fill colors
    scale_y_continuous(limits = c(0, 4), breaks = seq(0, 4, by = 1)) +  # Specify breaks for y-axis labels and ensure continuous scale
    geom_text(data = counts, aes(label = paste("n =", total_samples), 
                                 x = "2021", y = 2 - (0.3 * seq_along(total_samples)), color = Region), 
              hjust = 1, vjust = 0, size = 3, family = "Times New Roman")  # Add text annotations for counts
  # Add the arrow only for the Ammonium.N graph
  if (nutrient == "Ammonium.N") {
    plot <- plot + annotate("segment", x = "2022", y = 1.6, xend = "2022", yend = 1.3, 
                            arrow = arrow(type = "open", length = unit(0.05, "inches")), color = "red")
  }
if (nutrient == "Ammonium.N") {
    plot <- plot + annotate("segment", x = "2022", y = 2, xend = "2022", yend = 1.65, 
                            arrow = arrow(type = "open", length = unit(0.05, "inches")), color = "darkgreen")
}
   if (nutrient == "Potassium") {
    plot <- plot + annotate("segment", x = "2022", y = 1.3, xend = "2022", yend = 1.65, 
                            arrow = arrow(type = "open", length = unit(0.05, "inches")), color = "red")
  }
  # Add the plot to the list
  plots[[nutrient]] <- plot
}

# Arrange the plots together using patchwork
broilerstack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] + plot_layout(guides = "collect")

# Print the final plot
print(broilerstack_final_plot)

Chicken - Layer

Chicken - Layer Solid Manure by Region Overview Table (%)

Code
MDB_layerstack<-filter(MDB_stack5pl_clean, Animal.or.Other.Amendment.Type=="Chicken - Layer") 


df_nest = MDB_layerstack %>% 
  group_by(Region) %>% 
  nest()

MDB_layerstack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_layerstack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Southeast
Total_N 2.130 0.922 43.4 2.140 0.935 43.7 1.50e-02 0.9560 1.480 2.730 3.330 7.320 0.389 0.8460 2797
Amm_N 0.218 0.130 59.7 0.236 0.144 61.1 0.00e+00 0.0656 0.140 0.313 0.409 0.996 1.090 2.5300 1349
P2O5 2.780 1.090 39.4 2.850 1.340 47.2 0.00e+00 1.3500 2.100 3.580 4.310 21.000 2.780 31.8000 2862
K2O 2.080 0.741 35.6 2.090 1.040 50.0 0.00e+00 0.9530 1.580 2.570 3.080 24.700 6.260 117.0000 2862
Midwest
Total_N 2.230 0.682 30.6 2.440 0.993 40.7 2.00e-02 1.4600 1.920 3.010 3.580 6.470 0.855 1.8400 176
Amm_N 0.480 0.261 54.4 0.518 0.354 68.5 8.42e-06 0.1290 0.320 0.656 0.906 1.900 1.280 2.6200 109
P2O5 2.630 1.390 52.7 2.880 1.320 45.8 1.30e-01 1.3500 1.810 3.850 4.760 6.340 0.457 -0.4370 176
K2O 2.070 0.967 46.7 2.110 0.837 39.7 2.10e-01 1.0800 1.480 2.780 3.170 4.470 0.121 -0.5230 176
Northeast
Total_N 2.280 0.989 43.4 2.420 0.975 40.3 3.98e-01 1.2100 1.700 3.050 3.750 5.800 0.566 0.0752 507
Amm_N 0.419 0.241 57.7 0.463 0.281 60.8 1.90e-02 0.1460 0.265 0.589 0.826 1.610 1.110 1.6600 507
P2O5 2.290 1.130 49.5 2.490 1.070 43.1 1.96e-01 1.2700 1.640 3.160 3.890 7.170 0.779 0.6280 507
K2O 1.570 0.783 49.9 1.640 0.763 46.6 1.71e-01 0.6240 1.080 2.130 2.650 4.840 0.567 0.2730 507
Southwest
Total_N 2.150 0.838 39.0 2.590 1.190 46.2 5.80e-01 1.5000 1.790 3.230 4.060 6.230 1.170 1.1100 46
Amm_N NA NA NA NA NA NA NA NA NA NA NA NA NA NA 0
P2O5 2.050 0.815 39.8 2.340 1.090 46.4 2.29e-02 1.2600 1.650 2.770 3.730 5.540 0.903 1.0400 46
K2O 1.240 0.543 43.9 1.270 0.562 44.2 9.60e-02 0.7380 0.855 1.470 1.910 2.990 0.869 1.0300 46

Chicken - Layer Solid Manure by Region Overview Table (lbs/ton)

Code
 #Convert Units from % to lbs/ton
multiply_by_20<- MDB_layerstack %>%
 mutate(
    Value = Value * 20)
df_nest = multiply_by_20 %>% 
  group_by(Region) %>% 
  nest()

MDB_layerstack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_layerstack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Southeast
Total_N 42.50 18.40 43.4 42.80 18.70 43.7 3.00e-01 19.10 29.70 54.70 66.50 146.0 0.389 0.8460 2797
Amm_N 4.36 2.60 59.7 4.71 2.88 61.1 0.00e+00 1.31 2.79 6.26 8.19 19.9 1.090 2.5300 1349
P2O5 55.50 21.90 39.4 57.00 26.90 47.2 0.00e+00 27.00 42.00 71.50 86.10 421.0 2.780 31.8000 2862
K2O 41.60 14.80 35.6 41.70 20.90 50.0 0.00e+00 19.10 31.50 51.50 61.60 493.0 6.260 117.0000 2862
Midwest
Total_N 44.60 13.60 30.6 48.80 19.90 40.7 4.00e-01 29.20 38.30 60.20 71.70 129.0 0.855 1.8400 176
Amm_N 9.60 5.22 54.4 10.40 7.09 68.5 1.68e-04 2.58 6.40 13.10 18.10 38.0 1.280 2.6200 109
P2O5 52.60 27.70 52.7 57.50 26.40 45.8 2.60e+00 27.00 36.20 77.00 95.20 127.0 0.457 -0.4370 176
K2O 41.40 19.30 46.7 42.10 16.70 39.7 4.20e+00 21.50 29.50 55.60 63.40 89.4 0.121 -0.5230 176
Northeast
Total_N 45.60 19.80 43.4 48.40 19.50 40.3 7.96e+00 24.30 33.90 61.00 75.00 116.0 0.566 0.0752 507
Amm_N 8.38 4.83 57.7 9.27 5.63 60.8 3.81e-01 2.91 5.30 11.80 16.50 32.3 1.110 1.6600 507
P2O5 45.90 22.70 49.5 49.80 21.50 43.1 3.93e+00 25.30 32.90 63.20 77.80 143.0 0.779 0.6280 507
K2O 31.40 15.70 49.9 32.80 15.30 46.6 3.43e+00 12.50 21.60 42.60 52.90 96.7 0.567 0.2730 507
Southwest
Total_N 43.00 16.80 39.0 51.70 23.90 46.2 1.16e+01 29.90 35.80 64.60 81.10 125.0 1.170 1.1100 46
Amm_N NA NA NA NA NA NA NA NA NA NA NA NA NA NA 0
P2O5 41.00 16.30 39.8 46.70 21.70 46.4 4.58e-01 25.20 33.10 55.30 74.70 111.0 0.903 1.0400 46
K2O 24.70 10.90 43.9 25.40 11.20 44.2 1.92e+00 14.80 17.10 29.50 38.30 59.8 0.869 1.0300 46

Table of samples by region by year for Chicken Layer manure

Code
# Sort out for layer while still having separate NPK columns
MDB_layerstackNPK <- filter(MDB_stack5fix, Animal.or.Other.Amendment.Type == "Chicken - Layer")
# Create a table of number of samples for each region per year
sample_table <- table(MDB_layerstackNPK$Region, MDB_layerstackNPK$Year.Analyzed)

# Print the sample table
print(sample_table)
           
            2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
  Midwest     13    8   11   15   20   22   17   18   18   22   12
  Northeast  111   30   25   29   79   80   30   42   22   28   31
  Southeast  253  288  301  306  291  249  303  270  206  193  215
  Southwest    0    0    3    0    0    0   32    6    5    0    0

Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Chicken - Layer)

Code
# Load required library
library(Kendall)

# List of selected regions
selected_regions <- c("Northeast", "Southeast")

# List of analytes
analytes <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Function to perform Mann-Kendall trend test for a given analyte across all selected regions
perform_mk_test_combined <- function(data, analyte, selected_regions) {
  # Subset data for the selected regions and analyte
  filtered_data <- data[data$Region %in% selected_regions, ]
  cleaned_data <- filtered_data[!is.na(filtered_data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk_combined <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk_combined))
}

# Perform Mann-Kendall trend test for each analyte combined across selected regions
for (analyte in analytes) {
  cat("Analyzing", analyte, "for all regions combined:\n")
  print(perform_mk_test_combined(MDB_layerstackNPK, analyte, selected_regions))
  cat("\n")
}
Analyzing Total_N for all regions combined:
Score =  -13 , Var(Score) = 165
denominator =  55
tau = -0.236, 2-sided pvalue =0.3502
NULL

Analyzing Ammonium.N for all regions combined:
Score =  -41 , Var(Score) = 165
denominator =  55
tau = -0.745, 2-sided pvalue =0.0018457
NULL

Analyzing Phosphorus for all regions combined:
Score =  -33 , Var(Score) = 165
denominator =  55
tau = -0.6, 2-sided pvalue =0.012731
NULL

Analyzing Potassium for all regions combined:
Score =  -27 , Var(Score) = 165
denominator =  55
tau = -0.491, 2-sided pvalue =0.04296
NULL
Code
# Function to perform Mann-Kendall trend test
perform_mk_test <- function(data, analyte) {
  # Filter out NA values in the analyte column
  cleaned_data <- data[!is.na(data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk))
}

# Iterate over selected regions and analytes
for (region in selected_regions) {
  for (analyte in analytes) {
    # Subset data for the current region
    region_data <- MDB_layerstackNPK[MDB_layerstackNPK$Region == region, ]
    
    # Print information about the current region and analyte
    cat("Analyzing", analyte, "in", region, "region:\n")
    
    # Perform Mann-Kendall trend test
    result_summary <- perform_mk_test(region_data, analyte)
    
    # Print summary of the Mann-Kendall test result
    print(result_summary)
    cat("\n")
  }
}
Analyzing Total_N in Northeast region:
Score =  25 , Var(Score) = 165
denominator =  55
tau = 0.455, 2-sided pvalue =0.061707
NULL

Analyzing Ammonium.N in Northeast region:
Score =  -19 , Var(Score) = 165
denominator =  55
tau = -0.345, 2-sided pvalue =0.16112
NULL

Analyzing Phosphorus in Northeast region:
Score =  13 , Var(Score) = 165
denominator =  55
tau = 0.236, 2-sided pvalue =0.3502
NULL

Analyzing Potassium in Northeast region:
Score =  25 , Var(Score) = 165
denominator =  55
tau = 0.455, 2-sided pvalue =0.061707
NULL

Analyzing Total_N in Southeast region:
Score =  -17 , Var(Score) = 165
denominator =  55
tau = -0.309, 2-sided pvalue =0.21291
NULL

Analyzing Ammonium.N in Southeast region:
Score =  -27 , Var(Score) = 165
denominator =  55
tau = -0.491, 2-sided pvalue =0.04296
NULL

Analyzing Phosphorus in Southeast region:
Score =  -43 , Var(Score) = 165
denominator =  55
tau = -0.782, 2-sided pvalue =0.0010766
NULL

Analyzing Potassium in Southeast region:
Score =  -33 , Var(Score) = 165
denominator =  55
tau = -0.6, 2-sided pvalue =0.012731
NULL

Four analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Chicken - Layer Solid)

Code
# Filter data for selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")
filtered_data <- MDB_layerstackNPK[MDB_layerstackNPK$Region %in% selected_regions, ]

# Initialize an empty list to store cleaned data for each nutrient
cleaned_data_list <- list()

# Loop over each nutrient
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Remove missing values for the current nutrient
  cleaned_data <- filtered_data[!is.na(filtered_data[[nutrient]]), ]
  cleaned_data_list[[nutrient]] <- cleaned_data
}

# Create a Function for Wilcoxon Test and Correction
perform_wilcoxon_test <- function(region1, region2, year, nutrient, data) {
    # Extract data for the given regions, year, and nutrient
    region1_data <- data[data$Region == region1 & data$Year.Analyzed == year, nutrient]
    region2_data <- data[data$Region == region2 & data$Year.Analyzed == year, nutrient]
    
    # Perform Wilcoxon rank sum test
    wilcox_result <- wilcox.test(region1_data, region2_data)
    # Perform Wilcoxon rank sum test with 'exact' argument
    #wilcox_result <- wilcox.test(region1_data, region2_data, exact = TRUE)
    
    # Apply Benjamini-Hochberg correction
    adjusted_p_value <- p.adjust(wilcox_result$p.value, method = "BH")
    
    # Return results
    return(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))
}

# Iterate Over Years, Regions, and Nutrients
years <- unique(filtered_data$Year.Analyzed)
regions <- unique(filtered_data$Region)
nutrients <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Initialize empty vectors
all_p_values <- c()
comparison_results <- list()
comparison_names <- c()

# Loop over years, regions, and nutrients, perform tests, and gather p-values
index <- 1
for (year in years) {
    for (nutrient in nutrients) {
        for (i in 1:(length(regions)-1)) {
            for (j in (i+1):length(regions)) {
                region1 <- regions[i]
                region2 <- regions[j]
                
                # Perform Wilcoxon test and correction
                results <- perform_wilcoxon_test(region1, region2, year, nutrient, cleaned_data_list[[nutrient]])
                
                # Store p-value
                all_p_values <- c(all_p_values, results$wilcox_result$p.value)
                
                # Store comparison name
                comparison_names <- c(comparison_names, paste(region1, region2, year, nutrient, sep = "_"))
                
                # Store results
                comparison_results[[index]] <- results
                
                index <- index + 1
            }
        }
    }
}

# Sort comparison names based on year
sorted_indices <- order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))
sorted_comparison_names <- comparison_names[sorted_indices]

# Apply Benjamini-Hochberg Procedure
adjusted_p_values <- p.adjust(all_p_values, method = "BH")

# Print results in chronological order
for (name in sorted_comparison_names) {
    index <- match(name, comparison_names)
    year <- as.numeric(unlist(strsplit(name, "_"))[3])
    nutrient <- unlist(strsplit(name, "_"))[4]
    
    # Extract Wilcoxon test results
    result <- comparison_results[[index]]$wilcox_result
    
    # Print results with adjusted p-values
    cat("Comparison:", name, "\n")
    cat("Year:", year, "\n")
    cat("Nutrient:", nutrient, "\n")
    cat("Wilcoxon Test Statistic:", result$statistic, "\n")
    cat("Wilcoxon Test P-value:", result$p.value, "\n")
    cat("Adjusted p-value:", adjusted_p_values[index], "\n")
    cat("\n")
}
Comparison: Southeast_Midwest_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 1453 
Wilcoxon Test P-value: 0.6280891 
Adjusted p-value: 0.7676644 

Comparison: Southeast_Northeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 16263 
Wilcoxon Test P-value: 0.001885838 
Adjusted p-value: 0.008030019 

Comparison: Midwest_Northeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 939 
Wilcoxon Test P-value: 0.07673466 
Adjusted p-value: 0.1716776 

Comparison: Southeast_Midwest_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 5 
Wilcoxon Test P-value: 0.00417844 
Adjusted p-value: 0.01662743 

Comparison: Southeast_Northeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1378 
Wilcoxon Test P-value: 5.88928e-26 
Adjusted p-value: 7.77385e-24 

Comparison: Midwest_Northeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 196 
Wilcoxon Test P-value: 0.6077032 
Adjusted p-value: 0.7567625 

Comparison: Southeast_Midwest_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1899 
Wilcoxon Test P-value: 0.3477627 
Adjusted p-value: 0.4989639 

Comparison: Southeast_Northeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 20689 
Wilcoxon Test P-value: 6.386111e-13 
Adjusted p-value: 2.107417e-11 

Comparison: Midwest_Northeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 972 
Wilcoxon Test P-value: 0.0414382 
Adjusted p-value: 0.1139551 

Comparison: Southeast_Midwest_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1701 
Wilcoxon Test P-value: 0.8360021 
Adjusted p-value: 0.9464435 

Comparison: Southeast_Northeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 20700 
Wilcoxon Test P-value: 5.852419e-13 
Adjusted p-value: 2.107417e-11 

Comparison: Midwest_Northeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1036 
Wilcoxon Test P-value: 0.01043303 
Adjusted p-value: 0.03358925 

Comparison: Southeast_Midwest_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 684.5 
Wilcoxon Test P-value: 0.05822502 
Adjusted p-value: 0.1380952 

Comparison: Southeast_Northeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 4307 
Wilcoxon Test P-value: 0.8706341 
Adjusted p-value: 0.9497827 

Comparison: Midwest_Northeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 170 
Wilcoxon Test P-value: 0.07604255 
Adjusted p-value: 0.1716776 

Comparison: Southeast_Midwest_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 10 
Wilcoxon Test P-value: 0.1556771 
Adjusted p-value: 0.2854081 

Comparison: Southeast_Northeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 872 
Wilcoxon Test P-value: 1.105768e-05 
Adjusted p-value: 7.68218e-05 

Comparison: Midwest_Northeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 19 
Wilcoxon Test P-value: 0.7741935 
Adjusted p-value: 0.9027055 

Comparison: Southeast_Midwest_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1702 
Wilcoxon Test P-value: 0.02138494 
Adjusted p-value: 0.06272916 

Comparison: Southeast_Northeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 5617 
Wilcoxon Test P-value: 0.006824839 
Adjusted p-value: 0.02252197 

Comparison: Midwest_Northeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 87 
Wilcoxon Test P-value: 0.2499664 
Adjusted p-value: 0.4023849 

Comparison: Southeast_Midwest_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1343 
Wilcoxon Test P-value: 0.4250161 
Adjusted p-value: 0.5905487 

Comparison: Southeast_Northeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 5693 
Wilcoxon Test P-value: 0.004185208 
Adjusted p-value: 0.01662743 

Comparison: Midwest_Northeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 141 
Wilcoxon Test P-value: 0.4704272 
Adjusted p-value: 0.6209639 

Comparison: Southeast_Midwest_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 1351.5 
Wilcoxon Test P-value: 0.3317742 
Adjusted p-value: 0.4863093 

Comparison: Southeast_Northeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 3719 
Wilcoxon Test P-value: 0.9892917 
Adjusted p-value: 0.9936914 

Comparison: Midwest_Northeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 173 
Wilcoxon Test P-value: 0.2332888 
Adjusted p-value: 0.3801744 

Comparison: Southeast_Midwest_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 75 
Wilcoxon Test P-value: 0.04915145 
Adjusted p-value: 0.1247581 

Comparison: Southeast_Northeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 770 
Wilcoxon Test P-value: 2.481988e-06 
Adjusted p-value: 2.520172e-05 

Comparison: Midwest_Northeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 42 
Wilcoxon Test P-value: 0.7796093 
Adjusted p-value: 0.9027055 

Comparison: Southeast_Midwest_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1678 
Wilcoxon Test P-value: 0.9252017 
Adjusted p-value: 0.9928994 

Comparison: Southeast_Northeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 3973 
Wilcoxon Test P-value: 0.6220653 
Adjusted p-value: 0.7674076 

Comparison: Midwest_Northeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 149 
Wilcoxon Test P-value: 0.7098086 
Adjusted p-value: 0.8365601 

Comparison: Southeast_Midwest_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1424 
Wilcoxon Test P-value: 0.4413921 
Adjusted p-value: 0.5945282 

Comparison: Southeast_Northeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 3769 
Wilcoxon Test P-value: 0.967308 
Adjusted p-value: 0.9936914 

Comparison: Midwest_Northeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 164 
Wilcoxon Test P-value: 0.377525 
Adjusted p-value: 0.5326553 

Comparison: Southeast_Midwest_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 2197.5 
Wilcoxon Test P-value: 0.9833434 
Adjusted p-value: 0.9936914 

Comparison: Southeast_Northeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 5584 
Wilcoxon Test P-value: 0.004639691 
Adjusted p-value: 0.0170122 

Comparison: Midwest_Northeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 317 
Wilcoxon Test P-value: 0.01423552 
Adjusted p-value: 0.04369973 

Comparison: Southeast_Midwest_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 228 
Wilcoxon Test P-value: 0.0001602837 
Adjusted p-value: 0.0008532465 

Comparison: Southeast_Northeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 877 
Wilcoxon Test P-value: 1.164663e-07 
Adjusted p-value: 1.921694e-06 

Comparison: Midwest_Northeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 165 
Wilcoxon Test P-value: 0.5364991 
Adjusted p-value: 0.6875523 

Comparison: Southeast_Midwest_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1296 
Wilcoxon Test P-value: 0.00443912 
Adjusted p-value: 0.01674182 

Comparison: Southeast_Northeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 6358 
Wilcoxon Test P-value: 0.0001167785 
Adjusted p-value: 0.0006702071 

Comparison: Midwest_Northeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 382 
Wilcoxon Test P-value: 1.312236e-05 
Adjusted p-value: 8.660755e-05 

Comparison: Southeast_Midwest_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1292 
Wilcoxon Test P-value: 0.004282823 
Adjusted p-value: 0.01662743 

Comparison: Southeast_Northeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 6851 
Wilcoxon Test P-value: 1.286557e-06 
Adjusted p-value: 1.415213e-05 

Comparison: Midwest_Northeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 398 
Wilcoxon Test P-value: 9.238159e-07 
Adjusted p-value: 1.108579e-05 

Comparison: Southeast_Midwest_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 2239 
Wilcoxon Test P-value: 0.1464574 
Adjusted p-value: 0.275767 

Comparison: Southeast_Northeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 6849.5 
Wilcoxon Test P-value: 3.33417e-07 
Adjusted p-value: 4.401104e-06 

Comparison: Midwest_Northeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 618.5 
Wilcoxon Test P-value: 0.1361569 
Adjusted p-value: 0.2682495 

Comparison: Southeast_Midwest_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 771 
Wilcoxon Test P-value: 0.01112864 
Adjusted p-value: 0.03497573 

Comparison: Southeast_Northeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2820 
Wilcoxon Test P-value: 8.419571e-12 
Adjusted p-value: 2.222767e-10 

Comparison: Midwest_Northeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 614 
Wilcoxon Test P-value: 0.8618433 
Adjusted p-value: 0.9497827 

Comparison: Southeast_Midwest_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 3295 
Wilcoxon Test P-value: 0.3089055 
Adjusted p-value: 0.4633583 

Comparison: Southeast_Northeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 13347 
Wilcoxon Test P-value: 0.02441572 
Adjusted p-value: 0.07006251 

Comparison: Midwest_Northeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 808 
Wilcoxon Test P-value: 0.8787842 
Adjusted p-value: 0.9508157 

Comparison: Southeast_Midwest_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2910 
Wilcoxon Test P-value: 0.9804514 
Adjusted p-value: 0.9936914 

Comparison: Southeast_Northeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 15306 
Wilcoxon Test P-value: 4.619636e-06 
Adjusted p-value: 3.387733e-05 

Comparison: Midwest_Northeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1023 
Wilcoxon Test P-value: 0.04274299 
Adjusted p-value: 0.1151444 

Comparison: Southeast_Midwest_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 2105.5 
Wilcoxon Test P-value: 0.07639628 
Adjusted p-value: 0.1716776 

Comparison: Southeast_Northeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 7893 
Wilcoxon Test P-value: 0.006001457 
Adjusted p-value: 0.0214106 

Comparison: Midwest_Northeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 890 
Wilcoxon Test P-value: 0.9383906 
Adjusted p-value: 0.9936914 

Comparison: Southeast_Midwest_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 217 
Wilcoxon Test P-value: 3.604954e-06 
Adjusted p-value: 2.974087e-05 

Comparison: Southeast_Northeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2397 
Wilcoxon Test P-value: 6.003115e-08 
Adjusted p-value: 1.132016e-06 

Comparison: Midwest_Northeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 808 
Wilcoxon Test P-value: 0.03419303 
Adjusted p-value: 0.09603148 

Comparison: Southeast_Midwest_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2996 
Wilcoxon Test P-value: 0.4666637 
Adjusted p-value: 0.6209639 

Comparison: Southeast_Northeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 13429 
Wilcoxon Test P-value: 2.781796e-06 
Adjusted p-value: 2.622837e-05 

Comparison: Midwest_Northeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1046 
Wilcoxon Test P-value: 0.178134 
Adjusted p-value: 0.3135159 

Comparison: Southeast_Midwest_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 3204 
Wilcoxon Test P-value: 0.1874371 
Adjusted p-value: 0.3255486 

Comparison: Southeast_Northeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 15741 
Wilcoxon Test P-value: 5.716059e-15 
Adjusted p-value: 3.772599e-13 

Comparison: Midwest_Northeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1216 
Wilcoxon Test P-value: 0.006339862 
Adjusted p-value: 0.02145799 

Comparison: Southeast_Midwest_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 2856.5 
Wilcoxon Test P-value: 0.3352587 
Adjusted p-value: 0.4863093 

Comparison: Southeast_Northeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 3571 
Wilcoxon Test P-value: 0.08174325 
Adjusted p-value: 0.174034 

Comparison: Midwest_Northeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 181.5 
Wilcoxon Test P-value: 0.1060308 
Adjusted p-value: 0.2170272 

Comparison: Southeast_Midwest_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 387 
Wilcoxon Test P-value: 0.0003930479 
Adjusted p-value: 0.001921568 

Comparison: Southeast_Northeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 787 
Wilcoxon Test P-value: 3.186285e-07 
Adjusted p-value: 4.401104e-06 

Comparison: Midwest_Northeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 192 
Wilcoxon Test P-value: 0.6629693 
Adjusted p-value: 0.7955632 

Comparison: Southeast_Midwest_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2295 
Wilcoxon Test P-value: 0.5020103 
Adjusted p-value: 0.6496604 

Comparison: Southeast_Northeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 5112 
Wilcoxon Test P-value: 0.2071588 
Adjusted p-value: 0.3505765 

Comparison: Midwest_Northeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 305 
Wilcoxon Test P-value: 0.276251 
Adjusted p-value: 0.4290015 

Comparison: Southeast_Midwest_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2548 
Wilcoxon Test P-value: 0.9869361 
Adjusted p-value: 0.9936914 

Comparison: Southeast_Northeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 5631 
Wilcoxon Test P-value: 0.02108892 
Adjusted p-value: 0.06272916 

Comparison: Midwest_Northeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 303 
Wilcoxon Test P-value: 0.2929241 
Adjusted p-value: 0.4496045 

Comparison: Southeast_Midwest_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 1350 
Wilcoxon Test P-value: 0.001871887 
Adjusted p-value: 0.008030019 

Comparison: Southeast_Northeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 3576 
Wilcoxon Test P-value: 0.0001615997 
Adjusted p-value: 0.0008532465 

Comparison: Midwest_Northeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 391 
Wilcoxon Test P-value: 0.8401923 
Adjusted p-value: 0.9464435 

Comparison: Southeast_Midwest_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 213 
Wilcoxon Test P-value: 6.091222e-05 
Adjusted p-value: 0.0003828768 

Comparison: Southeast_Northeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1042 
Wilcoxon Test P-value: 1.633798e-08 
Adjusted p-value: 3.594356e-07 

Comparison: Midwest_Northeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 315 
Wilcoxon Test P-value: 0.1961672 
Adjusted p-value: 0.3362867 

Comparison: Southeast_Midwest_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2046 
Wilcoxon Test P-value: 0.2623061 
Adjusted p-value: 0.4121953 

Comparison: Southeast_Northeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 5659 
Wilcoxon Test P-value: 0.9845967 
Adjusted p-value: 0.9936914 

Comparison: Midwest_Northeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 433 
Wilcoxon Test P-value: 0.3793152 
Adjusted p-value: 0.5326553 

Comparison: Southeast_Midwest_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2076 
Wilcoxon Test P-value: 0.3014785 
Adjusted p-value: 0.4574156 

Comparison: Southeast_Northeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 6724 
Wilcoxon Test P-value: 0.05273616 
Adjusted p-value: 0.1289106 

Comparison: Midwest_Northeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 487 
Wilcoxon Test P-value: 0.08001779 
Adjusted p-value: 0.174034 

Comparison: Southeast_Midwest_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 1288 
Wilcoxon Test P-value: 0.05858586 
Adjusted p-value: 0.1380952 

Comparison: Southeast_Northeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 1389 
Wilcoxon Test P-value: 0.006287827 
Adjusted p-value: 0.02145799 

Comparison: Midwest_Northeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 169 
Wilcoxon Test P-value: 0.4384297 
Adjusted p-value: 0.5945282 

Comparison: Southeast_Midwest_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 76 
Wilcoxon Test P-value: 3.277429e-06 
Adjusted p-value: 2.884137e-05 

Comparison: Southeast_Northeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 398 
Wilcoxon Test P-value: 4.055134e-06 
Adjusted p-value: 3.148692e-05 

Comparison: Midwest_Northeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 174 
Wilcoxon Test P-value: 0.04495086 
Adjusted p-value: 0.1168283 

Comparison: Southeast_Midwest_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1429 
Wilcoxon Test P-value: 0.1483292 
Adjusted p-value: 0.275767 

Comparison: Southeast_Northeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2032 
Wilcoxon Test P-value: 0.5580305 
Adjusted p-value: 0.7050077 

Comparison: Midwest_Northeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 224 
Wilcoxon Test P-value: 0.4924008 
Adjusted p-value: 0.6435337 

Comparison: Southeast_Midwest_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1599 
Wilcoxon Test P-value: 0.4340824 
Adjusted p-value: 0.5945282 

Comparison: Southeast_Northeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2617 
Wilcoxon Test P-value: 0.1452393 
Adjusted p-value: 0.275767 

Comparison: Midwest_Northeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 257 
Wilcoxon Test P-value: 0.1124158 
Adjusted p-value: 0.2248316 

Comparison: Southeast_Midwest_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 1704 
Wilcoxon Test P-value: 0.1670453 
Adjusted p-value: 0.3020546 

Comparison: Southeast_Northeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 1444.5 
Wilcoxon Test P-value: 0.0001073132 
Adjusted p-value: 0.0006438792 

Comparison: Midwest_Northeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 205 
Wilcoxon Test P-value: 0.04513822 
Adjusted p-value: 0.1168283 

Comparison: Southeast_Midwest_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 225 
Wilcoxon Test P-value: 0.0003365128 
Adjusted p-value: 0.00170845 

Comparison: Southeast_Northeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 631 
Wilcoxon Test P-value: 0.0004920331 
Adjusted p-value: 0.002319584 

Comparison: Midwest_Northeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 248 
Wilcoxon Test P-value: 0.1693911 
Adjusted p-value: 0.3021572 

Comparison: Southeast_Midwest_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2460 
Wilcoxon Test P-value: 0.2235315 
Adjusted p-value: 0.3734956 

Comparison: Southeast_Northeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2393 
Wilcoxon Test P-value: 0.3292184 
Adjusted p-value: 0.4863093 

Comparison: Midwest_Northeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 223 
Wilcoxon Test P-value: 0.09893432 
Adjusted p-value: 0.207291 

Comparison: Southeast_Midwest_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2526 
Wilcoxon Test P-value: 0.1454126 
Adjusted p-value: 0.275767 

Comparison: Southeast_Northeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2699 
Wilcoxon Test P-value: 0.9936914 
Adjusted p-value: 0.9936914 

Comparison: Midwest_Northeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 225 
Wilcoxon Test P-value: 0.1068695 
Adjusted p-value: 0.2170272 

Comparison: Southeast_Midwest_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 1157 
Wilcoxon Test P-value: 0.635746 
Adjusted p-value: 0.7698943 

Comparison: Southeast_Northeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 2021 
Wilcoxon Test P-value: 0.0006632632 
Adjusted p-value: 0.003018991 

Comparison: Midwest_Northeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 121 
Wilcoxon Test P-value: 0.08066288 
Adjusted p-value: 0.174034 

Comparison: Southeast_Midwest_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 515 
Wilcoxon Test P-value: 0.7951143 
Adjusted p-value: 0.912653 

Comparison: Southeast_Northeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1738 
Wilcoxon Test P-value: 0.2284288 
Adjusted p-value: 0.3769076 

Comparison: Midwest_Northeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 148 
Wilcoxon Test P-value: 0.8460631 
Adjusted p-value: 0.9464435 

Comparison: Southeast_Midwest_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1271 
Wilcoxon Test P-value: 0.954773 
Adjusted p-value: 0.9936914 

Comparison: Southeast_Northeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2594 
Wilcoxon Test P-value: 0.05009227 
Adjusted p-value: 0.1247581 

Comparison: Midwest_Northeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 143 
Wilcoxon Test P-value: 0.2536444 
Adjusted p-value: 0.4033863 

Comparison: Southeast_Midwest_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1322 
Wilcoxon Test P-value: 0.8648985 
Adjusted p-value: 0.9497827 

Comparison: Southeast_Northeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 3102 
Wilcoxon Test P-value: 0.5608016 
Adjusted p-value: 0.7050077 

Comparison: Midwest_Northeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 171 
Wilcoxon Test P-value: 0.6984722 
Adjusted p-value: 0.8306156 

Four analyte median + IQR plots 2012-2022 with counts + arrows (Chicken-Layer )

Code
# Filter out rows with non-missing Total_N values and for specific regions
MDB_layerstackNPK_filtered <- MDB_layerstackNPK[!is.na(MDB_layerstackNPK$Total_N) & 
                                                 MDB_layerstackNPK$Region %in% c("Northeast", "Southeast"), ]
custom_theme <- theme_minimal() +
  theme(
    text = element_text(family = "Times New Roman", size = 11),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

# Create separate plots for each nutrient type
plots <- list()

# Initialize variables to store the minimum and maximum values of lower and upper quartiles
min_lower_quantile <- Inf
max_upper_quantile <- -Inf
# Function to calculate counts for each nutrient
calculate_counts <- function(data, nutrient) {
  counts <- data %>%
    filter(!is.na(.data[[nutrient]]) & Year.Analyzed >= 2012 & Year.Analyzed <= 2022) %>%
    group_by(Region) %>%
    summarise(total_samples = n()) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  return(counts)
}

  
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Filter data for the current nutrient type
  filtered_data <- MDB_layerstackNPK_filtered[!is.na(MDB_layerstackNPK_filtered[[nutrient]]), ]
  
  # Calculate median and IQR for each combination of Year.Analyzed and Region
  summary_stats <- filtered_data %>%
    group_by(Year.Analyzed, Region) %>%
    summarise(median_value = median(.data[[nutrient]]),
              lower_quantile = quantile(.data[[nutrient]], 0.25),
              upper_quantile = quantile(.data[[nutrient]], 0.75)) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  
  # Update minimum and maximum values of lower and upper quartiles
  min_lower_quantile <- min(min_lower_quantile, min(summary_stats$lower_quantile))
  max_upper_quantile <- max(max_upper_quantile, max(summary_stats$upper_quantile))
  
  # Determine a suitable step size for the y-axis
  y_range <- max_upper_quantile - min_lower_quantile
  num_breaks <- max(4, floor(y_range * 10))  # Ensure at least four breaks
  step_size <- y_range / num_breaks
    # Calculate counts for the current nutrient
  counts <- calculate_counts(filtered_data, nutrient)

  # Create line graph with median for each year and region, with connecting lines
  plot <- ggplot(summary_stats, aes(x = factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +
    geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha = 0.3) +  # Add shaded area for IQR
    geom_line(aes(linetype = Region)) +  # Add line graph with connecting lines
    geom_point() +  # Add points for each data point
    labs(x = "Year Analyzed", 
         y = switch(nutrient,
                    Total_N = "Total N (%)",
                    Ammonium.N = expression(paste("NH"[4], "-N (%)")),
                    Phosphorus = expression(paste("P"[2], "O"[5], " (%)")),
                    Potassium = expression(paste("K"[2], "O (%)"))
         ), 
         title = NULL) +  # Remove titles for all graphs
    custom_theme +  # Apply custom theme with font settings
    scale_color_manual(values = c("Northeast" = "darkgreen", "Southeast" = "red", "Midwest" = "blue")) +  # Define colors for regions
    scale_fill_manual(values = c("Northeast" = "lightgreen", "Southeast" = "lightpink", "Midwest" = "lightblue")) +  # Define fill colors for IQR
    guides(fill = FALSE) +  # Remove legend for IQR fill colors
    scale_y_continuous(limits = c(0, 5), breaks = seq(0, 5, by = 1))+
                       #breaks = seq(ceiling(min_lower_quantile), floor(max_upper_quantile), by = step_size),
                       #expand = c(0, 0))  # Specify breaks for y-axis labels and ensure continuous scale
  geom_text(data = counts, aes(label = paste("n =", total_samples), 
                                 x = "2021", y = 5 - (0.4* seq_along(total_samples)), color = Region), 
              hjust = 1, vjust = 0, size = 3, family = "Times New Roman")  # Add text annotations for counts              
# Add the arrow only for the Ammonium.N graph
  if (nutrient == "Ammonium.N") {
    plot <- plot + annotate("segment", x = "2022", y = 4.5, xend = "2022", yend = 4.1, 
                            arrow = arrow(type = "open", length = unit(0.05, "inches")), color = "red")
  }
if (nutrient == "Phosphorus") {
    plot <- plot + annotate("segment", x = "2022", y = 4.5, xend = "2022", yend = 4.1, 
                            arrow = arrow(type = "open", length = unit(0.05, "inches")), color = "red")
}
  if (nutrient == "Potassium") {
    plot <- plot + annotate("segment", x = "2022", y = 4.5, xend = "2022", yend = 4.1, 
                            arrow = arrow(type = "open", length = unit(0.05, "inches")), color = "red")
}
  # Add the plot to the list
  plots[[nutrient]] <- plot
}

# Arrange the plots together using patchwork
layerstack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] + plot_layout(guides = "collect")

# Print the final plot
print(layerstack_final_plot)

Dairy Solid

Dairy Solid Manure by Region Overview Table (%)

Code
MDB_dairystack<-filter(MDB_stack5pl_clean, Animal.or.Other.Amendment.Type=="Dairy") 

df_nest = MDB_dairystack %>% 
  group_by(Region) %>% 
  nest()

MDB_dairystack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_dairystack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Northeast
Total_N 0.4230 0.13900 32.9 0.4700 0.24300 51.6 0.000000 2.70e-01 0.34100 0.5350 0.7050 3.890 3.570 25.6000 5925
Amm_N 0.0876 0.06950 79.4 0.0954 0.06640 69.6 0.001200 1.44e-02 0.04260 0.1370 0.1810 0.651 0.940 2.0700 5817
P2O5 0.1880 0.07770 41.3 0.2320 0.17500 75.2 0.000000 1.14e-01 0.14400 0.2610 0.3900 4.690 6.310 93.9000 6022
K2O 0.4050 0.21200 52.4 0.4780 0.32100 67.2 0.000000 1.98e-01 0.28200 0.5880 0.8150 5.740 3.740 32.8000 5937
Midwest
Total_N 0.4100 0.20800 50.6 0.4980 0.35500 71.3 0.000000 2.19e-01 0.30000 0.6100 0.8600 9.130 4.580 55.1000 16338
Amm_N 0.0800 0.10400 130.0 0.0968 0.10900 112.0 0.000000 6.69e-06 0.00400 0.1500 0.2120 1.940 3.880 40.6000 7500
P2O5 0.1770 0.10400 58.7 0.2590 0.31300 121.0 0.000000 9.00e-02 0.12000 0.2970 0.5100 13.100 12.900 364.0000 16332
K2O 0.3330 0.18900 56.7 0.4590 0.38200 83.3 0.000000 1.70e-01 0.23700 0.5600 0.8940 8.320 3.570 29.0000 16331
Southwest
Total_N 1.1900 0.78600 65.8 1.2400 0.63900 51.7 0.180000 4.30e-01 0.66000 1.7200 2.1200 3.540 0.375 -0.7060 870
Amm_N 0.0192 0.00245 12.7 0.0202 0.00544 26.9 0.014100 1.58e-02 0.01760 0.0209 0.0254 0.030 0.713 -0.9490 6
P2O5 0.7900 0.62300 78.8 0.8180 0.51500 63.0 0.045800 1.80e-01 0.35000 1.1700 1.5300 3.430 0.546 0.0185 872
K2O 1.6500 1.72000 104.0 1.6900 1.28000 75.5 0.010000 1.71e-01 0.42000 2.6700 3.3600 5.750 0.438 -0.7130 872
Southeast
Total_N 0.5000 0.31300 62.7 0.6410 0.50600 79.0 0.000024 2.10e-01 0.32500 0.8090 1.2800 5.930 2.270 9.3000 2621
Amm_N 0.0184 0.01960 106.0 0.0390 0.09650 247.0 0.000000 1.91e-03 0.00719 0.0364 0.0674 1.530 8.480 94.5000 1513
P2O5 0.2780 0.22200 79.9 0.5420 3.09000 570.0 0.000000 8.05e-02 0.15800 0.4870 0.8760 108.000 30.400 963.0000 3017
K2O 0.4090 0.41500 101.0 0.8290 5.23000 630.0 0.003140 7.57e-02 0.18100 0.7950 1.3900 180.000 30.500 963.0000 3017
Northern Plains
Total_N 0.4120 0.22200 54.0 0.4890 0.29900 61.1 0.020000 2.20e-01 0.28000 0.6230 0.8900 1.900 1.410 2.5400 280
Amm_N 0.1030 0.05650 54.8 0.1340 0.19900 148.0 0.001000 1.84e-02 0.06210 0.1400 0.1890 1.630 5.570 35.2000 115
P2O5 0.1570 0.11600 73.7 0.2520 0.22500 88.9 0.007140 7.71e-02 0.09970 0.3130 0.5630 1.400 1.850 3.8200 280
K2O 0.3040 0.19700 64.9 0.5110 0.44900 87.7 0.015000 1.72e-01 0.21100 0.6870 1.1000 2.960 2.070 5.5600 280
Pacific Northwest
Total_N 0.4580 0.24600 53.8 0.5750 0.35100 61.1 0.006600 2.64e-01 0.32000 0.6490 1.1400 1.720 1.200 1.0300 51
Amm_N 0.0573 0.03590 62.6 0.0633 0.04330 68.5 0.001400 1.28e-02 0.03330 0.0814 0.1300 0.168 0.702 -0.2760 50
P2O5 0.2890 0.21200 73.4 0.4360 0.40100 91.9 0.036000 1.13e-01 0.15900 0.5670 0.9410 2.200 2.040 5.2500 53
K2O 0.3630 0.38800 107.0 0.7950 0.92800 117.0 0.068200 9.93e-02 0.18200 0.9990 2.1600 4.350 1.730 2.7000 53
Southern Plains
Total_N 0.4410 0.65400 148.0 0.5670 0.55000 97.0 0.000000 3.00e-04 0.00400 1.0700 1.2800 1.310 0.159 -1.8700 14
Amm_N 0.1900 0.02970 15.6 0.1600 0.09130 56.9 0.002000 6.92e-02 0.17000 0.2100 0.2220 0.230 -0.916 -1.1000 5
P2O5 0.6320 0.57100 90.3 0.6050 0.46800 77.4 0.000000 1.94e-02 0.17100 0.7790 1.2800 1.370 0.208 -1.2800 14
K2O 0.3350 0.49700 148.0 0.7360 0.74100 101.0 0.000000 2.89e-02 0.15200 1.4400 1.6400 2.080 0.465 -1.5600 14

Dairy Solid Manure by Region Overview Table (lbs/ton)

Code
 #Convert Units from % to lbs/ton
multiply_by_20<- MDB_dairystack %>%
 mutate(
    Value = Value * 20)

df_nest = multiply_by_20 %>% 
  group_by(Region) %>% 
  nest()

MDB_dairystack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_dairystack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Northeast
Total_N 8.460 2.7800 32.9 9.400 4.850 51.6 0.00000 5.410000 6.820 10.700 14.100 77.90 3.570 25.6000 5925
Amm_N 1.750 1.3900 79.4 1.910 1.330 69.6 0.02410 0.288000 0.852 2.740 3.610 13.00 0.940 2.0700 5817
P2O5 3.760 1.5500 41.3 4.640 3.490 75.2 0.00000 2.290000 2.880 5.210 7.800 93.80 6.310 93.9000 6022
K2O 8.100 4.2400 52.4 9.560 6.430 67.2 0.00000 3.960000 5.640 11.800 16.300 115.00 3.740 32.8000 5937
Midwest
Total_N 8.200 4.1500 50.6 9.970 7.110 71.3 0.00000 4.380000 6.000 12.200 17.200 183.00 4.580 55.1000 16338
Amm_N 1.600 2.0800 130.0 1.940 2.180 112.0 0.00000 0.000134 0.080 3.000 4.240 38.80 3.880 40.6000 7500
P2O5 3.530 2.0700 58.7 5.180 6.260 121.0 0.00000 1.800000 2.400 5.940 10.200 261.00 12.900 364.0000 16332
K2O 6.650 3.7700 56.7 9.170 7.640 83.3 0.00000 3.400000 4.750 11.200 17.900 166.00 3.570 29.0000 16331
Southwest
Total_N 23.900 15.7000 65.8 24.700 12.800 51.7 3.60000 8.600000 13.200 34.400 42.400 70.80 0.375 -0.7060 870
Amm_N 0.385 0.0489 12.7 0.404 0.109 26.9 0.28200 0.317000 0.352 0.418 0.509 0.60 0.713 -0.9490 6
P2O5 15.800 12.5000 78.8 16.400 10.300 63.0 0.91600 3.600000 7.000 23.400 30.600 68.60 0.546 0.0185 872
K2O 33.000 34.4000 104.0 33.900 25.600 75.5 0.20000 3.420000 8.400 53.400 67.100 115.00 0.438 -0.7130 872
Southeast
Total_N 10.000 6.2700 62.7 12.800 10.100 79.0 0.00048 4.200000 6.500 16.200 25.700 119.00 2.270 9.3000 2621
Amm_N 0.369 0.3910 106.0 0.780 1.930 247.0 0.00000 0.038200 0.144 0.728 1.350 30.60 8.480 94.5000 1513
P2O5 5.560 4.4400 79.9 10.800 61.800 570.0 0.00000 1.610000 3.170 9.750 17.500 2160.00 30.400 963.0000 3017
K2O 8.190 8.3000 101.0 16.600 105.000 630.0 0.06270 1.510000 3.620 15.900 27.700 3610.00 30.500 963.0000 3017
Northern Plains
Total_N 8.240 4.4500 54.0 9.780 5.980 61.1 0.40000 4.400000 5.600 12.500 17.800 38.10 1.410 2.5400 280
Amm_N 2.060 1.1300 54.8 2.680 3.980 148.0 0.02000 0.367000 1.240 2.800 3.780 32.50 5.570 35.2000 115
P2O5 3.140 2.3200 73.7 5.050 4.490 88.9 0.14300 1.540000 1.990 6.260 11.300 28.10 1.850 3.8200 280
K2O 6.070 3.9400 64.9 10.200 8.970 87.7 0.29900 3.440000 4.220 13.700 21.900 59.20 2.070 5.5600 280
Pacific Northwest
Total_N 9.160 4.9300 53.8 11.500 7.030 61.1 0.13200 5.290000 6.400 13.000 22.700 34.40 1.200 1.0300 51
Amm_N 1.150 0.7180 62.6 1.270 0.867 68.5 0.02800 0.257000 0.666 1.630 2.610 3.36 0.702 -0.2760 50
P2O5 5.780 4.2400 73.4 8.730 8.020 91.9 0.71900 2.270000 3.180 11.300 18.800 44.00 2.040 5.2500 53
K2O 7.250 7.7600 107.0 15.900 18.600 117.0 1.36000 1.990000 3.640 20.000 43.100 87.00 1.730 2.7000 53
Southern Plains
Total_N 8.820 13.1000 148.0 11.300 11.000 97.0 0.00000 0.006000 0.080 21.300 25.600 26.30 0.159 -1.8700 14
Amm_N 3.800 0.5930 15.6 3.210 1.830 56.9 0.04000 1.380000 3.400 4.200 4.440 4.60 -0.916 -1.1000 5
P2O5 12.600 11.4000 90.3 12.100 9.360 77.4 0.00000 0.387000 3.420 15.600 25.600 27.30 0.208 -1.2800 14
K2O 6.700 9.9300 148.0 14.700 14.800 101.0 0.00000 0.577000 3.040 28.900 32.800 41.60 0.465 -1.5600 14

Table of samples by region by year for Dairy Solid manure

Code
# Sort out for dairy while still having separate NPK columns
MDB_dairystackNPK <- filter(MDB_stack5fix, Animal.or.Other.Amendment.Type == "Dairy")
# Create a table of number of samples for each region per year
sample_table <- table(MDB_dairystackNPK$Region, MDB_dairystackNPK$Year.Analyzed)

# Print the sample table
print(sample_table)
                   
                    2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
  Midwest            784 1148 1172 1133 1685 1892 1871 1799 2085 2036 1318
  Northeast          579  631  525  631  643  597  587  587  358  461  451
  Northern Plains      0    0    0    0   45   57   40   27   32   73    6
  Pacific Northwest    0    0    0    0    0    0    2    4    6   27   14
  Southeast          367  327  322  302  299  295  275  240  231  183  181
  Southern Plains      0    0    0    0    0    0    0    1    4    6    3
  Southwest            0    0   16   11   10    9    0    7    1    5  813

Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Dairy solid)

Code
# Load required library
library(Kendall)

# List of selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")

# List of analytes
analytes <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Function to perform Mann-Kendall trend test for a given analyte across all selected regions
perform_mk_test_combined <- function(data, analyte, selected_regions) {
  # Subset data for the selected regions and analyte
  filtered_data <- data[data$Region %in% selected_regions, ]
  cleaned_data <- filtered_data[!is.na(filtered_data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk_combined <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk_combined))
}

# Perform Mann-Kendall trend test for each analyte combined across selected regions
for (analyte in analytes) {
  cat("Analyzing", analyte, "for all regions combined:\n")
  print(perform_mk_test_combined(MDB_dairystackNPK, analyte, selected_regions))
  cat("\n")
}
Analyzing Total_N for all regions combined:
Score =  -24 , Var(Score) = 164
denominator =  54.4977
tau = -0.44, 2-sided pvalue =0.072495
NULL

Analyzing Ammonium.N for all regions combined:
Score =  5 , Var(Score) = 165
denominator =  55
tau = 0.0909, 2-sided pvalue =0.7555
NULL

Analyzing Phosphorus for all regions combined:
Score =  -39 , Var(Score) = 163
denominator =  53.99074
tau = -0.722, 2-sided pvalue =0.0029166
NULL

Analyzing Potassium for all regions combined:
Score =  -29 , Var(Score) = 165
denominator =  55
tau = -0.527, 2-sided pvalue =0.029273
NULL
Code
# Function to perform Mann-Kendall trend test
perform_mk_test <- function(data, analyte) {
  # Filter out NA values in the analyte column
  cleaned_data <- data[!is.na(data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk))
}

# Iterate over selected regions and analytes
for (region in selected_regions) {
  for (analyte in analytes) {
    # Subset data for the current region
    region_data <- MDB_dairystackNPK[MDB_dairystackNPK$Region == region, ]
    
    # Print information about the current region and analyte
    cat("Analyzing", analyte, "in", region, "region:\n")
    
    # Perform Mann-Kendall trend test
    result_summary <- perform_mk_test(region_data, analyte)
    
    # Print summary of the Mann-Kendall test result
    print(result_summary)
    cat("\n")
  }
}
Analyzing Total_N in Midwest region:
Score =  -11 , Var(Score) = 156.3333
denominator =  51.91339
tau = -0.212, 2-sided pvalue =0.42383
NULL

Analyzing Ammonium.N in Midwest region:
Score =  8 , Var(Score) = 164
denominator =  54.4977
tau = 0.147, 2-sided pvalue =0.58465
NULL

Analyzing Phosphorus in Midwest region:
Score =  -38 , Var(Score) = 162
denominator =  53.47897
tau = -0.711, 2-sided pvalue =0.0036492
NULL

Analyzing Potassium in Midwest region:
Score =  -24 , Var(Score) = 164
denominator =  54.4977
tau = -0.44, 2-sided pvalue =0.072495
NULL

Analyzing Total_N in Northeast region:
Score =  -27 , Var(Score) = 165
denominator =  55
tau = -0.491, 2-sided pvalue =0.04296
NULL

Analyzing Ammonium.N in Northeast region:
Score =  -29 , Var(Score) = 165
denominator =  55
tau = -0.527, 2-sided pvalue =0.029273
NULL

Analyzing Phosphorus in Northeast region:
Score =  7 , Var(Score) = 165
denominator =  55
tau = 0.127, 2-sided pvalue =0.64043
NULL

Analyzing Potassium in Northeast region:
Score =  -1 , Var(Score) = 165
denominator =  55
tau = -0.0182, 2-sided pvalue =1
NULL

Analyzing Total_N in Southeast region:
Score =  5 , Var(Score) = 165
denominator =  55
tau = 0.0909, 2-sided pvalue =0.7555
NULL

Analyzing Ammonium.N in Southeast region:
Score =  -7 , Var(Score) = 165
denominator =  55
tau = -0.127, 2-sided pvalue =0.64043
NULL

Analyzing Phosphorus in Southeast region:
Score =  -1 , Var(Score) = 165
denominator =  55
tau = -0.0182, 2-sided pvalue =1
NULL

Analyzing Potassium in Southeast region:
Score =  1 , Var(Score) = 165
denominator =  55
tau = 0.0182, 2-sided pvalue =1
NULL

Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Dairy Solid)

Code
# Filter data for selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")
filtered_data <- MDB_dairystackNPK[MDB_dairystackNPK$Region %in% selected_regions, ]

# Initialize an empty list to store cleaned data for each nutrient
cleaned_data_list <- list()

# Loop over each nutrient
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Remove missing values for the current nutrient
  cleaned_data <- filtered_data[!is.na(filtered_data[[nutrient]]), ]
  cleaned_data_list[[nutrient]] <- cleaned_data
}

# Create a Function for Wilcoxon Test and Correction
perform_wilcoxon_test <- function(region1, region2, year, nutrient, data) {
    # Extract data for the given regions, year, and nutrient
    region1_data <- data[data$Region == region1 & data$Year.Analyzed == year, nutrient]
    region2_data <- data[data$Region == region2 & data$Year.Analyzed == year, nutrient]
    
    # Perform Wilcoxon rank sum test
    wilcox_result <- wilcox.test(region1_data, region2_data)
        
    # Apply Benjamini-Hochberg correction
    adjusted_p_value <- p.adjust(wilcox_result$p.value, method = "BH")
    
    # Return results
    return(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))
}

# Iterate Over Years, Regions, and Nutrients
years <- unique(filtered_data$Year.Analyzed)
regions <- unique(filtered_data$Region)
nutrients <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Initialize empty vectors
all_p_values <- c()
comparison_results <- list()
comparison_names <- c()

# Loop over years, regions, and nutrients, perform tests, and gather p-values
index <- 1
for (year in years) {
    for (nutrient in nutrients) {
        for (i in 1:(length(regions)-1)) {
            for (j in (i+1):length(regions)) {
                region1 <- regions[i]
                region2 <- regions[j]
                
                # Perform Wilcoxon test and correction
                results <- perform_wilcoxon_test(region1, region2, year, nutrient, cleaned_data_list[[nutrient]])
                
                # Store p-value
                all_p_values <- c(all_p_values, results$wilcox_result$p.value)
                
                # Store comparison name
                comparison_names <- c(comparison_names, paste(region1, region2, year, nutrient, sep = "_"))
                
                # Store results
                comparison_results[[index]] <- results
                
                index <- index + 1
            }
        }
    }
}

# Sort comparison names based on year
sorted_indices <- order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))
sorted_comparison_names <- comparison_names[sorted_indices]

# Apply Benjamini-Hochberg Procedure
adjusted_p_values <- p.adjust(all_p_values, method = "BH")

# Print results in chronological order
for (name in sorted_comparison_names) {
    index <- match(name, comparison_names)
    year <- as.numeric(unlist(strsplit(name, "_"))[3])
    nutrient <- unlist(strsplit(name, "_"))[4]
    
    # Extract Wilcoxon test results
    result <- comparison_results[[index]]$wilcox_result
    
    # Print results with adjusted p-values
    cat("Comparison:", name, "\n")
    cat("Year:", year, "\n")
    cat("Nutrient:", nutrient, "\n")
    cat("Wilcoxon Test Statistic:", result$statistic, "\n")
    cat("Wilcoxon Test P-value:", result$p.value, "\n")
    cat("Adjusted p-value:", adjusted_p_values[index], "\n")
    cat("\n")
}
Comparison: Northeast_Midwest_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 246760.5 
Wilcoxon Test P-value: 0.004084169 
Adjusted p-value: 0.005924289 

Comparison: Northeast_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 70989 
Wilcoxon Test P-value: 4.413459e-05 
Adjusted p-value: 8.567304e-05 

Comparison: Midwest_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 93605 
Wilcoxon Test P-value: 9.339594e-07 
Adjusted p-value: 2.450997e-06 

Comparison: Northeast_Midwest_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 67359 
Wilcoxon Test P-value: 7.623292e-06 
Adjusted p-value: 1.677124e-05 

Comparison: Northeast_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 97755 
Wilcoxon Test P-value: 7.951853e-41 
Adjusted p-value: 5.248223e-39 

Comparison: Midwest_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 51155 
Wilcoxon Test P-value: 8.561367e-38 
Adjusted p-value: 1.883501e-36 

Comparison: Northeast_Midwest_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 228998 
Wilcoxon Test P-value: 0.614742 
Adjusted p-value: 0.6460259 

Comparison: Northeast_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 62666.5 
Wilcoxon Test P-value: 6.884542e-26 
Adjusted p-value: 9.087595e-25 

Comparison: Midwest_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 98487 
Wilcoxon Test P-value: 5.914025e-18 
Adjusted p-value: 4.592066e-17 

Comparison: Northeast_Midwest_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 264548.5 
Wilcoxon Test P-value: 4.325327e-08 
Adjusted p-value: 1.327775e-07 

Comparison: Northeast_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 100813.5 
Wilcoxon Test P-value: 0.2485859 
Adjusted p-value: 0.2804559 

Comparison: Midwest_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 125307 
Wilcoxon Test P-value: 0.0004142282 
Adjusted p-value: 0.0007010016 

Comparison: Northeast_Midwest_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 346888 
Wilcoxon Test P-value: 0.9660515 
Adjusted p-value: 0.9741233 

Comparison: Northeast_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 69179 
Wilcoxon Test P-value: 0.002012525 
Adjusted p-value: 0.003200642 

Comparison: Midwest_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 129709 
Wilcoxon Test P-value: 0.008185077 
Adjusted p-value: 0.01149394 

Comparison: Northeast_Midwest_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 189201 
Wilcoxon Test P-value: 0.000143334 
Adjusted p-value: 0.0002556769 

Comparison: Northeast_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 81478 
Wilcoxon Test P-value: 4.223562e-40 
Adjusted p-value: 1.858367e-38 

Comparison: Midwest_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 56765.5 
Wilcoxon Test P-value: 4.791936e-08 
Adjusted p-value: 1.437581e-07 

Comparison: Northeast_Midwest_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 338772 
Wilcoxon Test P-value: 0.1197335 
Adjusted p-value: 0.1386388 

Comparison: Northeast_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 79109 
Wilcoxon Test P-value: 1.509764e-08 
Adjusted p-value: 5.109972e-08 

Comparison: Midwest_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 149864 
Wilcoxon Test P-value: 1.716324e-06 
Adjusted p-value: 4.274619e-06 

Comparison: Northeast_Midwest_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 360201 
Wilcoxon Test P-value: 0.5839728 
Adjusted p-value: 0.6216485 

Comparison: Northeast_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 109002 
Wilcoxon Test P-value: 0.07812447 
Adjusted p-value: 0.09460945 

Comparison: Midwest_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 193482 
Wilcoxon Test P-value: 0.07086208 
Adjusted p-value: 0.08660921 

Comparison: Northeast_Midwest_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 277932 
Wilcoxon Test P-value: 0.004520865 
Adjusted p-value: 0.006416712 

Comparison: Northeast_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 58813 
Wilcoxon Test P-value: 9.958806e-06 
Adjusted p-value: 2.120262e-05 

Comparison: Midwest_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 141914 
Wilcoxon Test P-value: 0.002357684 
Adjusted p-value: 0.003665989 

Comparison: Northeast_Midwest_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 197505 
Wilcoxon Test P-value: 4.572024e-06 
Adjusted p-value: 1.097286e-05 

Comparison: Northeast_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 52605 
Wilcoxon Test P-value: 1.528035e-26 
Adjusted p-value: 2.241119e-25 

Comparison: Midwest_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 51786.5 
Wilcoxon Test P-value: 1.241026e-05 
Adjusted p-value: 2.600245e-05 

Comparison: Northeast_Midwest_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 282473.5 
Wilcoxon Test P-value: 0.01886458 
Adjusted p-value: 0.02540943 

Comparison: Northeast_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 54782 
Wilcoxon Test P-value: 1.018144e-17 
Adjusted p-value: 7.466391e-17 

Comparison: Midwest_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 137508 
Wilcoxon Test P-value: 3.647548e-13 
Adjusted p-value: 1.851832e-12 

Comparison: Northeast_Midwest_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 311595.5 
Wilcoxon Test P-value: 0.4226448 
Adjusted p-value: 0.4572878 

Comparison: Northeast_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 86366 
Wilcoxon Test P-value: 0.5619299 
Adjusted p-value: 0.6030468 

Comparison: Midwest_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 186800 
Wilcoxon Test P-value: 0.9858598 
Adjusted p-value: 0.9858598 

Comparison: Northeast_Midwest_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 280198 
Wilcoxon Test P-value: 2.597903e-07 
Adjusted p-value: 7.454851e-07 

Comparison: Northeast_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 61754 
Wilcoxon Test P-value: 8.399055e-06 
Adjusted p-value: 1.817501e-05 

Comparison: Midwest_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 139190 
Wilcoxon Test P-value: 0.1161507 
Adjusted p-value: 0.1356804 

Comparison: Northeast_Midwest_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 247031 
Wilcoxon Test P-value: 1.129195e-09 
Adjusted p-value: 4.258677e-09 

Comparison: Northeast_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 67705 
Wilcoxon Test P-value: 5.742038e-38 
Adjusted p-value: 1.515898e-36 

Comparison: Midwest_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 57323.5 
Wilcoxon Test P-value: 0.001268767 
Adjusted p-value: 0.002067621 

Comparison: Northeast_Midwest_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 306732.5 
Wilcoxon Test P-value: 7.527035e-07 
Adjusted p-value: 2.069935e-06 

Comparison: Northeast_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 66701 
Wilcoxon Test P-value: 1.163667e-13 
Adjusted p-value: 6.144162e-13 

Comparison: Midwest_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 144882 
Wilcoxon Test P-value: 4.222846e-05 
Adjusted p-value: 8.319637e-05 

Comparison: Northeast_Midwest_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 340280 
Wilcoxon Test P-value: 0.3319069 
Adjusted p-value: 0.3650975 

Comparison: Northeast_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 95907 
Wilcoxon Test P-value: 0.03244919 
Adjusted p-value: 0.04283293 

Comparison: Midwest_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 182947.5 
Wilcoxon Test P-value: 0.06372236 
Adjusted p-value: 0.07935237 

Comparison: Northeast_Midwest_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 564786 
Wilcoxon Test P-value: 5.555452e-09 
Adjusted p-value: 1.929789e-08 

Comparison: Northeast_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 79187 
Wilcoxon Test P-value: 0.1570006 
Adjusted p-value: 0.1802093 

Comparison: Midwest_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 182670.5 
Wilcoxon Test P-value: 5.844736e-06 
Adjusted p-value: 1.330181e-05 

Comparison: Northeast_Midwest_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 283433.5 
Wilcoxon Test P-value: 4.470619e-15 
Adjusted p-value: 2.810104e-14 

Comparison: Northeast_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 80555 
Wilcoxon Test P-value: 2.514458e-48 
Adjusted p-value: 3.319085e-46 

Comparison: Midwest_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 72371 
Wilcoxon Test P-value: 2.871603e-05 
Adjusted p-value: 5.743206e-05 

Comparison: Northeast_Midwest_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 570376.5 
Wilcoxon Test P-value: 2.679e-05 
Adjusted p-value: 5.440432e-05 

Comparison: Northeast_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 72654 
Wilcoxon Test P-value: 2.867424e-09 
Adjusted p-value: 1.022973e-08 

Comparison: Midwest_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 169533 
Wilcoxon Test P-value: 3.331992e-15 
Adjusted p-value: 2.199115e-14 

Comparison: Northeast_Midwest_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 570211 
Wilcoxon Test P-value: 4.518487e-10 
Adjusted p-value: 1.807395e-09 

Comparison: Northeast_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 95142 
Wilcoxon Test P-value: 0.2519117 
Adjusted p-value: 0.2817996 

Comparison: Midwest_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 223053 
Wilcoxon Test P-value: 0.08874713 
Adjusted p-value: 0.1064966 

Comparison: Northeast_Midwest_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 555413 
Wilcoxon Test P-value: 0.03351545 
Adjusted p-value: 0.04321371 

Comparison: Northeast_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 68507.5 
Wilcoxon Test P-value: 0.02053193 
Adjusted p-value: 0.0273759 

Comparison: Midwest_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 195854.5 
Wilcoxon Test P-value: 0.0001390923 
Adjusted p-value: 0.0002515093 

Comparison: Northeast_Midwest_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 332481 
Wilcoxon Test P-value: 1.455765e-18 
Adjusted p-value: 1.201006e-17 

Comparison: Northeast_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 64025.5 
Wilcoxon Test P-value: 3.220525e-33 
Adjusted p-value: 6.07299e-32 

Comparison: Midwest_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 71852 
Wilcoxon Test P-value: 0.002728452 
Adjusted p-value: 0.004103315 

Comparison: Northeast_Midwest_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 572715.5 
Wilcoxon Test P-value: 0.003073116 
Adjusted p-value: 0.004557879 

Comparison: Northeast_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 71025 
Wilcoxon Test P-value: 3.004788e-06 
Adjusted p-value: 7.345038e-06 

Comparison: Midwest_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 203201 
Wilcoxon Test P-value: 5.558481e-10 
Adjusted p-value: 2.157999e-09 

Comparison: Northeast_Midwest_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 604671.5 
Wilcoxon Test P-value: 2.224184e-08 
Adjusted p-value: 7.160788e-08 

Comparison: Northeast_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 85230 
Wilcoxon Test P-value: 0.6166611 
Adjusted p-value: 0.6460259 

Comparison: Midwest_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 234988 
Wilcoxon Test P-value: 0.004374776 
Adjusted p-value: 0.006276852 

Comparison: Northeast_Midwest_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 509223 
Wilcoxon Test P-value: 0.9667436 
Adjusted p-value: 0.9741233 

Comparison: Northeast_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 56306 
Wilcoxon Test P-value: 1.490095e-07 
Adjusted p-value: 4.370944e-07 

Comparison: Midwest_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 173083 
Wilcoxon Test P-value: 3.367378e-07 
Adjusted p-value: 9.457316e-07 

Comparison: Northeast_Midwest_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 326598.5 
Wilcoxon Test P-value: 6.780247e-15 
Adjusted p-value: 4.068148e-14 

Comparison: Northeast_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 62534.5 
Wilcoxon Test P-value: 5.652881e-40 
Adjusted p-value: 1.865451e-38 

Comparison: Midwest_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 70231.5 
Wilcoxon Test P-value: 5.488944e-06 
Adjusted p-value: 1.271124e-05 

Comparison: Northeast_Midwest_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 554494.5 
Wilcoxon Test P-value: 0.001476552 
Adjusted p-value: 0.002376889 

Comparison: Northeast_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 55134 
Wilcoxon Test P-value: 6.049585e-14 
Adjusted p-value: 3.471936e-13 

Comparison: Midwest_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 162692 
Wilcoxon Test P-value: 1.791052e-17 
Adjusted p-value: 1.24431e-16 

Comparison: Northeast_Midwest_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 566331 
Wilcoxon Test P-value: 5.786372e-05 
Adjusted p-value: 0.0001106958 

Comparison: Northeast_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 70504 
Wilcoxon Test P-value: 0.002735543 
Adjusted p-value: 0.004103315 

Comparison: Midwest_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 194988 
Wilcoxon Test P-value: 9.655442e-07 
Adjusted p-value: 2.450997e-06 

Comparison: Northeast_Midwest_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 531420.5 
Wilcoxon Test P-value: 0.06592649 
Adjusted p-value: 0.08132988 

Comparison: Northeast_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 56996.5 
Wilcoxon Test P-value: 0.03336698 
Adjusted p-value: 0.04321371 

Comparison: Midwest_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 161206 
Wilcoxon Test P-value: 0.001038113 
Adjusted p-value: 0.001712887 

Comparison: Northeast_Midwest_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 154144.5 
Wilcoxon Test P-value: 0.03371979 
Adjusted p-value: 0.04321371 

Comparison: Northeast_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 57950 
Wilcoxon Test P-value: 1.389194e-31 
Adjusted p-value: 2.29217e-30 

Comparison: Midwest_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 53950 
Wilcoxon Test P-value: 2.792914e-25 
Adjusted p-value: 3.351496e-24 

Comparison: Northeast_Midwest_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 595401 
Wilcoxon Test P-value: 1.369899e-10 
Adjusted p-value: 5.833118e-10 

Comparison: Northeast_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 57910.5 
Wilcoxon Test P-value: 7.715694e-05 
Adjusted p-value: 0.0001434467 

Comparison: Midwest_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 149141 
Wilcoxon Test P-value: 1.568246e-12 
Adjusted p-value: 7.666979e-12 

Comparison: Northeast_Midwest_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 609861.5 
Wilcoxon Test P-value: 9.029353e-14 
Adjusted p-value: 4.966144e-13 

Comparison: Northeast_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 75348.5 
Wilcoxon Test P-value: 0.09772543 
Adjusted p-value: 0.116214 

Comparison: Midwest_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 196954 
Wilcoxon Test P-value: 0.2022155 
Adjusted p-value: 0.2301073 

Comparison: Northeast_Midwest_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 393665.5 
Wilcoxon Test P-value: 0.002585431 
Adjusted p-value: 0.003968336 

Comparison: Northeast_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 30530 
Wilcoxon Test P-value: 0.003443199 
Adjusted p-value: 0.005050026 

Comparison: Midwest_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 162306 
Wilcoxon Test P-value: 5.292636e-06 
Adjusted p-value: 1.24755e-05 

Comparison: Northeast_Midwest_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 122280 
Wilcoxon Test P-value: 0.09986155 
Adjusted p-value: 0.117694 

Comparison: Northeast_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 34937 
Wilcoxon Test P-value: 4.891053e-24 
Adjusted p-value: 5.380159e-23 

Comparison: Midwest_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 69136 
Wilcoxon Test P-value: 5.642035e-22 
Adjusted p-value: 5.728836e-21 

Comparison: Northeast_Midwest_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 410850 
Wilcoxon Test P-value: 6.421143e-06 
Adjusted p-value: 1.436595e-05 

Comparison: Northeast_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 31375 
Wilcoxon Test P-value: 9.591379e-07 
Adjusted p-value: 2.450997e-06 

Comparison: Midwest_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 169873 
Wilcoxon Test P-value: 3.769156e-11 
Adjusted p-value: 1.776888e-10 

Comparison: Northeast_Midwest_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 431883.5 
Wilcoxon Test P-value: 3.255908e-10 
Adjusted p-value: 1.343062e-09 

Comparison: Northeast_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 41708 
Wilcoxon Test P-value: 0.8137448 
Adjusted p-value: 0.8391743 

Comparison: Midwest_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 211969 
Wilcoxon Test P-value: 0.03790381 
Adjusted p-value: 0.04810869 

Comparison: Northeast_Midwest_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 479433.5 
Wilcoxon Test P-value: 0.05641487 
Adjusted p-value: 0.07092155 

Comparison: Northeast_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 31485.5 
Wilcoxon Test P-value: 0.002360674 
Adjusted p-value: 0.003665989 

Comparison: Midwest_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 133553 
Wilcoxon Test P-value: 0.0003290656 
Adjusted p-value: 0.000571535 

Comparison: Northeast_Midwest_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 130920 
Wilcoxon Test P-value: 0.0003409495 
Adjusted p-value: 0.0005844849 

Comparison: Northeast_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 33031.5 
Wilcoxon Test P-value: 3.619046e-21 
Adjusted p-value: 3.412244e-20 

Comparison: Midwest_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 48301 
Wilcoxon Test P-value: 2.596849e-19 
Adjusted p-value: 2.285227e-18 

Comparison: Northeast_Midwest_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 503632 
Wilcoxon Test P-value: 0.0001918379 
Adjusted p-value: 0.0003376348 

Comparison: Northeast_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 30350 
Wilcoxon Test P-value: 3.328208e-08 
Adjusted p-value: 1.046008e-07 

Comparison: Midwest_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 127501 
Wilcoxon Test P-value: 5.454988e-11 
Adjusted p-value: 2.48296e-10 

Comparison: Northeast_Midwest_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 499665.5 
Wilcoxon Test P-value: 0.000588744 
Adjusted p-value: 0.0009837242 

Comparison: Northeast_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 41897 
Wilcoxon Test P-value: 0.927836 
Adjusted p-value: 0.9494136 

Comparison: Midwest_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 173363 
Wilcoxon Test P-value: 0.3916142 
Adjusted p-value: 0.4272155 

Comparison: Northeast_Midwest_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 264457 
Wilcoxon Test P-value: 0.01840668 
Adjusted p-value: 0.02504826 

Comparison: Northeast_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 25112.5 
Wilcoxon Test P-value: 1.599739e-08 
Adjusted p-value: 5.27914e-08 

Comparison: Midwest_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 88053 
Wilcoxon Test P-value: 6.65995e-05 
Adjusted p-value: 0.0001255876 

Comparison: Northeast_Midwest_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 136167 
Wilcoxon Test P-value: 0.01626237 
Adjusted p-value: 0.02236075 

Comparison: Northeast_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 38837 
Wilcoxon Test P-value: 9.810351e-11 
Adjusted p-value: 4.316555e-10 

Comparison: Midwest_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 63453.5 
Wilcoxon Test P-value: 1.858819e-09 
Adjusted p-value: 6.81567e-09 

Comparison: Northeast_Midwest_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 295643 
Wilcoxon Test P-value: 0.2917679 
Adjusted p-value: 0.3236417 

Comparison: Northeast_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 31444.5 
Wilcoxon Test P-value: 9.610247e-05 
Adjusted p-value: 0.0001761879 

Comparison: Midwest_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 95544 
Wilcoxon Test P-value: 1.380516e-05 
Adjusted p-value: 2.847314e-05 

Comparison: Northeast_Midwest_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 331108.5 
Wilcoxon Test P-value: 8.054879e-07 
Adjusted p-value: 2.169886e-06 

Comparison: Northeast_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 38669 
Wilcoxon Test P-value: 0.7622511 
Adjusted p-value: 0.792261 

Comparison: Midwest_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 104978.5 
Wilcoxon Test P-value: 0.008824136 
Adjusted p-value: 0.01226091 

Four analyte median + IQR plots 2012-2022 + counts and arrows (Dairy Solid)

Code
# Filter out rows with non-missing Total_N values and for specific regions
MDB_dairystackNPK_filtered <- MDB_dairystackNPK[!is.na(MDB_dairystackNPK$Total_N) & 
                                                 MDB_dairystackNPK$Region %in% c("Northeast", "Southeast", "Midwest"), ]

custom_theme <- theme_minimal() +
  theme(
    text = element_text(family = "Times New Roman", size = 11),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

# Create separate plots for each nutrient type
plots <- list()

# Initialize variables to store the minimum and maximum values of lower and upper quartiles
min_lower_quantile <- Inf
max_upper_quantile <- -Inf

# Function to calculate counts for each nutrient
calculate_counts <- function(data) {
  counts <- data %>%
    filter(!is.na(Total_N) & Year.Analyzed >= 2012 & Year.Analyzed <= 2022) %>%
    group_by(Region) %>%
    summarise(total_samples = n()) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  return(counts)
}

for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Filter data for the current nutrient type
  filtered_data <- MDB_dairystackNPK_filtered[!is.na(MDB_dairystackNPK_filtered[[nutrient]]), ]

  # Calculate median and IQR for each combination of Year.Analyzed and Region
  summary_stats <- filtered_data %>%
    group_by(Year.Analyzed, Region) %>%
    summarise(median_value = median(.data[[nutrient]]),
              lower_quantile = quantile(.data[[nutrient]], 0.25),
              upper_quantile = quantile(.data[[nutrient]], 0.75)) %>%
    ungroup()  # Remove grouping to avoid issues with plotting

  # Update minimum and maximum values of lower and upper quartiles
  min_lower_quantile <- min(min_lower_quantile, min(summary_stats$lower_quantile))
  max_upper_quantile <- max(max_upper_quantile, max(summary_stats$upper_quantile))

  # Determine a suitable step size for the y-axis
  y_range <- max_upper_quantile - min_lower_quantile
  num_breaks <- max(4, floor(y_range * 10))  # Ensure at least four breaks
  step_size <- y_range / num_breaks

  # Calculate counts for all regions
  counts <- calculate_counts(filtered_data)

  # Create line graph with median for each year and region, with connecting lines
  plot <- ggplot(summary_stats, aes(x = factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +
    geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha = 0.3) +  # Add shaded area for IQR
    geom_line(aes(linetype = Region)) +  # Add line graph with connecting lines
    geom_point() +  # Add points for each data point
    labs(x = "Year Analyzed",
         y = switch(nutrient,
                    Total_N = "Total N (%)",
                    Ammonium.N = expression(paste("NH"[4], "-N (%)")),
                    Phosphorus = expression(paste("P"[2], "O"[5], " (%)")),
                    Potassium = expression(paste("K"[2], "O (%)"))
         ),
         title = NULL) +  # Remove titles for all graphs
    custom_theme +  # Apply custom theme with font settings
    scale_color_manual(values = c("Northeast" = "darkgreen", "Southeast" = "red", "Midwest" = "blue")) +  # Define colors for regions
    scale_fill_manual(values = c("Northeast" = "lightgreen", "Southeast" = "lightpink", "Midwest" = "lightblue")) +  # Define fill colors for IQR
    guides(fill = FALSE) +  # Remove legend for IQR fill colors
    scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = .25)) +
    geom_text(data = counts, aes(label = paste("n =", total_samples),
                                 x = "2021", y = 1 - (0.08 * seq_along(total_samples)), color = Region),
              hjust = 1, vjust = 0, size = 3, family = "Times New Roman")  # Add text annotations for counts
  # Add the arrow only for the Ammonium.N graph
    if (nutrient == "Total_N") {
    plot <- plot + annotate("segment", x = "2022", y = .9, xend = "2022", yend = .81, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "darkgreen")
  }
  if (nutrient == "Ammonium.N") {
    plot <- plot + annotate("segment", x = "2022", y = .9, xend = "2022", yend = .81, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "darkgreen")
  }
  if (nutrient == "Phosphorus") {
    plot <- plot + annotate("segment", x = "2022", y = 1, xend = "2022", yend = .9, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "blue")
  }

  # Add the plot to the list
  plots[[nutrient]] <- plot
}

# Arrange the plots together using patchwork
dairystack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] + plot_layout(guides = "collect")

# Print the final plot
print(dairystack_final_plot)

Turkey Solid

Turkey Solid Manure by Region Overview Table (%)

Code
MDB_turkeystack<-filter(MDB_stack5pl_clean, Animal.or.Other.Amendment.Type=="Turkey") 

df_nest = MDB_turkeystack %>% 
  group_by(Region) %>% 
  nest()

MDB_turkeystack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_turkeystack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Midwest
Total_N 2.560 0.713 27.8 2.560 0.827 32.3 0.00e+00 1.550 2.080 3.040 3.520 9.00 0.4360 3.330 1713
Amm_N 0.710 0.297 41.8 0.710 0.312 43.9 3.00e-08 0.290 0.510 0.910 1.070 2.00 0.1900 0.523 1007
P2O5 2.320 0.993 42.8 2.450 1.080 44.1 0.00e+00 1.290 1.700 3.080 3.750 13.30 1.6700 10.300 1713
K2O 1.970 0.608 30.9 1.960 0.725 36.9 0.00e+00 1.160 1.570 2.390 2.740 10.10 1.7800 18.200 1713
Southeast
Total_N 2.460 0.772 31.4 2.460 0.832 33.8 1.68e-01 1.350 1.930 2.980 3.500 6.28 0.1510 0.362 3659
Amm_N 0.501 0.172 34.3 0.494 0.186 37.6 0.00e+00 0.257 0.378 0.611 0.719 1.56 0.0655 0.902 2261
P2O5 2.610 0.778 29.8 2.600 0.847 32.6 1.84e-01 1.510 2.060 3.110 3.590 11.40 0.6220 5.620 3662
K2O 2.100 0.640 30.4 2.030 0.762 37.5 1.64e-02 1.090 1.590 2.490 2.800 16.80 2.9100 51.300 3662
Northeast
Total_N 2.570 0.598 23.2 2.530 0.692 27.4 5.54e-01 1.680 2.120 2.940 3.270 4.57 -0.0537 0.508 259
Amm_N 0.467 0.329 70.4 0.509 0.291 57.2 4.40e-03 0.160 0.264 0.733 0.926 1.29 0.3630 -0.856 258
P2O5 2.400 0.913 38.0 2.580 1.000 38.8 1.84e-01 1.410 1.850 3.130 3.920 6.27 0.7520 0.681 259
K2O 2.230 0.792 35.5 2.220 0.776 34.9 2.29e-01 1.330 1.640 2.740 3.200 4.93 0.2700 0.164 259

Turkey Solid Manure by Region Overview Table (lbs/ton)

Code
#Convert Units from % to lbs/ton
multiply_by_20<- MDB_turkeystack %>%
 mutate(
    Value = Value * 20)

df_nest = multiply_by_20 %>% 
  group_by(Region) %>% 
  nest()

MDB_turkeystack_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
           MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_turkeystack_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Midwest
Total_N 51.20 14.30 27.8 51.30 16.50 32.3 0.00e+00 31.10 41.60 60.8 70.4 180.0 0.4360 3.330 1713
Amm_N 14.20 5.93 41.8 14.20 6.23 43.9 6.00e-07 5.80 10.20 18.2 21.4 40.0 0.1900 0.523 1007
P2O5 46.40 19.90 42.8 49.10 21.60 44.1 0.00e+00 25.80 34.00 61.6 75.0 267.0 1.6700 10.300 1713
K2O 39.40 12.20 30.9 39.30 14.50 36.9 0.00e+00 23.20 31.40 47.8 54.8 202.0 1.7800 18.200 1713
Southeast
Total_N 49.30 15.40 31.4 49.20 16.60 33.8 3.36e+00 27.10 38.70 59.6 70.1 126.0 0.1510 0.362 3659
Amm_N 10.00 3.43 34.3 9.89 3.72 37.6 0.00e+00 5.13 7.56 12.2 14.4 31.2 0.0655 0.902 2261
P2O5 52.20 15.60 29.8 52.00 16.90 32.6 3.68e+00 30.30 41.10 62.2 71.8 228.0 0.6220 5.620 3662
K2O 42.00 12.80 30.4 40.70 15.20 37.5 3.29e-01 21.80 31.90 49.9 56.0 336.0 2.9100 51.300 3662
Northeast
Total_N 51.50 12.00 23.2 50.60 13.80 27.4 1.11e+01 33.50 42.40 58.8 65.4 91.4 -0.0537 0.508 259
Amm_N 9.34 6.58 70.4 10.20 5.83 57.2 8.80e-02 3.19 5.28 14.7 18.5 25.8 0.3630 -0.856 258
P2O5 48.00 18.30 38.0 51.60 20.00 38.8 3.68e+00 28.20 37.00 62.7 78.4 125.0 0.7520 0.681 259
K2O 44.60 15.80 35.5 44.50 15.50 34.9 4.58e+00 26.60 32.80 54.8 63.9 98.6 0.2700 0.164 259

Table of samples by region by year for Turkey solid manure

Code
# Sort out for turkey while still having separate NPK columns
MDB_turkeystackNPK <- filter(MDB_stack5fix, Animal.or.Other.Amendment.Type == "Turkey")
# Create a table of number of samples for each region per year
sample_table <- table(MDB_turkeystackNPK$Region, MDB_turkeystackNPK$Year.Analyzed)

# Print the sample table
print(sample_table)
           
            2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
  Midwest    124   77  113  141  161  195  187  198  194  171  152
  Northeast   21    9   36   68   26   37    9   13    7   12   21
  Southeast  493  416  412  348  457  316  305  296  238  186  201

Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Turkey Solid)

Code
# Load required library
library(Kendall)

# List of selected regions
selected_regions <- c("Midwest", "Southeast")

# List of analytes
analytes <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Function to perform Mann-Kendall trend test for a given analyte across all selected regions
perform_mk_test_combined <- function(data, analyte, selected_regions) {
  # Subset data for the selected regions and analyte
  filtered_data <- data[data$Region %in% selected_regions, ]
  cleaned_data <- filtered_data[!is.na(filtered_data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk_combined <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk_combined))
}

# Perform Mann-Kendall trend test for each analyte combined across selected regions
for (analyte in analytes) {
  cat("Analyzing", analyte, "for all regions combined:\n")
  print(perform_mk_test_combined(MDB_turkeystackNPK, analyte, selected_regions))
  cat("\n")
}
Analyzing Total_N for all regions combined:
Score =  -5 , Var(Score) = 165
denominator =  55
tau = -0.0909, 2-sided pvalue =0.7555
NULL

Analyzing Ammonium.N for all regions combined:
Score =  -17 , Var(Score) = 165
denominator =  55
tau = -0.309, 2-sided pvalue =0.21291
NULL

Analyzing Phosphorus for all regions combined:
Score =  -39 , Var(Score) = 165
denominator =  55
tau = -0.709, 2-sided pvalue =0.0030935
NULL

Analyzing Potassium for all regions combined:
Score =  15 , Var(Score) = 165
denominator =  55
tau = 0.273, 2-sided pvalue =0.27576
NULL
Code
# Function to perform Mann-Kendall trend test
perform_mk_test <- function(data, analyte) {
  # Filter out NA values in the analyte column
  cleaned_data <- data[!is.na(data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk))
}

# Iterate over selected regions and analytes
for (region in selected_regions) {
  for (analyte in analytes) {
    # Subset data for the current region
    region_data <- MDB_turkeystackNPK[MDB_turkeystackNPK$Region == region, ]
    
    # Print information about the current region and analyte
    cat("Analyzing", analyte, "in", region, "region:\n")
    
    # Perform Mann-Kendall trend test
    result_summary <- perform_mk_test(region_data, analyte)
    
    # Print summary of the Mann-Kendall test result
    print(result_summary)
    cat("\n")
  }
}
Analyzing Total_N in Midwest region:
Score =  1 , Var(Score) = 165
denominator =  55
tau = 0.0182, 2-sided pvalue =1
NULL

Analyzing Ammonium.N in Midwest region:
Score =  -11 , Var(Score) = 165
denominator =  55
tau = -0.2, 2-sided pvalue =0.43627
NULL

Analyzing Phosphorus in Midwest region:
Score =  30 , Var(Score) = 164
denominator =  54.4977
tau = 0.55, 2-sided pvalue =0.023542
NULL

Analyzing Potassium in Midwest region:
Score =  9 , Var(Score) = 163
denominator =  53.99074
tau = 0.167, 2-sided pvalue =0.53092
NULL

Analyzing Total_N in Southeast region:
Score =  -17 , Var(Score) = 165
denominator =  55
tau = -0.309, 2-sided pvalue =0.21291
NULL

Analyzing Ammonium.N in Southeast region:
Score =  -23 , Var(Score) = 165
denominator =  55
tau = -0.418, 2-sided pvalue =0.086768
NULL

Analyzing Phosphorus in Southeast region:
Score =  -37 , Var(Score) = 165
denominator =  55
tau = -0.673, 2-sided pvalue =0.0050693
NULL

Analyzing Potassium in Southeast region:
Score =  11 , Var(Score) = 165
denominator =  55
tau = 0.2, 2-sided pvalue =0.43627
NULL

Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Turkey Solid)

Code
# Filter data for selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")
filtered_data <- MDB_turkeystackNPK[MDB_turkeystackNPK$Region %in% selected_regions, ]

# Initialize an empty list to store cleaned data for each nutrient
cleaned_data_list <- list()

# Loop over each nutrient
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Remove missing values for the current nutrient
  cleaned_data <- filtered_data[!is.na(filtered_data[[nutrient]]), ]
  cleaned_data_list[[nutrient]] <- cleaned_data
}

# Create a Function for Wilcoxon Test and Correction
perform_wilcoxon_test <- function(region1, region2, year, nutrient, data) {
    # Extract data for the given regions, year, and nutrient
    region1_data <- data[data$Region == region1 & data$Year.Analyzed == year, nutrient]
    region2_data <- data[data$Region == region2 & data$Year.Analyzed == year, nutrient]
    
    # Perform Wilcoxon rank sum test
    wilcox_result <- wilcox.test(region1_data, region2_data)
        
    # Apply Benjamini-Hochberg correction
    adjusted_p_value <- p.adjust(wilcox_result$p.value, method = "BH")
    
    # Return results
    return(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))
}

# Iterate Over Years, Regions, and Nutrients
years <- unique(filtered_data$Year.Analyzed)
regions <- unique(filtered_data$Region)
nutrients <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Initialize empty vectors
all_p_values <- c()
comparison_results <- list()
comparison_names <- c()

# Loop over years, regions, and nutrients, perform tests, and gather p-values
index <- 1
for (year in years) {
    for (nutrient in nutrients) {
        for (i in 1:(length(regions)-1)) {
            for (j in (i+1):length(regions)) {
                region1 <- regions[i]
                region2 <- regions[j]
                
                # Perform Wilcoxon test and correction
                results <- perform_wilcoxon_test(region1, region2, year, nutrient, cleaned_data_list[[nutrient]])
                
                # Store p-value
                all_p_values <- c(all_p_values, results$wilcox_result$p.value)
                
                # Store comparison name
                comparison_names <- c(comparison_names, paste(region1, region2, year, nutrient, sep = "_"))
                
                # Store results
                comparison_results[[index]] <- results
                
                index <- index + 1
            }
        }
    }
}

# Sort comparison names based on year
sorted_indices <- order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))
sorted_comparison_names <- comparison_names[sorted_indices]

# Apply Benjamini-Hochberg Procedure
adjusted_p_values <- p.adjust(all_p_values, method = "BH")

# Print results in chronological order
for (name in sorted_comparison_names) {
    index <- match(name, comparison_names)
    year <- as.numeric(unlist(strsplit(name, "_"))[3])
    nutrient <- unlist(strsplit(name, "_"))[4]
    
    # Extract Wilcoxon test results
    result <- comparison_results[[index]]$wilcox_result
    
    # Print results with adjusted p-values
    cat("Comparison:", name, "\n")
    cat("Year:", year, "\n")
    cat("Nutrient:", nutrient, "\n")
    cat("Wilcoxon Test Statistic:", result$statistic, "\n")
    cat("Wilcoxon Test P-value:", result$p.value, "\n")
    cat("Adjusted p-value:", adjusted_p_values[index], "\n")
    cat("\n")
}
Comparison: Midwest_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 30433 
Wilcoxon Test P-value: 0.9961638 
Adjusted p-value: 0.9961638 

Comparison: Midwest_Northeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 1509 
Wilcoxon Test P-value: 0.2459761 
Adjusted p-value: 0.3491273 

Comparison: Southeast_Northeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 5934 
Wilcoxon Test P-value: 0.2412691 
Adjusted p-value: 0.3484366 

Comparison: Midwest_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 16598 
Wilcoxon Test P-value: 3.619086e-16 
Adjusted p-value: 4.777194e-14 

Comparison: Midwest_Northeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1337 
Wilcoxon Test P-value: 0.0001930398 
Adjusted p-value: 0.0007962893 

Comparison: Southeast_Northeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2665 
Wilcoxon Test P-value: 0.4130107 
Adjusted p-value: 0.5047909 

Comparison: Midwest_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 21920 
Wilcoxon Test P-value: 1.434156e-06 
Adjusted p-value: 9.963609e-06 

Comparison: Midwest_Northeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 465 
Wilcoxon Test P-value: 2.604999e-06 
Adjusted p-value: 1.637428e-05 

Comparison: Southeast_Northeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2975 
Wilcoxon Test P-value: 0.001025238 
Adjusted p-value: 0.003657607 

Comparison: Midwest_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 28806 
Wilcoxon Test P-value: 0.3549019 
Adjusted p-value: 0.4461623 

Comparison: Midwest_Northeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 826.5 
Wilcoxon Test P-value: 0.007612942 
Adjusted p-value: 0.02050833 

Comparison: Southeast_Northeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 3592 
Wilcoxon Test P-value: 0.01856339 
Adjusted p-value: 0.04375655 

Comparison: Midwest_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 13591 
Wilcoxon Test P-value: 0.0347434 
Adjusted p-value: 0.0727957 

Comparison: Midwest_Northeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 335 
Wilcoxon Test P-value: 0.8766659 
Adjusted p-value: 0.9184119 

Comparison: Southeast_Northeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 2119 
Wilcoxon Test P-value: 0.4989535 
Adjusted p-value: 0.5933502 

Comparison: Midwest_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 7379 
Wilcoxon Test P-value: 0.001851498 
Adjusted p-value: 0.005960921 

Comparison: Midwest_Northeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 268 
Wilcoxon Test P-value: 0.2115274 
Adjusted p-value: 0.3172911 

Comparison: Southeast_Northeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1062 
Wilcoxon Test P-value: 0.8692122 
Adjusted p-value: 0.9178881 

Comparison: Midwest_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 10456 
Wilcoxon Test P-value: 1.289274e-06 
Adjusted p-value: 9.454677e-06 

Comparison: Midwest_Northeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 158 
Wilcoxon Test P-value: 0.007991053 
Adjusted p-value: 0.02109638 

Comparison: Southeast_Northeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1476 
Wilcoxon Test P-value: 0.277994 
Adjusted p-value: 0.3822417 

Comparison: Midwest_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 12402 
Wilcoxon Test P-value: 0.001650912 
Adjusted p-value: 0.005448009 

Comparison: Midwest_Northeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 167 
Wilcoxon Test P-value: 0.0115501 
Adjusted p-value: 0.02989437 

Comparison: Southeast_Northeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1223 
Wilcoxon Test P-value: 0.07527204 
Adjusted p-value: 0.1419416 

Comparison: Midwest_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 23418 
Wilcoxon Test P-value: 0.8906342 
Adjusted p-value: 0.9254175 

Comparison: Midwest_Northeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 2042 
Wilcoxon Test P-value: 0.9734644 
Adjusted p-value: 0.9808954 

Comparison: Southeast_Northeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 7059 
Wilcoxon Test P-value: 0.6487875 
Adjusted p-value: 0.7319654 

Comparison: Midwest_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 6181 
Wilcoxon Test P-value: 6.301401e-06 
Adjusted p-value: 3.78084e-05 

Comparison: Midwest_Northeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1327 
Wilcoxon Test P-value: 1.076335e-08 
Adjusted p-value: 1.092894e-07 

Comparison: Southeast_Northeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 5760 
Wilcoxon Test P-value: 5.463243e-08 
Adjusted p-value: 4.507176e-07 

Comparison: Midwest_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 17244 
Wilcoxon Test P-value: 2.75167e-05 
Adjusted p-value: 0.0001397002 

Comparison: Midwest_Northeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2100 
Wilcoxon Test P-value: 0.7714325 
Adjusted p-value: 0.8457171 

Comparison: Southeast_Northeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 9977 
Wilcoxon Test P-value: 0.000521743 
Adjusted p-value: 0.001967716 

Comparison: Midwest_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 22814 
Wilcoxon Test P-value: 0.7752407 
Adjusted p-value: 0.8457171 

Comparison: Midwest_Northeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 660 
Wilcoxon Test P-value: 1.11681e-09 
Adjusted p-value: 1.340172e-08 

Comparison: Southeast_Northeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1965 
Wilcoxon Test P-value: 2.684232e-13 
Adjusted p-value: 7.086373e-12 

Comparison: Midwest_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 25190 
Wilcoxon Test P-value: 0.4709207 
Adjusted p-value: 0.5702893 

Comparison: Midwest_Northeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 4627 
Wilcoxon Test P-value: 0.68439 
Adjusted p-value: 0.7655889 

Comparison: Southeast_Northeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 10720 
Wilcoxon Test P-value: 0.2927494 
Adjusted p-value: 0.3903326 

Comparison: Midwest_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 13867 
Wilcoxon Test P-value: 3.406871e-14 
Adjusted p-value: 1.124268e-12 

Comparison: Midwest_Northeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 3810 
Wilcoxon Test P-value: 5.755428e-05 
Adjusted p-value: 0.0002813765 

Comparison: Southeast_Northeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 7368 
Wilcoxon Test P-value: 0.9417615 
Adjusted p-value: 0.9562501 

Comparison: Midwest_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 20250 
Wilcoxon Test P-value: 0.003754155 
Adjusted p-value: 0.01126247 

Comparison: Midwest_Northeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 6015 
Wilcoxon Test P-value: 0.002886071 
Adjusted p-value: 0.008859566 

Comparison: Southeast_Northeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 17718 
Wilcoxon Test P-value: 2.82595e-11 
Adjusted p-value: 5.328934e-10 

Comparison: Midwest_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 21499 
Wilcoxon Test P-value: 0.0445183 
Adjusted p-value: 0.09040639 

Comparison: Midwest_Northeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 5808 
Wilcoxon Test P-value: 0.01335111 
Adjusted p-value: 0.03263604 

Comparison: Southeast_Northeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 15504 
Wilcoxon Test P-value: 2.735836e-05 
Adjusted p-value: 0.0001397002 

Comparison: Midwest_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 41139 
Wilcoxon Test P-value: 0.0255574 
Adjusted p-value: 0.05622629 

Comparison: Midwest_Northeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 1696.5 
Wilcoxon Test P-value: 0.1220123 
Adjusted p-value: 0.2147416 

Comparison: Southeast_Northeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 4290 
Wilcoxon Test P-value: 0.01711732 
Adjusted p-value: 0.04108156 

Comparison: Midwest_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 20333 
Wilcoxon Test P-value: 2.144748e-14 
Adjusted p-value: 9.43689e-13 

Comparison: Midwest_Northeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1375 
Wilcoxon Test P-value: 0.2092982 
Adjusted p-value: 0.3172911 

Comparison: Southeast_Northeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2042 
Wilcoxon Test P-value: 9.492162e-05 
Adjusted p-value: 0.0004474877 

Comparison: Midwest_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 32383 
Wilcoxon Test P-value: 0.02375285 
Adjusted p-value: 0.05405821 

Comparison: Midwest_Northeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1558 
Wilcoxon Test P-value: 0.03686767 
Adjusted p-value: 0.07603958 

Comparison: Southeast_Northeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 5074 
Wilcoxon Test P-value: 0.2106881 
Adjusted p-value: 0.3172911 

Comparison: Midwest_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 33093 
Wilcoxon Test P-value: 0.05787306 
Adjusted p-value: 0.1157461 

Comparison: Midwest_Northeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1539 
Wilcoxon Test P-value: 0.03066126 
Adjusted p-value: 0.06527881 

Comparison: Southeast_Northeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 5132 
Wilcoxon Test P-value: 0.2428498 
Adjusted p-value: 0.3484366 

Comparison: Midwest_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 34464 
Wilcoxon Test P-value: 0.02424487 
Adjusted p-value: 0.05424276 

Comparison: Midwest_Northeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 3013.5 
Wilcoxon Test P-value: 0.1128031 
Adjusted p-value: 0.2039728 

Comparison: Southeast_Northeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 4185 
Wilcoxon Test P-value: 0.004693128 
Adjusted p-value: 0.0131807 

Comparison: Midwest_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 20027 
Wilcoxon Test P-value: 4.499384e-13 
Adjusted p-value: 9.898644e-12 

Comparison: Midwest_Northeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2017 
Wilcoxon Test P-value: 0.1353588 
Adjusted p-value: 0.2261692 

Comparison: Southeast_Northeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1439 
Wilcoxon Test P-value: 1.042922e-09 
Adjusted p-value: 1.340172e-08 

Comparison: Midwest_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 25012 
Wilcoxon Test P-value: 0.0003495631 
Adjusted p-value: 0.001357127 

Comparison: Midwest_Northeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2192 
Wilcoxon Test P-value: 0.0001564536 
Adjusted p-value: 0.000688396 

Comparison: Southeast_Northeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 4550 
Wilcoxon Test P-value: 0.02739236 
Adjusted p-value: 0.05927527 

Comparison: Midwest_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 25682 
Wilcoxon Test P-value: 0.001565332 
Adjusted p-value: 0.005298047 

Comparison: Midwest_Northeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1691 
Wilcoxon Test P-value: 3.067943e-07 
Adjusted p-value: 2.382168e-06 

Comparison: Southeast_Northeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 3730 
Wilcoxon Test P-value: 0.0003156382 
Adjusted p-value: 0.001262553 

Comparison: Midwest_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 33605 
Wilcoxon Test P-value: 0.0006905474 
Adjusted p-value: 0.002532007 

Comparison: Midwest_Northeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 1136 
Wilcoxon Test P-value: 0.07693033 
Adjusted p-value: 0.1430254 

Comparison: Southeast_Northeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 1636 
Wilcoxon Test P-value: 0.3174314 
Adjusted p-value: 0.4148608 

Comparison: Midwest_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 14440 
Wilcoxon Test P-value: 1.969954e-09 
Adjusted p-value: 2.16695e-08 

Comparison: Midwest_Northeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 631 
Wilcoxon Test P-value: 0.2114133 
Adjusted p-value: 0.3172911 

Comparison: Southeast_Northeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 662 
Wilcoxon Test P-value: 0.3336877 
Adjusted p-value: 0.4318311 

Comparison: Midwest_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 23670 
Wilcoxon Test P-value: 0.001543212 
Adjusted p-value: 0.005298047 

Comparison: Midwest_Northeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 941 
Wilcoxon Test P-value: 0.5514352 
Adjusted p-value: 0.6441544 

Comparison: Southeast_Northeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1782 
Wilcoxon Test P-value: 0.1275942 
Adjusted p-value: 0.2212087 

Comparison: Midwest_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 26248 
Wilcoxon Test P-value: 0.1382643 
Adjusted p-value: 0.22787 

Comparison: Midwest_Northeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1031 
Wilcoxon Test P-value: 0.2555021 
Adjusted p-value: 0.3587902 

Comparison: Southeast_Northeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1701 
Wilcoxon Test P-value: 0.2217429 
Adjusted p-value: 0.3288772 

Comparison: Midwest_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 35207 
Wilcoxon Test P-value: 0.000146945 
Adjusted p-value: 0.000668853 

Comparison: Midwest_Northeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 1269.5 
Wilcoxon Test P-value: 0.9364584 
Adjusted p-value: 0.9562501 

Comparison: Southeast_Northeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 1458 
Wilcoxon Test P-value: 0.1398293 
Adjusted p-value: 0.22787 

Comparison: Midwest_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 15548 
Wilcoxon Test P-value: 7.000339e-11 
Adjusted p-value: 1.026716e-09 

Comparison: Midwest_Northeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 900 
Wilcoxon Test P-value: 0.1290384 
Adjusted p-value: 0.2212087 

Comparison: Southeast_Northeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1071 
Wilcoxon Test P-value: 0.3509276 
Adjusted p-value: 0.4459361 

Comparison: Midwest_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 26861 
Wilcoxon Test P-value: 0.116209 
Adjusted p-value: 0.2072918 

Comparison: Midwest_Northeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 918 
Wilcoxon Test P-value: 0.0839735 
Adjusted p-value: 0.1539514 

Comparison: Southeast_Northeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1335 
Wilcoxon Test P-value: 0.06196501 
Adjusted p-value: 0.1220803 

Comparison: Midwest_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 27098 
Wilcoxon Test P-value: 0.1560558 
Adjusted p-value: 0.2512117 

Comparison: Midwest_Northeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 678 
Wilcoxon Test P-value: 0.004322514 
Adjusted p-value: 0.01267937 

Comparison: Southeast_Northeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1133 
Wilcoxon Test P-value: 0.01216816 
Adjusted p-value: 0.03088841 

Comparison: Midwest_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 28898 
Wilcoxon Test P-value: 6.717948e-06 
Adjusted p-value: 3.855518e-05 

Comparison: Midwest_Northeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 699 
Wilcoxon Test P-value: 0.8973745 
Adjusted p-value: 0.9254175 

Comparison: Southeast_Northeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 634 
Wilcoxon Test P-value: 0.2827764 
Adjusted p-value: 0.3830504 

Comparison: Midwest_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 14982 
Wilcoxon Test P-value: 4.231015e-15 
Adjusted p-value: 2.79247e-13 

Comparison: Midwest_Northeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 636 
Wilcoxon Test P-value: 0.004891504 
Adjusted p-value: 0.01345164 

Comparison: Southeast_Northeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 755 
Wilcoxon Test P-value: 0.2843859 
Adjusted p-value: 0.3830504 

Comparison: Midwest_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 26048 
Wilcoxon Test P-value: 0.02176719 
Adjusted p-value: 0.05040823 

Comparison: Midwest_Northeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 513 
Wilcoxon Test P-value: 0.2736704 
Adjusted p-value: 0.3802578 

Comparison: Southeast_Northeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 579 
Wilcoxon Test P-value: 0.1701525 
Adjusted p-value: 0.270604 

Comparison: Midwest_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 24708 
Wilcoxon Test P-value: 0.2090245 
Adjusted p-value: 0.3172911 

Comparison: Midwest_Northeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 592 
Wilcoxon Test P-value: 0.567223 
Adjusted p-value: 0.6510733 

Comparison: Southeast_Northeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 677 
Wilcoxon Test P-value: 0.4001093 
Adjusted p-value: 0.4982493 

Comparison: Midwest_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 17375 
Wilcoxon Test P-value: 0.1308836 
Adjusted p-value: 0.2214953 

Comparison: Midwest_Northeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 1152 
Wilcoxon Test P-value: 0.4792282 
Adjusted p-value: 0.5750738 

Comparison: Southeast_Northeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 1155 
Wilcoxon Test P-value: 0.8413913 
Adjusted p-value: 0.8956746 

Comparison: Midwest_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 11207 
Wilcoxon Test P-value: 3.967106e-08 
Adjusted p-value: 3.740414e-07 

Comparison: Midwest_Northeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1026 
Wilcoxon Test P-value: 0.0001927083 
Adjusted p-value: 0.0007962893 

Comparison: Southeast_Northeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1227 
Wilcoxon Test P-value: 0.06615038 
Adjusted p-value: 0.1265486 

Comparison: Midwest_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 14755 
Wilcoxon Test P-value: 0.2387929 
Adjusted p-value: 0.3484366 

Comparison: Midwest_Northeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 880 
Wilcoxon Test P-value: 0.4120516 
Adjusted p-value: 0.5047909 

Comparison: Southeast_Northeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 990 
Wilcoxon Test P-value: 0.5141947 
Adjusted p-value: 0.6060152 

Comparison: Midwest_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 14097 
Wilcoxon Test P-value: 0.06380964 
Adjusted p-value: 0.1238658 

Comparison: Midwest_Northeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 847 
Wilcoxon Test P-value: 0.3142481 
Adjusted p-value: 0.4148075 

Comparison: Southeast_Northeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1018 
Wilcoxon Test P-value: 0.6123074 
Adjusted p-value: 0.6967636 

Comparison: Midwest_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 14722 
Wilcoxon Test P-value: 0.5598755 
Adjusted p-value: 0.6482769 

Comparison: Midwest_Northeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 2129.5 
Wilcoxon Test P-value: 0.01322865 
Adjusted p-value: 0.03263604 

Comparison: Southeast_Northeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 2959 
Wilcoxon Test P-value: 0.002463457 
Adjusted p-value: 0.007742294 

Comparison: Midwest_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 10283 
Wilcoxon Test P-value: 4.750791e-11 
Adjusted p-value: 7.838805e-10 

Comparison: Midwest_Northeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1722 
Wilcoxon Test P-value: 5.140668e-08 
Adjusted p-value: 4.507176e-07 

Comparison: Southeast_Northeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2435 
Wilcoxon Test P-value: 1.926582e-05 
Adjusted p-value: 0.000105962 

Comparison: Midwest_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 15532 
Wilcoxon Test P-value: 0.7878298 
Adjusted p-value: 0.8524061 

Comparison: Midwest_Northeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1550 
Wilcoxon Test P-value: 0.8324974 
Adjusted p-value: 0.8934118 

Comparison: Southeast_Northeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2019 
Wilcoxon Test P-value: 0.7452442 
Adjusted p-value: 0.8266574 

Comparison: Midwest_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 10767 
Wilcoxon Test P-value: 2.044214e-06 
Adjusted p-value: 1.349181e-05 

Comparison: Midwest_Northeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1797 
Wilcoxon Test P-value: 0.3513436 
Adjusted p-value: 0.4459361 

Comparison: Southeast_Northeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2907 
Wilcoxon Test P-value: 0.004481354 
Adjusted p-value: 0.01285954 

Four analyte median + IQR plots 2012-2022 + counts and arrows (Turkey Solid )

Code
# Filter out rows with non-missing Total_N values and for specific regions
MDB_turkeystackNPK_filtered <- MDB_turkeystackNPK[!is.na(MDB_turkeystackNPK$Total_N) & 
                                                   MDB_turkeystackNPK$Region %in% c("Southeast", "Midwest"), ]

custom_theme <- theme_minimal() +
  theme(
    text = element_text(family = "Times New Roman", size = 11),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

# Create separate plots for each nutrient type
plots <- list()

# Initialize variables to store the minimum and maximum values of lower and upper quartiles
min_lower_quantile <- Inf
max_upper_quantile <- -Inf

# Function to calculate counts for each nutrient
calculate_counts <- function(data) {
  counts <- data %>%
    filter(!is.na(Total_N) & Year.Analyzed >= 2012 & Year.Analyzed <= 2022 & Region != "Northeast") %>%
    group_by(Region) %>%
    summarise(total_samples = n()) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  return(counts)
}

for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Filter data for the current nutrient type
  filtered_data <- MDB_turkeystackNPK_filtered[!is.na(MDB_turkeystackNPK_filtered[[nutrient]]), ]
  
  # Calculate median and IQR for each combination of Year.Analyzed and Region
  summary_stats <- filtered_data %>%
    group_by(Year.Analyzed, Region) %>%
    summarise(median_value = median(.data[[nutrient]]),
              lower_quantile = quantile(.data[[nutrient]], 0.25),
              upper_quantile = quantile(.data[[nutrient]], 0.75)) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  
  # Update minimum and maximum values of lower and upper quartiles
  min_lower_quantile <- min(min_lower_quantile, min(summary_stats$lower_quantile))
  max_upper_quantile <- max(max_upper_quantile, max(summary_stats$upper_quantile))
  
  # Determine a suitable step size for the y-axis
  y_range <- max_upper_quantile - min_lower_quantile
  num_breaks <- max(4, floor(y_range * 10))  # Ensure at least four breaks
  step_size <- y_range / num_breaks
  
  # Calculate counts for all regions
  counts <- calculate_counts(filtered_data)

  # Create line graph with median for each year and region, with connecting lines
  plot <- ggplot(summary_stats, aes(x = factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +
    geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha = 0.3) +  # Add shaded area for IQR
    geom_line(aes(linetype = Region)) +  # Add line graph with connecting lines
    geom_point() +  # Add points for each data point
    labs(x = "Year Analyzed", 
         y = switch(nutrient,
                    Total_N = "Total N (%)",
                    Ammonium.N = expression(paste("NH"[4], "-N (%)")),
                    Phosphorus = expression(paste("P"[2], "O"[5], " (%)")),
                    Potassium = expression(paste("K"[2], "O (%)"))
         ), 
         title = NULL) +  # Remove titles for all graphs
    custom_theme +  # Apply custom theme with font settings
    scale_color_manual(values = c("Northeast" = "darkgreen", "Southeast" = "red", "Midwest" = "blue")) +  # Define colors for regions
    scale_fill_manual(values = c("Northeast" = "lightgreen", "Southeast" = "lightpink", "Midwest" = "lightblue")) +  # Define fill colors for IQR
    guides(fill = FALSE) +  # Remove legend for IQR fill colors
    scale_y_continuous(limits = c(0, 3.5), breaks = seq(0, 3.5, by = 1)) +
    geom_text(data = counts, aes(label = paste("n =", total_samples),
                                 x = "2021", y = 1.6 - (0.3 * seq_along(total_samples)), color = Region),
              hjust = 1, vjust = 0, size = 3, family = "Times New Roman")  # Add text annotations for counts
  # Add the arrow only for the Ammonium.N graph
  if (nutrient == "Phosphorus") {
    plot <- plot + annotate("segment", x = "2022", y = 1.2, xend = "2022", yend = 1, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "red")
  }
  if (nutrient == "Phosphorus") {
    plot <- plot + annotate("segment", x = "2022", y = 1.25, xend = "2022", yend = 1.5, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "blue")
  }

  # Add the plot to the list
  plots[[nutrient]] <- plot
}

# Arrange the plots together using patchwork
turkeystack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] + plot_layout(guides = "collect")

# Print the final plot
print(turkeystack_final_plot)

Beef Liquid

Beef Liquid Manure by Region Overview Table (%)

Code
MDB_beefpump<-filter(MDB_pump3pl_clean, Animal.or.Other.Amendment.Type=="Beef") 


df_nest = MDB_beefpump %>% 
  group_by(Region) %>% 
  nest()

MDB_beefpump_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_beefpump_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Midwest
Total_N 0.27000 0.34100 126.0 0.30100 0.2450 81.6 0.00e+00 0.010000 0.057400 0.51600 0.6300 1.0600 0.377 -1.040 2927
Amm_N 0.16000 0.22200 139.0 0.17500 0.1650 94.1 0.00e+00 0.000000 0.010000 0.30000 0.3910 0.8160 0.556 -0.556 1742
P2O5 0.13000 0.15600 120.0 0.17400 0.1510 86.6 0.00e+00 0.010000 0.040000 0.28000 0.3800 0.9600 0.915 0.521 2927
K2O 0.22000 0.16300 74.1 0.22800 0.1400 61.5 0.00e+00 0.060000 0.110000 0.33000 0.4200 1.3800 0.669 1.430 2927
Southeast
Total_N 0.01620 0.01590 97.8 0.04820 0.1050 218.0 2.00e-05 0.002000 0.007130 0.03670 0.1520 1.3800 7.090 75.700 348
Amm_N 0.00277 0.00339 123.0 0.00544 0.0110 203.0 1.20e-05 0.000106 0.000527 0.00512 0.0105 0.0851 5.000 29.600 92
P2O5 0.01660 0.01680 101.0 0.16300 0.3450 212.0 4.56e-05 0.005720 0.008360 0.11000 0.4980 2.3000 3.130 10.500 349
K2O 0.07170 0.07480 104.0 0.26600 0.5170 194.0 9.86e-04 0.016400 0.039300 0.19900 0.7210 3.1900 3.190 10.300 349
Northeast
Total_N 0.12300 0.14500 118.0 0.15200 0.1310 86.6 6.40e-03 0.014400 0.028100 0.26200 0.3430 0.4580 0.554 -0.927 28
Amm_N 0.03220 0.04170 129.0 0.05010 0.0528 105.0 2.33e-04 0.003200 0.008090 0.07730 0.1200 0.2050 1.130 0.564 28
P2O5 0.08070 0.10600 132.0 0.09240 0.0952 103.0 1.96e-03 0.006430 0.009020 0.14900 0.2100 0.3760 1.010 0.472 28
K2O 0.12200 0.08910 72.8 0.13800 0.1190 86.7 8.59e-03 0.022100 0.058200 0.16800 0.2650 0.5830 1.810 4.390 28
Northern Plains
Total_N 0.02700 0.03440 127.0 0.51600 0.7440 144.0 2.00e-04 0.003070 0.008230 1.24000 1.6700 2.7800 1.090 -0.320 318
Amm_N 0.01280 0.01620 127.0 0.10700 0.1630 153.0 1.00e-04 0.001200 0.003000 0.20400 0.3870 0.7140 1.340 0.423 314
P2O5 0.01400 0.01630 116.0 0.44500 0.6910 155.0 1.00e-03 0.003000 0.006000 1.07000 1.5400 3.1200 1.290 0.348 318
K2O 0.10300 0.12600 122.0 0.59900 0.8410 140.0 1.90e-03 0.014900 0.039000 1.43000 1.9200 3.0600 1.180 -0.158 318
Southern Plains
Total_N 0.66500 0.96000 144.0 0.73900 0.7290 98.7 1.00e-04 0.007400 0.018200 1.34000 1.7900 2.7100 0.464 -1.110 1731
Amm_N 0.01790 0.02510 140.0 0.07790 0.1140 146.0 1.00e-04 0.002000 0.004000 0.11100 0.2530 0.6500 1.890 3.240 1706
P2O5 0.66000 0.95900 145.0 0.70800 0.7430 105.0 0.00e+00 0.006000 0.011000 1.21000 1.6600 7.4600 1.150 3.830 1727
K2O 0.70300 0.92500 132.0 0.85200 0.7940 93.2 1.70e-03 0.049100 0.099900 1.47000 1.9200 5.9000 0.781 0.438 1720
Pacific Northwest
Total_N 0.04840 0.01450 30.0 0.30800 0.3670 119.0 3.86e-02 0.041200 0.045100 0.61200 0.7210 0.7940 0.358 -2.130 5
Amm_N 0.01800 0.00593 32.9 0.02490 0.0159 63.8 1.40e-02 0.014200 0.014500 0.02600 0.0416 0.0520 0.855 -1.220 5
P2O5 0.03400 0.02080 61.0 0.33800 0.4250 126.0 2.00e-02 0.023200 0.028000 0.80300 0.8040 0.8040 0.292 -2.250 5
K2O 0.23200 0.12800 55.0 0.35800 0.2610 72.9 1.46e-01 0.150000 0.157000 0.51200 0.6500 0.7420 0.459 -1.870 5

Beef Liquid Manure by Region Overview Table (lbs/1000 gal)

Code
#Convert Units from % to lbs/1000 gal
multiply_by_83pt4<- MDB_beefpump  %>% mutate( Value = Value * 83.4)
#print(multiply_by_83pt4) #View(multiply_by_83pt4)

df_nest = multiply_by_83pt4 %>% 
  group_by(Region) %>% 
  nest()

MDB_beefpump_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_beefpump_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Midwest
Total_N 22.500 28.400 126.0 25.100 20.500 81.6 0.00000 0.83400 4.7900 43.000 52.500 88.40 0.377 -1.040 2927
Amm_N 13.300 18.500 139.0 14.600 13.700 94.1 0.00000 0.00000 0.8340 25.000 32.600 68.10 0.556 -0.556 1742
P2O5 10.800 13.100 120.0 14.500 12.600 86.6 0.00000 0.83400 3.3400 23.400 31.700 80.10 0.915 0.521 2927
K2O 18.300 13.600 74.1 19.000 11.700 61.5 0.00000 5.00000 9.1700 27.500 35.000 115.00 0.669 1.430 2927
Southeast
Total_N 1.350 1.320 97.8 4.020 8.790 218.0 0.00167 0.16700 0.5940 3.060 12.600 115.00 7.090 75.700 348
Amm_N 0.231 0.283 123.0 0.454 0.919 203.0 0.00100 0.00881 0.0439 0.427 0.873 7.09 5.000 29.600 92
P2O5 1.390 1.400 101.0 13.600 28.800 212.0 0.00380 0.47700 0.6970 9.190 41.600 192.00 3.130 10.500 349
K2O 5.980 6.240 104.0 22.200 43.100 194.0 0.08220 1.37000 3.2800 16.600 60.100 266.00 3.190 10.300 349
Northeast
Total_N 10.300 12.100 118.0 12.600 11.000 86.6 0.53400 1.20000 2.3400 21.800 28.600 38.20 0.554 -0.927 28
Amm_N 2.680 3.480 129.0 4.180 4.400 105.0 0.01950 0.26700 0.6750 6.450 10.000 17.10 1.130 0.564 28
P2O5 6.730 8.880 132.0 7.700 7.940 103.0 0.16400 0.53700 0.7530 12.400 17.500 31.30 1.010 0.472 28
K2O 10.200 7.430 72.8 11.500 9.950 86.7 0.71600 1.85000 4.8500 14.000 22.100 48.60 1.810 4.390 28
Northern Plains
Total_N 2.260 2.870 127.0 43.000 62.100 144.0 0.01670 0.25600 0.6860 104.000 140.000 232.00 1.090 -0.320 318
Amm_N 1.060 1.350 127.0 8.910 13.600 153.0 0.00834 0.10000 0.2500 17.000 32.300 59.50 1.340 0.423 314
P2O5 1.170 1.360 116.0 37.100 57.600 155.0 0.08340 0.25000 0.5000 89.600 128.000 261.00 1.290 0.348 318
K2O 8.590 10.500 122.0 50.000 70.100 140.0 0.15800 1.25000 3.2500 119.000 160.000 255.00 1.180 -0.158 318
Southern Plains
Total_N 55.500 80.100 144.0 61.600 60.800 98.7 0.00834 0.61700 1.5200 112.000 149.000 226.00 0.464 -1.110 1731
Amm_N 1.490 2.090 140.0 6.490 9.480 146.0 0.00834 0.16700 0.3340 9.260 21.100 54.20 1.890 3.240 1706
P2O5 55.000 80.000 145.0 59.000 62.000 105.0 0.00000 0.50000 0.9170 101.000 139.000 622.00 1.150 3.830 1727
K2O 58.600 77.100 132.0 71.100 66.200 93.2 0.14200 4.09000 8.3300 123.000 160.000 492.00 0.781 0.438 1720
Pacific Northwest
Total_N 4.040 1.210 30.0 25.700 30.600 119.0 3.22000 3.44000 3.7600 51.000 60.100 66.20 0.358 -2.130 5
Amm_N 1.500 0.495 32.9 2.080 1.330 63.8 1.17000 1.18000 1.2100 2.170 3.470 4.34 0.855 -1.220 5
P2O5 2.840 1.730 61.0 28.200 35.500 126.0 1.67000 1.93000 2.3400 67.000 67.000 67.10 0.292 -2.250 5
K2O 19.300 10.600 55.0 29.800 21.800 72.9 12.20000 12.50000 13.1000 42.700 54.200 61.90 0.459 -1.870 5

Table of samples by region by year for Beef Liquid manure

Code
# Sort out for beef while still having separate NPK columns
MDB_beefpumpNPK <- filter(MDB_pump3, Animal.Type.Combined.Category == "Beef")
# Create a table of number of samples for each region per year
sample_table <- table(MDB_beefpumpNPK$Region, MDB_beefpumpNPK$Year.Analyzed)

# Print the sample table
print(sample_table)
                   
                    2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
  Midwest             79  231  262  240  301  315  262  344  284  318  292
  Northeast            7    1    4    3    2    1    3    2    1    1    3
  Northern Plains      0    0    0    0    0    2    2  123   47   77   67
  Pacific Northwest    0    0    0    0    0    0    0    3    0    2    0
  Southeast           39   29   34   43   43   32   31   28   34   21   15
  Southern Plains      0    0    0    0    0    0    0  392  497  569  273

Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Beef Liquid)

Code
# Load required library
library(Kendall)

# List of selected regions
selected_regions <- c("Midwest")

# List of analytes
analytes <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Function to perform Mann-Kendall trend test for a given analyte across all selected regions
perform_mk_test_combined <- function(data, analyte, selected_regions) {
  # Subset data for the selected regions and analyte
  filtered_data <- data[data$Region %in% selected_regions, ]
  cleaned_data <- filtered_data[!is.na(filtered_data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk_combined <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk_combined))
}

# Perform Mann-Kendall trend test for each analyte combined across selected regions
for (analyte in analytes) {
  cat("Analyzing", analyte, "for all regions combined:\n")
  print(perform_mk_test_combined(MDB_beefpumpNPK, analyte, selected_regions))
  cat("\n")
}
Analyzing Total_N for all regions combined:
Score =  17 , Var(Score) = 165
denominator =  55
tau = 0.309, 2-sided pvalue =0.21291
NULL

Analyzing Ammonium.N for all regions combined:
Score =  3 , Var(Score) = 165
denominator =  55
tau = 0.0545, 2-sided pvalue =0.87627
NULL

Analyzing Phosphorus for all regions combined:
Score =  15 , Var(Score) = 163
denominator =  53.99074
tau = 0.278, 2-sided pvalue =0.27283
NULL

Analyzing Potassium for all regions combined:
Score =  9 , Var(Score) = 163
denominator =  53.99074
tau = 0.167, 2-sided pvalue =0.53092
NULL
Code
# Function to perform Mann-Kendall trend test
perform_mk_test <- function(data, analyte) {
  # Filter out NA values in the analyte column
  cleaned_data <- data[!is.na(data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk))
}

# Iterate over selected regions and analytes
for (region in selected_regions) {
  for (analyte in analytes) {
    # Subset data for the current region
    region_data <- MDB_beefpumpNPK[MDB_beefpumpNPK$Region == region, ]
    
    # Print information about the current region and analyte
    cat("Analyzing", analyte, "in", region, "region:\n")
    
    # Perform Mann-Kendall trend test
    result_summary <- perform_mk_test(region_data, analyte)
    
    # Print summary of the Mann-Kendall test result
    print(result_summary)
    cat("\n")
  }
}
Analyzing Total_N in Midwest region:
Score =  17 , Var(Score) = 165
denominator =  55
tau = 0.309, 2-sided pvalue =0.21291
NULL

Analyzing Ammonium.N in Midwest region:
Score =  3 , Var(Score) = 165
denominator =  55
tau = 0.0545, 2-sided pvalue =0.87627
NULL

Analyzing Phosphorus in Midwest region:
Score =  15 , Var(Score) = 163
denominator =  53.99074
tau = 0.278, 2-sided pvalue =0.27283
NULL

Analyzing Potassium in Midwest region:
Score =  9 , Var(Score) = 163
denominator =  53.99074
tau = 0.167, 2-sided pvalue =0.53092
NULL

Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Beef Liquid)

Code
# Filter data for selected regions
selected_regions <- c("Midwest", "Southeast")
filtered_data <- MDB_beefpumpNPK[MDB_beefpumpNPK$Region %in% selected_regions, ]

# Initialize an empty list to store cleaned data for each nutrient
cleaned_data_list <- list()

# Loop over each nutrient
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Remove missing values for the current nutrient
  cleaned_data <- filtered_data[!is.na(filtered_data[[nutrient]]), ]
  cleaned_data_list[[nutrient]] <- cleaned_data
}

# Create a Function for Wilcoxon Test and Correction
perform_wilcoxon_test <- function(region1, region2, year, nutrient, data) {
    # Extract data for the given regions, year, and nutrient
    region1_data <- data[data$Region == region1 & data$Year.Analyzed == year, nutrient]
    region2_data <- data[data$Region == region2 & data$Year.Analyzed == year, nutrient]
    
    # Perform Wilcoxon rank sum test
    wilcox_result <- wilcox.test(region1_data, region2_data)
        
    # Apply Benjamini-Hochberg correction
    adjusted_p_value <- p.adjust(wilcox_result$p.value, method = "BH")
    
    # Return results
    return(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))
}

# Iterate Over Years, Regions, and Nutrients
years <- unique(filtered_data$Year.Analyzed)
regions <- unique(filtered_data$Region)
nutrients <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Initialize empty vectors
all_p_values <- c()
comparison_results <- list()
comparison_names <- c()

# Loop over years, regions, and nutrients, perform tests, and gather p-values
index <- 1
for (year in years) {
    for (nutrient in nutrients) {
        for (i in 1:(length(regions)-1)) {
            for (j in (i+1):length(regions)) {
                region1 <- regions[i]
                region2 <- regions[j]
                
                # Perform Wilcoxon test and correction
                results <- perform_wilcoxon_test(region1, region2, year, nutrient, cleaned_data_list[[nutrient]])
                
                # Store p-value
                all_p_values <- c(all_p_values, results$wilcox_result$p.value)
                
                # Store comparison name
                comparison_names <- c(comparison_names, paste(region1, region2, year, nutrient, sep = "_"))
                
                # Store results
                comparison_results[[index]] <- results
                
                index <- index + 1
            }
        }
    }
}

# Sort comparison names based on year
sorted_indices <- order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))
sorted_comparison_names <- comparison_names[sorted_indices]

# Apply Benjamini-Hochberg Procedure
adjusted_p_values <- p.adjust(all_p_values, method = "BH")

# Print results in chronological order
for (name in sorted_comparison_names) {
    index <- match(name, comparison_names)
    year <- as.numeric(unlist(strsplit(name, "_"))[3])
    nutrient <- unlist(strsplit(name, "_"))[4]
    
    # Extract Wilcoxon test results
    result <- comparison_results[[index]]$wilcox_result
    
    # Print results with adjusted p-values
    cat("Comparison:", name, "\n")
    cat("Year:", year, "\n")
    cat("Nutrient:", nutrient, "\n")
    cat("Wilcoxon Test Statistic:", result$statistic, "\n")
    cat("Wilcoxon Test P-value:", result$p.value, "\n")
    cat("Adjusted p-value:", adjusted_p_values[index], "\n")
    cat("\n")
}
Comparison: Midwest_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 2777 
Wilcoxon Test P-value: 1.528612e-12 
Adjusted p-value: 2.241964e-11 

Comparison: Midwest_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 628 
Wilcoxon Test P-value: 5.33411e-07 
Adjusted p-value: 1.805391e-06 

Comparison: Midwest_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2297 
Wilcoxon Test P-value: 1.520691e-05 
Adjusted p-value: 3.521599e-05 

Comparison: Midwest_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2016 
Wilcoxon Test P-value: 0.006551099 
Adjusted p-value: 0.008734799 

Comparison: Midwest_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 5250 
Wilcoxon Test P-value: 6.291119e-07 
Adjusted p-value: 1.977209e-06 

Comparison: Midwest_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 527 
Wilcoxon Test P-value: 0.0628645 
Adjusted p-value: 0.07092405 

Comparison: Midwest_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 4604 
Wilcoxon Test P-value: 0.0009975354 
Adjusted p-value: 0.001512298 

Comparison: Midwest_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 4798 
Wilcoxon Test P-value: 0.0001476697 
Adjusted p-value: 0.0002824986 

Comparison: Midwest_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 6928 
Wilcoxon Test P-value: 1.347364e-07 
Adjusted p-value: 8.469145e-07 

Comparison: Midwest_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 916 
Wilcoxon Test P-value: 0.03521129 
Adjusted p-value: 0.04077097 

Comparison: Midwest_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 5597 
Wilcoxon Test P-value: 0.0148054 
Adjusted p-value: 0.01915993 

Comparison: Midwest_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 5995 
Wilcoxon Test P-value: 0.001031112 
Adjusted p-value: 0.001512298 

Comparison: Midwest_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 8358 
Wilcoxon Test P-value: 9.506481e-11 
Adjusted p-value: 1.045713e-09 

Comparison: Midwest_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 768 
Wilcoxon Test P-value: 0.2700855 
Adjusted p-value: 0.2829467 

Comparison: Midwest_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 7721 
Wilcoxon Test P-value: 2.178664e-07 
Adjusted p-value: 8.714657e-07 

Comparison: Midwest_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 7814 
Wilcoxon Test P-value: 7.859056e-08 
Adjusted p-value: 5.763308e-07 

Comparison: Midwest_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 11126 
Wilcoxon Test P-value: 2.30375e-14 
Adjusted p-value: 1.01365e-12 

Comparison: Midwest_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1575 
Wilcoxon Test P-value: 0.004433569 
Adjusted p-value: 0.006096157 

Comparison: Midwest_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 9211 
Wilcoxon Test P-value: 7.07104e-06 
Adjusted p-value: 1.830152e-05 

Comparison: Midwest_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 9410 
Wilcoxon Test P-value: 1.457761e-06 
Adjusted p-value: 4.2761e-06 

Comparison: Midwest_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 7898 
Wilcoxon Test P-value: 1.378193e-08 
Adjusted p-value: 1.21281e-07 

Comparison: Midwest_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 710 
Wilcoxon Test P-value: 0.1473867 
Adjusted p-value: 0.1581711 

Comparison: Midwest_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 7164 
Wilcoxon Test P-value: 8.541013e-05 
Adjusted p-value: 0.0001708203 

Comparison: Midwest_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 7242 
Wilcoxon Test P-value: 4.648508e-05 
Adjusted p-value: 9.739732e-05 

Comparison: Midwest_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 6381 
Wilcoxon Test P-value: 1.964344e-07 
Adjusted p-value: 8.714657e-07 

Comparison: Midwest_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 628 
Wilcoxon Test P-value: 0.1165849 
Adjusted p-value: 0.1282434 

Comparison: Midwest_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 6024 
Wilcoxon Test P-value: 1.079385e-05 
Adjusted p-value: 2.638498e-05 

Comparison: Midwest_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 5621 
Wilcoxon Test P-value: 0.0004709772 
Adjusted p-value: 0.0007675184 

Comparison: Midwest_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 7658 
Wilcoxon Test P-value: 2.04494e-07 
Adjusted p-value: 8.714657e-07 

Comparison: Midwest_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1156 
Wilcoxon Test P-value: 0.02278816 
Adjusted p-value: 0.02785219 

Comparison: Midwest_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 6524 
Wilcoxon Test P-value: 0.001797968 
Adjusted p-value: 0.002551955 

Comparison: Midwest_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 6138 
Wilcoxon Test P-value: 0.01570584 
Adjusted p-value: 0.01974449 

Comparison: Midwest_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 8482 
Wilcoxon Test P-value: 5.502264e-13 
Adjusted p-value: 1.210498e-11 

Comparison: Midwest_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 2133 
Wilcoxon Test P-value: 3.149917e-06 
Adjusted p-value: 8.662272e-06 

Comparison: Midwest_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 6931 
Wilcoxon Test P-value: 3.31472e-05 
Adjusted p-value: 7.292385e-05 

Comparison: Midwest_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 6570 
Wilcoxon Test P-value: 0.0005863852 
Adjusted p-value: 0.0009214625 

Comparison: Midwest_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 5594 
Wilcoxon Test P-value: 2.174864e-07 
Adjusted p-value: 8.714657e-07 

Comparison: Midwest_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 414 
Wilcoxon Test P-value: 0.03204478 
Adjusted p-value: 0.0381073 

Comparison: Midwest_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 4953 
Wilcoxon Test P-value: 0.0002071057 
Adjusted p-value: 0.0003796938 

Comparison: Midwest_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 4939 
Wilcoxon Test P-value: 0.0002348743 
Adjusted p-value: 0.0003974795 

Comparison: Midwest_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 3890 
Wilcoxon Test P-value: 3.975484e-07 
Adjusted p-value: 1.457677e-06 

Comparison: Midwest_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1710 
Wilcoxon Test P-value: 0.000216527 
Adjusted p-value: 0.0003810875 

Comparison: Midwest_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2173 
Wilcoxon Test P-value: 0.9607369 
Adjusted p-value: 0.9607369 

Comparison: Midwest_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2258 
Wilcoxon Test P-value: 0.8403874 
Adjusted p-value: 0.8599313 

Four analyte median + IQR plots 2012-2022 (Beef Liquid)

Code
# Filter out rows with non-missing Total_N values and for specific regions
MDB_beefpumpNPK_filtered <- MDB_beefpumpNPK[!is.na(MDB_beefpumpNPK$Total_N) & 
                                              MDB_beefpumpNPK$Region %in% c("Midwest"), ]
custom_theme <- theme_minimal() +
  theme(
    text = element_text(family = "Times New Roman", size = 11),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

# Create separate plots for each nutrient type
plots <- list()

# Initialize variables to store the minimum and maximum values of lower and upper quartiles
min_lower_quantile <- Inf
max_upper_quantile <- -Inf
# Function to calculate counts for each nutrient
calculate_counts <- function(data) {
  counts <- data %>%
    filter(!is.na(Total_N) & Year.Analyzed >= 2012 & Year.Analyzed <= 2022) %>%
    group_by(Region) %>%
    filter(Region == "Midwest") %>%  # Filter by Midwest region only
    summarise(total_samples = n()) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  return(counts)
}


for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Filter data for the current nutrient type
  filtered_data <- MDB_beefpumpNPK_filtered[!is.na(MDB_beefpumpNPK_filtered[[nutrient]]), ]
  
  # Calculate median and IQR for each combination of Year.Analyzed and Region
  summary_stats <- filtered_data %>%
    group_by(Year.Analyzed, Region) %>%
    summarise(median_value = median(.data[[nutrient]]),
              lower_quantile = quantile(.data[[nutrient]], 0.25),
              upper_quantile = quantile(.data[[nutrient]], 0.75)) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  
  # Update minimum and maximum values of lower and upper quartiles
  min_lower_quantile <- min(min_lower_quantile, min(summary_stats$lower_quantile))
  max_upper_quantile <- max(max_upper_quantile, max(summary_stats$upper_quantile))
  
  # Determine a suitable step size for the y-axis
  y_range <- max_upper_quantile - min_lower_quantile
  num_breaks <- max(4, floor(y_range * 10))  # Ensure at least four breaks
  step_size <- y_range / num_breaks
  
  # Calculate counts for all regions
  counts <- calculate_counts(filtered_data)

  # Create line graph with median for each year and region, with connecting lines
  plot <- ggplot(summary_stats, aes(x = factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +
    geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha = 0.3) +  # Add shaded area for IQR
    geom_line(aes(linetype = Region)) +  # Add line graph with connecting lines
    geom_point() +  # Add points for each data point
    labs(x = "Year Analyzed", 
         y = switch(nutrient,
                    Total_N = "Total N (%)",
                    Ammonium.N = expression(paste("NH"[4], "-N (%)")),
                    Phosphorus = expression(paste("P"[2], "O"[5], " (%)")),
                    Potassium = expression(paste("K"[2], "O (%)"))
         ), 
         title = NULL) +  # Remove titles for all graphs
    custom_theme +  # Apply custom theme with font settings
    scale_color_manual(values = c("Midwest" = "blue")) +  # Define color for Midwest region only
    scale_fill_manual(values = c("Midwest" = "lightblue")) +  # Define fill color for IQR of Midwest region only
    guides(fill = FALSE) +  # Remove legend for IQR fill colors
    scale_y_continuous(limits = c(0, .75), breaks = seq(0, .75, by = 0.1)) +
    geom_text(data = counts, aes(label = paste("n =", total_samples),
                                 x = "2022", y = 0.75 - (0.08 * seq_along(total_samples)), color = Region),
              hjust = 1, vjust = 0, size = 3, family = "Times New Roman")  # Add text annotations for counts

  # Add the plot to the list
  plots[[nutrient]] <- plot
}

# Arrange the plots together using patchwork
beefpump_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] + plot_layout(guides = "collect")

# Print the final plot
print(beefpump_final_plot)

Dairy Liquid

Dairy Liquid Manure by Region Overview Table (%)

Code
MDB_dairypump<-filter(MDB_pump3pl_clean, Animal.or.Other.Amendment.Type=="Dairy") 


df_nest = MDB_dairypump %>% 
  group_by(Region) %>% 
  nest()

MDB_dairypump_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
           MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_dairypump_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Southeast
Total_N 0.04700 0.06350 135.0 0.0863 0.0970 112.0 0.00e+00 2.01e-03 0.01340 0.1370 0.2260 1.000 1.7900 5.48e+00 5794
Amm_N 0.00742 0.00971 131.0 0.0153 0.0243 159.0 0.00e+00 9.97e-05 0.00189 0.0182 0.0429 0.448 6.2700 7.82e+01 2816
P2O5 0.03790 0.04350 115.0 0.1830 1.9500 1070.0 0.00e+00 6.26e-03 0.01280 0.0992 0.4350 103.000 47.4000 2.32e+03 7080
K2O 0.11100 0.09930 89.7 0.3600 3.5500 987.0 0.00e+00 2.21e-02 0.05390 0.2030 0.8770 182.000 47.3000 2.30e+03 7079
Midwest
Total_N 0.21800 0.07860 36.0 0.2210 0.9050 410.0 0.00e+00 9.96e-02 0.16000 0.2700 0.3200 183.000 194.0000 3.91e+04 42955
Amm_N 0.10000 0.05930 59.3 0.1000 0.1220 122.0 0.00e+00 1.18e-05 0.05700 0.1400 0.1700 10.000 43.5000 3.04e+03 21129
P2O5 0.08880 0.04460 50.2 0.0891 0.0566 63.5 0.00e+00 2.21e-02 0.05740 0.1170 0.1500 2.820 5.6700 1.66e+02 42888
K2O 0.21300 0.06950 32.6 0.2180 0.2590 119.0 0.00e+00 1.20e-01 0.16800 0.2600 0.3200 49.600 163.0000 3.09e+04 42974
Northeast
Total_N 0.23500 0.10300 44.0 0.2240 0.1120 50.2 0.00e+00 6.67e-02 0.15300 0.2950 0.3440 2.410 1.9400 3.68e+01 3923
Amm_N 0.10900 0.05060 46.6 0.1040 0.0533 51.3 6.58e-04 2.53e-02 0.06760 0.1380 0.1660 0.354 0.0805 -1.12e-02 3859
P2O5 0.09160 0.04350 47.4 0.0902 0.0650 72.1 0.00e+00 1.83e-02 0.05500 0.1170 0.1440 2.100 9.5600 2.50e+02 3988
K2O 0.21500 0.09200 42.7 0.2160 0.1110 51.2 0.00e+00 7.95e-02 0.15300 0.2770 0.3320 1.950 2.8100 3.42e+01 3988
Southwest
Total_N 0.04760 0.03450 72.4 0.0761 0.1640 215.0 1.02e-04 1.30e-02 0.02670 0.0758 0.1100 1.720 7.9300 6.81e+01 1484
Amm_N 0.02920 0.01970 67.5 0.0332 0.0358 108.0 5.62e-05 5.87e-03 0.01550 0.0417 0.0514 0.438 5.6200 4.49e+01 1435
P2O5 0.01730 0.01080 62.6 0.0359 0.0954 266.0 0.00e+00 6.63e-03 0.01130 0.0297 0.0514 1.500 8.2500 8.09e+01 1486
K2O 0.08030 0.04840 60.3 0.1240 0.3400 274.0 0.00e+00 2.54e-02 0.04660 0.1120 0.1560 3.980 8.8700 8.14e+01 1487
Northern Plains
Total_N 0.22000 0.06890 31.4 0.2260 0.1380 61.0 4.00e-03 1.10e-01 0.17500 0.2660 0.3140 1.720 5.5500 5.08e+01 626
Amm_N 0.10700 0.04890 45.7 0.1130 0.0591 52.3 2.00e-03 4.62e-02 0.07850 0.1400 0.1900 0.418 1.0200 2.41e+00 361
P2O5 0.07240 0.02970 41.0 0.0808 0.0854 106.0 1.20e-04 2.10e-02 0.05030 0.0910 0.1200 1.250 8.0000 8.86e+01 627
K2O 0.20100 0.05390 26.8 0.2140 0.1860 86.8 5.44e-04 1.43e-01 0.16000 0.2310 0.2630 3.050 10.1000 1.24e+02 627
Pacific Northwest
Total_N 0.07320 0.05290 72.2 0.1370 0.1330 97.1 7.80e-03 3.74e-02 0.05300 0.2080 0.2710 0.710 2.1900 5.68e+00 62
Amm_N 0.04480 0.02700 60.2 0.0714 0.0918 129.0 1.50e-03 1.20e-02 0.03070 0.0700 0.1570 0.640 4.2800 2.31e+01 57
P2O5 0.01720 0.01490 87.0 0.0313 0.0326 104.0 2.29e-04 5.63e-03 0.01010 0.0380 0.0887 0.131 1.4200 8.03e-01 67
K2O 0.11100 0.08610 77.8 0.1580 0.1550 97.8 0.00e+00 3.62e-02 0.06590 0.2200 0.3230 0.948 2.6000 9.32e+00 67
Southern Plains
Total_N 0.38100 0.51700 136.0 0.5550 0.5780 104.0 1.00e-04 1.44e-02 0.07980 0.8400 1.4000 5.440 1.8000 7.89e+00 611
Amm_N 0.04200 0.05490 131.0 0.0598 0.0656 110.0 1.00e-04 3.00e-03 0.00940 0.0900 0.1300 0.430 2.1800 7.44e+00 609
P2O5 0.17600 0.25200 143.0 0.4290 1.1000 256.0 1.00e-03 7.00e-03 0.01800 0.6490 1.0300 25.100 18.5000 4.09e+02 611
K2O 0.33200 0.42700 129.0 0.7010 0.8230 117.0 3.70e-03 3.57e-02 0.11200 1.0800 1.9300 4.490 1.5200 1.79e+00 598

Dairy Liquid Manure by Region Overview Table (lbs/1000 gal)

Code
#Convert Units from % to lbs/1000 gal
multiply_by_83pt4<- MDB_dairypump  %>% mutate( Value = Value * 83.4)
#print(multiply_by_83pt4) #View(multiply_by_83pt4)

df_nest = multiply_by_83pt4 %>% 
  group_by(Region) %>% 
  nest()

MDB_dairypump_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
           MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_dairypump_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Southeast
Total_N 3.920 5.300 135.0 7.20 8.09 112.0 0.00000 1.67e-01 1.120 11.40 18.90 83.4 1.7900 5.48e+00 5794
Amm_N 0.619 0.810 131.0 1.28 2.03 159.0 0.00000 8.31e-03 0.157 1.52 3.58 37.3 6.2700 7.82e+01 2816
P2O5 3.160 3.620 115.0 15.20 162.00 1070.0 0.00000 5.22e-01 1.070 8.27 36.30 8620.0 47.4000 2.32e+03 7080
K2O 9.230 8.280 89.7 30.00 296.00 987.0 0.00000 1.84e+00 4.490 16.90 73.20 15100.0 47.3000 2.30e+03 7079
Midwest
Total_N 18.200 6.550 36.0 18.40 75.50 410.0 0.00000 8.31e+00 13.300 22.50 26.70 15300.0 194.0000 3.91e+04 42955
Amm_N 8.340 4.950 59.3 8.34 10.20 122.0 0.00000 9.81e-04 4.750 11.70 14.20 834.0 43.5000 3.04e+03 21129
P2O5 7.400 3.720 50.2 7.43 4.72 63.5 0.00000 1.84e+00 4.790 9.76 12.50 235.0 5.6700 1.66e+02 42888
K2O 17.800 5.800 32.6 18.20 21.60 119.0 0.00000 1.00e+01 14.000 21.70 26.70 4140.0 163.0000 3.09e+04 42974
Northeast
Total_N 19.600 8.620 44.0 18.70 9.37 50.2 0.00000 5.56e+00 12.800 24.60 28.70 201.0 1.9400 3.68e+01 3923
Amm_N 9.060 4.220 46.6 8.66 4.44 51.3 0.05490 2.11e+00 5.630 11.50 13.80 29.5 0.0805 -1.12e-02 3859
P2O5 7.640 3.620 47.4 7.52 5.42 72.1 0.00000 1.53e+00 4.580 9.73 12.00 176.0 9.5600 2.50e+02 3988
K2O 18.000 7.670 42.7 18.00 9.23 51.2 0.00000 6.63e+00 12.800 23.10 27.60 163.0 2.8100 3.42e+01 3988
Southwest
Total_N 3.970 2.870 72.4 6.35 13.70 215.0 0.00851 1.09e+00 2.220 6.32 9.17 144.0 7.9300 6.81e+01 1484
Amm_N 2.440 1.640 67.5 2.77 2.98 108.0 0.00469 4.89e-01 1.290 3.48 4.29 36.5 5.6200 4.49e+01 1435
P2O5 1.440 0.904 62.6 2.99 7.96 266.0 0.00000 5.53e-01 0.944 2.48 4.29 125.0 8.2500 8.09e+01 1486
K2O 6.700 4.040 60.3 10.40 28.40 274.0 0.00000 2.12e+00 3.880 9.31 13.10 332.0 8.8700 8.14e+01 1487
Northern Plains
Total_N 18.300 5.750 31.4 18.90 11.50 61.0 0.33400 9.17e+00 14.600 22.20 26.10 144.0 5.5500 5.08e+01 626
Amm_N 8.920 4.080 45.7 9.43 4.93 52.3 0.16700 3.85e+00 6.550 11.70 15.80 34.9 1.0200 2.41e+00 361
P2O5 6.040 2.470 41.0 6.74 7.12 106.0 0.01000 1.75e+00 4.200 7.59 10.00 104.0 8.0000 8.86e+01 627
K2O 16.800 4.500 26.8 17.90 15.50 86.8 0.04540 1.19e+01 13.400 19.30 22.00 254.0 10.1000 1.24e+02 627
Pacific Northwest
Total_N 6.110 4.410 72.2 11.40 11.10 97.1 0.65100 3.12e+00 4.420 17.40 22.60 59.2 2.1900 5.68e+00 62
Amm_N 3.740 2.250 60.2 5.96 7.65 129.0 0.12500 9.97e-01 2.560 5.84 13.10 53.4 4.2800 2.31e+01 57
P2O5 1.430 1.250 87.0 2.61 2.72 104.0 0.01910 4.70e-01 0.840 3.17 7.40 10.9 1.4200 8.03e-01 67
K2O 9.230 7.180 77.8 13.20 12.90 97.8 0.00000 3.02e+00 5.500 18.30 26.90 79.1 2.6000 9.32e+00 67
Southern Plains
Total_N 31.800 43.200 136.0 46.30 48.20 104.0 0.00834 1.20e+00 6.660 70.10 117.00 454.0 1.8000 7.89e+00 611
Amm_N 3.500 4.580 131.0 4.99 5.47 110.0 0.00834 2.50e-01 0.784 7.51 10.90 35.9 2.1800 7.44e+00 609
P2O5 14.700 21.000 143.0 35.80 91.80 256.0 0.08340 5.84e-01 1.500 54.10 85.80 2090.0 18.5000 4.09e+02 611
K2O 27.700 35.600 129.0 58.50 68.70 117.0 0.30900 2.98e+00 9.360 90.50 161.00 375.0 1.5200 1.79e+00 598
Code
#### Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately dairy liquid
# Sort out for dairy while still having separate NPK columns
MDB_dairypumpNPK <- filter(MDB_pump3, Animal.Type.Combined.Category == "Dairy")
# Load required library
library(Kendall)

# List of selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")

# List of analytes
analytes <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Function to perform Mann-Kendall trend test for a given analyte across all selected regions
perform_mk_test_combined <- function(data, analyte, selected_regions) {
  # Subset data for the selected regions and analyte
  filtered_data <- data[data$Region %in% selected_regions, ]
  cleaned_data <- filtered_data[!is.na(filtered_data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk_combined <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk_combined))
}

# Perform Mann-Kendall trend test for each analyte combined across selected regions
for (analyte in analytes) {
  cat("Analyzing", analyte, "for all regions combined:\n")
  print(perform_mk_test_combined(MDB_dairypumpNPK, analyte, selected_regions))
  cat("\n")
}
Analyzing Total_N for all regions combined:
Score =  33 , Var(Score) = 156.3333
denominator =  51.91339
tau = 0.636, 2-sided pvalue =0.010488
NULL

Analyzing Ammonium.N for all regions combined:
Score =  21 , Var(Score) = 163
denominator =  53.99074
tau = 0.389, 2-sided pvalue =0.11723
NULL

Analyzing Phosphorus for all regions combined:
Score =  15 , Var(Score) = 156.3333
denominator =  51.91339
tau = 0.289, 2-sided pvalue =0.26284
NULL

Analyzing Potassium for all regions combined:
Score =  24 , Var(Score) = 147.3333
denominator =  49.1935
tau = 0.488, 2-sided pvalue =0.058111
NULL
Code
# Function to perform Mann-Kendall trend test
perform_mk_test <- function(data, analyte) {
  # Filter out NA values in the analyte column
  cleaned_data <- data[!is.na(data[[analyte]]), ]
  
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  
  # Perform Mann-Kendall trend test
  result_mk <- MannKendall(aggregated_data[[2]])
  
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk))
}

# Iterate over selected regions and analytes
for (region in selected_regions) {
  for (analyte in analytes) {
    # Subset data for the current region
    region_data <- MDB_dairypumpNPK[MDB_dairypumpNPK$Region == region, ]
    
    # Print information about the current region and analyte
    cat("Analyzing", analyte, "in", region, "region:\n")
    
    # Perform Mann-Kendall trend test
    result_summary <- perform_mk_test(region_data, analyte)
    
    # Print summary of the Mann-Kendall test result
    print(result_summary)
    cat("\n")
  }
}
Analyzing Total_N in Midwest region:
Score =  18 , Var(Score) = 144.6667
denominator =  48.06246
tau = 0.375, 2-sided pvalue =0.15754
NULL

Analyzing Ammonium.N in Midwest region:
Score =  12 , Var(Score) = 159.3333
denominator =  52.44044
tau = 0.229, 2-sided pvalue =0.38351
NULL

Analyzing Phosphorus in Midwest region:
Score =  3 , Var(Score) = 156.3333
denominator =  51.91339
tau = 0.0578, 2-sided pvalue =0.87291
NULL

Analyzing Potassium in Midwest region:
Score =  20 , Var(Score) = 164
denominator =  54.4977
tau = 0.367, 2-sided pvalue =0.1379
NULL

Analyzing Total_N in Northeast region:
Score =  -33 , Var(Score) = 165
denominator =  55
tau = -0.6, 2-sided pvalue =0.012731
NULL

Analyzing Ammonium.N in Northeast region:
Score =  -29 , Var(Score) = 165
denominator =  55
tau = -0.527, 2-sided pvalue =0.029273
NULL

Analyzing Phosphorus in Northeast region:
Score =  -32 , Var(Score) = 164
denominator =  54.4977
tau = -0.587, 2-sided pvalue =0.015491
NULL

Analyzing Potassium in Northeast region:
Score =  -27 , Var(Score) = 165
denominator =  55
tau = -0.491, 2-sided pvalue =0.04296
NULL

Analyzing Total_N in Southeast region:
Score =  -1 , Var(Score) = 165
denominator =  55
tau = -0.0182, 2-sided pvalue =1
NULL

Analyzing Ammonium.N in Southeast region:
Score =  5 , Var(Score) = 165
denominator =  55
tau = 0.0909, 2-sided pvalue =0.7555
NULL

Analyzing Phosphorus in Southeast region:
Score =  -21 , Var(Score) = 165
denominator =  55
tau = -0.382, 2-sided pvalue =0.11947
NULL

Analyzing Potassium in Southeast region:
Score =  13 , Var(Score) = 165
denominator =  55
tau = 0.236, 2-sided pvalue =0.3502
NULL

Table of samples by region by year for Dairy Liquid manure

Code
# Sort out for beef while still having separate NPK columns
MDB_dairypumpNPK <- filter(MDB_pump3, Animal.Type.Combined.Category == "Dairy")
# Create a table of number of samples for each region per year
sample_table <- table(MDB_dairypumpNPK$Region, MDB_dairypumpNPK$Year.Analyzed)

# Print the sample table
print(sample_table)
                   
                    2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
  Midwest           1447 2252 2537 2947 4523 4745 4417 4890 5870 5508 4580
  Northeast          296  493  408  382  395  473  436  505  398  493  448
  Northern Plains      0    0    0    0  113  128  146   64   81   81   14
  Pacific Northwest    0    0    0    0    0    0    8    3   15   27   14
  Southeast          757  775  747  707  761  665  661  611  551  511  343
  Southern Plains      0    0    0    0    0    0    0  135  128  238  128
  Southwest            0    0    0    1   30   17   13   34    8   18 1368

Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Dairy Liquid)

Code
# Filter data for selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")
filtered_data <- MDB_dairypumpNPK[MDB_dairypumpNPK$Region %in% selected_regions, ]

# Initialize an empty list to store cleaned data for each nutrient
cleaned_data_list <- list()

# Loop over each nutrient
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Remove missing values for the current nutrient
  cleaned_data <- filtered_data[!is.na(filtered_data[[nutrient]]), ]
  cleaned_data_list[[nutrient]] <- cleaned_data
}

# Create a Function for Wilcoxon Test and Correction
perform_wilcoxon_test <- function(region1, region2, year, nutrient, data) {
    # Extract data for the given regions, year, and nutrient
    region1_data <- data[data$Region == region1 & data$Year.Analyzed == year, nutrient]
    region2_data <- data[data$Region == region2 & data$Year.Analyzed == year, nutrient]
    
    # Perform Wilcoxon rank sum test
    wilcox_result <- wilcox.test(region1_data, region2_data)
        
    # Apply Benjamini-Hochberg correction
    adjusted_p_value <- p.adjust(wilcox_result$p.value, method = "BH")
    
    # Return results
    return(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))
}

# Iterate Over Years, Regions, and Nutrients
years <- unique(filtered_data$Year.Analyzed)
regions <- unique(filtered_data$Region)
nutrients <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Initialize empty vectors
all_p_values <- c()
comparison_results <- list()
comparison_names <- c()

# Loop over years, regions, and nutrients, perform tests, and gather p-values
index <- 1
for (year in years) {
    for (nutrient in nutrients) {
        for (i in 1:(length(regions)-1)) {
            for (j in (i+1):length(regions)) {
                region1 <- regions[i]
                region2 <- regions[j]
                
                # Perform Wilcoxon test and correction
                results <- perform_wilcoxon_test(region1, region2, year, nutrient, cleaned_data_list[[nutrient]])
                
                # Store p-value
                all_p_values <- c(all_p_values, results$wilcox_result$p.value)
                
                # Store comparison name
                comparison_names <- c(comparison_names, paste(region1, region2, year, nutrient, sep = "_"))
                
                # Store results
                comparison_results[[index]] <- results
                
                index <- index + 1
            }
        }
    }
}

# Sort comparison names based on year
sorted_indices <- order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))
sorted_comparison_names <- comparison_names[sorted_indices]

# Apply Benjamini-Hochberg Procedure
adjusted_p_values <- p.adjust(all_p_values, method = "BH")

# Print results in chronological order
for (name in sorted_comparison_names) {
    index <- match(name, comparison_names)
    year <- as.numeric(unlist(strsplit(name, "_"))[3])
    nutrient <- unlist(strsplit(name, "_"))[4]
    
    # Extract Wilcoxon test results
    result <- comparison_results[[index]]$wilcox_result
    
    # Print results with adjusted p-values
    cat("Comparison:", name, "\n")
    cat("Year:", year, "\n")
    cat("Nutrient:", nutrient, "\n")
    cat("Wilcoxon Test Statistic:", result$statistic, "\n")
    cat("Wilcoxon Test P-value:", result$p.value, "\n")
    cat("Adjusted p-value:", adjusted_p_values[index], "\n")
    cat("\n")
}
Comparison: Northeast_Midwest_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 272032.5 
Wilcoxon Test P-value: 2.180234e-13 
Adjusted p-value: 3.346405e-13 

Comparison: Northeast_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 146490.5 
Wilcoxon Test P-value: 6.643084e-50 
Adjusted p-value: 1.789566e-49 

Comparison: Midwest_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 684350 
Wilcoxon Test P-value: 8.1338e-83 
Adjusted p-value: 4.294647e-82 

Comparison: Northeast_Midwest_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 136801.5 
Wilcoxon Test P-value: 0.1746292 
Adjusted p-value: 0.1905045 

Comparison: Northeast_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 92109 
Wilcoxon Test P-value: 1.057232e-83 
Adjusted p-value: 5.814775e-83 

Comparison: Midwest_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 279468.5 
Wilcoxon Test P-value: 1.296574e-136 
Adjusted p-value: 2.139347e-135 

Comparison: Northeast_Midwest_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 258753.5 
Wilcoxon Test P-value: 8.421824e-09 
Adjusted p-value: 1.12291e-08 

Comparison: Northeast_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 133056.5 
Wilcoxon Test P-value: 1.100337e-06 
Adjusted p-value: 1.438064e-06 

Comparison: Midwest_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 592716 
Wilcoxon Test P-value: 0.001243408 
Adjusted p-value: 0.00151972 

Comparison: Northeast_Midwest_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 260497 
Wilcoxon Test P-value: 2.223906e-09 
Adjusted p-value: 2.995465e-09 

Comparison: Northeast_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 140499.5 
Wilcoxon Test P-value: 5.524134e-11 
Adjusted p-value: 7.925932e-11 

Comparison: Midwest_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 646751 
Wilcoxon Test P-value: 1.898763e-12 
Adjusted p-value: 2.846e-12 

Comparison: Northeast_Midwest_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 556730 
Wilcoxon Test P-value: 9.127792e-12 
Adjusted p-value: 1.338743e-11 

Comparison: Northeast_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 227343 
Wilcoxon Test P-value: 1.139598e-100 
Adjusted p-value: 1.253558e-99 

Comparison: Midwest_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 1214863 
Wilcoxon Test P-value: 2.247614e-170 
Adjusted p-value: 2.966851e-168 

Comparison: Northeast_Midwest_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 322473 
Wilcoxon Test P-value: 1.729477e-06 
Adjusted p-value: 2.238147e-06 

Comparison: Northeast_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 120009 
Wilcoxon Test P-value: 1.194875e-96 
Adjusted p-value: 1.213257e-95 

Comparison: Midwest_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 326767.5 
Wilcoxon Test P-value: 1.081363e-45 
Adjusted p-value: 2.693205e-45 

Comparison: Northeast_Midwest_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 592947 
Wilcoxon Test P-value: 0.0004675581 
Adjusted p-value: 0.0005822422 

Comparison: Northeast_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 264211 
Wilcoxon Test P-value: 1.120532e-38 
Adjusted p-value: 2.506952e-38 

Comparison: Midwest_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1198566 
Wilcoxon Test P-value: 3.414199e-57 
Adjusted p-value: 9.797268e-57 

Comparison: Northeast_Midwest_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 654934.5 
Wilcoxon Test P-value: 8.766789e-14 
Adjusted p-value: 1.377638e-13 

Comparison: Northeast_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 285162.5 
Wilcoxon Test P-value: 8.213187e-61 
Adjusted p-value: 2.710352e-60 

Comparison: Midwest_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1290172 
Wilcoxon Test P-value: 2.023515e-92 
Adjusted p-value: 1.780693e-91 

Comparison: Northeast_Midwest_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 533454.5 
Wilcoxon Test P-value: 0.1597503 
Adjusted p-value: 0.1757253 

Comparison: Northeast_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 214472 
Wilcoxon Test P-value: 4.816523e-82 
Adjusted p-value: 2.445312e-81 

Comparison: Midwest_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 1350410 
Wilcoxon Test P-value: 5.602667e-166 
Adjusted p-value: 3.69776e-164 

Comparison: Northeast_Midwest_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 328037.5 
Wilcoxon Test P-value: 0.2047964 
Adjusted p-value: 0.2197815 

Comparison: Northeast_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 104686 
Wilcoxon Test P-value: 1.857506e-88 
Adjusted p-value: 1.167575e-87 

Comparison: Midwest_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 316561 
Wilcoxon Test P-value: 3.247567e-37 
Adjusted p-value: 6.914175e-37 

Comparison: Northeast_Midwest_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 491679 
Wilcoxon Test P-value: 0.216944 
Adjusted p-value: 0.2309404 

Comparison: Northeast_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 189811 
Wilcoxon Test P-value: 1.875458e-13 
Adjusted p-value: 2.912476e-13 

Comparison: Midwest_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1232339 
Wilcoxon Test P-value: 2.825664e-36 
Adjusted p-value: 5.738272e-36 

Comparison: Northeast_Midwest_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 505480 
Wilcoxon Test P-value: 0.7175226 
Adjusted p-value: 0.7285614 

Comparison: Northeast_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 219221 
Wilcoxon Test P-value: 1.023268e-37 
Adjusted p-value: 2.214285e-37 

Comparison: Midwest_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1409132 
Wilcoxon Test P-value: 5.581194e-92 
Adjusted p-value: 4.604485e-91 

Comparison: Northeast_Midwest_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 668912.5 
Wilcoxon Test P-value: 1.962364e-09 
Adjusted p-value: 2.670433e-09 

Comparison: Northeast_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 182621 
Wilcoxon Test P-value: 6.319709e-71 
Adjusted p-value: 2.527883e-70 

Comparison: Midwest_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 1354092 
Wilcoxon Test P-value: 9.682675e-120 
Adjusted p-value: 1.278113e-118 

Comparison: Northeast_Midwest_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 423409 
Wilcoxon Test P-value: 0.0001043432 
Adjusted p-value: 0.0001311743 

Comparison: Northeast_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 94667.5 
Wilcoxon Test P-value: 5.596549e-80 
Adjusted p-value: 2.638373e-79 

Comparison: Midwest_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 360016 
Wilcoxon Test P-value: 2.615288e-23 
Adjusted p-value: 4.665109e-23 

Comparison: Northeast_Midwest_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 566573 
Wilcoxon Test P-value: 0.834267 
Adjusted p-value: 0.834267 

Comparison: Northeast_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 191271.5 
Wilcoxon Test P-value: 7.113767e-30 
Adjusted p-value: 1.401518e-29 

Comparison: Midwest_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1443234 
Wilcoxon Test P-value: 3.072157e-57 
Adjusted p-value: 9.011661e-57 

Comparison: Northeast_Midwest_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 611053.5 
Wilcoxon Test P-value: 0.006405539 
Adjusted p-value: 0.007617398 

Comparison: Northeast_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 199880 
Wilcoxon Test P-value: 3.673333e-39 
Adjusted p-value: 8.506665e-39 

Comparison: Midwest_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1505215 
Wilcoxon Test P-value: 1.312937e-75 
Adjusted p-value: 5.776921e-75 

Comparison: Northeast_Midwest_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 1102977 
Wilcoxon Test P-value: 5.480711e-17 
Adjusted p-value: 9.043173e-17 

Comparison: Northeast_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 214767.5 
Wilcoxon Test P-value: 3.619027e-87 
Adjusted p-value: 2.171416e-86 

Comparison: Midwest_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 2319830 
Wilcoxon Test P-value: 6.89189e-159 
Adjusted p-value: 2.274324e-157 

Comparison: Northeast_Midwest_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 503007.5 
Wilcoxon Test P-value: 4.864543e-18 
Adjusted p-value: 8.128097e-18 

Comparison: Northeast_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 104888.5 
Wilcoxon Test P-value: 6.88548e-89 
Adjusted p-value: 4.544416e-88 

Comparison: Midwest_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 459610.5 
Wilcoxon Test P-value: 8.712288e-39 
Adjusted p-value: 1.982797e-38 

Comparison: Northeast_Midwest_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1051043 
Wilcoxon Test P-value: 3.810109e-10 
Adjusted p-value: 5.294046e-10 

Comparison: Northeast_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 210384 
Wilcoxon Test P-value: 6.321324e-29 
Adjusted p-value: 1.227081e-28 

Comparison: Midwest_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2236535 
Wilcoxon Test P-value: 9.137054e-44 
Adjusted p-value: 2.192893e-43 

Comparison: Northeast_Midwest_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 959379.5 
Wilcoxon Test P-value: 0.00443825 
Adjusted p-value: 0.005374762 

Comparison: Northeast_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 218476.5 
Wilcoxon Test P-value: 9.327034e-37 
Adjusted p-value: 1.954236e-36 

Comparison: Midwest_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2479763 
Wilcoxon Test P-value: 1.004813e-90 
Adjusted p-value: 7.80208e-90 

Comparison: Northeast_Midwest_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 1232833 
Wilcoxon Test P-value: 9.74087e-07 
Adjusted p-value: 1.285795e-06 

Comparison: Northeast_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 217730 
Wilcoxon Test P-value: 5.76037e-81 
Adjusted p-value: 2.816181e-80 

Comparison: Midwest_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 2178115 
Wilcoxon Test P-value: 3.262386e-144 
Adjusted p-value: 7.17725e-143 

Comparison: Northeast_Midwest_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 616925.5 
Wilcoxon Test P-value: 1.830791e-12 
Adjusted p-value: 2.777751e-12 

Comparison: Northeast_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 111411 
Wilcoxon Test P-value: 2.749769e-96 
Adjusted p-value: 2.592639e-95 

Comparison: Midwest_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 435545.5 
Wilcoxon Test P-value: 1.985272e-43 
Adjusted p-value: 4.679569e-43 

Comparison: Northeast_Midwest_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1181424 
Wilcoxon Test P-value: 0.0006876605 
Adjusted p-value: 0.0008483288 

Comparison: Northeast_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 208506.5 
Wilcoxon Test P-value: 7.67265e-25 
Adjusted p-value: 1.406653e-24 

Comparison: Midwest_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2037312 
Wilcoxon Test P-value: 2.380524e-38 
Adjusted p-value: 5.237153e-38 

Comparison: Northeast_Midwest_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1150406 
Wilcoxon Test P-value: 0.02921266 
Adjusted p-value: 0.03353105 

Comparison: Northeast_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 208343.5 
Wilcoxon Test P-value: 1.051465e-24 
Adjusted p-value: 1.901279e-24 

Comparison: Midwest_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2115421 
Wilcoxon Test P-value: 2.628892e-49 
Adjusted p-value: 6.80419e-49 

Comparison: Northeast_Midwest_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 991677 
Wilcoxon Test P-value: 0.04589819 
Adjusted p-value: 0.05222897 

Comparison: Northeast_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 201249 
Wilcoxon Test P-value: 1.023626e-70 
Adjusted p-value: 3.860534e-70 

Comparison: Midwest_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 2031212 
Wilcoxon Test P-value: 1.147471e-148 
Adjusted p-value: 3.029324e-147 

Comparison: Northeast_Midwest_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 539427 
Wilcoxon Test P-value: 7.145306e-10 
Adjusted p-value: 9.824795e-10 

Comparison: Northeast_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 105424.5 
Wilcoxon Test P-value: 1.636131e-78 
Adjusted p-value: 7.447219e-78 

Comparison: Midwest_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 409975.5 
Wilcoxon Test P-value: 1.265907e-35 
Adjusted p-value: 2.531814e-35 

Comparison: Northeast_Midwest_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 946114 
Wilcoxon Test P-value: 0.7320608 
Adjusted p-value: 0.7376491 

Comparison: Northeast_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 191185.5 
Wilcoxon Test P-value: 6.478761e-21 
Adjusted p-value: 1.110645e-20 

Comparison: Midwest_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1906529 
Wilcoxon Test P-value: 1.182255e-44 
Adjusted p-value: 2.889958e-44 

Comparison: Northeast_Midwest_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 911543.5 
Wilcoxon Test P-value: 0.311506 
Adjusted p-value: 0.3263396 

Comparison: Northeast_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 190597.5 
Wilcoxon Test P-value: 1.916404e-20 
Adjusted p-value: 3.243145e-20 

Comparison: Midwest_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1991038 
Wilcoxon Test P-value: 5.802677e-60 
Adjusted p-value: 1.868179e-59 

Comparison: Northeast_Midwest_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 1133164 
Wilcoxon Test P-value: 0.2033833 
Adjusted p-value: 0.2197815 

Comparison: Northeast_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 194133 
Wilcoxon Test P-value: 1.146031e-57 
Adjusted p-value: 3.438092e-57 

Comparison: Midwest_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 2027145 
Wilcoxon Test P-value: 1.344444e-139 
Adjusted p-value: 2.535238e-138 

Comparison: Northeast_Midwest_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 431030 
Wilcoxon Test P-value: 0.4667242 
Adjusted p-value: 0.4813094 

Comparison: Northeast_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 91799.5 
Wilcoxon Test P-value: 5.38743e-68 
Adjusted p-value: 1.922002e-67 

Comparison: Midwest_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 346579 
Wilcoxon Test P-value: 6.519314e-87 
Adjusted p-value: 3.741519e-86 

Comparison: Northeast_Midwest_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1071879 
Wilcoxon Test P-value: 0.004579875 
Adjusted p-value: 0.00549585 

Comparison: Northeast_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 188383.5 
Wilcoxon Test P-value: 1.104405e-14 
Adjusted p-value: 1.777823e-14 

Comparison: Midwest_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2036193 
Wilcoxon Test P-value: 8.185697e-56 
Adjusted p-value: 2.298962e-55 

Comparison: Northeast_Midwest_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1116699 
Wilcoxon Test P-value: 0.117724 
Adjusted p-value: 0.1305846 

Comparison: Northeast_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 204575 
Wilcoxon Test P-value: 2.286479e-27 
Adjusted p-value: 4.374133e-27 

Comparison: Midwest_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2204732 
Wilcoxon Test P-value: 2.436573e-90 
Adjusted p-value: 1.78682e-89 

Comparison: Northeast_Midwest_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 1125887 
Wilcoxon Test P-value: 0.3830358 
Adjusted p-value: 0.398116 

Comparison: Northeast_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 148802.5 
Wilcoxon Test P-value: 9.906137e-71 
Adjusted p-value: 3.845912e-70 

Comparison: Midwest_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 2267005 
Wilcoxon Test P-value: 1.772761e-159 
Adjusted p-value: 7.800149e-158 

Comparison: Northeast_Midwest_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 393544.5 
Wilcoxon Test P-value: 1.043971e-10 
Adjusted p-value: 1.481766e-10 

Comparison: Northeast_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 79095 
Wilcoxon Test P-value: 4.683904e-63 
Adjusted p-value: 1.585321e-62 

Comparison: Midwest_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 518768 
Wilcoxon Test P-value: 3.440091e-101 
Adjusted p-value: 4.128109e-100 

Comparison: Northeast_Midwest_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1129930 
Wilcoxon Test P-value: 0.6458615 
Adjusted p-value: 0.6608815 

Comparison: Northeast_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 148541.5 
Wilcoxon Test P-value: 4.142016e-22 
Adjusted p-value: 7.194028e-22 

Comparison: Midwest_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 2211418 
Wilcoxon Test P-value: 2.158591e-49 
Adjusted p-value: 5.698681e-49 

Comparison: Northeast_Midwest_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1108735 
Wilcoxon Test P-value: 0.2315061 
Adjusted p-value: 0.2444704 

Comparison: Northeast_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 151446 
Wilcoxon Test P-value: 3.40838e-25 
Adjusted p-value: 6.336706e-25 

Comparison: Midwest_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2325485 
Wilcoxon Test P-value: 2.824803e-67 
Adjusted p-value: 9.812473e-67 

Comparison: Northeast_Midwest_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 1253020 
Wilcoxon Test P-value: 0.01343406 
Adjusted p-value: 0.015833 

Comparison: Northeast_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 165990.5 
Wilcoxon Test P-value: 1.609119e-59 
Adjusted p-value: 5.057233e-59 

Comparison: Midwest_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 1903976 
Wilcoxon Test P-value: 7.565914e-121 
Adjusted p-value: 1.109667e-119 

Comparison: Northeast_Midwest_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 436172 
Wilcoxon Test P-value: 4.590649e-06 
Adjusted p-value: 5.883162e-06 

Comparison: Northeast_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 83674.5 
Wilcoxon Test P-value: 2.163772e-58 
Adjusted p-value: 6.642277e-58 

Comparison: Midwest_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 354807.5 
Wilcoxon Test P-value: 1.194895e-72 
Adjusted p-value: 4.928941e-72 

Comparison: Northeast_Midwest_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1275683 
Wilcoxon Test P-value: 0.08083038 
Adjusted p-value: 0.09119325 

Comparison: Northeast_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 161970 
Wilcoxon Test P-value: 2.640913e-15 
Adjusted p-value: 4.30371e-15 

Comparison: Midwest_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1860007 
Wilcoxon Test P-value: 1.513001e-36 
Adjusted p-value: 3.120565e-36 

Comparison: Northeast_Midwest_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1255807 
Wilcoxon Test P-value: 0.0198139 
Adjusted p-value: 0.02314544 

Comparison: Northeast_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 173785.5 
Wilcoxon Test P-value: 1.033928e-25 
Adjusted p-value: 1.949692e-25 

Comparison: Midwest_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 2044912 
Wilcoxon Test P-value: 6.875139e-69 
Adjusted p-value: 2.520884e-68 

Comparison: Northeast_Midwest_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 948949 
Wilcoxon Test P-value: 0.0201098 
Adjusted p-value: 0.02328503 

Comparison: Northeast_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 96023 
Wilcoxon Test P-value: 3.546028e-48 
Adjusted p-value: 9.001455e-48 

Comparison: Midwest_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 1038418 
Wilcoxon Test P-value: 1.284729e-89 
Adjusted p-value: 8.925489e-89 

Comparison: Northeast_Midwest_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 441219 
Wilcoxon Test P-value: 4.419919e-14 
Adjusted p-value: 7.029268e-14 

Comparison: Northeast_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 77094.5 
Wilcoxon Test P-value: 1.041729e-55 
Adjusted p-value: 2.864754e-55 

Comparison: Midwest_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 470424 
Wilcoxon Test P-value: 4.179851e-75 
Adjusted p-value: 1.779807e-74 

Comparison: Northeast_Midwest_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 812257.5 
Wilcoxon Test P-value: 1.918894e-12 
Adjusted p-value: 2.846e-12 

Comparison: Northeast_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 81720.5 
Wilcoxon Test P-value: 0.08812422 
Adjusted p-value: 0.09857963 

Comparison: Midwest_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 952523 
Wilcoxon Test P-value: 3.089815e-11 
Adjusted p-value: 4.48193e-11 

Comparison: Northeast_Midwest_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 834107 
Wilcoxon Test P-value: 2.421526e-10 
Adjusted p-value: 3.40044e-10 

Comparison: Northeast_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 89389.5 
Wilcoxon Test P-value: 3.688012e-05 
Adjusted p-value: 4.680939e-05 

Comparison: Midwest_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1036921 
Wilcoxon Test P-value: 3.8801e-23 
Adjusted p-value: 6.828977e-23 

Four analyte median + IQR plots 2012-2022 with counts + arrows (Dairy liquid)

Code
# Filter out rows with non-missing Total_N values and for specific regions
MDB_dairypumpNPK_filtered <- MDB_dairypumpNPK[                                                MDB_dairypumpNPK$Region %in% c("Northeast", "Southeast", "Midwest"), ]

custom_theme <- theme_minimal() +
  theme(
    text = element_text(family = "Times New Roman", size = 11),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )
# Create separate plots for each nutrient type
plots <- list()
# Initialize variables to store the minimum and maximum values of lower and upper quartiles
min_lower_quantile <- Inf
max_upper_quantile <- -Inf

# Function to calculate counts for each nutrient and region
calculate_counts <- function(data) {
  counts <- data %>%
    filter(!is.na(Total_N) & Year.Analyzed >= 2012 & Year.Analyzed <= 2022) %>%
    group_by(Region) %>%
    summarise(total_samples = n()) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  return(counts)
}


for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Filter data for the current nutrient type
  filtered_data <- MDB_dairypumpNPK_filtered[!is.na(MDB_dairypumpNPK_filtered[[nutrient]]), ]
  
  # Calculate median and IQR for each combination of Year.Analyzed and Region
  summary_stats <- filtered_data %>%
    group_by(Year.Analyzed, Region) %>%
    summarise(median_value = median(.data[[nutrient]]),
              lower_quantile = quantile(.data[[nutrient]], 0.25),
              upper_quantile = quantile(.data[[nutrient]], 0.75)) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  
  # Update minimum and maximum values of lower and upper quartiles
  min_lower_quantile <- min(min_lower_quantile, min(summary_stats$lower_quantile))
  max_upper_quantile <- max(max_upper_quantile, max(summary_stats$upper_quantile))
  
  # Calculate counts for all regions
  counts <- calculate_counts(filtered_data)
  
  # Create line graph with median for each year and region, with connecting lines
  plot <- ggplot(summary_stats, aes(x = factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +
    geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha = 0.3) +  # Add shaded area for IQR
    geom_line(aes(linetype = Region)) +  # Add line graph with connecting lines
    geom_point() +  # Add points for each data point
    labs(x = "Year Analyzed", 
         y = switch(nutrient,
                    Total_N = "Total N (%)",
                    Ammonium.N = expression(paste("NH"[4], "-N (%)")),
                    Phosphorus = expression(paste("P"[2], "O"[5], " (%)")),
                    Potassium = expression(paste("K"[2], "O (%)"))
         ), 
         title = NULL) +  # Remove titles for all graphs
    custom_theme +  # Apply custom theme with font settings
    scale_color_manual(values = c("Northeast" = "darkgreen", "Southeast" = "red", "Midwest" = "blue")) +  # Define colors for regions
    scale_fill_manual(values = c("Northeast" = "lightgreen", "Southeast" = "lightpink", "Midwest" = "lightblue")) +  # Define fill colors for IQR
    guides(fill = FALSE) +  # Remove legend for IQR fill colors
    scale_y_continuous(limits = c(0, .4), breaks = seq(0, .4, by = 0.1)) +
    geom_text(data = counts, aes(label = paste("n =", total_samples),
                                 x = "2021", y = 0.4 - (0.03 * seq_along(total_samples)), color = Region),
              hjust = 1, vjust = 0, size = 3, family = "Times New Roman")  # Add text annotations for counts
    # Add the arrow only for certain nutrients
  if (nutrient == "Total_N") {
    plot <- plot + annotate("segment", x = "2022", y = .365, xend = "2022", yend = .33, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "darkgreen")
  }
    if (nutrient == "Ammonium.N") {
    plot <- plot + annotate("segment", x = "2022", y = .365, xend = "2022", yend = .33, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "darkgreen")
  }
  if (nutrient == "Phosphorus") {
    plot <- plot + annotate("segment", x = "2022", y = .365, xend = "2022", yend = .33, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "darkgreen")
  }
  if (nutrient == "Potassium") {
    plot <- plot + annotate("segment", x = "2022", y = .365, xend = "2022", yend = .33, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "darkgreen")
  }
  # Add the plot to the list
  plots[[nutrient]] <- plot
}

# Arrange the plots together using patchwork
dairy_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] + plot_layout(guides = "collect")

# Print the final plot
print(dairy_final_plot)

Swine Liquid

Swine Liquid Manure by Region Overview Table (%)

Code
MDB_swinepump<-filter(MDB_pump3pl_clean, Animal.or.Other.Amendment.Type=="Swine") 


df_nest = MDB_swinepump %>% 
  group_by(Region) %>% 
  nest()

MDB_swinepump_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
           MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_swinepump_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Southeast
Total_N 0.032800 0.01840 56.1 0.03610 0.0289 80.1 0.00e+00 8.61e-03 0.021300 0.04640 0.0615 1.310 7.2900 146.000 110125
Amm_N 0.000311 0.00044 141.0 0.00742 0.0245 330.0 0.00e+00 7.64e-06 0.000061 0.00262 0.0185 0.422 6.9200 68.600 2919
P2O5 0.008720 0.00400 45.9 0.01560 0.1170 754.0 0.00e+00 4.57e-03 0.006310 0.01190 0.0165 17.200 59.0000 5690.000 110437
K2O 0.068200 0.03490 51.2 0.07310 0.0462 63.2 0.00e+00 2.75e-02 0.046500 0.09410 0.1210 2.680 8.4100 261.000 110437
Midwest
Total_N 0.490000 0.25200 51.4 0.48400 0.2350 48.5 0.00e+00 1.80e-01 0.300000 0.65000 0.7800 11.900 2.5800 106.000 74205
Amm_N 0.300000 0.16800 55.8 0.30700 0.1630 53.1 0.00e+00 1.00e-01 0.188000 0.41400 0.5180 1.350 0.3680 0.192 31662
P2O5 0.230000 0.14800 64.5 0.23900 0.1760 73.9 0.00e+00 5.00e-02 0.120000 0.32700 0.4200 8.000 7.6700 266.000 74203
K2O 0.340000 0.16300 48.0 0.32700 0.1450 44.3 0.00e+00 1.40e-01 0.200000 0.44000 0.5100 4.120 0.6920 13.800 74205
Northeast
Total_N 0.286000 0.20500 71.8 0.31000 0.2040 65.8 2.60e-03 8.00e-02 0.156000 0.42600 0.6190 1.030 0.8010 0.231 238
Amm_N 0.174000 0.11800 67.9 0.18800 0.1140 60.6 1.07e-03 5.61e-02 0.103000 0.26000 0.3370 0.551 0.6720 0.229 229
P2O5 0.102000 0.13100 128.0 0.24200 0.4240 175.0 0.00e+00 1.42e-02 0.039100 0.33500 0.5840 5.340 7.6700 85.000 242
K2O 0.181000 0.11000 61.0 0.23800 0.2620 110.0 3.48e-03 7.63e-02 0.122000 0.29300 0.3870 2.690 6.8800 59.600 242
Northern Plains
Total_N 0.455000 0.29800 65.5 0.43500 0.2420 55.5 5.00e-03 9.10e-02 0.238000 0.64600 0.7530 1.040 -0.0272 -1.120 737
Amm_N 0.313000 0.24100 77.1 0.30700 0.1790 58.3 0.00e+00 6.95e-02 0.141000 0.46900 0.5400 0.806 0.0720 -1.110 711
P2O5 0.260000 0.21900 84.4 0.25900 0.1970 76.0 0.00e+00 1.80e-02 0.070000 0.38200 0.4680 1.190 0.8700 1.550 737
K2O 0.272000 0.17300 63.5 0.26900 0.1370 51.0 2.00e-02 8.33e-02 0.150000 0.37900 0.4430 0.786 0.1700 -0.690 734
Southern Plains
Total_N 0.133000 0.15600 117.0 0.43100 0.6730 156.0 2.41e-05 1.24e-02 0.041500 0.49000 1.5700 3.010 1.9900 3.000 122
Amm_N 0.074000 0.09040 122.0 0.14700 0.2140 145.0 1.63e-05 2.92e-03 0.019000 0.15900 0.4130 1.150 2.4200 6.230 103
P2O5 0.024900 0.02740 110.0 0.38600 0.8300 215.0 2.29e-04 7.00e-03 0.012400 0.20700 1.1900 4.290 2.9400 8.670 120
K2O 0.138000 0.14800 107.0 0.31900 0.4730 148.0 4.04e-03 2.62e-02 0.085500 0.35400 0.7120 3.080 3.0800 11.300 110

Swine Liquid Manure by Region Overview Table (lbs/1000 gal)

Code
#Convert Units from % to lbs/1000 gal
multiply_by_83pt4<- MDB_swinepump  %>% mutate( Value = Value * 83.4)
#print(multiply_by_83pt4) #View(multiply_by_83pt4)

df_nest = multiply_by_83pt4 %>% 
  group_by(Region) %>% 
  nest()

MDB_swinepump_long_summary <- function(df_long) {
  x = df_long %>%
  select(ManureDB.Sample.ID: Variable, Value) %>%    
  as_tibble() %>% 
  group_by(Variable) %>% 
  summarise(Median =if(any(!is.na(Value))) median(Value, na.rm = T)else NA,
            MAD = if(any(!is.na(Value))) mad(Value, na.rm = T) else NA,
            MCV= if(any(!is.na(Value))) MAD/Median * 100 else NA, #median coefficient of variation
            Mean = if(any(!is.na(Value))) mean(Value, na.rm = T) else NA,
            StDev =if(any(!is.na(Value))) sd(Value, na.rm = T) else NA,
            CV = if (any(!is.na(Value))) sd(Value, na.rm = TRUE) / mean(Value, na.rm = TRUE) * 100 else NA, # coefficient of variation
            MIN = if(any(!is.na(Value)))min(Value, na.rm = T) else NA,
            "10th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.1, na.rm = T) else NA,
            "25th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.25, na.rm = T) else NA,
            "75th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.75, na.rm = T) else NA,
            "90th_%"=if(any(!is.na(Value)))quantile(Value, probs = 0.90, na.rm = T) else NA,
            MAX = if(any(!is.na(Value)))max(Value, na.rm = T) else NA,
            Skew = if(any(!is.na(Value)))skew(Value, na.rm=T) else NA,
            Kurtosis = if(any(!is.na(Value)))kurtosi(Value, na.rm=T) else NA,
            Count = sum(!is.na(Value)))%>% 
  mutate(Variable = factor(Variable, levels = c("Total_N", "Amm_N", "P2O5", "K2O"))) %>% 
    arrange(Variable) %>% 
  mutate(across(Median:Kurtosis, \(x) signif(x, digits = 3))) # need to revisit significant digits

  return(x) }

df_by_Animal_Type = df_nest %>% 
  mutate(summ = purrr::map(data, \(x) MDB_swinepump_long_summary(x)))|>
  select(c(Region, summ))|>
  unnest(cols = c(summ))

df_by_Animal_Type %>% 
  gt()
Variable Median MAD MCV Mean StDev CV MIN 10th_% 25th_% 75th_% 90th_% MAX Skew Kurtosis Count
Southeast
Total_N 2.7400 1.5300 56.1 3.010 2.41 80.1 0.00000 7.18e-01 1.78000 3.870 5.13 109.0 7.2900 146.000 110125
Amm_N 0.0259 0.0367 141.0 0.619 2.04 330.0 0.00000 6.37e-04 0.00509 0.219 1.54 35.2 6.9200 68.600 2919
P2O5 0.7270 0.3340 45.9 1.300 9.78 754.0 0.00000 3.81e-01 0.52600 0.995 1.37 1430.0 59.0000 5690.000 110437
K2O 5.6900 2.9100 51.2 6.100 3.85 63.2 0.00000 2.29e+00 3.88000 7.850 10.10 223.0 8.4100 261.000 110437
Midwest
Total_N 40.9000 21.0000 51.4 40.400 19.60 48.5 0.00000 1.50e+01 25.00000 54.200 65.10 995.0 2.5800 106.000 74205
Amm_N 25.0000 14.0000 55.8 25.600 13.60 53.1 0.00000 8.34e+00 15.70000 34.500 43.20 112.0 0.3680 0.192 31662
P2O5 19.2000 12.4000 64.5 19.900 14.70 73.9 0.00000 4.17e+00 10.00000 27.300 35.00 667.0 7.6700 266.000 74203
K2O 28.4000 13.6000 48.0 27.300 12.10 44.3 0.00000 1.17e+01 16.70000 36.700 42.50 344.0 0.6920 13.800 74205
Northeast
Total_N 23.9000 17.1000 71.8 25.800 17.00 65.8 0.21700 6.67e+00 13.00000 35.600 51.60 86.0 0.8010 0.231 238
Amm_N 14.5000 9.8700 67.9 15.700 9.50 60.6 0.08920 4.68e+00 8.61000 21.700 28.10 46.0 0.6720 0.229 229
P2O5 8.5300 10.9000 128.0 20.200 35.40 175.0 0.00000 1.18e+00 3.26000 28.000 48.70 446.0 7.6700 85.000 242
K2O 15.1000 9.2100 61.0 19.800 21.80 110.0 0.29000 6.37e+00 10.20000 24.400 32.20 224.0 6.8800 59.600 242
Northern Plains
Total_N 37.9000 24.9000 65.5 36.300 20.20 55.5 0.41700 7.59e+00 19.80000 53.900 62.80 86.7 -0.0272 -1.120 737
Amm_N 26.1000 20.1000 77.1 25.600 14.90 58.3 0.00000 5.80e+00 11.80000 39.100 45.00 67.2 0.0720 -1.110 711
P2O5 21.7000 18.3000 84.4 21.600 16.40 76.0 0.00000 1.50e+00 5.84000 31.900 39.00 99.2 0.8700 1.550 737
K2O 22.7000 14.4000 63.5 22.500 11.40 51.0 1.67000 6.95e+00 12.50000 31.600 37.00 65.6 0.1700 -0.690 734
Southern Plains
Total_N 11.1000 13.0000 117.0 35.900 56.10 156.0 0.00201 1.03e+00 3.46000 40.900 131.00 251.0 1.9900 3.000 122
Amm_N 6.1700 7.5400 122.0 12.300 17.80 145.0 0.00136 2.44e-01 1.58000 13.300 34.40 95.9 2.4200 6.230 103
P2O5 2.0700 2.2900 110.0 32.200 69.20 215.0 0.01910 5.84e-01 1.04000 17.200 99.20 358.0 2.9400 8.670 120
K2O 11.6000 12.3000 107.0 26.600 39.50 148.0 0.33700 2.19e+00 7.13000 29.600 59.40 257.0 3.0800 11.300 110

Table of samples by region by year for Swine Liquid manure

Code
# Sort out for swine while still having separate NPK columns
MDB_swinepumpNPK <- filter(MDB_pump3, Animal.Type.Combined.Category == "Swine")
# Create a table of number of samples for each region per year
sample_table <- table(MDB_swinepumpNPK$Region, MDB_swinepumpNPK$Year.Analyzed)

# Print the sample table
print(sample_table)
                 
                   2012  2013  2014  2015  2016  2017  2018  2019  2020  2021
  Midwest          1847  6348  6801  7082  7335  7795  8288  8579  8610  8425
  Northeast         112    48    62    39    66    50    48    35    60    49
  Northern Plains     0     0     0     0    20    21    18    32    16   443
  Southeast       12127 11872 11317 11841 11657 11336 11873 11831 10192 10852
  Southern Plains     0     0     0     0     0     0     0    40    18    50
                 
                   2022
  Midwest          7042
  Northeast          69
  Northern Plains   204
  Southeast        5535
  Southern Plains    14

Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Swine Liquid)

Code
# Load required library
library(Kendall)
# List of selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")
# List of analytes
analytes <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")
# Function to perform Mann-Kendall trend test for a given analyte across all selected regions
perform_mk_test_combined <- function(data, analyte, selected_regions) {
  # Subset data for the selected regions and analyte
  filtered_data <- data[data$Region %in% selected_regions, ]
  cleaned_data <- filtered_data[!is.na(filtered_data[[analyte]]), ]

  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  # Perform Mann-Kendall trend test
  result_mk_combined <- MannKendall(aggregated_data[[2]])
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk_combined))
}
# Perform Mann-Kendall trend test for each analyte combined across selected regions
for (analyte in analytes) {
  cat("Analyzing", analyte, "for all regions combined:\n")
  print(perform_mk_test_combined(MDB_swinepumpNPK, analyte, selected_regions))
  cat("\n")
}
Analyzing Total_N for all regions combined:
Score =  39 , Var(Score) = 165
denominator =  55
tau = 0.709, 2-sided pvalue =0.0030935
NULL

Analyzing Ammonium.N for all regions combined:
Score =  -1 , Var(Score) = 163
denominator =  53.99074
tau = -0.0185, 2-sided pvalue =1
NULL

Analyzing Phosphorus for all regions combined:
Score =  33 , Var(Score) = 165
denominator =  55
tau = 0.6, 2-sided pvalue =0.012731
NULL

Analyzing Potassium for all regions combined:
Score =  17 , Var(Score) = 165
denominator =  55
tau = 0.309, 2-sided pvalue =0.21291
NULL
Code
# Function to perform Mann-Kendall trend test
perform_mk_test <- function(data, analyte) {
  # Filter out NA values in the analyte column
  cleaned_data <- data[!is.na(data[[analyte]]), ]
  # Aggregate data by year and calculate median for the analyte
  aggregated_data <- aggregate(cleaned_data[[analyte]] ~ Year.Analyzed, data = cleaned_data, FUN = median)
  # Perform Mann-Kendall trend test
  result_mk <- MannKendall(aggregated_data[[2]])
  # Return summary of the Mann-Kendall test result
  return(summary(result_mk))
}

# Iterate over selected regions and analytes
for (region in selected_regions) {
  for (analyte in analytes) {
    # Subset data for the current region
    region_data <- MDB_swinepumpNPK[MDB_swinepumpNPK$Region == region, ]
    
    # Print information about the current region and analyte
    cat("Analyzing", analyte, "in", region, "region:\n")
    
    # Perform Mann-Kendall trend test
    result_summary <- perform_mk_test(region_data, analyte)
    
    # Print summary of the Mann-Kendall test result
    print(result_summary)
    cat("\n")
  }
}
Analyzing Total_N in Midwest region:
Score =  10 , Var(Score) = 147.3333
denominator =  49.1935
tau = 0.203, 2-sided pvalue =0.45841
NULL

Analyzing Ammonium.N in Midwest region:
Score =  -3 , Var(Score) = 163
denominator =  53.99074
tau = -0.0556, 2-sided pvalue =0.87552
NULL

Analyzing Phosphorus in Midwest region:
Score =  21 , Var(Score) = 146.3333
denominator =  48.63126
tau = 0.432, 2-sided pvalue =0.098265
NULL

Analyzing Potassium in Midwest region:
Score =  -13 , Var(Score) = 146.3333
denominator =  48.63126
tau = -0.267, 2-sided pvalue =0.3212
NULL

Analyzing Total_N in Northeast region:
Score =  -5 , Var(Score) = 165
denominator =  55
tau = -0.0909, 2-sided pvalue =0.7555
NULL

Analyzing Ammonium.N in Northeast region:
Score =  -7 , Var(Score) = 165
denominator =  55
tau = -0.127, 2-sided pvalue =0.64043
NULL

Analyzing Phosphorus in Northeast region:
Score =  -7 , Var(Score) = 165
denominator =  55
tau = -0.127, 2-sided pvalue =0.64043
NULL

Analyzing Potassium in Northeast region:
Score =  -1 , Var(Score) = 165
denominator =  55
tau = -0.0182, 2-sided pvalue =1
NULL

Analyzing Total_N in Southeast region:
Score =  13 , Var(Score) = 165
denominator =  55
tau = 0.236, 2-sided pvalue =0.3502
NULL

Analyzing Ammonium.N in Southeast region:
Score =  35 , Var(Score) = 165
denominator =  55
tau = 0.636, 2-sided pvalue =0.0081234
NULL

Analyzing Phosphorus in Southeast region:
Score =  1 , Var(Score) = 165
denominator =  55
tau = 0.0182, 2-sided pvalue =1
NULL

Analyzing Potassium in Southeast region:
Score =  -9 , Var(Score) = 165
denominator =  55
tau = -0.164, 2-sided pvalue =0.53342
NULL

Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Swine Liquid)

Code
# Filter data for selected regions
selected_regions <- c("Midwest", "Northeast", "Southeast")
filtered_data <- MDB_swinepumpNPK[MDB_swinepumpNPK$Region %in% selected_regions, ]

# Initialize an empty list to store cleaned data for each nutrient
cleaned_data_list <- list()

# Loop over each nutrient
for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Remove missing values for the current nutrient
  cleaned_data <- filtered_data[!is.na(filtered_data[[nutrient]]), ]
  cleaned_data_list[[nutrient]] <- cleaned_data
}

# Create a Function for Wilcoxon Test and Correction
perform_wilcoxon_test <- function(region1, region2, year, nutrient, data) {
    # Extract data for the given regions, year, and nutrient
    region1_data <- data[data$Region == region1 & data$Year.Analyzed == year, nutrient]
    region2_data <- data[data$Region == region2 & data$Year.Analyzed == year, nutrient]
    
    # Perform Wilcoxon rank sum test
    wilcox_result <- wilcox.test(region1_data, region2_data)
        
    # Apply Benjamini-Hochberg correction
    adjusted_p_value <- p.adjust(wilcox_result$p.value, method = "BH")
    
    # Return results
    return(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))
}

# Iterate Over Years, Regions, and Nutrients
years <- unique(filtered_data$Year.Analyzed)
regions <- unique(filtered_data$Region)
nutrients <- c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")

# Initialize empty vectors
all_p_values <- c()
comparison_results <- list()
comparison_names <- c()

# Loop over years, regions, and nutrients, perform tests, and gather p-values
index <- 1
for (year in years) {
    for (nutrient in nutrients) {
        for (i in 1:(length(regions)-1)) {
            for (j in (i+1):length(regions)) {
                region1 <- regions[i]
                region2 <- regions[j]
                
                # Perform Wilcoxon test and correction
                results <- perform_wilcoxon_test(region1, region2, year, nutrient, cleaned_data_list[[nutrient]])
                
                # Store p-value
                all_p_values <- c(all_p_values, results$wilcox_result$p.value)
                
                # Store comparison name
                comparison_names <- c(comparison_names, paste(region1, region2, year, nutrient, sep = "_"))
                
                # Store results
                comparison_results[[index]] <- results
                
                index <- index + 1
            }
        }
    }
}

# Sort comparison names based on year
sorted_indices <- order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))
sorted_comparison_names <- comparison_names[sorted_indices]

# Apply Benjamini-Hochberg Procedure
adjusted_p_values <- p.adjust(all_p_values, method = "BH")

# Print results in chronological order
for (name in sorted_comparison_names) {
    index <- match(name, comparison_names)
    year <- as.numeric(unlist(strsplit(name, "_"))[3])
    nutrient <- unlist(strsplit(name, "_"))[4]
    
    # Extract Wilcoxon test results
    result <- comparison_results[[index]]$wilcox_result
    
    # Print results with adjusted p-values
    cat("Comparison:", name, "\n")
    cat("Year:", year, "\n")
    cat("Nutrient:", nutrient, "\n")
    cat("Wilcoxon Test Statistic:", result$statistic, "\n")
    cat("Wilcoxon Test P-value:", result$p.value, "\n")
    cat("Adjusted p-value:", adjusted_p_values[index], "\n")
    cat("\n")
}
Comparison: Midwest_Northeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 123520 
Wilcoxon Test P-value: 0.0005481884 
Adjusted p-value: 0.0006460792 

Comparison: Midwest_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 21757423 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2012_Total_N 
Year: 2012 
Nutrient: Total 
Wilcoxon Test Statistic: 1344646 
Wilcoxon Test P-value: 1.749789e-72 
Adjusted p-value: 5.132714e-72 

Comparison: Midwest_Northeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 89018 
Wilcoxon Test P-value: 7.146e-06 
Adjusted p-value: 9.528e-06 

Comparison: Midwest_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 295752.5 
Wilcoxon Test P-value: 3.279581e-128 
Adjusted p-value: 9.838742e-128 

Comparison: Northeast_Southeast_2012_Ammonium.N 
Year: 2012 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 26173 
Wilcoxon Test P-value: 3.590248e-50 
Adjusted p-value: 1.008325e-49 

Comparison: Midwest_Northeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 116734 
Wilcoxon Test P-value: 0.02207701 
Adjusted p-value: 0.02331332 

Comparison: Midwest_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 21145175 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2012_Phosphorus 
Year: 2012 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 1308101 
Wilcoxon Test P-value: 3.417861e-64 
Adjusted p-value: 9.807775e-64 

Comparison: Midwest_Northeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 126380 
Wilcoxon Test P-value: 7.858913e-05 
Adjusted p-value: 9.879776e-05 

Comparison: Midwest_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 19874642 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2012_Potassium 
Year: 2012 
Nutrient: Potassium 
Wilcoxon Test Statistic: 1155022 
Wilcoxon Test P-value: 1.60954e-37 
Adjusted p-value: 4.085756e-37 

Comparison: Midwest_Northeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 175773 
Wilcoxon Test P-value: 0.001119591 
Adjusted p-value: 0.001285096 

Comparison: Midwest_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 74393823 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2013_Total_N 
Year: 2013 
Nutrient: Total 
Wilcoxon Test Statistic: 496979 
Wilcoxon Test P-value: 4.313587e-27 
Adjusted p-value: 7.591914e-27 

Comparison: Midwest_Northeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 73804 
Wilcoxon Test P-value: 7.194924e-05 
Adjusted p-value: 9.132019e-05 

Comparison: Midwest_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 580677.5 
Wilcoxon Test P-value: 4.272428e-133 
Adjusted p-value: 1.311536e-132 

Comparison: Northeast_Southeast_2013_Ammonium.N 
Year: 2013 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 9959 
Wilcoxon Test P-value: 3.164588e-25 
Adjusted p-value: 5.355456e-25 

Comparison: Midwest_Northeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 176301 
Wilcoxon Test P-value: 0.05988631 
Adjusted p-value: 0.06175776 

Comparison: Midwest_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 73518718 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2013_Phosphorus 
Year: 2013 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 544848 
Wilcoxon Test P-value: 8.462132e-28 
Adjusted p-value: 1.551391e-27 

Comparison: Midwest_Northeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 198434 
Wilcoxon Test P-value: 0.000296003 
Adjusted p-value: 0.0003520035 

Comparison: Midwest_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 72638471 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2013_Potassium 
Year: 2013 
Nutrient: Potassium 
Wilcoxon Test Statistic: 536121.5 
Wilcoxon Test P-value: 4.506294e-26 
Adjusted p-value: 7.725075e-26 

Comparison: Midwest_Northeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 263070.5 
Wilcoxon Test P-value: 0.0007685328 
Adjusted p-value: 0.0008898801 

Comparison: Midwest_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 76141994 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2014_Total_N 
Year: 2014 
Nutrient: Total 
Wilcoxon Test Statistic: 698516 
Wilcoxon Test P-value: 8.121661e-42 
Adjusted p-value: 2.187876e-41 

Comparison: Midwest_Northeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 111716 
Wilcoxon Test P-value: 2.270242e-06 
Adjusted p-value: 3.089402e-06 

Comparison: Midwest_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 706193.5 
Wilcoxon Test P-value: 1.781012e-144 
Adjusted p-value: 5.877338e-144 

Comparison: Northeast_Southeast_2014_Ammonium.N 
Year: 2014 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 16814 
Wilcoxon Test P-value: 7.334472e-34 
Adjusted p-value: 1.669225e-33 

Comparison: Midwest_Northeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 247960.5 
Wilcoxon Test P-value: 0.01679292 
Adjusted p-value: 0.01787633 

Comparison: Midwest_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 75167335 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2014_Phosphorus 
Year: 2014 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 673304 
Wilcoxon Test P-value: 7.131927e-36 
Adjusted p-value: 1.74336e-35 

Comparison: Midwest_Northeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 273354 
Wilcoxon Test P-value: 5.66267e-05 
Adjusted p-value: 7.257013e-05 

Comparison: Midwest_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 74976953 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2014_Potassium 
Year: 2014 
Nutrient: Potassium 
Wilcoxon Test Statistic: 672384 
Wilcoxon Test P-value: 1.11647e-35 
Adjusted p-value: 2.679527e-35 

Comparison: Midwest_Northeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 172022.5 
Wilcoxon Test P-value: 0.008055795 
Adjusted p-value: 0.00893584 

Comparison: Midwest_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 82786528 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2015_Total_N 
Year: 2015 
Nutrient: Total 
Wilcoxon Test Statistic: 459711 
Wilcoxon Test P-value: 6.174899e-27 
Adjusted p-value: 1.072482e-26 

Comparison: Midwest_Northeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 78405.5 
Wilcoxon Test P-value: 0.001830979 
Adjusted p-value: 0.002083528 

Comparison: Midwest_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 911104 
Wilcoxon Test P-value: 9.011685e-145 
Adjusted p-value: 3.050109e-144 

Comparison: Northeast_Southeast_2015_Ammonium.N 
Year: 2015 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 11891 
Wilcoxon Test P-value: 1.353041e-22 
Adjusted p-value: 2.126207e-22 

Comparison: Midwest_Northeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 155771 
Wilcoxon Test P-value: 0.1674467 
Adjusted p-value: 0.1687249 

Comparison: Midwest_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 82469980 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2015_Phosphorus 
Year: 2015 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 448796 
Wilcoxon Test P-value: 2.190906e-24 
Adjusted p-value: 3.570365e-24 

Comparison: Midwest_Northeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 172894 
Wilcoxon Test P-value: 0.00656825 
Adjusted p-value: 0.007347534 

Comparison: Midwest_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 81581079 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2015_Potassium 
Year: 2015 
Nutrient: Potassium 
Wilcoxon Test Statistic: 445431.5 
Wilcoxon Test P-value: 1.092028e-23 
Adjusted p-value: 1.73672e-23 

Comparison: Midwest_Northeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 330572.5 
Wilcoxon Test P-value: 2.929926e-07 
Adjusted p-value: 4.158605e-07 

Comparison: Midwest_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 84426509 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2016_Total_N 
Year: 2016 
Nutrient: Total 
Wilcoxon Test Statistic: 701445 
Wilcoxon Test P-value: 3.359682e-31 
Adjusted p-value: 7.152872e-31 

Comparison: Midwest_Northeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 111854 
Wilcoxon Test P-value: 0.05818842 
Adjusted p-value: 0.0604793 

Comparison: Midwest_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 882232 
Wilcoxon Test P-value: 1.252108e-142 
Adjusted p-value: 4.031176e-142 

Comparison: Northeast_Southeast_2016_Ammonium.N 
Year: 2016 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 15381 
Wilcoxon Test P-value: 6.755259e-30 
Adjusted p-value: 1.330887e-29 

Comparison: Midwest_Northeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 285353 
Wilcoxon Test P-value: 0.01221125 
Adjusted p-value: 0.01310476 

Comparison: Midwest_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 83670963 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2016_Phosphorus 
Year: 2016 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 755286 
Wilcoxon Test P-value: 1.155504e-41 
Adjusted p-value: 3.050531e-41 

Comparison: Midwest_Northeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 286125.5 
Wilcoxon Test P-value: 0.01075385 
Adjusted p-value: 0.01163532 

Comparison: Midwest_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 83575918 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2016_Potassium 
Year: 2016 
Nutrient: Potassium 
Wilcoxon Test Statistic: 701283 
Wilcoxon Test P-value: 7.194454e-31 
Adjusted p-value: 1.483856e-30 

Comparison: Midwest_Northeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 253164 
Wilcoxon Test P-value: 0.0002605855 
Adjusted p-value: 0.0003127025 

Comparison: Midwest_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 87303499 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2017_Total_N 
Year: 2017 
Nutrient: Total 
Wilcoxon Test Statistic: 564703 
Wilcoxon Test P-value: 5.099301e-34 
Adjusted p-value: 1.180891e-33 

Comparison: Midwest_Northeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 92609 
Wilcoxon Test P-value: 0.009554488 
Adjusted p-value: 0.01050994 

Comparison: Midwest_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 776869 
Wilcoxon Test P-value: 1.938366e-138 
Adjusted p-value: 6.092008e-138 

Comparison: Northeast_Southeast_2017_Ammonium.N 
Year: 2017 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 13125 
Wilcoxon Test P-value: 1.49955e-28 
Adjusted p-value: 2.827723e-28 

Comparison: Midwest_Northeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 223761 
Wilcoxon Test P-value: 0.07004447 
Adjusted p-value: 0.07112208 

Comparison: Midwest_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 86606596 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2017_Phosphorus 
Year: 2017 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 547296 
Wilcoxon Test P-value: 5.322342e-30 
Adjusted p-value: 1.064468e-29 

Comparison: Midwest_Northeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 262644.5 
Wilcoxon Test P-value: 2.176157e-05 
Adjusted p-value: 2.844086e-05 

Comparison: Midwest_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 85775591 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2017_Potassium 
Year: 2017 
Nutrient: Potassium 
Wilcoxon Test Statistic: 546735 
Wilcoxon Test P-value: 7.021477e-30 
Adjusted p-value: 1.362993e-29 

Comparison: Midwest_Northeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 293426 
Wilcoxon Test P-value: 1.304995e-08 
Adjusted p-value: 1.89296e-08 

Comparison: Midwest_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 97275234 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2018_Total_N 
Year: 2018 
Nutrient: Total 
Wilcoxon Test Statistic: 556028 
Wilcoxon Test P-value: 2.843882e-30 
Adjusted p-value: 5.775269e-30 

Comparison: Midwest_Northeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 112738.5 
Wilcoxon Test P-value: 4.849931e-06 
Adjusted p-value: 6.532561e-06 

Comparison: Midwest_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 918953 
Wilcoxon Test P-value: 3.453249e-154 
Adjusted p-value: 1.19955e-153 

Comparison: Northeast_Southeast_2018_Ammonium.N 
Year: 2018 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 13139 
Wilcoxon Test P-value: 3.461719e-27 
Adjusted p-value: 6.174959e-27 

Comparison: Midwest_Northeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 277960 
Wilcoxon Test P-value: 1.958534e-06 
Adjusted p-value: 2.692984e-06 

Comparison: Midwest_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 96747890 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2018_Phosphorus 
Year: 2018 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 516885 
Wilcoxon Test P-value: 1.830422e-22 
Adjusted p-value: 2.842538e-22 

Comparison: Midwest_Northeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 284466 
Wilcoxon Test P-value: 2.646617e-07 
Adjusted p-value: 3.79732e-07 

Comparison: Midwest_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 95107411 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2018_Potassium 
Year: 2018 
Nutrient: Potassium 
Wilcoxon Test Statistic: 526310 
Wilcoxon Test P-value: 3.418924e-24 
Adjusted p-value: 5.503633e-24 

Comparison: Midwest_Northeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 195640 
Wilcoxon Test P-value: 0.001937396 
Adjusted p-value: 0.00218578 

Comparison: Midwest_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 99759283 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2019_Total_N 
Year: 2019 
Nutrient: Total 
Wilcoxon Test Statistic: 412197 
Wilcoxon Test P-value: 1.842494e-24 
Adjusted p-value: 3.040116e-24 

Comparison: Midwest_Northeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 80305 
Wilcoxon Test P-value: 0.01046169 
Adjusted p-value: 0.01141275 

Comparison: Midwest_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 1069528 
Wilcoxon Test P-value: 3.404384e-170 
Adjusted p-value: 1.321702e-169 

Comparison: Northeast_Southeast_2019_Ammonium.N 
Year: 2019 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 10310 
Wilcoxon Test P-value: 9.594756e-22 
Adjusted p-value: 1.455756e-21 

Comparison: Midwest_Northeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 169078 
Wilcoxon Test P-value: 0.1968546 
Adjusted p-value: 0.1968546 

Comparison: Midwest_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 99604969 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2019_Phosphorus 
Year: 2019 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 400198 
Wilcoxon Test P-value: 1.359511e-21 
Adjusted p-value: 2.039267e-21 

Comparison: Midwest_Northeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 180349 
Wilcoxon Test P-value: 0.03955781 
Adjusted p-value: 0.04144152 

Comparison: Midwest_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 98215690 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2019_Potassium 
Year: 2019 
Nutrient: Potassium 
Wilcoxon Test Statistic: 401525 
Wilcoxon Test P-value: 7.206062e-22 
Adjusted p-value: 1.106047e-21 

Comparison: Midwest_Northeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 329660 
Wilcoxon Test P-value: 0.0002210222 
Adjusted p-value: 0.0002701383 

Comparison: Midwest_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 86337166 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2020_Total_N 
Year: 2020 
Nutrient: Total 
Wilcoxon Test Statistic: 597825 
Wilcoxon Test P-value: 4.760018e-38 
Adjusted p-value: 1.232005e-37 

Comparison: Midwest_Northeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 162011 
Wilcoxon Test P-value: 1.227392e-08 
Adjusted p-value: 1.800176e-08 

Comparison: Midwest_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 979497 
Wilcoxon Test P-value: 1.319472e-154 
Adjusted p-value: 4.707306e-154 

Comparison: Northeast_Southeast_2020_Ammonium.N 
Year: 2020 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 15413 
Wilcoxon Test P-value: 3.621312e-31 
Adjusted p-value: 7.587511e-31 

Comparison: Midwest_Northeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 329367 
Wilcoxon Test P-value: 0.0002341905 
Adjusted p-value: 0.0002836069 

Comparison: Midwest_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 86030422 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2020_Phosphorus 
Year: 2020 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 564711 
Wilcoxon Test P-value: 9.469724e-30 
Adjusted p-value: 1.811599e-29 

Comparison: Midwest_Northeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 330291.5 
Wilcoxon Test P-value: 0.0001938892 
Adjusted p-value: 0.0002391904 

Comparison: Midwest_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 85948841 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2020_Potassium 
Year: 2020 
Nutrient: Potassium 
Wilcoxon Test Statistic: 572342 
Wilcoxon Test P-value: 1.982528e-31 
Adjusted p-value: 4.290061e-31 

Comparison: Midwest_Northeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 309342 
Wilcoxon Test P-value: 1.655735e-09 
Adjusted p-value: 2.455697e-09 

Comparison: Midwest_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 90294394 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2021_Total_N 
Year: 2021 
Nutrient: Total 
Wilcoxon Test Statistic: 527822 
Wilcoxon Test P-value: 4.527257e-33 
Adjusted p-value: 1.012878e-32 

Comparison: Midwest_Northeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 119377 
Wilcoxon Test P-value: 1.837611e-05 
Adjusted p-value: 2.425647e-05 

Comparison: Midwest_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 989377.5 
Wilcoxon Test P-value: 4.861635e-162 
Adjusted p-value: 1.833531e-161 

Comparison: Northeast_Southeast_2021_Ammonium.N 
Year: 2021 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 13579 
Wilcoxon Test P-value: 3.261208e-28 
Adjusted p-value: 6.063091e-28 

Comparison: Midwest_Northeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 264230 
Wilcoxon Test P-value: 0.0007074292 
Adjusted p-value: 0.0008263775 

Comparison: Midwest_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 89538552 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2021_Phosphorus 
Year: 2021 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 491197 
Wilcoxon Test P-value: 1.164713e-24 
Adjusted p-value: 1.946103e-24 

Comparison: Midwest_Northeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 293631 
Wilcoxon Test P-value: 3.240323e-07 
Adjusted p-value: 4.550241e-07 

Comparison: Midwest_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 89579853 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2021_Potassium 
Year: 2021 
Nutrient: Potassium 
Wilcoxon Test Statistic: 504406 
Wilcoxon Test P-value: 1.940151e-27 
Adjusted p-value: 3.508218e-27 

Comparison: Midwest_Northeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 312552.5 
Wilcoxon Test P-value: 4.0988e-05 
Adjusted p-value: 5.304329e-05 

Comparison: Midwest_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 38290764 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2022_Total_N 
Year: 2022 
Nutrient: Total 
Wilcoxon Test Statistic: 376700 
Wilcoxon Test P-value: 7.721653e-45 
Adjusted p-value: 2.123455e-44 

Comparison: Midwest_Northeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 170720 
Wilcoxon Test P-value: 5.090393e-07 
Adjusted p-value: 7.072967e-07 

Comparison: Midwest_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 975207 
Wilcoxon Test P-value: 2.941063e-155 
Adjusted p-value: 1.07839e-154 

Comparison: Northeast_Southeast_2022_Ammonium.N 
Year: 2022 
Nutrient: Ammonium.N 
Wilcoxon Test Statistic: 17793 
Wilcoxon Test P-value: 2.362054e-34 
Adjusted p-value: 5.5677e-34 

Comparison: Midwest_Northeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 270710 
Wilcoxon Test P-value: 0.06293047 
Adjusted p-value: 0.06439397 

Comparison: Midwest_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 37737539 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2022_Phosphorus 
Year: 2022 
Nutrient: Phosphorus 
Wilcoxon Test Statistic: 356607 
Wilcoxon Test P-value: 5.676479e-37 
Adjusted p-value: 1.413765e-36 

Comparison: Midwest_Northeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 305106 
Wilcoxon Test P-value: 9.631783e-05 
Adjusted p-value: 0.000119943 

Comparison: Midwest_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 37222776 
Wilcoxon Test P-value: 0 
Adjusted p-value: 0 

Comparison: Northeast_Southeast_2022_Potassium 
Year: 2022 
Nutrient: Potassium 
Wilcoxon Test Statistic: 346986 
Wilcoxon Test P-value: 4.661233e-33 
Adjusted p-value: 1.025471e-32 

Four analyte median + IQR plots 2012-2022 + counts and trend arrows (Swine Liquid)

Code
# Filter out rows with non-missing Total_N values and for specific regions
MDB_swinepumpNPK_filtered <- MDB_swinepumpNPK[                                              MDB_swinepumpNPK$Region %in% c("Southeast", "Midwest"), ]

custom_theme <- theme_minimal() +
  theme(
    text = element_text(family = "Times New Roman", size = 11),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

# Create separate plots for each nutrient type
plots <- list()

# Initialize variables to store the minimum and maximum values of lower and upper quartiles
min_lower_quantile <- Inf
max_upper_quantile <- -Inf

# Function to calculate counts for each nutrient and region excluding zeroes
calculate_counts <- function(data) {
  counts <- data %>%
    filter(!is.na(Total_N) & Total_N != 0 & Year.Analyzed >= 2012 & Year.Analyzed <= 2022) %>%
    group_by(Region) %>%
    summarise(total_samples = sum(!is.na(Total_N))) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  return(counts)
}


for (nutrient in c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {
  # Filter data for the current nutrient type
  filtered_data <- MDB_swinepumpNPK_filtered[!is.na(MDB_swinepumpNPK_filtered[[nutrient]]), ]
  
  # Calculate median and IQR for each combination of Year.Analyzed and Region
  summary_stats <- filtered_data %>%
    group_by(Year.Analyzed, Region) %>%
    summarise(median_value = median(.data[[nutrient]]),
              lower_quantile = quantile(.data[[nutrient]], 0.25),
              upper_quantile = quantile(.data[[nutrient]], 0.75)) %>%
    ungroup()  # Remove grouping to avoid issues with plotting
  
  # Update minimum and maximum values of lower and upper quartiles
  min_lower_quantile <- min(min_lower_quantile, min(summary_stats$lower_quantile))
  max_upper_quantile <- max(max_upper_quantile, max(summary_stats$upper_quantile))
  
  # Determine a suitable step size for the y-axis
  y_range <- max_upper_quantile - min_lower_quantile
  num_breaks <- max(4, floor(y_range * 10))  # Ensure at least four breaks
  step_size <- y_range / num_breaks
  # Calculate counts for all regions
  counts <- calculate_counts(filtered_data)

  
  # Create line graph with median for each year and region, with connecting lines
  plot <- ggplot(summary_stats, aes(x = factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +
    geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha = 0.3) +  # Add shaded area for IQR
    geom_line(aes(linetype = Region)) +  # Add line graph with connecting lines
    geom_point() +  # Add points for each data point
    labs(x = "Year Analyzed", 
         y = switch(nutrient,
                    Total_N = "Total N (%)",
                    Ammonium.N = expression(paste("NH"[4], "-N (%)")),
                    Phosphorus = expression(paste("P"[2], "O"[5], " (%)")),
                    Potassium = expression(paste("K"[2], "O (%)"))
         ), 
         title = NULL) +  # Remove titles for all graphs
    custom_theme +  # Apply custom theme with font settings
    scale_color_manual(values = c("Northeast" = "green", "Southeast" = "red", "Midwest" = "blue")) +  # Define colors for regions
    scale_fill_manual(values = c("Northeast" = "lightgreen", "Southeast" = "lightpink", "Midwest" = "lightblue")) +  # Define fill colors for IQR
    guides(fill = FALSE) +  # Remove legend for IQR fill colors
    scale_y_continuous(limits = c(0, .7), breaks = seq(0, .7, by = 0.1))+
                       #breaks = seq(ceiling(min_lower_quantile), floor(max_upper_quantile), by = step_size),
                       #expand = c(0, 0))  # Specify breaks for y-axis labels and ensure continuous scale
  geom_text(data = counts, aes(label = paste("n =", total_samples),
                                 x = "2021", y = .7 - (0.08 * seq_along(total_samples)), color = Region),
              hjust = 1, vjust = 0, size = 3, family = "Times New Roman")  # Add text annotations for counts
if (nutrient == "Ammonium.N") {
    plot <- plot + annotate("segment", x = "2022", y = .52, xend = "2022", yend = .58, 
                            arrow = arrow(type = "open", length = unit(0.1, "inches")), color = "red")
  }
  # Add the plot to the list
  plots[[nutrient]] <- plot
}

# Arrange the plots together using patchwork
swinepump_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] + plot_layout(guides = "collect")

# Print the final plot
print(swinepump_final_plot)

Code
#`{=tex}

#\endgroup

Calculations used from the updated Recommended Methods of Manure Analysis (Wilson et al. 2022).

Bibliography

Wilson, Melissa L., Scott Cortus, Rachel Brimmer, Jerry Floren, Larry Gunderson, Kristin Hicks, Tim Hoerner, et al. 2022. Recommended Methods of Manure Analysis, Second Edition. University of Minnesota Libraries Publishing. http://conservancy.umn.edu/handle/11299/227650.