#Filter data to work with from years 2012-2022df <-data.frame(df)# Define the range of yearsstart_year <-2012end_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 withMDB_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 functionwrite_clip <-function(data) {# Create a data frame with column names and percentages df <-data.frame(Column =names(data), Percentage = data)# Write to clipboardwrite.table(df, "clipboard", sep="\t", row.names=FALSE, col.names=TRUE)}# Replace "Unknown" values and blanks with NAMDB_year_data_count <- MDB_year_dataMDB_year_data_count[MDB_year_data_count =="Unknown"| MDB_year_data_count ==""] <-NA# Calculate the percentage of non-NA values in each columnpercent_non_na <-colMeans(!is.na(MDB_year_data_count)) *100# Print the percentagesprint(percent_non_na)
Sort out for beef while still having separate NPK columns
Code
# Sort out for beef while still having separate NPK columnsMDB_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 columncleaned_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 dataqqnorm(cleaned_data$Total_N)qqline(cleaned_data$Total_N, col =2) # Add a reference line
Code
#Histogramhist(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 columncleaned_data$log_Total_N <-log(cleaned_data$Total_N, base =10)# Square root transformation of Total_N columncleaned_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 datapar(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 dataqqnorm(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 dataqqnorm(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 Regionjarque_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 Regionjarque_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 yearsample_table <-table(MDB_beefstackNPK$Region, MDB_beefstackNPK$Year.Analyzed)# Print the sample tableprint(sample_table) #NE and SE have samples all years
Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Beef Solid)
Code
# Filter data for selected regionsselected_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 nutrientcleaned_data_list <-list()# Loop over each nutrientfor (nutrient inc("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 Correctionperform_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 resultsreturn(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))}# Iterate Over Years, Regions, and Nutrientsyears <-unique(cleaned_data$Year.Analyzed)regions <-unique(cleaned_data$Region)nutrients <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Initialize empty vectorsall_p_values <-c()comparison_results <-list()comparison_names <-c()# Loop over years, regions, and nutrients, perform tests, and gather p-valuesindex <-1for (year in years) {for (nutrient in nutrients) {for (i in1:(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 yearsorted_indices <-order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))sorted_comparison_names <- comparison_names[sorted_indices]# Apply Benjamini-Hochberg Procedureadjusted_p_values <-p.adjust(all_p_values, method ="BH")# Print results in chronological orderfor (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-valuescat("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 regionsselected_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 nutrientcleaned_data_list <-list()# Loop over each nutrientfor (nutrient inc("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 Correctionperform_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 resultsreturn(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))}# Initialize empty vectorsall_p_values <-c()comparison_results <-list()comparison_names <-c()# Loop over regions, nutrients, perform tests, and gather p-valuesfor (region1 in selected_regions) {for (region2 in selected_regions) {if (region1 != region2) {for (nutrient inc("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 Procedureadjusted_p_values <-p.adjust(all_p_values, method ="BH")# Print resultsfor (i inseq_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-valuescat("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 librarylibrary(Kendall)# Function to perform Mann-Kendall trend test for a given analyte across all selected regionsperform_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 resultreturn(summary(result_mk))}# List of selected regionsselected_regions <-c("Midwest", "Northeast", "Southeast")# List of analytesanalytes <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Perform Mann-Kendall trend test for each analyte combined across selected regionsfor (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 regionsselected_regions <-c("Midwest", "Northeast", "Southeast")# List of analytesanalytes <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Function to perform Mann-Kendall trend testperform_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 resultreturn(summary(result_mk))}# Iterate over selected regions and analytesfor (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 analytecat("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 resultprint(result_summary)cat("\n") }}
Four Analytes Line + IQR graph with counts + trend arrow (Beef Solid)
Code
# Filter out rows with non-missing Total_N values and for specific regionsMDB_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 typeplots <-list()# Initialize variables to store the minimum and maximum values of lower and upper quartilesmin_lower_quantile <-Infmax_upper_quantile <--Inf# Function to calculate counts for each nutrientcalculate_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 inc("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 IQRgeom_line(aes(linetype = Region)) +# Add line graph with connecting linesgeom_point() +# Add points for each data pointlabs(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 settingsscale_color_manual(values =c("Midwest"="blue", "Northeast"="darkgreen", "Southeast"="red")) +# Define colors for regionsscale_fill_manual(values =c("Midwest"="lightblue", "Northeast"="lightgreen", "Southeast"="lightpink")) +# Define fill colors for IQRguides(fill =FALSE) +# Remove legend for IQR fill colorsscale_y_continuous(limits =c(0, 1.5), breaks =seq(0, 1.5, by =0.25)) +# Specify breaks for y-axis labels and ensure continuous scalegeom_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 graphif (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 patchworkbeefstack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] +plot_layout(guides ="collect")# Print the final plotprint(beefstack_final_plot)
Chicken - Broiler
Chicken - Broiler Solid Manure by Region Overview Table (%)
Table of samples by region by year for Chicken - Broiler solid manure
Code
# Create a table of number of samples for each region per yearMDB_broilerstackNPK <-filter(MDB_stack5fix, Animal.or.Other.Amendment.Type =="Chicken - Broiler")sample_table <-table(MDB_broilerstackNPK$Region, MDB_broilerstackNPK$Year.Analyzed)# Print the sample tableprint(sample_table) #NE and SE have samples all years
Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Chicken - Broiler solid)
Code
# Load required librarylibrary(Kendall)# List of selected regionsselected_regions <-c("Northeast", "Southeast")# List of analytesanalytes <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Function to perform Mann-Kendall trend test for a given analyte across all selected regionsperform_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 resultreturn(summary(result_mk_combined))}# Perform Mann-Kendall trend test for each analyte combined across selected regionsfor (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 testperform_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 resultreturn(summary(result_mk))}# Iterate over selected regions and analytesfor (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 analytecat("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 resultprint(result_summary)cat("\n") }}
Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Chicken - Broiler solid)
Code
# Filter data for selected regionsselected_regions <-c("Northeast", "Southeast")filtered_data <- MDB_broilerstackNPK[MDB_broilerstackNPK$Region %in% selected_regions, ]# Initialize an empty list to store cleaned data for each nutrientcleaned_data_list <-list()# Loop over each nutrientfor (nutrient inc("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 Correctionperform_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 resultsreturn(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))}# Iterate Over Years, Regions, and Nutrientsyears <-unique(filtered_data$Year.Analyzed)regions <-unique(filtered_data$Region)nutrients <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Initialize empty vectorsall_p_values <-c()comparison_results <-list()comparison_names <-c()# Loop over years, regions, and nutrients, perform tests, and gather p-valuesindex <-1for (year in years) {for (nutrient in nutrients) {for (i in1:(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 yearsorted_indices <-order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))sorted_comparison_names <- comparison_names[sorted_indices]# Apply Benjamini-Hochberg Procedureadjusted_p_values <-p.adjust(all_p_values, method ="BH")# Print results in chronological orderfor (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-valuescat("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 regionsMDB_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 adjustmentscustom_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 typeplots <-list()# Initialize variables to store the minimum and maximum values of lower and upper quartilesmin_lower_quantile <-Infmax_upper_quantile <--Inf# Function to calculate counts for each nutrientcalculate_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 inc("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 IQRgeom_line(aes(linetype = Region)) +# Add line graph with connecting linesgeom_point() +# Add points for each data pointlabs(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 backgroundscale_color_manual(values =c("Northeast"="darkgreen", "Southeast"="red")) +# Define colors for regionsscale_fill_manual(values =c("Northeast"="lightgreen", "Southeast"="lightpink")) +# Define fill colors for IQRguides(fill =FALSE) +# Remove legend for IQR fill colorsscale_y_continuous(limits =c(0, 4), breaks =seq(0, 4, by =1)) +# Specify breaks for y-axis labels and ensure continuous scalegeom_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 graphif (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 patchworkbroilerstack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] +plot_layout(guides ="collect")# Print the final plotprint(broilerstack_final_plot)
Chicken - Layer
Chicken - Layer Solid Manure by Region Overview Table (%)
Table of samples by region by year for Chicken Layer manure
Code
# Sort out for layer while still having separate NPK columnsMDB_layerstackNPK <-filter(MDB_stack5fix, Animal.or.Other.Amendment.Type =="Chicken - Layer")# Create a table of number of samples for each region per yearsample_table <-table(MDB_layerstackNPK$Region, MDB_layerstackNPK$Year.Analyzed)# Print the sample tableprint(sample_table)
Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Chicken - Layer)
Code
# Load required librarylibrary(Kendall)# List of selected regionsselected_regions <-c("Northeast", "Southeast")# List of analytesanalytes <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Function to perform Mann-Kendall trend test for a given analyte across all selected regionsperform_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 resultreturn(summary(result_mk_combined))}# Perform Mann-Kendall trend test for each analyte combined across selected regionsfor (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 testperform_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 resultreturn(summary(result_mk))}# Iterate over selected regions and analytesfor (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 analytecat("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 resultprint(result_summary)cat("\n") }}
Four analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Chicken - Layer Solid)
Code
# Filter data for selected regionsselected_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 nutrientcleaned_data_list <-list()# Loop over each nutrientfor (nutrient inc("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 Correctionperform_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 resultsreturn(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))}# Iterate Over Years, Regions, and Nutrientsyears <-unique(filtered_data$Year.Analyzed)regions <-unique(filtered_data$Region)nutrients <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Initialize empty vectorsall_p_values <-c()comparison_results <-list()comparison_names <-c()# Loop over years, regions, and nutrients, perform tests, and gather p-valuesindex <-1for (year in years) {for (nutrient in nutrients) {for (i in1:(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 yearsorted_indices <-order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))sorted_comparison_names <- comparison_names[sorted_indices]# Apply Benjamini-Hochberg Procedureadjusted_p_values <-p.adjust(all_p_values, method ="BH")# Print results in chronological orderfor (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-valuescat("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 regionsMDB_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 typeplots <-list()# Initialize variables to store the minimum and maximum values of lower and upper quartilesmin_lower_quantile <-Infmax_upper_quantile <--Inf# Function to calculate counts for each nutrientcalculate_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 plottingreturn(counts)}for (nutrient inc("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 IQRgeom_line(aes(linetype = Region)) +# Add line graph with connecting linesgeom_point() +# Add points for each data pointlabs(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 settingsscale_color_manual(values =c("Northeast"="darkgreen", "Southeast"="red", "Midwest"="blue")) +# Define colors for regionsscale_fill_manual(values =c("Northeast"="lightgreen", "Southeast"="lightpink", "Midwest"="lightblue")) +# Define fill colors for IQRguides(fill =FALSE) +# Remove legend for IQR fill colorsscale_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 scalegeom_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 graphif (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 patchworklayerstack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] +plot_layout(guides ="collect")# Print the final plotprint(layerstack_final_plot)
Table of samples by region by year for Dairy Solid manure
Code
# Sort out for dairy while still having separate NPK columnsMDB_dairystackNPK <-filter(MDB_stack5fix, Animal.or.Other.Amendment.Type =="Dairy")# Create a table of number of samples for each region per yearsample_table <-table(MDB_dairystackNPK$Region, MDB_dairystackNPK$Year.Analyzed)# Print the sample tableprint(sample_table)
Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Dairy solid)
Code
# Load required librarylibrary(Kendall)# List of selected regionsselected_regions <-c("Midwest", "Northeast", "Southeast")# List of analytesanalytes <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Function to perform Mann-Kendall trend test for a given analyte across all selected regionsperform_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 resultreturn(summary(result_mk_combined))}# Perform Mann-Kendall trend test for each analyte combined across selected regionsfor (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 testperform_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 resultreturn(summary(result_mk))}# Iterate over selected regions and analytesfor (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 analytecat("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 resultprint(result_summary)cat("\n") }}
Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Dairy Solid)
Code
# Filter data for selected regionsselected_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 nutrientcleaned_data_list <-list()# Loop over each nutrientfor (nutrient inc("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 Correctionperform_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 resultsreturn(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))}# Iterate Over Years, Regions, and Nutrientsyears <-unique(filtered_data$Year.Analyzed)regions <-unique(filtered_data$Region)nutrients <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Initialize empty vectorsall_p_values <-c()comparison_results <-list()comparison_names <-c()# Loop over years, regions, and nutrients, perform tests, and gather p-valuesindex <-1for (year in years) {for (nutrient in nutrients) {for (i in1:(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 yearsorted_indices <-order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))sorted_comparison_names <- comparison_names[sorted_indices]# Apply Benjamini-Hochberg Procedureadjusted_p_values <-p.adjust(all_p_values, method ="BH")# Print results in chronological orderfor (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-valuescat("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 regionsMDB_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 typeplots <-list()# Initialize variables to store the minimum and maximum values of lower and upper quartilesmin_lower_quantile <-Infmax_upper_quantile <--Inf# Function to calculate counts for each nutrientcalculate_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 plottingreturn(counts)}for (nutrient inc("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 IQRgeom_line(aes(linetype = Region)) +# Add line graph with connecting linesgeom_point() +# Add points for each data pointlabs(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 settingsscale_color_manual(values =c("Northeast"="darkgreen", "Southeast"="red", "Midwest"="blue")) +# Define colors for regionsscale_fill_manual(values =c("Northeast"="lightgreen", "Southeast"="lightpink", "Midwest"="lightblue")) +# Define fill colors for IQRguides(fill =FALSE) +# Remove legend for IQR fill colorsscale_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 graphif (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 patchworkdairystack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] +plot_layout(guides ="collect")# Print the final plotprint(dairystack_final_plot)
Table of samples by region by year for Turkey solid manure
Code
# Sort out for turkey while still having separate NPK columnsMDB_turkeystackNPK <-filter(MDB_stack5fix, Animal.or.Other.Amendment.Type =="Turkey")# Create a table of number of samples for each region per yearsample_table <-table(MDB_turkeystackNPK$Region, MDB_turkeystackNPK$Year.Analyzed)# Print the sample tableprint(sample_table)
Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Turkey Solid)
Code
# Load required librarylibrary(Kendall)# List of selected regionsselected_regions <-c("Midwest", "Southeast")# List of analytesanalytes <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Function to perform Mann-Kendall trend test for a given analyte across all selected regionsperform_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 resultreturn(summary(result_mk_combined))}# Perform Mann-Kendall trend test for each analyte combined across selected regionsfor (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 testperform_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 resultreturn(summary(result_mk))}# Iterate over selected regions and analytesfor (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 analytecat("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 resultprint(result_summary)cat("\n") }}
Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Turkey Solid)
Code
# Filter data for selected regionsselected_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 nutrientcleaned_data_list <-list()# Loop over each nutrientfor (nutrient inc("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 Correctionperform_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 resultsreturn(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))}# Iterate Over Years, Regions, and Nutrientsyears <-unique(filtered_data$Year.Analyzed)regions <-unique(filtered_data$Region)nutrients <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Initialize empty vectorsall_p_values <-c()comparison_results <-list()comparison_names <-c()# Loop over years, regions, and nutrients, perform tests, and gather p-valuesindex <-1for (year in years) {for (nutrient in nutrients) {for (i in1:(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 yearsorted_indices <-order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))sorted_comparison_names <- comparison_names[sorted_indices]# Apply Benjamini-Hochberg Procedureadjusted_p_values <-p.adjust(all_p_values, method ="BH")# Print results in chronological orderfor (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-valuescat("Comparison:", name, "\n")cat("Year:", year, "\n")cat("Nutrient:", nutrient, "\n")cat("Wilcoxon Test Statistic:", result$statistic, "\n")cat("Wilcoxon Test P-value:", result$p.value, "\n")cat("Adjusted p-value:", adjusted_p_values[index], "\n")cat("\n")}
Comparison: Midwest_Southeast_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 30433
Wilcoxon Test P-value: 0.9961638
Adjusted p-value: 0.9961638
Comparison: Midwest_Northeast_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 1509
Wilcoxon Test P-value: 0.2459761
Adjusted p-value: 0.3491273
Comparison: Southeast_Northeast_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 5934
Wilcoxon Test P-value: 0.2412691
Adjusted p-value: 0.3484366
Comparison: Midwest_Southeast_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 16598
Wilcoxon Test P-value: 3.619086e-16
Adjusted p-value: 4.777194e-14
Comparison: Midwest_Northeast_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1337
Wilcoxon Test P-value: 0.0001930398
Adjusted p-value: 0.0007962893
Comparison: Southeast_Northeast_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 2665
Wilcoxon Test P-value: 0.4130107
Adjusted p-value: 0.5047909
Comparison: Midwest_Southeast_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 21920
Wilcoxon Test P-value: 1.434156e-06
Adjusted p-value: 9.963609e-06
Comparison: Midwest_Northeast_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 465
Wilcoxon Test P-value: 2.604999e-06
Adjusted p-value: 1.637428e-05
Comparison: Southeast_Northeast_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2975
Wilcoxon Test P-value: 0.001025238
Adjusted p-value: 0.003657607
Comparison: Midwest_Southeast_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 28806
Wilcoxon Test P-value: 0.3549019
Adjusted p-value: 0.4461623
Comparison: Midwest_Northeast_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 826.5
Wilcoxon Test P-value: 0.007612942
Adjusted p-value: 0.02050833
Comparison: Southeast_Northeast_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 3592
Wilcoxon Test P-value: 0.01856339
Adjusted p-value: 0.04375655
Comparison: Midwest_Southeast_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 13591
Wilcoxon Test P-value: 0.0347434
Adjusted p-value: 0.0727957
Comparison: Midwest_Northeast_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 335
Wilcoxon Test P-value: 0.8766659
Adjusted p-value: 0.9184119
Comparison: Southeast_Northeast_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 2119
Wilcoxon Test P-value: 0.4989535
Adjusted p-value: 0.5933502
Comparison: Midwest_Southeast_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 7379
Wilcoxon Test P-value: 0.001851498
Adjusted p-value: 0.005960921
Comparison: Midwest_Northeast_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 268
Wilcoxon Test P-value: 0.2115274
Adjusted p-value: 0.3172911
Comparison: Southeast_Northeast_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1062
Wilcoxon Test P-value: 0.8692122
Adjusted p-value: 0.9178881
Comparison: Midwest_Southeast_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 10456
Wilcoxon Test P-value: 1.289274e-06
Adjusted p-value: 9.454677e-06
Comparison: Midwest_Northeast_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 158
Wilcoxon Test P-value: 0.007991053
Adjusted p-value: 0.02109638
Comparison: Southeast_Northeast_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1476
Wilcoxon Test P-value: 0.277994
Adjusted p-value: 0.3822417
Comparison: Midwest_Southeast_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 12402
Wilcoxon Test P-value: 0.001650912
Adjusted p-value: 0.005448009
Comparison: Midwest_Northeast_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 167
Wilcoxon Test P-value: 0.0115501
Adjusted p-value: 0.02989437
Comparison: Southeast_Northeast_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 1223
Wilcoxon Test P-value: 0.07527204
Adjusted p-value: 0.1419416
Comparison: Midwest_Southeast_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 23418
Wilcoxon Test P-value: 0.8906342
Adjusted p-value: 0.9254175
Comparison: Midwest_Northeast_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 2042
Wilcoxon Test P-value: 0.9734644
Adjusted p-value: 0.9808954
Comparison: Southeast_Northeast_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 7059
Wilcoxon Test P-value: 0.6487875
Adjusted p-value: 0.7319654
Comparison: Midwest_Southeast_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 6181
Wilcoxon Test P-value: 6.301401e-06
Adjusted p-value: 3.78084e-05
Comparison: Midwest_Northeast_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1327
Wilcoxon Test P-value: 1.076335e-08
Adjusted p-value: 1.092894e-07
Comparison: Southeast_Northeast_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 5760
Wilcoxon Test P-value: 5.463243e-08
Adjusted p-value: 4.507176e-07
Comparison: Midwest_Southeast_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 17244
Wilcoxon Test P-value: 2.75167e-05
Adjusted p-value: 0.0001397002
Comparison: Midwest_Northeast_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2100
Wilcoxon Test P-value: 0.7714325
Adjusted p-value: 0.8457171
Comparison: Southeast_Northeast_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 9977
Wilcoxon Test P-value: 0.000521743
Adjusted p-value: 0.001967716
Comparison: Midwest_Southeast_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 22814
Wilcoxon Test P-value: 0.7752407
Adjusted p-value: 0.8457171
Comparison: Midwest_Northeast_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 660
Wilcoxon Test P-value: 1.11681e-09
Adjusted p-value: 1.340172e-08
Comparison: Southeast_Northeast_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 1965
Wilcoxon Test P-value: 2.684232e-13
Adjusted p-value: 7.086373e-12
Comparison: Midwest_Southeast_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 25190
Wilcoxon Test P-value: 0.4709207
Adjusted p-value: 0.5702893
Comparison: Midwest_Northeast_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 4627
Wilcoxon Test P-value: 0.68439
Adjusted p-value: 0.7655889
Comparison: Southeast_Northeast_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 10720
Wilcoxon Test P-value: 0.2927494
Adjusted p-value: 0.3903326
Comparison: Midwest_Southeast_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 13867
Wilcoxon Test P-value: 3.406871e-14
Adjusted p-value: 1.124268e-12
Comparison: Midwest_Northeast_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 3810
Wilcoxon Test P-value: 5.755428e-05
Adjusted p-value: 0.0002813765
Comparison: Southeast_Northeast_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 7368
Wilcoxon Test P-value: 0.9417615
Adjusted p-value: 0.9562501
Comparison: Midwest_Southeast_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 20250
Wilcoxon Test P-value: 0.003754155
Adjusted p-value: 0.01126247
Comparison: Midwest_Northeast_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 6015
Wilcoxon Test P-value: 0.002886071
Adjusted p-value: 0.008859566
Comparison: Southeast_Northeast_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 17718
Wilcoxon Test P-value: 2.82595e-11
Adjusted p-value: 5.328934e-10
Comparison: Midwest_Southeast_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 21499
Wilcoxon Test P-value: 0.0445183
Adjusted p-value: 0.09040639
Comparison: Midwest_Northeast_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 5808
Wilcoxon Test P-value: 0.01335111
Adjusted p-value: 0.03263604
Comparison: Southeast_Northeast_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 15504
Wilcoxon Test P-value: 2.735836e-05
Adjusted p-value: 0.0001397002
Comparison: Midwest_Southeast_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 41139
Wilcoxon Test P-value: 0.0255574
Adjusted p-value: 0.05622629
Comparison: Midwest_Northeast_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 1696.5
Wilcoxon Test P-value: 0.1220123
Adjusted p-value: 0.2147416
Comparison: Southeast_Northeast_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 4290
Wilcoxon Test P-value: 0.01711732
Adjusted p-value: 0.04108156
Comparison: Midwest_Southeast_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 20333
Wilcoxon Test P-value: 2.144748e-14
Adjusted p-value: 9.43689e-13
Comparison: Midwest_Northeast_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1375
Wilcoxon Test P-value: 0.2092982
Adjusted p-value: 0.3172911
Comparison: Southeast_Northeast_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 2042
Wilcoxon Test P-value: 9.492162e-05
Adjusted p-value: 0.0004474877
Comparison: Midwest_Southeast_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 32383
Wilcoxon Test P-value: 0.02375285
Adjusted p-value: 0.05405821
Comparison: Midwest_Northeast_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1558
Wilcoxon Test P-value: 0.03686767
Adjusted p-value: 0.07603958
Comparison: Southeast_Northeast_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 5074
Wilcoxon Test P-value: 0.2106881
Adjusted p-value: 0.3172911
Comparison: Midwest_Southeast_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 33093
Wilcoxon Test P-value: 0.05787306
Adjusted p-value: 0.1157461
Comparison: Midwest_Northeast_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 1539
Wilcoxon Test P-value: 0.03066126
Adjusted p-value: 0.06527881
Comparison: Southeast_Northeast_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 5132
Wilcoxon Test P-value: 0.2428498
Adjusted p-value: 0.3484366
Comparison: Midwest_Southeast_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 34464
Wilcoxon Test P-value: 0.02424487
Adjusted p-value: 0.05424276
Comparison: Midwest_Northeast_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 3013.5
Wilcoxon Test P-value: 0.1128031
Adjusted p-value: 0.2039728
Comparison: Southeast_Northeast_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 4185
Wilcoxon Test P-value: 0.004693128
Adjusted p-value: 0.0131807
Comparison: Midwest_Southeast_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 20027
Wilcoxon Test P-value: 4.499384e-13
Adjusted p-value: 9.898644e-12
Comparison: Midwest_Northeast_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 2017
Wilcoxon Test P-value: 0.1353588
Adjusted p-value: 0.2261692
Comparison: Southeast_Northeast_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1439
Wilcoxon Test P-value: 1.042922e-09
Adjusted p-value: 1.340172e-08
Comparison: Midwest_Southeast_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 25012
Wilcoxon Test P-value: 0.0003495631
Adjusted p-value: 0.001357127
Comparison: Midwest_Northeast_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2192
Wilcoxon Test P-value: 0.0001564536
Adjusted p-value: 0.000688396
Comparison: Southeast_Northeast_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 4550
Wilcoxon Test P-value: 0.02739236
Adjusted p-value: 0.05927527
Comparison: Midwest_Southeast_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 25682
Wilcoxon Test P-value: 0.001565332
Adjusted p-value: 0.005298047
Comparison: Midwest_Northeast_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 1691
Wilcoxon Test P-value: 3.067943e-07
Adjusted p-value: 2.382168e-06
Comparison: Southeast_Northeast_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 3730
Wilcoxon Test P-value: 0.0003156382
Adjusted p-value: 0.001262553
Comparison: Midwest_Southeast_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 33605
Wilcoxon Test P-value: 0.0006905474
Adjusted p-value: 0.002532007
Comparison: Midwest_Northeast_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 1136
Wilcoxon Test P-value: 0.07693033
Adjusted p-value: 0.1430254
Comparison: Southeast_Northeast_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 1636
Wilcoxon Test P-value: 0.3174314
Adjusted p-value: 0.4148608
Comparison: Midwest_Southeast_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 14440
Wilcoxon Test P-value: 1.969954e-09
Adjusted p-value: 2.16695e-08
Comparison: Midwest_Northeast_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 631
Wilcoxon Test P-value: 0.2114133
Adjusted p-value: 0.3172911
Comparison: Southeast_Northeast_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 662
Wilcoxon Test P-value: 0.3336877
Adjusted p-value: 0.4318311
Comparison: Midwest_Southeast_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 23670
Wilcoxon Test P-value: 0.001543212
Adjusted p-value: 0.005298047
Comparison: Midwest_Northeast_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 941
Wilcoxon Test P-value: 0.5514352
Adjusted p-value: 0.6441544
Comparison: Southeast_Northeast_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1782
Wilcoxon Test P-value: 0.1275942
Adjusted p-value: 0.2212087
Comparison: Midwest_Southeast_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 26248
Wilcoxon Test P-value: 0.1382643
Adjusted p-value: 0.22787
Comparison: Midwest_Northeast_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 1031
Wilcoxon Test P-value: 0.2555021
Adjusted p-value: 0.3587902
Comparison: Southeast_Northeast_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 1701
Wilcoxon Test P-value: 0.2217429
Adjusted p-value: 0.3288772
Comparison: Midwest_Southeast_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 35207
Wilcoxon Test P-value: 0.000146945
Adjusted p-value: 0.000668853
Comparison: Midwest_Northeast_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 1269.5
Wilcoxon Test P-value: 0.9364584
Adjusted p-value: 0.9562501
Comparison: Southeast_Northeast_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 1458
Wilcoxon Test P-value: 0.1398293
Adjusted p-value: 0.22787
Comparison: Midwest_Southeast_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 15548
Wilcoxon Test P-value: 7.000339e-11
Adjusted p-value: 1.026716e-09
Comparison: Midwest_Northeast_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 900
Wilcoxon Test P-value: 0.1290384
Adjusted p-value: 0.2212087
Comparison: Southeast_Northeast_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1071
Wilcoxon Test P-value: 0.3509276
Adjusted p-value: 0.4459361
Comparison: Midwest_Southeast_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 26861
Wilcoxon Test P-value: 0.116209
Adjusted p-value: 0.2072918
Comparison: Midwest_Northeast_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 918
Wilcoxon Test P-value: 0.0839735
Adjusted p-value: 0.1539514
Comparison: Southeast_Northeast_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1335
Wilcoxon Test P-value: 0.06196501
Adjusted p-value: 0.1220803
Comparison: Midwest_Southeast_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 27098
Wilcoxon Test P-value: 0.1560558
Adjusted p-value: 0.2512117
Comparison: Midwest_Northeast_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 678
Wilcoxon Test P-value: 0.004322514
Adjusted p-value: 0.01267937
Comparison: Southeast_Northeast_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 1133
Wilcoxon Test P-value: 0.01216816
Adjusted p-value: 0.03088841
Comparison: Midwest_Southeast_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 28898
Wilcoxon Test P-value: 6.717948e-06
Adjusted p-value: 3.855518e-05
Comparison: Midwest_Northeast_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 699
Wilcoxon Test P-value: 0.8973745
Adjusted p-value: 0.9254175
Comparison: Southeast_Northeast_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 634
Wilcoxon Test P-value: 0.2827764
Adjusted p-value: 0.3830504
Comparison: Midwest_Southeast_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 14982
Wilcoxon Test P-value: 4.231015e-15
Adjusted p-value: 2.79247e-13
Comparison: Midwest_Northeast_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 636
Wilcoxon Test P-value: 0.004891504
Adjusted p-value: 0.01345164
Comparison: Southeast_Northeast_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 755
Wilcoxon Test P-value: 0.2843859
Adjusted p-value: 0.3830504
Comparison: Midwest_Southeast_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 26048
Wilcoxon Test P-value: 0.02176719
Adjusted p-value: 0.05040823
Comparison: Midwest_Northeast_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 513
Wilcoxon Test P-value: 0.2736704
Adjusted p-value: 0.3802578
Comparison: Southeast_Northeast_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 579
Wilcoxon Test P-value: 0.1701525
Adjusted p-value: 0.270604
Comparison: Midwest_Southeast_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 24708
Wilcoxon Test P-value: 0.2090245
Adjusted p-value: 0.3172911
Comparison: Midwest_Northeast_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 592
Wilcoxon Test P-value: 0.567223
Adjusted p-value: 0.6510733
Comparison: Southeast_Northeast_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 677
Wilcoxon Test P-value: 0.4001093
Adjusted p-value: 0.4982493
Comparison: Midwest_Southeast_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 17375
Wilcoxon Test P-value: 0.1308836
Adjusted p-value: 0.2214953
Comparison: Midwest_Northeast_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 1152
Wilcoxon Test P-value: 0.4792282
Adjusted p-value: 0.5750738
Comparison: Southeast_Northeast_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 1155
Wilcoxon Test P-value: 0.8413913
Adjusted p-value: 0.8956746
Comparison: Midwest_Southeast_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 11207
Wilcoxon Test P-value: 3.967106e-08
Adjusted p-value: 3.740414e-07
Comparison: Midwest_Northeast_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1026
Wilcoxon Test P-value: 0.0001927083
Adjusted p-value: 0.0007962893
Comparison: Southeast_Northeast_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1227
Wilcoxon Test P-value: 0.06615038
Adjusted p-value: 0.1265486
Comparison: Midwest_Southeast_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 14755
Wilcoxon Test P-value: 0.2387929
Adjusted p-value: 0.3484366
Comparison: Midwest_Northeast_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 880
Wilcoxon Test P-value: 0.4120516
Adjusted p-value: 0.5047909
Comparison: Southeast_Northeast_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 990
Wilcoxon Test P-value: 0.5141947
Adjusted p-value: 0.6060152
Comparison: Midwest_Southeast_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 14097
Wilcoxon Test P-value: 0.06380964
Adjusted p-value: 0.1238658
Comparison: Midwest_Northeast_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 847
Wilcoxon Test P-value: 0.3142481
Adjusted p-value: 0.4148075
Comparison: Southeast_Northeast_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 1018
Wilcoxon Test P-value: 0.6123074
Adjusted p-value: 0.6967636
Comparison: Midwest_Southeast_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 14722
Wilcoxon Test P-value: 0.5598755
Adjusted p-value: 0.6482769
Comparison: Midwest_Northeast_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 2129.5
Wilcoxon Test P-value: 0.01322865
Adjusted p-value: 0.03263604
Comparison: Southeast_Northeast_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 2959
Wilcoxon Test P-value: 0.002463457
Adjusted p-value: 0.007742294
Comparison: Midwest_Southeast_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 10283
Wilcoxon Test P-value: 4.750791e-11
Adjusted p-value: 7.838805e-10
Comparison: Midwest_Northeast_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1722
Wilcoxon Test P-value: 5.140668e-08
Adjusted p-value: 4.507176e-07
Comparison: Southeast_Northeast_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 2435
Wilcoxon Test P-value: 1.926582e-05
Adjusted p-value: 0.000105962
Comparison: Midwest_Southeast_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 15532
Wilcoxon Test P-value: 0.7878298
Adjusted p-value: 0.8524061
Comparison: Midwest_Northeast_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1550
Wilcoxon Test P-value: 0.8324974
Adjusted p-value: 0.8934118
Comparison: Southeast_Northeast_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2019
Wilcoxon Test P-value: 0.7452442
Adjusted p-value: 0.8266574
Comparison: Midwest_Southeast_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 10767
Wilcoxon Test P-value: 2.044214e-06
Adjusted p-value: 1.349181e-05
Comparison: Midwest_Northeast_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 1797
Wilcoxon Test P-value: 0.3513436
Adjusted p-value: 0.4459361
Comparison: Southeast_Northeast_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 2907
Wilcoxon Test P-value: 0.004481354
Adjusted p-value: 0.01285954
Four analyte median + IQR plots 2012-2022 + counts and arrows (Turkey Solid )
Code
# Filter out rows with non-missing Total_N values and for specific regionsMDB_turkeystackNPK_filtered <- MDB_turkeystackNPK[!is.na(MDB_turkeystackNPK$Total_N) & MDB_turkeystackNPK$Region %in%c("Southeast", "Midwest"), ]custom_theme <-theme_minimal() +theme(text =element_text(family ="Times New Roman", size =11),axis.text.x =element_text(angle =45, hjust =1) )# Create separate plots for each nutrient typeplots <-list()# Initialize variables to store the minimum and maximum values of lower and upper quartilesmin_lower_quantile <-Infmax_upper_quantile <--Inf# Function to calculate counts for each nutrientcalculate_counts <-function(data) { counts <- data %>%filter(!is.na(Total_N) & Year.Analyzed >=2012& Year.Analyzed <=2022& Region !="Northeast") %>%group_by(Region) %>%summarise(total_samples =n()) %>%ungroup() # Remove grouping to avoid issues with plottingreturn(counts)}for (nutrient inc("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {# Filter data for the current nutrient type filtered_data <- MDB_turkeystackNPK_filtered[!is.na(MDB_turkeystackNPK_filtered[[nutrient]]), ]# Calculate median and IQR for each combination of Year.Analyzed and Region summary_stats <- filtered_data %>%group_by(Year.Analyzed, Region) %>%summarise(median_value =median(.data[[nutrient]]),lower_quantile =quantile(.data[[nutrient]], 0.25),upper_quantile =quantile(.data[[nutrient]], 0.75)) %>%ungroup() # Remove grouping to avoid issues with plotting# Update minimum and maximum values of lower and upper quartiles min_lower_quantile <-min(min_lower_quantile, min(summary_stats$lower_quantile)) max_upper_quantile <-max(max_upper_quantile, max(summary_stats$upper_quantile))# Determine a suitable step size for the y-axis y_range <- max_upper_quantile - min_lower_quantile num_breaks <-max(4, floor(y_range *10)) # Ensure at least four breaks step_size <- y_range / num_breaks# Calculate counts for all regions counts <-calculate_counts(filtered_data)# Create line graph with median for each year and region, with connecting lines plot <-ggplot(summary_stats, aes(x =factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha =0.3) +# Add shaded area for IQRgeom_line(aes(linetype = Region)) +# Add line graph with connecting linesgeom_point() +# Add points for each data pointlabs(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 settingsscale_color_manual(values =c("Northeast"="darkgreen", "Southeast"="red", "Midwest"="blue")) +# Define colors for regionsscale_fill_manual(values =c("Northeast"="lightgreen", "Southeast"="lightpink", "Midwest"="lightblue")) +# Define fill colors for IQRguides(fill =FALSE) +# Remove legend for IQR fill colorsscale_y_continuous(limits =c(0, 3.5), breaks =seq(0, 3.5, by =1)) +geom_text(data = counts, aes(label =paste("n =", total_samples),x ="2021", y =1.6- (0.3*seq_along(total_samples)), color = Region),hjust =1, vjust =0, size =3, family ="Times New Roman") # Add text annotations for counts# Add the arrow only for the Ammonium.N graphif (nutrient =="Phosphorus") { plot <- plot +annotate("segment", x ="2022", y =1.2, xend ="2022", yend =1, arrow =arrow(type ="open", length =unit(0.1, "inches")), color ="red") }if (nutrient =="Phosphorus") { plot <- plot +annotate("segment", x ="2022", y =1.25, xend ="2022", yend =1.5, arrow =arrow(type ="open", length =unit(0.1, "inches")), color ="blue") }# Add the plot to the list plots[[nutrient]] <- plot}# Arrange the plots together using patchworkturkeystack_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] +plot_layout(guides ="collect")# Print the final plotprint(turkeystack_final_plot)
Table of samples by region by year for Beef Liquid manure
Code
# Sort out for beef while still having separate NPK columnsMDB_beefpumpNPK <-filter(MDB_pump3, Animal.Type.Combined.Category =="Beef")# Create a table of number of samples for each region per yearsample_table <-table(MDB_beefpumpNPK$Region, MDB_beefpumpNPK$Year.Analyzed)# Print the sample tableprint(sample_table)
Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Beef Liquid)
Code
# Load required librarylibrary(Kendall)# List of selected regionsselected_regions <-c("Midwest")# List of analytesanalytes <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Function to perform Mann-Kendall trend test for a given analyte across all selected regionsperform_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 resultreturn(summary(result_mk_combined))}# Perform Mann-Kendall trend test for each analyte combined across selected regionsfor (analyte in analytes) {cat("Analyzing", analyte, "for all regions combined:\n")print(perform_mk_test_combined(MDB_beefpumpNPK, analyte, selected_regions))cat("\n")}
Analyzing Total_N for all regions combined:
Score = 17 , Var(Score) = 165
denominator = 55
tau = 0.309, 2-sided pvalue =0.21291
NULL
Analyzing Ammonium.N for all regions combined:
Score = 3 , Var(Score) = 165
denominator = 55
tau = 0.0545, 2-sided pvalue =0.87627
NULL
Analyzing Phosphorus for all regions combined:
Score = 15 , Var(Score) = 163
denominator = 53.99074
tau = 0.278, 2-sided pvalue =0.27283
NULL
Analyzing Potassium for all regions combined:
Score = 9 , Var(Score) = 163
denominator = 53.99074
tau = 0.167, 2-sided pvalue =0.53092
NULL
Code
# Function to perform Mann-Kendall trend testperform_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 resultreturn(summary(result_mk))}# Iterate over selected regions and analytesfor (region in selected_regions) {for (analyte in analytes) {# Subset data for the current region region_data <- MDB_beefpumpNPK[MDB_beefpumpNPK$Region == region, ]# Print information about the current region and analytecat("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 resultprint(result_summary)cat("\n") }}
Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Beef Liquid)
Code
# Filter data for selected regionsselected_regions <-c("Midwest", "Southeast")filtered_data <- MDB_beefpumpNPK[MDB_beefpumpNPK$Region %in% selected_regions, ]# Initialize an empty list to store cleaned data for each nutrientcleaned_data_list <-list()# Loop over each nutrientfor (nutrient inc("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 Correctionperform_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 resultsreturn(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))}# Iterate Over Years, Regions, and Nutrientsyears <-unique(filtered_data$Year.Analyzed)regions <-unique(filtered_data$Region)nutrients <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Initialize empty vectorsall_p_values <-c()comparison_results <-list()comparison_names <-c()# Loop over years, regions, and nutrients, perform tests, and gather p-valuesindex <-1for (year in years) {for (nutrient in nutrients) {for (i in1:(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 yearsorted_indices <-order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))sorted_comparison_names <- comparison_names[sorted_indices]# Apply Benjamini-Hochberg Procedureadjusted_p_values <-p.adjust(all_p_values, method ="BH")# Print results in chronological orderfor (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-valuescat("Comparison:", name, "\n")cat("Year:", year, "\n")cat("Nutrient:", nutrient, "\n")cat("Wilcoxon Test Statistic:", result$statistic, "\n")cat("Wilcoxon Test P-value:", result$p.value, "\n")cat("Adjusted p-value:", adjusted_p_values[index], "\n")cat("\n")}
Comparison: Midwest_Southeast_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 2777
Wilcoxon Test P-value: 1.528612e-12
Adjusted p-value: 2.241964e-11
Comparison: Midwest_Southeast_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 628
Wilcoxon Test P-value: 5.33411e-07
Adjusted p-value: 1.805391e-06
Comparison: Midwest_Southeast_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2297
Wilcoxon Test P-value: 1.520691e-05
Adjusted p-value: 3.521599e-05
Comparison: Midwest_Southeast_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 2016
Wilcoxon Test P-value: 0.006551099
Adjusted p-value: 0.008734799
Comparison: Midwest_Southeast_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 5250
Wilcoxon Test P-value: 6.291119e-07
Adjusted p-value: 1.977209e-06
Comparison: Midwest_Southeast_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 527
Wilcoxon Test P-value: 0.0628645
Adjusted p-value: 0.07092405
Comparison: Midwest_Southeast_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 4604
Wilcoxon Test P-value: 0.0009975354
Adjusted p-value: 0.001512298
Comparison: Midwest_Southeast_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 4798
Wilcoxon Test P-value: 0.0001476697
Adjusted p-value: 0.0002824986
Comparison: Midwest_Southeast_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 6928
Wilcoxon Test P-value: 1.347364e-07
Adjusted p-value: 8.469145e-07
Comparison: Midwest_Southeast_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 916
Wilcoxon Test P-value: 0.03521129
Adjusted p-value: 0.04077097
Comparison: Midwest_Southeast_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 5597
Wilcoxon Test P-value: 0.0148054
Adjusted p-value: 0.01915993
Comparison: Midwest_Southeast_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 5995
Wilcoxon Test P-value: 0.001031112
Adjusted p-value: 0.001512298
Comparison: Midwest_Southeast_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 8358
Wilcoxon Test P-value: 9.506481e-11
Adjusted p-value: 1.045713e-09
Comparison: Midwest_Southeast_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 768
Wilcoxon Test P-value: 0.2700855
Adjusted p-value: 0.2829467
Comparison: Midwest_Southeast_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 7721
Wilcoxon Test P-value: 2.178664e-07
Adjusted p-value: 8.714657e-07
Comparison: Midwest_Southeast_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 7814
Wilcoxon Test P-value: 7.859056e-08
Adjusted p-value: 5.763308e-07
Comparison: Midwest_Southeast_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 11126
Wilcoxon Test P-value: 2.30375e-14
Adjusted p-value: 1.01365e-12
Comparison: Midwest_Southeast_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1575
Wilcoxon Test P-value: 0.004433569
Adjusted p-value: 0.006096157
Comparison: Midwest_Southeast_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 9211
Wilcoxon Test P-value: 7.07104e-06
Adjusted p-value: 1.830152e-05
Comparison: Midwest_Southeast_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 9410
Wilcoxon Test P-value: 1.457761e-06
Adjusted p-value: 4.2761e-06
Comparison: Midwest_Southeast_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 7898
Wilcoxon Test P-value: 1.378193e-08
Adjusted p-value: 1.21281e-07
Comparison: Midwest_Southeast_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 710
Wilcoxon Test P-value: 0.1473867
Adjusted p-value: 0.1581711
Comparison: Midwest_Southeast_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 7164
Wilcoxon Test P-value: 8.541013e-05
Adjusted p-value: 0.0001708203
Comparison: Midwest_Southeast_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 7242
Wilcoxon Test P-value: 4.648508e-05
Adjusted p-value: 9.739732e-05
Comparison: Midwest_Southeast_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 6381
Wilcoxon Test P-value: 1.964344e-07
Adjusted p-value: 8.714657e-07
Comparison: Midwest_Southeast_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 628
Wilcoxon Test P-value: 0.1165849
Adjusted p-value: 0.1282434
Comparison: Midwest_Southeast_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 6024
Wilcoxon Test P-value: 1.079385e-05
Adjusted p-value: 2.638498e-05
Comparison: Midwest_Southeast_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 5621
Wilcoxon Test P-value: 0.0004709772
Adjusted p-value: 0.0007675184
Comparison: Midwest_Southeast_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 7658
Wilcoxon Test P-value: 2.04494e-07
Adjusted p-value: 8.714657e-07
Comparison: Midwest_Southeast_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1156
Wilcoxon Test P-value: 0.02278816
Adjusted p-value: 0.02785219
Comparison: Midwest_Southeast_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 6524
Wilcoxon Test P-value: 0.001797968
Adjusted p-value: 0.002551955
Comparison: Midwest_Southeast_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 6138
Wilcoxon Test P-value: 0.01570584
Adjusted p-value: 0.01974449
Comparison: Midwest_Southeast_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 8482
Wilcoxon Test P-value: 5.502264e-13
Adjusted p-value: 1.210498e-11
Comparison: Midwest_Southeast_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 2133
Wilcoxon Test P-value: 3.149917e-06
Adjusted p-value: 8.662272e-06
Comparison: Midwest_Southeast_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 6931
Wilcoxon Test P-value: 3.31472e-05
Adjusted p-value: 7.292385e-05
Comparison: Midwest_Southeast_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 6570
Wilcoxon Test P-value: 0.0005863852
Adjusted p-value: 0.0009214625
Comparison: Midwest_Southeast_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 5594
Wilcoxon Test P-value: 2.174864e-07
Adjusted p-value: 8.714657e-07
Comparison: Midwest_Southeast_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 414
Wilcoxon Test P-value: 0.03204478
Adjusted p-value: 0.0381073
Comparison: Midwest_Southeast_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 4953
Wilcoxon Test P-value: 0.0002071057
Adjusted p-value: 0.0003796938
Comparison: Midwest_Southeast_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 4939
Wilcoxon Test P-value: 0.0002348743
Adjusted p-value: 0.0003974795
Comparison: Midwest_Southeast_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 3890
Wilcoxon Test P-value: 3.975484e-07
Adjusted p-value: 1.457677e-06
Comparison: Midwest_Southeast_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1710
Wilcoxon Test P-value: 0.000216527
Adjusted p-value: 0.0003810875
Comparison: Midwest_Southeast_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2173
Wilcoxon Test P-value: 0.9607369
Adjusted p-value: 0.9607369
Comparison: Midwest_Southeast_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 2258
Wilcoxon Test P-value: 0.8403874
Adjusted p-value: 0.8599313
Four analyte median + IQR plots 2012-2022 (Beef Liquid)
Code
# Filter out rows with non-missing Total_N values and for specific regionsMDB_beefpumpNPK_filtered <- MDB_beefpumpNPK[!is.na(MDB_beefpumpNPK$Total_N) & MDB_beefpumpNPK$Region %in%c("Midwest"), ]custom_theme <-theme_minimal() +theme(text =element_text(family ="Times New Roman", size =11),axis.text.x =element_text(angle =45, hjust =1) )# Create separate plots for each nutrient typeplots <-list()# Initialize variables to store the minimum and maximum values of lower and upper quartilesmin_lower_quantile <-Infmax_upper_quantile <--Inf# Function to calculate counts for each nutrientcalculate_counts <-function(data) { counts <- data %>%filter(!is.na(Total_N) & Year.Analyzed >=2012& Year.Analyzed <=2022) %>%group_by(Region) %>%filter(Region =="Midwest") %>%# Filter by Midwest region onlysummarise(total_samples =n()) %>%ungroup() # Remove grouping to avoid issues with plottingreturn(counts)}for (nutrient inc("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {# Filter data for the current nutrient type filtered_data <- MDB_beefpumpNPK_filtered[!is.na(MDB_beefpumpNPK_filtered[[nutrient]]), ]# Calculate median and IQR for each combination of Year.Analyzed and Region summary_stats <- filtered_data %>%group_by(Year.Analyzed, Region) %>%summarise(median_value =median(.data[[nutrient]]),lower_quantile =quantile(.data[[nutrient]], 0.25),upper_quantile =quantile(.data[[nutrient]], 0.75)) %>%ungroup() # Remove grouping to avoid issues with plotting# Update minimum and maximum values of lower and upper quartiles min_lower_quantile <-min(min_lower_quantile, min(summary_stats$lower_quantile)) max_upper_quantile <-max(max_upper_quantile, max(summary_stats$upper_quantile))# Determine a suitable step size for the y-axis y_range <- max_upper_quantile - min_lower_quantile num_breaks <-max(4, floor(y_range *10)) # Ensure at least four breaks step_size <- y_range / num_breaks# Calculate counts for all regions counts <-calculate_counts(filtered_data)# Create line graph with median for each year and region, with connecting lines plot <-ggplot(summary_stats, aes(x =factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha =0.3) +# Add shaded area for IQRgeom_line(aes(linetype = Region)) +# Add line graph with connecting linesgeom_point() +# Add points for each data pointlabs(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 settingsscale_color_manual(values =c("Midwest"="blue")) +# Define color for Midwest region onlyscale_fill_manual(values =c("Midwest"="lightblue")) +# Define fill color for IQR of Midwest region onlyguides(fill =FALSE) +# Remove legend for IQR fill colorsscale_y_continuous(limits =c(0, .75), breaks =seq(0, .75, by =0.1)) +geom_text(data = counts, aes(label =paste("n =", total_samples),x ="2022", y =0.75- (0.08*seq_along(total_samples)), color = Region),hjust =1, vjust =0, size =3, family ="Times New Roman") # Add text annotations for counts# Add the plot to the list plots[[nutrient]] <- plot}# Arrange the plots together using patchworkbeefpump_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] +plot_layout(guides ="collect")# Print the final plotprint(beefpump_final_plot)
#### Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately dairy liquid# Sort out for dairy while still having separate NPK columnsMDB_dairypumpNPK <-filter(MDB_pump3, Animal.Type.Combined.Category =="Dairy")# Load required librarylibrary(Kendall)# List of selected regionsselected_regions <-c("Midwest", "Northeast", "Southeast")# List of analytesanalytes <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Function to perform Mann-Kendall trend test for a given analyte across all selected regionsperform_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 resultreturn(summary(result_mk_combined))}# Perform Mann-Kendall trend test for each analyte combined across selected regionsfor (analyte in analytes) {cat("Analyzing", analyte, "for all regions combined:\n")print(perform_mk_test_combined(MDB_dairypumpNPK, analyte, selected_regions))cat("\n")}
Analyzing Total_N for all regions combined:
Score = 33 , Var(Score) = 156.3333
denominator = 51.91339
tau = 0.636, 2-sided pvalue =0.010488
NULL
Analyzing Ammonium.N for all regions combined:
Score = 21 , Var(Score) = 163
denominator = 53.99074
tau = 0.389, 2-sided pvalue =0.11723
NULL
Analyzing Phosphorus for all regions combined:
Score = 15 , Var(Score) = 156.3333
denominator = 51.91339
tau = 0.289, 2-sided pvalue =0.26284
NULL
Analyzing Potassium for all regions combined:
Score = 24 , Var(Score) = 147.3333
denominator = 49.1935
tau = 0.488, 2-sided pvalue =0.058111
NULL
Code
# Function to perform Mann-Kendall trend testperform_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 resultreturn(summary(result_mk))}# Iterate over selected regions and analytesfor (region in selected_regions) {for (analyte in analytes) {# Subset data for the current region region_data <- MDB_dairypumpNPK[MDB_dairypumpNPK$Region == region, ]# Print information about the current region and analytecat("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 resultprint(result_summary)cat("\n") }}
Table of samples by region by year for Dairy Liquid manure
Code
# Sort out for beef while still having separate NPK columnsMDB_dairypumpNPK <-filter(MDB_pump3, Animal.Type.Combined.Category =="Dairy")# Create a table of number of samples for each region per yearsample_table <-table(MDB_dairypumpNPK$Region, MDB_dairypumpNPK$Year.Analyzed)# Print the sample tableprint(sample_table)
Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Dairy Liquid)
Code
# Filter data for selected regionsselected_regions <-c("Midwest", "Northeast", "Southeast")filtered_data <- MDB_dairypumpNPK[MDB_dairypumpNPK$Region %in% selected_regions, ]# Initialize an empty list to store cleaned data for each nutrientcleaned_data_list <-list()# Loop over each nutrientfor (nutrient inc("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 Correctionperform_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 resultsreturn(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))}# Iterate Over Years, Regions, and Nutrientsyears <-unique(filtered_data$Year.Analyzed)regions <-unique(filtered_data$Region)nutrients <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Initialize empty vectorsall_p_values <-c()comparison_results <-list()comparison_names <-c()# Loop over years, regions, and nutrients, perform tests, and gather p-valuesindex <-1for (year in years) {for (nutrient in nutrients) {for (i in1:(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 yearsorted_indices <-order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))sorted_comparison_names <- comparison_names[sorted_indices]# Apply Benjamini-Hochberg Procedureadjusted_p_values <-p.adjust(all_p_values, method ="BH")# Print results in chronological orderfor (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-valuescat("Comparison:", name, "\n")cat("Year:", year, "\n")cat("Nutrient:", nutrient, "\n")cat("Wilcoxon Test Statistic:", result$statistic, "\n")cat("Wilcoxon Test P-value:", result$p.value, "\n")cat("Adjusted p-value:", adjusted_p_values[index], "\n")cat("\n")}
Comparison: Northeast_Midwest_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 272032.5
Wilcoxon Test P-value: 2.180234e-13
Adjusted p-value: 3.346405e-13
Comparison: Northeast_Southeast_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 146490.5
Wilcoxon Test P-value: 6.643084e-50
Adjusted p-value: 1.789566e-49
Comparison: Midwest_Southeast_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 684350
Wilcoxon Test P-value: 8.1338e-83
Adjusted p-value: 4.294647e-82
Comparison: Northeast_Midwest_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 136801.5
Wilcoxon Test P-value: 0.1746292
Adjusted p-value: 0.1905045
Comparison: Northeast_Southeast_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 92109
Wilcoxon Test P-value: 1.057232e-83
Adjusted p-value: 5.814775e-83
Comparison: Midwest_Southeast_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 279468.5
Wilcoxon Test P-value: 1.296574e-136
Adjusted p-value: 2.139347e-135
Comparison: Northeast_Midwest_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 258753.5
Wilcoxon Test P-value: 8.421824e-09
Adjusted p-value: 1.12291e-08
Comparison: Northeast_Southeast_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 133056.5
Wilcoxon Test P-value: 1.100337e-06
Adjusted p-value: 1.438064e-06
Comparison: Midwest_Southeast_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 592716
Wilcoxon Test P-value: 0.001243408
Adjusted p-value: 0.00151972
Comparison: Northeast_Midwest_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 260497
Wilcoxon Test P-value: 2.223906e-09
Adjusted p-value: 2.995465e-09
Comparison: Northeast_Southeast_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 140499.5
Wilcoxon Test P-value: 5.524134e-11
Adjusted p-value: 7.925932e-11
Comparison: Midwest_Southeast_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 646751
Wilcoxon Test P-value: 1.898763e-12
Adjusted p-value: 2.846e-12
Comparison: Northeast_Midwest_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 556730
Wilcoxon Test P-value: 9.127792e-12
Adjusted p-value: 1.338743e-11
Comparison: Northeast_Southeast_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 227343
Wilcoxon Test P-value: 1.139598e-100
Adjusted p-value: 1.253558e-99
Comparison: Midwest_Southeast_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 1214863
Wilcoxon Test P-value: 2.247614e-170
Adjusted p-value: 2.966851e-168
Comparison: Northeast_Midwest_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 322473
Wilcoxon Test P-value: 1.729477e-06
Adjusted p-value: 2.238147e-06
Comparison: Northeast_Southeast_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 120009
Wilcoxon Test P-value: 1.194875e-96
Adjusted p-value: 1.213257e-95
Comparison: Midwest_Southeast_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 326767.5
Wilcoxon Test P-value: 1.081363e-45
Adjusted p-value: 2.693205e-45
Comparison: Northeast_Midwest_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 592947
Wilcoxon Test P-value: 0.0004675581
Adjusted p-value: 0.0005822422
Comparison: Northeast_Southeast_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 264211
Wilcoxon Test P-value: 1.120532e-38
Adjusted p-value: 2.506952e-38
Comparison: Midwest_Southeast_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1198566
Wilcoxon Test P-value: 3.414199e-57
Adjusted p-value: 9.797268e-57
Comparison: Northeast_Midwest_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 654934.5
Wilcoxon Test P-value: 8.766789e-14
Adjusted p-value: 1.377638e-13
Comparison: Northeast_Southeast_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 285162.5
Wilcoxon Test P-value: 8.213187e-61
Adjusted p-value: 2.710352e-60
Comparison: Midwest_Southeast_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 1290172
Wilcoxon Test P-value: 2.023515e-92
Adjusted p-value: 1.780693e-91
Comparison: Northeast_Midwest_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 533454.5
Wilcoxon Test P-value: 0.1597503
Adjusted p-value: 0.1757253
Comparison: Northeast_Southeast_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 214472
Wilcoxon Test P-value: 4.816523e-82
Adjusted p-value: 2.445312e-81
Comparison: Midwest_Southeast_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 1350410
Wilcoxon Test P-value: 5.602667e-166
Adjusted p-value: 3.69776e-164
Comparison: Northeast_Midwest_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 328037.5
Wilcoxon Test P-value: 0.2047964
Adjusted p-value: 0.2197815
Comparison: Northeast_Southeast_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 104686
Wilcoxon Test P-value: 1.857506e-88
Adjusted p-value: 1.167575e-87
Comparison: Midwest_Southeast_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 316561
Wilcoxon Test P-value: 3.247567e-37
Adjusted p-value: 6.914175e-37
Comparison: Northeast_Midwest_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 491679
Wilcoxon Test P-value: 0.216944
Adjusted p-value: 0.2309404
Comparison: Northeast_Southeast_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 189811
Wilcoxon Test P-value: 1.875458e-13
Adjusted p-value: 2.912476e-13
Comparison: Midwest_Southeast_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1232339
Wilcoxon Test P-value: 2.825664e-36
Adjusted p-value: 5.738272e-36
Comparison: Northeast_Midwest_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 505480
Wilcoxon Test P-value: 0.7175226
Adjusted p-value: 0.7285614
Comparison: Northeast_Southeast_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 219221
Wilcoxon Test P-value: 1.023268e-37
Adjusted p-value: 2.214285e-37
Comparison: Midwest_Southeast_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 1409132
Wilcoxon Test P-value: 5.581194e-92
Adjusted p-value: 4.604485e-91
Comparison: Northeast_Midwest_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 668912.5
Wilcoxon Test P-value: 1.962364e-09
Adjusted p-value: 2.670433e-09
Comparison: Northeast_Southeast_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 182621
Wilcoxon Test P-value: 6.319709e-71
Adjusted p-value: 2.527883e-70
Comparison: Midwest_Southeast_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 1354092
Wilcoxon Test P-value: 9.682675e-120
Adjusted p-value: 1.278113e-118
Comparison: Northeast_Midwest_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 423409
Wilcoxon Test P-value: 0.0001043432
Adjusted p-value: 0.0001311743
Comparison: Northeast_Southeast_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 94667.5
Wilcoxon Test P-value: 5.596549e-80
Adjusted p-value: 2.638373e-79
Comparison: Midwest_Southeast_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 360016
Wilcoxon Test P-value: 2.615288e-23
Adjusted p-value: 4.665109e-23
Comparison: Northeast_Midwest_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 566573
Wilcoxon Test P-value: 0.834267
Adjusted p-value: 0.834267
Comparison: Northeast_Southeast_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 191271.5
Wilcoxon Test P-value: 7.113767e-30
Adjusted p-value: 1.401518e-29
Comparison: Midwest_Southeast_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1443234
Wilcoxon Test P-value: 3.072157e-57
Adjusted p-value: 9.011661e-57
Comparison: Northeast_Midwest_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 611053.5
Wilcoxon Test P-value: 0.006405539
Adjusted p-value: 0.007617398
Comparison: Northeast_Southeast_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 199880
Wilcoxon Test P-value: 3.673333e-39
Adjusted p-value: 8.506665e-39
Comparison: Midwest_Southeast_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 1505215
Wilcoxon Test P-value: 1.312937e-75
Adjusted p-value: 5.776921e-75
Comparison: Northeast_Midwest_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 1102977
Wilcoxon Test P-value: 5.480711e-17
Adjusted p-value: 9.043173e-17
Comparison: Northeast_Southeast_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 214767.5
Wilcoxon Test P-value: 3.619027e-87
Adjusted p-value: 2.171416e-86
Comparison: Midwest_Southeast_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 2319830
Wilcoxon Test P-value: 6.89189e-159
Adjusted p-value: 2.274324e-157
Comparison: Northeast_Midwest_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 503007.5
Wilcoxon Test P-value: 4.864543e-18
Adjusted p-value: 8.128097e-18
Comparison: Northeast_Southeast_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 104888.5
Wilcoxon Test P-value: 6.88548e-89
Adjusted p-value: 4.544416e-88
Comparison: Midwest_Southeast_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 459610.5
Wilcoxon Test P-value: 8.712288e-39
Adjusted p-value: 1.982797e-38
Comparison: Northeast_Midwest_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1051043
Wilcoxon Test P-value: 3.810109e-10
Adjusted p-value: 5.294046e-10
Comparison: Northeast_Southeast_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 210384
Wilcoxon Test P-value: 6.321324e-29
Adjusted p-value: 1.227081e-28
Comparison: Midwest_Southeast_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2236535
Wilcoxon Test P-value: 9.137054e-44
Adjusted p-value: 2.192893e-43
Comparison: Northeast_Midwest_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 959379.5
Wilcoxon Test P-value: 0.00443825
Adjusted p-value: 0.005374762
Comparison: Northeast_Southeast_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 218476.5
Wilcoxon Test P-value: 9.327034e-37
Adjusted p-value: 1.954236e-36
Comparison: Midwest_Southeast_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 2479763
Wilcoxon Test P-value: 1.004813e-90
Adjusted p-value: 7.80208e-90
Comparison: Northeast_Midwest_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 1232833
Wilcoxon Test P-value: 9.74087e-07
Adjusted p-value: 1.285795e-06
Comparison: Northeast_Southeast_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 217730
Wilcoxon Test P-value: 5.76037e-81
Adjusted p-value: 2.816181e-80
Comparison: Midwest_Southeast_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 2178115
Wilcoxon Test P-value: 3.262386e-144
Adjusted p-value: 7.17725e-143
Comparison: Northeast_Midwest_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 616925.5
Wilcoxon Test P-value: 1.830791e-12
Adjusted p-value: 2.777751e-12
Comparison: Northeast_Southeast_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 111411
Wilcoxon Test P-value: 2.749769e-96
Adjusted p-value: 2.592639e-95
Comparison: Midwest_Southeast_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 435545.5
Wilcoxon Test P-value: 1.985272e-43
Adjusted p-value: 4.679569e-43
Comparison: Northeast_Midwest_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1181424
Wilcoxon Test P-value: 0.0006876605
Adjusted p-value: 0.0008483288
Comparison: Northeast_Southeast_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 208506.5
Wilcoxon Test P-value: 7.67265e-25
Adjusted p-value: 1.406653e-24
Comparison: Midwest_Southeast_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2037312
Wilcoxon Test P-value: 2.380524e-38
Adjusted p-value: 5.237153e-38
Comparison: Northeast_Midwest_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 1150406
Wilcoxon Test P-value: 0.02921266
Adjusted p-value: 0.03353105
Comparison: Northeast_Southeast_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 208343.5
Wilcoxon Test P-value: 1.051465e-24
Adjusted p-value: 1.901279e-24
Comparison: Midwest_Southeast_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 2115421
Wilcoxon Test P-value: 2.628892e-49
Adjusted p-value: 6.80419e-49
Comparison: Northeast_Midwest_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 991677
Wilcoxon Test P-value: 0.04589819
Adjusted p-value: 0.05222897
Comparison: Northeast_Southeast_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 201249
Wilcoxon Test P-value: 1.023626e-70
Adjusted p-value: 3.860534e-70
Comparison: Midwest_Southeast_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 2031212
Wilcoxon Test P-value: 1.147471e-148
Adjusted p-value: 3.029324e-147
Comparison: Northeast_Midwest_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 539427
Wilcoxon Test P-value: 7.145306e-10
Adjusted p-value: 9.824795e-10
Comparison: Northeast_Southeast_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 105424.5
Wilcoxon Test P-value: 1.636131e-78
Adjusted p-value: 7.447219e-78
Comparison: Midwest_Southeast_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 409975.5
Wilcoxon Test P-value: 1.265907e-35
Adjusted p-value: 2.531814e-35
Comparison: Northeast_Midwest_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 946114
Wilcoxon Test P-value: 0.7320608
Adjusted p-value: 0.7376491
Comparison: Northeast_Southeast_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 191185.5
Wilcoxon Test P-value: 6.478761e-21
Adjusted p-value: 1.110645e-20
Comparison: Midwest_Southeast_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1906529
Wilcoxon Test P-value: 1.182255e-44
Adjusted p-value: 2.889958e-44
Comparison: Northeast_Midwest_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 911543.5
Wilcoxon Test P-value: 0.311506
Adjusted p-value: 0.3263396
Comparison: Northeast_Southeast_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 190597.5
Wilcoxon Test P-value: 1.916404e-20
Adjusted p-value: 3.243145e-20
Comparison: Midwest_Southeast_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 1991038
Wilcoxon Test P-value: 5.802677e-60
Adjusted p-value: 1.868179e-59
Comparison: Northeast_Midwest_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 1133164
Wilcoxon Test P-value: 0.2033833
Adjusted p-value: 0.2197815
Comparison: Northeast_Southeast_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 194133
Wilcoxon Test P-value: 1.146031e-57
Adjusted p-value: 3.438092e-57
Comparison: Midwest_Southeast_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 2027145
Wilcoxon Test P-value: 1.344444e-139
Adjusted p-value: 2.535238e-138
Comparison: Northeast_Midwest_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 431030
Wilcoxon Test P-value: 0.4667242
Adjusted p-value: 0.4813094
Comparison: Northeast_Southeast_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 91799.5
Wilcoxon Test P-value: 5.38743e-68
Adjusted p-value: 1.922002e-67
Comparison: Midwest_Southeast_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 346579
Wilcoxon Test P-value: 6.519314e-87
Adjusted p-value: 3.741519e-86
Comparison: Northeast_Midwest_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1071879
Wilcoxon Test P-value: 0.004579875
Adjusted p-value: 0.00549585
Comparison: Northeast_Southeast_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 188383.5
Wilcoxon Test P-value: 1.104405e-14
Adjusted p-value: 1.777823e-14
Comparison: Midwest_Southeast_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2036193
Wilcoxon Test P-value: 8.185697e-56
Adjusted p-value: 2.298962e-55
Comparison: Northeast_Midwest_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 1116699
Wilcoxon Test P-value: 0.117724
Adjusted p-value: 0.1305846
Comparison: Northeast_Southeast_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 204575
Wilcoxon Test P-value: 2.286479e-27
Adjusted p-value: 4.374133e-27
Comparison: Midwest_Southeast_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 2204732
Wilcoxon Test P-value: 2.436573e-90
Adjusted p-value: 1.78682e-89
Comparison: Northeast_Midwest_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 1125887
Wilcoxon Test P-value: 0.3830358
Adjusted p-value: 0.398116
Comparison: Northeast_Southeast_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 148802.5
Wilcoxon Test P-value: 9.906137e-71
Adjusted p-value: 3.845912e-70
Comparison: Midwest_Southeast_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 2267005
Wilcoxon Test P-value: 1.772761e-159
Adjusted p-value: 7.800149e-158
Comparison: Northeast_Midwest_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 393544.5
Wilcoxon Test P-value: 1.043971e-10
Adjusted p-value: 1.481766e-10
Comparison: Northeast_Southeast_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 79095
Wilcoxon Test P-value: 4.683904e-63
Adjusted p-value: 1.585321e-62
Comparison: Midwest_Southeast_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 518768
Wilcoxon Test P-value: 3.440091e-101
Adjusted p-value: 4.128109e-100
Comparison: Northeast_Midwest_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1129930
Wilcoxon Test P-value: 0.6458615
Adjusted p-value: 0.6608815
Comparison: Northeast_Southeast_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 148541.5
Wilcoxon Test P-value: 4.142016e-22
Adjusted p-value: 7.194028e-22
Comparison: Midwest_Southeast_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 2211418
Wilcoxon Test P-value: 2.158591e-49
Adjusted p-value: 5.698681e-49
Comparison: Northeast_Midwest_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 1108735
Wilcoxon Test P-value: 0.2315061
Adjusted p-value: 0.2444704
Comparison: Northeast_Southeast_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 151446
Wilcoxon Test P-value: 3.40838e-25
Adjusted p-value: 6.336706e-25
Comparison: Midwest_Southeast_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 2325485
Wilcoxon Test P-value: 2.824803e-67
Adjusted p-value: 9.812473e-67
Comparison: Northeast_Midwest_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 1253020
Wilcoxon Test P-value: 0.01343406
Adjusted p-value: 0.015833
Comparison: Northeast_Southeast_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 165990.5
Wilcoxon Test P-value: 1.609119e-59
Adjusted p-value: 5.057233e-59
Comparison: Midwest_Southeast_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 1903976
Wilcoxon Test P-value: 7.565914e-121
Adjusted p-value: 1.109667e-119
Comparison: Northeast_Midwest_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 436172
Wilcoxon Test P-value: 4.590649e-06
Adjusted p-value: 5.883162e-06
Comparison: Northeast_Southeast_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 83674.5
Wilcoxon Test P-value: 2.163772e-58
Adjusted p-value: 6.642277e-58
Comparison: Midwest_Southeast_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 354807.5
Wilcoxon Test P-value: 1.194895e-72
Adjusted p-value: 4.928941e-72
Comparison: Northeast_Midwest_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1275683
Wilcoxon Test P-value: 0.08083038
Adjusted p-value: 0.09119325
Comparison: Northeast_Southeast_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 161970
Wilcoxon Test P-value: 2.640913e-15
Adjusted p-value: 4.30371e-15
Comparison: Midwest_Southeast_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1860007
Wilcoxon Test P-value: 1.513001e-36
Adjusted p-value: 3.120565e-36
Comparison: Northeast_Midwest_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 1255807
Wilcoxon Test P-value: 0.0198139
Adjusted p-value: 0.02314544
Comparison: Northeast_Southeast_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 173785.5
Wilcoxon Test P-value: 1.033928e-25
Adjusted p-value: 1.949692e-25
Comparison: Midwest_Southeast_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 2044912
Wilcoxon Test P-value: 6.875139e-69
Adjusted p-value: 2.520884e-68
Comparison: Northeast_Midwest_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 948949
Wilcoxon Test P-value: 0.0201098
Adjusted p-value: 0.02328503
Comparison: Northeast_Southeast_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 96023
Wilcoxon Test P-value: 3.546028e-48
Adjusted p-value: 9.001455e-48
Comparison: Midwest_Southeast_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 1038418
Wilcoxon Test P-value: 1.284729e-89
Adjusted p-value: 8.925489e-89
Comparison: Northeast_Midwest_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 441219
Wilcoxon Test P-value: 4.419919e-14
Adjusted p-value: 7.029268e-14
Comparison: Northeast_Southeast_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 77094.5
Wilcoxon Test P-value: 1.041729e-55
Adjusted p-value: 2.864754e-55
Comparison: Midwest_Southeast_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 470424
Wilcoxon Test P-value: 4.179851e-75
Adjusted p-value: 1.779807e-74
Comparison: Northeast_Midwest_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 812257.5
Wilcoxon Test P-value: 1.918894e-12
Adjusted p-value: 2.846e-12
Comparison: Northeast_Southeast_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 81720.5
Wilcoxon Test P-value: 0.08812422
Adjusted p-value: 0.09857963
Comparison: Midwest_Southeast_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 952523
Wilcoxon Test P-value: 3.089815e-11
Adjusted p-value: 4.48193e-11
Comparison: Northeast_Midwest_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 834107
Wilcoxon Test P-value: 2.421526e-10
Adjusted p-value: 3.40044e-10
Comparison: Northeast_Southeast_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 89389.5
Wilcoxon Test P-value: 3.688012e-05
Adjusted p-value: 4.680939e-05
Comparison: Midwest_Southeast_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 1036921
Wilcoxon Test P-value: 3.8801e-23
Adjusted p-value: 6.828977e-23
Four analyte median + IQR plots 2012-2022 with counts + arrows (Dairy liquid)
Code
# Filter out rows with non-missing Total_N values and for specific regionsMDB_dairypumpNPK_filtered <- MDB_dairypumpNPK[ MDB_dairypumpNPK$Region %in%c("Northeast", "Southeast", "Midwest"), ]custom_theme <-theme_minimal() +theme(text =element_text(family ="Times New Roman", size =11),axis.text.x =element_text(angle =45, hjust =1) )# Create separate plots for each nutrient typeplots <-list()# Initialize variables to store the minimum and maximum values of lower and upper quartilesmin_lower_quantile <-Infmax_upper_quantile <--Inf# Function to calculate counts for each nutrient and regioncalculate_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 plottingreturn(counts)}for (nutrient inc("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {# Filter data for the current nutrient type filtered_data <- MDB_dairypumpNPK_filtered[!is.na(MDB_dairypumpNPK_filtered[[nutrient]]), ]# Calculate median and IQR for each combination of Year.Analyzed and Region summary_stats <- filtered_data %>%group_by(Year.Analyzed, Region) %>%summarise(median_value =median(.data[[nutrient]]),lower_quantile =quantile(.data[[nutrient]], 0.25),upper_quantile =quantile(.data[[nutrient]], 0.75)) %>%ungroup() # Remove grouping to avoid issues with plotting# Update minimum and maximum values of lower and upper quartiles min_lower_quantile <-min(min_lower_quantile, min(summary_stats$lower_quantile)) max_upper_quantile <-max(max_upper_quantile, max(summary_stats$upper_quantile))# Calculate counts for all regions counts <-calculate_counts(filtered_data)# Create line graph with median for each year and region, with connecting lines plot <-ggplot(summary_stats, aes(x =factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha =0.3) +# Add shaded area for IQRgeom_line(aes(linetype = Region)) +# Add line graph with connecting linesgeom_point() +# Add points for each data pointlabs(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 settingsscale_color_manual(values =c("Northeast"="darkgreen", "Southeast"="red", "Midwest"="blue")) +# Define colors for regionsscale_fill_manual(values =c("Northeast"="lightgreen", "Southeast"="lightpink", "Midwest"="lightblue")) +# Define fill colors for IQRguides(fill =FALSE) +# Remove legend for IQR fill colorsscale_y_continuous(limits =c(0, .4), breaks =seq(0, .4, by =0.1)) +geom_text(data = counts, aes(label =paste("n =", total_samples),x ="2021", y =0.4- (0.03*seq_along(total_samples)), color = Region),hjust =1, vjust =0, size =3, family ="Times New Roman") # Add text annotations for counts# Add the arrow only for certain nutrientsif (nutrient =="Total_N") { plot <- plot +annotate("segment", x ="2022", y = .365, xend ="2022", yend = .33, arrow =arrow(type ="open", length =unit(0.1, "inches")), color ="darkgreen") }if (nutrient =="Ammonium.N") { plot <- plot +annotate("segment", x ="2022", y = .365, xend ="2022", yend = .33, arrow =arrow(type ="open", length =unit(0.1, "inches")), color ="darkgreen") }if (nutrient =="Phosphorus") { plot <- plot +annotate("segment", x ="2022", y = .365, xend ="2022", yend = .33, arrow =arrow(type ="open", length =unit(0.1, "inches")), color ="darkgreen") }if (nutrient =="Potassium") { plot <- plot +annotate("segment", x ="2022", y = .365, xend ="2022", yend = .33, arrow =arrow(type ="open", length =unit(0.1, "inches")), color ="darkgreen") }# Add the plot to the list plots[[nutrient]] <- plot}# Arrange the plots together using patchworkdairy_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] +plot_layout(guides ="collect")# Print the final plotprint(dairy_final_plot)
Table of samples by region by year for Swine Liquid manure
Code
# Sort out for swine while still having separate NPK columnsMDB_swinepumpNPK <-filter(MDB_pump3, Animal.Type.Combined.Category =="Swine")# Create a table of number of samples for each region per yearsample_table <-table(MDB_swinepumpNPK$Region, MDB_swinepumpNPK$Year.Analyzed)# Print the sample tableprint(sample_table)
Mann-Kendall trend test for 4 analytes combined across selected regions and for each region separately (Swine Liquid)
Code
# Load required librarylibrary(Kendall)# List of selected regionsselected_regions <-c("Midwest", "Northeast", "Southeast")# List of analytesanalytes <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Function to perform Mann-Kendall trend test for a given analyte across all selected regionsperform_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 resultreturn(summary(result_mk_combined))}# Perform Mann-Kendall trend test for each analyte combined across selected regionsfor (analyte in analytes) {cat("Analyzing", analyte, "for all regions combined:\n")print(perform_mk_test_combined(MDB_swinepumpNPK, analyte, selected_regions))cat("\n")}
Analyzing Total_N for all regions combined:
Score = 39 , Var(Score) = 165
denominator = 55
tau = 0.709, 2-sided pvalue =0.0030935
NULL
Analyzing Ammonium.N for all regions combined:
Score = -1 , Var(Score) = 163
denominator = 53.99074
tau = -0.0185, 2-sided pvalue =1
NULL
Analyzing Phosphorus for all regions combined:
Score = 33 , Var(Score) = 165
denominator = 55
tau = 0.6, 2-sided pvalue =0.012731
NULL
Analyzing Potassium for all regions combined:
Score = 17 , Var(Score) = 165
denominator = 55
tau = 0.309, 2-sided pvalue =0.21291
NULL
Code
# Function to perform Mann-Kendall trend testperform_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 resultreturn(summary(result_mk))}# Iterate over selected regions and analytesfor (region in selected_regions) {for (analyte in analytes) {# Subset data for the current region region_data <- MDB_swinepumpNPK[MDB_swinepumpNPK$Region == region, ]# Print information about the current region and analytecat("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 resultprint(result_summary)cat("\n") }}
Four Analytes Mann-Whitney comparison by year for not normal distribution with Benjamini-Hochberg correction LOOP (Swine Liquid)
Code
# Filter data for selected regionsselected_regions <-c("Midwest", "Northeast", "Southeast")filtered_data <- MDB_swinepumpNPK[MDB_swinepumpNPK$Region %in% selected_regions, ]# Initialize an empty list to store cleaned data for each nutrientcleaned_data_list <-list()# Loop over each nutrientfor (nutrient inc("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 Correctionperform_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 resultsreturn(list(wilcox_result = wilcox_result, adjusted_p_value = adjusted_p_value))}# Iterate Over Years, Regions, and Nutrientsyears <-unique(filtered_data$Year.Analyzed)regions <-unique(filtered_data$Region)nutrients <-c("Total_N", "Ammonium.N", "Phosphorus", "Potassium")# Initialize empty vectorsall_p_values <-c()comparison_results <-list()comparison_names <-c()# Loop over years, regions, and nutrients, perform tests, and gather p-valuesindex <-1for (year in years) {for (nutrient in nutrients) {for (i in1:(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 yearsorted_indices <-order(as.numeric(sapply(strsplit(comparison_names, "_"), `[`, 3)))sorted_comparison_names <- comparison_names[sorted_indices]# Apply Benjamini-Hochberg Procedureadjusted_p_values <-p.adjust(all_p_values, method ="BH")# Print results in chronological orderfor (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-valuescat("Comparison:", name, "\n")cat("Year:", year, "\n")cat("Nutrient:", nutrient, "\n")cat("Wilcoxon Test Statistic:", result$statistic, "\n")cat("Wilcoxon Test P-value:", result$p.value, "\n")cat("Adjusted p-value:", adjusted_p_values[index], "\n")cat("\n")}
Comparison: Midwest_Northeast_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 123520
Wilcoxon Test P-value: 0.0005481884
Adjusted p-value: 0.0006460792
Comparison: Midwest_Southeast_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 21757423
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2012_Total_N
Year: 2012
Nutrient: Total
Wilcoxon Test Statistic: 1344646
Wilcoxon Test P-value: 1.749789e-72
Adjusted p-value: 5.132714e-72
Comparison: Midwest_Northeast_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 89018
Wilcoxon Test P-value: 7.146e-06
Adjusted p-value: 9.528e-06
Comparison: Midwest_Southeast_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 295752.5
Wilcoxon Test P-value: 3.279581e-128
Adjusted p-value: 9.838742e-128
Comparison: Northeast_Southeast_2012_Ammonium.N
Year: 2012
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 26173
Wilcoxon Test P-value: 3.590248e-50
Adjusted p-value: 1.008325e-49
Comparison: Midwest_Northeast_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 116734
Wilcoxon Test P-value: 0.02207701
Adjusted p-value: 0.02331332
Comparison: Midwest_Southeast_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 21145175
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2012_Phosphorus
Year: 2012
Nutrient: Phosphorus
Wilcoxon Test Statistic: 1308101
Wilcoxon Test P-value: 3.417861e-64
Adjusted p-value: 9.807775e-64
Comparison: Midwest_Northeast_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 126380
Wilcoxon Test P-value: 7.858913e-05
Adjusted p-value: 9.879776e-05
Comparison: Midwest_Southeast_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 19874642
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2012_Potassium
Year: 2012
Nutrient: Potassium
Wilcoxon Test Statistic: 1155022
Wilcoxon Test P-value: 1.60954e-37
Adjusted p-value: 4.085756e-37
Comparison: Midwest_Northeast_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 175773
Wilcoxon Test P-value: 0.001119591
Adjusted p-value: 0.001285096
Comparison: Midwest_Southeast_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 74393823
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2013_Total_N
Year: 2013
Nutrient: Total
Wilcoxon Test Statistic: 496979
Wilcoxon Test P-value: 4.313587e-27
Adjusted p-value: 7.591914e-27
Comparison: Midwest_Northeast_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 73804
Wilcoxon Test P-value: 7.194924e-05
Adjusted p-value: 9.132019e-05
Comparison: Midwest_Southeast_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 580677.5
Wilcoxon Test P-value: 4.272428e-133
Adjusted p-value: 1.311536e-132
Comparison: Northeast_Southeast_2013_Ammonium.N
Year: 2013
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 9959
Wilcoxon Test P-value: 3.164588e-25
Adjusted p-value: 5.355456e-25
Comparison: Midwest_Northeast_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 176301
Wilcoxon Test P-value: 0.05988631
Adjusted p-value: 0.06175776
Comparison: Midwest_Southeast_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 73518718
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2013_Phosphorus
Year: 2013
Nutrient: Phosphorus
Wilcoxon Test Statistic: 544848
Wilcoxon Test P-value: 8.462132e-28
Adjusted p-value: 1.551391e-27
Comparison: Midwest_Northeast_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 198434
Wilcoxon Test P-value: 0.000296003
Adjusted p-value: 0.0003520035
Comparison: Midwest_Southeast_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 72638471
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2013_Potassium
Year: 2013
Nutrient: Potassium
Wilcoxon Test Statistic: 536121.5
Wilcoxon Test P-value: 4.506294e-26
Adjusted p-value: 7.725075e-26
Comparison: Midwest_Northeast_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 263070.5
Wilcoxon Test P-value: 0.0007685328
Adjusted p-value: 0.0008898801
Comparison: Midwest_Southeast_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 76141994
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2014_Total_N
Year: 2014
Nutrient: Total
Wilcoxon Test Statistic: 698516
Wilcoxon Test P-value: 8.121661e-42
Adjusted p-value: 2.187876e-41
Comparison: Midwest_Northeast_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 111716
Wilcoxon Test P-value: 2.270242e-06
Adjusted p-value: 3.089402e-06
Comparison: Midwest_Southeast_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 706193.5
Wilcoxon Test P-value: 1.781012e-144
Adjusted p-value: 5.877338e-144
Comparison: Northeast_Southeast_2014_Ammonium.N
Year: 2014
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 16814
Wilcoxon Test P-value: 7.334472e-34
Adjusted p-value: 1.669225e-33
Comparison: Midwest_Northeast_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 247960.5
Wilcoxon Test P-value: 0.01679292
Adjusted p-value: 0.01787633
Comparison: Midwest_Southeast_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 75167335
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2014_Phosphorus
Year: 2014
Nutrient: Phosphorus
Wilcoxon Test Statistic: 673304
Wilcoxon Test P-value: 7.131927e-36
Adjusted p-value: 1.74336e-35
Comparison: Midwest_Northeast_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 273354
Wilcoxon Test P-value: 5.66267e-05
Adjusted p-value: 7.257013e-05
Comparison: Midwest_Southeast_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 74976953
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2014_Potassium
Year: 2014
Nutrient: Potassium
Wilcoxon Test Statistic: 672384
Wilcoxon Test P-value: 1.11647e-35
Adjusted p-value: 2.679527e-35
Comparison: Midwest_Northeast_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 172022.5
Wilcoxon Test P-value: 0.008055795
Adjusted p-value: 0.00893584
Comparison: Midwest_Southeast_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 82786528
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2015_Total_N
Year: 2015
Nutrient: Total
Wilcoxon Test Statistic: 459711
Wilcoxon Test P-value: 6.174899e-27
Adjusted p-value: 1.072482e-26
Comparison: Midwest_Northeast_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 78405.5
Wilcoxon Test P-value: 0.001830979
Adjusted p-value: 0.002083528
Comparison: Midwest_Southeast_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 911104
Wilcoxon Test P-value: 9.011685e-145
Adjusted p-value: 3.050109e-144
Comparison: Northeast_Southeast_2015_Ammonium.N
Year: 2015
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 11891
Wilcoxon Test P-value: 1.353041e-22
Adjusted p-value: 2.126207e-22
Comparison: Midwest_Northeast_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 155771
Wilcoxon Test P-value: 0.1674467
Adjusted p-value: 0.1687249
Comparison: Midwest_Southeast_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 82469980
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2015_Phosphorus
Year: 2015
Nutrient: Phosphorus
Wilcoxon Test Statistic: 448796
Wilcoxon Test P-value: 2.190906e-24
Adjusted p-value: 3.570365e-24
Comparison: Midwest_Northeast_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 172894
Wilcoxon Test P-value: 0.00656825
Adjusted p-value: 0.007347534
Comparison: Midwest_Southeast_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 81581079
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2015_Potassium
Year: 2015
Nutrient: Potassium
Wilcoxon Test Statistic: 445431.5
Wilcoxon Test P-value: 1.092028e-23
Adjusted p-value: 1.73672e-23
Comparison: Midwest_Northeast_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 330572.5
Wilcoxon Test P-value: 2.929926e-07
Adjusted p-value: 4.158605e-07
Comparison: Midwest_Southeast_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 84426509
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2016_Total_N
Year: 2016
Nutrient: Total
Wilcoxon Test Statistic: 701445
Wilcoxon Test P-value: 3.359682e-31
Adjusted p-value: 7.152872e-31
Comparison: Midwest_Northeast_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 111854
Wilcoxon Test P-value: 0.05818842
Adjusted p-value: 0.0604793
Comparison: Midwest_Southeast_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 882232
Wilcoxon Test P-value: 1.252108e-142
Adjusted p-value: 4.031176e-142
Comparison: Northeast_Southeast_2016_Ammonium.N
Year: 2016
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 15381
Wilcoxon Test P-value: 6.755259e-30
Adjusted p-value: 1.330887e-29
Comparison: Midwest_Northeast_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 285353
Wilcoxon Test P-value: 0.01221125
Adjusted p-value: 0.01310476
Comparison: Midwest_Southeast_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 83670963
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2016_Phosphorus
Year: 2016
Nutrient: Phosphorus
Wilcoxon Test Statistic: 755286
Wilcoxon Test P-value: 1.155504e-41
Adjusted p-value: 3.050531e-41
Comparison: Midwest_Northeast_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 286125.5
Wilcoxon Test P-value: 0.01075385
Adjusted p-value: 0.01163532
Comparison: Midwest_Southeast_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 83575918
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2016_Potassium
Year: 2016
Nutrient: Potassium
Wilcoxon Test Statistic: 701283
Wilcoxon Test P-value: 7.194454e-31
Adjusted p-value: 1.483856e-30
Comparison: Midwest_Northeast_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 253164
Wilcoxon Test P-value: 0.0002605855
Adjusted p-value: 0.0003127025
Comparison: Midwest_Southeast_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 87303499
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2017_Total_N
Year: 2017
Nutrient: Total
Wilcoxon Test Statistic: 564703
Wilcoxon Test P-value: 5.099301e-34
Adjusted p-value: 1.180891e-33
Comparison: Midwest_Northeast_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 92609
Wilcoxon Test P-value: 0.009554488
Adjusted p-value: 0.01050994
Comparison: Midwest_Southeast_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 776869
Wilcoxon Test P-value: 1.938366e-138
Adjusted p-value: 6.092008e-138
Comparison: Northeast_Southeast_2017_Ammonium.N
Year: 2017
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 13125
Wilcoxon Test P-value: 1.49955e-28
Adjusted p-value: 2.827723e-28
Comparison: Midwest_Northeast_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 223761
Wilcoxon Test P-value: 0.07004447
Adjusted p-value: 0.07112208
Comparison: Midwest_Southeast_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 86606596
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2017_Phosphorus
Year: 2017
Nutrient: Phosphorus
Wilcoxon Test Statistic: 547296
Wilcoxon Test P-value: 5.322342e-30
Adjusted p-value: 1.064468e-29
Comparison: Midwest_Northeast_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 262644.5
Wilcoxon Test P-value: 2.176157e-05
Adjusted p-value: 2.844086e-05
Comparison: Midwest_Southeast_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 85775591
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2017_Potassium
Year: 2017
Nutrient: Potassium
Wilcoxon Test Statistic: 546735
Wilcoxon Test P-value: 7.021477e-30
Adjusted p-value: 1.362993e-29
Comparison: Midwest_Northeast_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 293426
Wilcoxon Test P-value: 1.304995e-08
Adjusted p-value: 1.89296e-08
Comparison: Midwest_Southeast_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 97275234
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2018_Total_N
Year: 2018
Nutrient: Total
Wilcoxon Test Statistic: 556028
Wilcoxon Test P-value: 2.843882e-30
Adjusted p-value: 5.775269e-30
Comparison: Midwest_Northeast_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 112738.5
Wilcoxon Test P-value: 4.849931e-06
Adjusted p-value: 6.532561e-06
Comparison: Midwest_Southeast_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 918953
Wilcoxon Test P-value: 3.453249e-154
Adjusted p-value: 1.19955e-153
Comparison: Northeast_Southeast_2018_Ammonium.N
Year: 2018
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 13139
Wilcoxon Test P-value: 3.461719e-27
Adjusted p-value: 6.174959e-27
Comparison: Midwest_Northeast_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 277960
Wilcoxon Test P-value: 1.958534e-06
Adjusted p-value: 2.692984e-06
Comparison: Midwest_Southeast_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 96747890
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2018_Phosphorus
Year: 2018
Nutrient: Phosphorus
Wilcoxon Test Statistic: 516885
Wilcoxon Test P-value: 1.830422e-22
Adjusted p-value: 2.842538e-22
Comparison: Midwest_Northeast_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 284466
Wilcoxon Test P-value: 2.646617e-07
Adjusted p-value: 3.79732e-07
Comparison: Midwest_Southeast_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 95107411
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2018_Potassium
Year: 2018
Nutrient: Potassium
Wilcoxon Test Statistic: 526310
Wilcoxon Test P-value: 3.418924e-24
Adjusted p-value: 5.503633e-24
Comparison: Midwest_Northeast_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 195640
Wilcoxon Test P-value: 0.001937396
Adjusted p-value: 0.00218578
Comparison: Midwest_Southeast_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 99759283
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2019_Total_N
Year: 2019
Nutrient: Total
Wilcoxon Test Statistic: 412197
Wilcoxon Test P-value: 1.842494e-24
Adjusted p-value: 3.040116e-24
Comparison: Midwest_Northeast_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 80305
Wilcoxon Test P-value: 0.01046169
Adjusted p-value: 0.01141275
Comparison: Midwest_Southeast_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 1069528
Wilcoxon Test P-value: 3.404384e-170
Adjusted p-value: 1.321702e-169
Comparison: Northeast_Southeast_2019_Ammonium.N
Year: 2019
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 10310
Wilcoxon Test P-value: 9.594756e-22
Adjusted p-value: 1.455756e-21
Comparison: Midwest_Northeast_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 169078
Wilcoxon Test P-value: 0.1968546
Adjusted p-value: 0.1968546
Comparison: Midwest_Southeast_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 99604969
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2019_Phosphorus
Year: 2019
Nutrient: Phosphorus
Wilcoxon Test Statistic: 400198
Wilcoxon Test P-value: 1.359511e-21
Adjusted p-value: 2.039267e-21
Comparison: Midwest_Northeast_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 180349
Wilcoxon Test P-value: 0.03955781
Adjusted p-value: 0.04144152
Comparison: Midwest_Southeast_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 98215690
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2019_Potassium
Year: 2019
Nutrient: Potassium
Wilcoxon Test Statistic: 401525
Wilcoxon Test P-value: 7.206062e-22
Adjusted p-value: 1.106047e-21
Comparison: Midwest_Northeast_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 329660
Wilcoxon Test P-value: 0.0002210222
Adjusted p-value: 0.0002701383
Comparison: Midwest_Southeast_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 86337166
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2020_Total_N
Year: 2020
Nutrient: Total
Wilcoxon Test Statistic: 597825
Wilcoxon Test P-value: 4.760018e-38
Adjusted p-value: 1.232005e-37
Comparison: Midwest_Northeast_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 162011
Wilcoxon Test P-value: 1.227392e-08
Adjusted p-value: 1.800176e-08
Comparison: Midwest_Southeast_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 979497
Wilcoxon Test P-value: 1.319472e-154
Adjusted p-value: 4.707306e-154
Comparison: Northeast_Southeast_2020_Ammonium.N
Year: 2020
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 15413
Wilcoxon Test P-value: 3.621312e-31
Adjusted p-value: 7.587511e-31
Comparison: Midwest_Northeast_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 329367
Wilcoxon Test P-value: 0.0002341905
Adjusted p-value: 0.0002836069
Comparison: Midwest_Southeast_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 86030422
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2020_Phosphorus
Year: 2020
Nutrient: Phosphorus
Wilcoxon Test Statistic: 564711
Wilcoxon Test P-value: 9.469724e-30
Adjusted p-value: 1.811599e-29
Comparison: Midwest_Northeast_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 330291.5
Wilcoxon Test P-value: 0.0001938892
Adjusted p-value: 0.0002391904
Comparison: Midwest_Southeast_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 85948841
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2020_Potassium
Year: 2020
Nutrient: Potassium
Wilcoxon Test Statistic: 572342
Wilcoxon Test P-value: 1.982528e-31
Adjusted p-value: 4.290061e-31
Comparison: Midwest_Northeast_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 309342
Wilcoxon Test P-value: 1.655735e-09
Adjusted p-value: 2.455697e-09
Comparison: Midwest_Southeast_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 90294394
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2021_Total_N
Year: 2021
Nutrient: Total
Wilcoxon Test Statistic: 527822
Wilcoxon Test P-value: 4.527257e-33
Adjusted p-value: 1.012878e-32
Comparison: Midwest_Northeast_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 119377
Wilcoxon Test P-value: 1.837611e-05
Adjusted p-value: 2.425647e-05
Comparison: Midwest_Southeast_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 989377.5
Wilcoxon Test P-value: 4.861635e-162
Adjusted p-value: 1.833531e-161
Comparison: Northeast_Southeast_2021_Ammonium.N
Year: 2021
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 13579
Wilcoxon Test P-value: 3.261208e-28
Adjusted p-value: 6.063091e-28
Comparison: Midwest_Northeast_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 264230
Wilcoxon Test P-value: 0.0007074292
Adjusted p-value: 0.0008263775
Comparison: Midwest_Southeast_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 89538552
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2021_Phosphorus
Year: 2021
Nutrient: Phosphorus
Wilcoxon Test Statistic: 491197
Wilcoxon Test P-value: 1.164713e-24
Adjusted p-value: 1.946103e-24
Comparison: Midwest_Northeast_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 293631
Wilcoxon Test P-value: 3.240323e-07
Adjusted p-value: 4.550241e-07
Comparison: Midwest_Southeast_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 89579853
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2021_Potassium
Year: 2021
Nutrient: Potassium
Wilcoxon Test Statistic: 504406
Wilcoxon Test P-value: 1.940151e-27
Adjusted p-value: 3.508218e-27
Comparison: Midwest_Northeast_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 312552.5
Wilcoxon Test P-value: 4.0988e-05
Adjusted p-value: 5.304329e-05
Comparison: Midwest_Southeast_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 38290764
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2022_Total_N
Year: 2022
Nutrient: Total
Wilcoxon Test Statistic: 376700
Wilcoxon Test P-value: 7.721653e-45
Adjusted p-value: 2.123455e-44
Comparison: Midwest_Northeast_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 170720
Wilcoxon Test P-value: 5.090393e-07
Adjusted p-value: 7.072967e-07
Comparison: Midwest_Southeast_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 975207
Wilcoxon Test P-value: 2.941063e-155
Adjusted p-value: 1.07839e-154
Comparison: Northeast_Southeast_2022_Ammonium.N
Year: 2022
Nutrient: Ammonium.N
Wilcoxon Test Statistic: 17793
Wilcoxon Test P-value: 2.362054e-34
Adjusted p-value: 5.5677e-34
Comparison: Midwest_Northeast_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 270710
Wilcoxon Test P-value: 0.06293047
Adjusted p-value: 0.06439397
Comparison: Midwest_Southeast_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 37737539
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2022_Phosphorus
Year: 2022
Nutrient: Phosphorus
Wilcoxon Test Statistic: 356607
Wilcoxon Test P-value: 5.676479e-37
Adjusted p-value: 1.413765e-36
Comparison: Midwest_Northeast_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 305106
Wilcoxon Test P-value: 9.631783e-05
Adjusted p-value: 0.000119943
Comparison: Midwest_Southeast_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 37222776
Wilcoxon Test P-value: 0
Adjusted p-value: 0
Comparison: Northeast_Southeast_2022_Potassium
Year: 2022
Nutrient: Potassium
Wilcoxon Test Statistic: 346986
Wilcoxon Test P-value: 4.661233e-33
Adjusted p-value: 1.025471e-32
Four analyte median + IQR plots 2012-2022 + counts and trend arrows (Swine Liquid)
Code
# Filter out rows with non-missing Total_N values and for specific regionsMDB_swinepumpNPK_filtered <- MDB_swinepumpNPK[ MDB_swinepumpNPK$Region %in%c("Southeast", "Midwest"), ]custom_theme <-theme_minimal() +theme(text =element_text(family ="Times New Roman", size =11),axis.text.x =element_text(angle =45, hjust =1) )# Create separate plots for each nutrient typeplots <-list()# Initialize variables to store the minimum and maximum values of lower and upper quartilesmin_lower_quantile <-Infmax_upper_quantile <--Inf# Function to calculate counts for each nutrient and region excluding zeroescalculate_counts <-function(data) { counts <- data %>%filter(!is.na(Total_N) & Total_N !=0& Year.Analyzed >=2012& Year.Analyzed <=2022) %>%group_by(Region) %>%summarise(total_samples =sum(!is.na(Total_N))) %>%ungroup() # Remove grouping to avoid issues with plottingreturn(counts)}for (nutrient inc("Total_N", "Ammonium.N", "Phosphorus", "Potassium")) {# Filter data for the current nutrient type filtered_data <- MDB_swinepumpNPK_filtered[!is.na(MDB_swinepumpNPK_filtered[[nutrient]]), ]# Calculate median and IQR for each combination of Year.Analyzed and Region summary_stats <- filtered_data %>%group_by(Year.Analyzed, Region) %>%summarise(median_value =median(.data[[nutrient]]),lower_quantile =quantile(.data[[nutrient]], 0.25),upper_quantile =quantile(.data[[nutrient]], 0.75)) %>%ungroup() # Remove grouping to avoid issues with plotting# Update minimum and maximum values of lower and upper quartiles min_lower_quantile <-min(min_lower_quantile, min(summary_stats$lower_quantile)) max_upper_quantile <-max(max_upper_quantile, max(summary_stats$upper_quantile))# Determine a suitable step size for the y-axis y_range <- max_upper_quantile - min_lower_quantile num_breaks <-max(4, floor(y_range *10)) # Ensure at least four breaks step_size <- y_range / num_breaks# Calculate counts for all regions counts <-calculate_counts(filtered_data)# Create line graph with median for each year and region, with connecting lines plot <-ggplot(summary_stats, aes(x =factor(Year.Analyzed), y = median_value, color = Region, group = Region)) +geom_ribbon(aes(ymin = lower_quantile, ymax = upper_quantile, fill = Region), alpha =0.3) +# Add shaded area for IQRgeom_line(aes(linetype = Region)) +# Add line graph with connecting linesgeom_point() +# Add points for each data pointlabs(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 settingsscale_color_manual(values =c("Northeast"="green", "Southeast"="red", "Midwest"="blue")) +# Define colors for regionsscale_fill_manual(values =c("Northeast"="lightgreen", "Southeast"="lightpink", "Midwest"="lightblue")) +# Define fill colors for IQRguides(fill =FALSE) +# Remove legend for IQR fill colorsscale_y_continuous(limits =c(0, .7), breaks =seq(0, .7, by =0.1))+#breaks = seq(ceiling(min_lower_quantile), floor(max_upper_quantile), by = step_size),#expand = c(0, 0)) # Specify breaks for y-axis labels and ensure continuous scalegeom_text(data = counts, aes(label =paste("n =", total_samples),x ="2021", y = .7- (0.08*seq_along(total_samples)), color = Region),hjust =1, vjust =0, size =3, family ="Times New Roman") # Add text annotations for countsif (nutrient =="Ammonium.N") { plot <- plot +annotate("segment", x ="2022", y = .52, xend ="2022", yend = .58, arrow =arrow(type ="open", length =unit(0.1, "inches")), color ="red") }# Add the plot to the list plots[[nutrient]] <- plot}# Arrange the plots together using patchworkswinepump_final_plot <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] +plot_layout(guides ="collect")# Print the final plotprint(swinepump_final_plot)
Code
#`{=tex}#\endgroup
Calculations used from the updated Recommended Methods of Manure Analysis (Wilson et al. 2022).
Bibliography
Wilson, Melissa L., Scott Cortus, Rachel Brimmer, Jerry Floren, Larry Gunderson, Kristin Hicks, Tim Hoerner, et al. 2022. Recommended Methods of Manure Analysis, Second Edition. University of Minnesota Libraries Publishing. http://conservancy.umn.edu/handle/11299/227650.