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_