-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregressions.R
1875 lines (1418 loc) · 89.1 KB
/
regressions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## REGRESSIONS
## Load packages ------------------------------------------------------------
if (!require(readxl, quietly = TRUE)) {
install.packages("readxl")
library(readxl)}
if (!require(tidyverse, quietly = TRUE)) {
install.packages("tidyverse")
library(tidyverse)}
if (!require(lubridate, quietly = TRUE)) {
install.packages("lubridate")
library(lubridate)}
if (!require(fuzzyjoin, quietly = TRUE)) {
install.packages("fuzzyjoin")
library(fuzzyjoin)}
if (!require(easystats, quietly = TRUE)) {
install.packages("easystats")
library(easystats)}
if (!require(performance, quietly = TRUE)) {
install.packages("performance")
library(performance)}
if (!require(nortest, quietly = TRUE)) {
install.packages("nortest")
library(nortest)}
if (!require(forecast, quietly = TRUE)) {
install.packages("forecast")
library(forecast)}
if (!require(MASS, quietly = TRUE)) {
install.packages("MASS")
library(MASS)}
if (!require(car, quietly = TRUE)) {
install.packages("car")
library(car)}
## Import data -----------------------------------------------------------------
# Water quality
wq_file_path <- "./data/water_quality/PWQMN_Mill_Creek_Data.xlsx"
wq_sheets <- excel_sheets(wq_file_path) # list all sheet names
# Precipitation
precip_file_path <- "./data/precipitation/CSM_raw_precip.xlsx"
precip_sheet_names <- excel_sheets(precip_file_path) # list all sheet names
precip_list <- lapply(precip_sheet_names, function(precip_sheet_names) {
read_excel(precip_file_path, sheet = precip_sheet_names)
}) # read sheet contents
precip_combined_data <- bind_rows(precip_list) # combine data
# Water temp
wt_file_path <- "./data/water_temp/SR10_raw_water_temp.xlsx"
wt_sheet_names <- excel_sheets(wt_file_path) # list all sheet names
wt_list <- lapply(wt_sheet_names, function(wt_sheet_names) {
read_excel(wt_file_path, sheet = wt_sheet_names)
}) # read sheet contents
wt_combined_data <- bind_rows(wt_list) # combine data
# Air temp
at_file_path <- "./data/air_temp/CSM_raw_air_temp.xlsx"
at_sheet_names <- excel_sheets(at_file_path) # list all sheet names
at_list <- lapply(at_sheet_names, function(sheet_name) {
read_excel(at_file_path, sheet = sheet_name)
}) # read sheet contents
at_combined_data <- bind_rows(at_list) # combine data
## Define data prep functions --------------------------------------------------
# Data prep functions take 20-30 mins to run, can skip to using prepared data
wq_precip_data_prep <- function(wq_file_path, sheet_name, precip_combined_data) {
# load the water quality data
parameter_data <- read_excel(wq_file_path, sheet = sheet_name) %>%
select(`Collection Timestamp`, Result, Units) %>%
rename(Collection_Timestamp = `Collection Timestamp`)
# Define 36 hr period before sample was taken
parameter_data <- parameter_data %>%
mutate(Start_36hr_Period = Collection_Timestamp - hours(36))
# Define year range in the dataset
start_year <- year(min(parameter_data$Collection_Timestamp))
end_year <- year(max(parameter_data$Collection_Timestamp))
# Extract unique months from water quality data
unique_months <- unique(month(parameter_data$Collection_Timestamp))
# Filter precip data based on year range and unique months
relevant_precip <- precip_combined_data %>%
filter(year(Timestamp) >= start_year & year(Timestamp) <= end_year & month(Timestamp) %in% unique_months) %>%
rename(Precip_Value = Value)
# Join the datasets
joined_data <- parameter_data %>%
fuzzy_left_join(relevant_precip, by = c("Start_36hr_Period" = "Timestamp", "Collection_Timestamp" = "Timestamp"), match_fun = list(`<=`, `>=`))
# Calculate the total precipitation for each sample's 36-hour period
parameter_data <- joined_data %>%
group_by(Collection_Timestamp, Result, Units, Start_36hr_Period) %>%
summarise(Total_Precipitation = sum(Precip_Value, na.rm = TRUE), .groups = 'drop')
return(parameter_data)
}
wq_temp_data_prep <- function(wt_file_path, sheet_name, wt_combined_data) {
# load the water quality data
parameter_data <- read_excel(wq_file_path, sheet = sheet_name) %>%
select(`Collection Timestamp`, Result, Units) %>%
rename(Collection_Timestamp = `Collection Timestamp`)
# Define 36 hr period before sample was taken
parameter_data <- parameter_data %>%
mutate(Start_36hr_Period = Collection_Timestamp - hours(36))
# Define year range in the dataset
start_year <- year(min(parameter_data$Collection_Timestamp))
end_year <- year(max(parameter_data$Collection_Timestamp))
# Extract unique months from water quality data
unique_months <- unique(month(parameter_data$Collection_Timestamp))
# Filter water temp data based on year range and unique months
relevant_temp <- wt_combined_data %>%
filter(year(Timestamp) >= start_year & year(Timestamp) <= end_year & month(Timestamp) %in% unique_months) %>%
rename(WT_Value = Value)
# Join the datasets
joined_data <- parameter_data %>%
fuzzy_left_join(relevant_temp, by = c("Start_36hr_Period" = "Timestamp", "Collection_Timestamp" = "Timestamp"), match_fun = list(`<=`, `>=`))
# Calculate the mean temperature for each sample's 36-hour period
parameter_data <- joined_data %>%
group_by(Collection_Timestamp, Result, Units, Start_36hr_Period) %>%
summarise(Mean_Water_Temp = mean(WT_Value, na.rm = TRUE), .groups = 'drop')
return(parameter_data)
}
## Ammonium data prep ----------------------------------------------------------
# Integrate precip data
ammonium_precip <- wq_precip_data_prep(wq_file_path, "Ammonium", precip_combined_data)
# Select specific columns of precip data
ammonium_precip <- ammonium_precip %>%
select(Collection_Timestamp, Total_Precipitation)
# Integrate water temp data
ammonium_temp <- wq_temp_data_prep(wq_file_path, "Ammonium", wt_combined_data)
# Remove blank value rows
ammonium_temp <- filter(ammonium_temp, Mean_Water_Temp!="NaN")
# Join temp and precip
ammonium <- left_join(ammonium_temp, ammonium_precip, by = "Collection_Timestamp")
# Ln transformation of ammonium
ammonium$log_transformed <- log(ammonium$Result)
ammonium$log_transformed[ammonium$log_transformed=="-Inf"]<-0
# Log transformation of ammonium
ammonium$log_transformed <- log10(ammonium$Result)
ammonium$log_transformed[ammonium$log_transformed=="-Inf"]<-0
# Square root transformation of ammonium
ammonium$sqrt_transformed <- sqrt(ammonium$Result)
# Save prepared data
write.csv(ammonium, "./data/water_quality/ammonium.csv")
## Chloride data prep ----------------------------------------------------------
# Integrate precip data
chloride_precip <- wq_precip_data_prep(wq_file_path, "Chloride", precip_combined_data)
# Select specific columns of precip data
chloride_precip <- chloride_precip %>%
select(Collection_Timestamp, Total_Precipitation)
# Integrate water temp data
chloride_temp <- wq_temp_data_prep(wq_file_path, "Chloride", wt_combined_data)
# Remove blank value rows
chloride_temp <- filter(chloride_temp, Mean_Water_Temp!="NaN")
# Join temp and precip
chloride <- left_join(chloride_temp, chloride_precip, by = "Collection_Timestamp")
# Ln transformation of chloride
chloride$ln_transformed <- log(chloride$Result)
chloride$ln_transformed[chloride$ln_transformed=="-Inf"]<-0
# Log transformation of chloride
chloride$log_transformed <- log10(chloride$Result)
chloride$log_transformed[chloride$log_transformed=="-Inf"]<-0
# Square root transformation of chloride
chloride$sqrt_transformed <- sqrt(chloride$Result)
# Save prepared data
write.csv(chloride, "./data/water_quality/chloride.csv")
## Conductivity data prep ------------------------------------------------------
# Integrate precip data
conductivity_precip <- wq_precip_data_prep(wq_file_path, "Conductivity", precip_combined_data)
# Select specific columns of precip data
conductivity_precip <- conductivity_precip %>%
select(Collection_Timestamp, Total_Precipitation)
# Integrate water temp data
conductivity_temp <- wq_temp_data_prep(wq_file_path, "Conductivity", wt_combined_data)
# Remove blank value rows
conductivity_temp <- filter(conductivity_temp, Mean_Water_Temp!="NaN")
# Delete duplicated value
conductivity_temp <- conductivity_temp[-53,]
# Join temp and precip
conductivity <- left_join(conductivity_temp, conductivity_precip, by = "Collection_Timestamp")
# Ln transformation of conductivity
conductivity$ln_transformed <- log(conductivity$Result)
conductivity$ln_transformed[conductivity$log_transformed=="-Inf"]<-0
# Log transformation of conductivity
conductivity$log_transformed <- log10(conductivity$Result)
conductivity$log_transformed[conductivity$log_transformed=="-Inf"]<-0
# Square root transformation of conductivity
conductivity$sqrt_transformed <- sqrt(conductivity$Result)
# Save prepared data
write.csv(conductivity, "./data/water_quality/conductivity.csv")
## Dissolved oxygen data prep --------------------------------------------------
# Integrate precip data
do_precip <- wq_precip_data_prep(wq_file_path, "Dissolved Oxygen", precip_combined_data)
# Select specific columns of precip data
do_precip <- do_precip %>%
select(Collection_Timestamp, Total_Precipitation)
# Integrate water temp data
do_temp <- wq_temp_data_prep(wq_file_path, "Dissolved Oxygen", wt_combined_data)
# Remove blank value rows
do_temp <- filter(do_temp, Mean_Water_Temp!="NaN")
# Join temp and precip
do <- left_join(do_temp, do_precip, by = "Collection_Timestamp")
# Ln transformation of do
do$ln_transformed <- log(do$Result)
do$ln_transformed[do$ln_transformed=="-Inf"]<-0
# Log transformation of do
do$log_transformed <- log10(do$Result)
do$log_transformed[do$log_transformed=="-Inf"]<-0
# Square root transformation of do
do$sqrt_transformed <- sqrt(do$Result)
# Save prepared data
write.csv(do, "./data/water_quality/do.csv")
## Nitrate data prep -----------------------------------------------------------
# Integrate precip data
nitrate_precip <- wq_precip_data_prep(wq_file_path, "Nitrate", precip_combined_data)
# Select specific columns of precip data
nitrate_precip <- nitrate_precip %>%
select(Collection_Timestamp, Total_Precipitation)
# Integrate water temp data
nitrate_temp <- wq_temp_data_prep(wq_file_path, "Nitrate", wt_combined_data)
# Remove blank value rows
nitrate_temp <- filter(nitrate_temp, Mean_Water_Temp!="NaN")
# Join temp and precip
nitrate <- left_join(nitrate_temp, nitrate_precip, by = "Collection_Timestamp")
# Ln transformation of nitrate
nitrate$ln_transformed <- log(nitrate$Result)
nitrate$ln_transformed[nitrate$ln_transformed=="-Inf"]<-0
# Log transformation of nitrate
nitrate$log_transformed <- log10(nitrate$Result)
nitrate$log_transformed[nitrate$log_transformed=="-Inf"]<-0
# Square root transformation of nitrate
nitrate$sqrt_transformed <- sqrt(nitrate$Result)
# Save prepared data
write.csv(nitrate, "./data/water_quality/nitrate.csv")
## Nitrite data prep -----------------------------------------------------------
# Integrate precip data
nitrite_precip <- wq_precip_data_prep(wq_file_path, "Nitrite", precip_combined_data)
# Select specific columns of precip data
nitrite_precip <- nitrite_precip %>%
select(Collection_Timestamp, Total_Precipitation)
# Integrate water temp data
nitrite_temp <- wq_temp_data_prep(wq_file_path, "Nitrite", wt_combined_data)
# Remove blank value rows
nitrite_temp <- filter(nitrite_temp, Mean_Water_Temp!="NaN")
# Join temp and precip
nitrite <- left_join(nitrite_temp, nitrite_precip, by = "Collection_Timestamp")
# Ln transformation of nitrite
nitrite$ln_transformed <- log(nitrite$Result)
nitrite$ln_transformed[nitrite$ln_transformed=="-Inf"]<-0
# Log transformation of nitrite
nitrite$log_transformed <- log10(nitrite$Result)
nitrite$log_transformed[nitrite$log_transformed=="-Inf"]<-0
# Square root transformation of nitrite
nitrite$sqrt_transformed <- sqrt(nitrite$Result)
# Save prepared data
write.csv(nitrite, "./data/water_quality/nitrite.csv")
## pH data prep ----------------------------------------------------------------
# Integrate precip data
ph_precip <- wq_precip_data_prep(wq_file_path, "pH", precip_combined_data)
# Select specific columns of precip data
ph_precip <- ph_precip %>%
select(Collection_Timestamp, Total_Precipitation)
# Integrate water temp data
ph_temp <- wq_temp_data_prep(wq_file_path, "pH", wt_combined_data)
# Remove blank value rows
ph_temp <- filter(ph_temp, Mean_Water_Temp!="NaN")
# Delete duplicated value
ph_temp <- ph_temp[-53,]
# Join temp and precip
ph <- left_join(ph_temp, ph_precip, by = "Collection_Timestamp")
# Ln transformation of pH
ph$ln_transformed <- log(ph$Result)
ph$ln_transformed[ph$ln_transformed=="-Inf"]<-0
# Log transformation of pH
ph$log_transformed <- log10(ph$Result)
ph$log_transformed[ph$log_transformed=="-Inf"]<-0
#Square root transformation of pH
ph$sqrt_transformed <- sqrt(ph$Result)
# Save prepared data
write.csv(ph, "./data/water_quality/ph.csv")
## Phosphate data prep ---------------------------------------------------------
# Integrate precip data
phosphate_precip <- wq_precip_data_prep(wq_file_path, "Phosphate", precip_combined_data)
# Select specific columns of precip data
phosphate_precip <- phosphate_precip %>%
select(Collection_Timestamp, Total_Precipitation)
# Integrate water temp data
phosphate_temp <- wq_temp_data_prep(wq_file_path, "Phosphate", wt_combined_data)
# Remove blank value rows
phosphate_temp <- filter(phosphate_temp, Mean_Water_Temp!="NaN")
# Delete negative value
phosphate_temp <- phosphate_temp[-10,]
# Join temp and precip
phosphate <- left_join(phosphate_temp, phosphate_precip, by = "Collection_Timestamp")
# Ln transformation of phosphate
phosphate$ln_transformed <- log(phosphate$Result)
phosphate$ln_transformed[phosphate$ln_transformed=="-Inf"]<-0
# Log transformation of phosphate
phosphate$log_transformed <- log10(phosphate$Result)
phosphate$log_transformed[phosphate$log_transformed=="-Inf"]<-0
# Square root transformation of phosphate
phosphate$sqrt_transformed <- sqrt(phosphate$Result)
# Save prepared data
write.csv(phosphate, "./data/water_quality/phosphate.csv")
## Temperature data prep -------------------------------------------------------
wt_combined_data$date <- as.Date(ymd_hms(wt_combined_data$Timestamp, truncated = 6)) # add date column
water_daily_avg <- wt_combined_data %>%
group_by(date) %>%
summarise(daily_avg_water_temp = mean(Value, na.rm = TRUE)) # get daily avg water temp
at_combined_data$date <- as.Date(ymd_hms(at_combined_data$Timestamp, truncated = 6)) # add date column
air_daily_avg <- at_combined_data %>%
group_by(date) %>%
summarise(daily_avg_air_temp = mean(Value, na.rm = TRUE)) # get daily avg air temp
# Combine water and air temp
combined_daily_avg <- left_join(water_daily_avg, air_daily_avg, by = "date")
# Delete rows with NA values
combined_daily_avg <- drop_na(combined_daily_avg)
# Ln transformations
combined_daily_avg$ln_transformed_air <- log(combined_daily_avg$daily_avg_air_temp)
combined_daily_avg$ln_transformed_air[combined_daily_avg$ln_transformed_air=="-Inf"]<-0
combined_daily_avg$ln_transformed_water <- log(combined_daily_avg$daily_avg_water_temp)
combined_daily_avg$ln_transformed_water[combined_daily_avg$ln_transformed_water=="-Inf"]<-0
# Log transformations
combined_daily_avg$log_transformed_air <- log10(combined_daily_avg$daily_avg_air_temp)
combined_daily_avg$log_transformed_air[combined_daily_avg$log_transformed_air=="-Inf"]<-0
combined_daily_avg$log_transformed_water <- log10(combined_daily_avg$daily_avg_water_temp)
combined_daily_avg$log_transformed_water[combined_daily_avg$log_transformed_water=="-Inf"]<-0
# Square root transformations
combined_daily_avg$sqrt_transformed_air <- sqrt(combined_daily_avg$daily_avg_air_temp)
combined_daily_avg$sqrt_transformed_air[combined_daily_avg$sqrt_transformed_air=="-Inf"]<-0
combined_daily_avg$sqrt_transformed_water <- sqrt(combined_daily_avg$daily_avg_water_temp)
combined_daily_avg$sqrt_transformed_water[combined_daily_avg$sqrt_transformed_water=="-Inf"]<-0
# Clean data
combined_daily_avg <- na.omit(combined_daily_avg)
View(combined_daily_avg)
## Ammonium regression ---------------------------------------------------------
ammonium <- read.csv("./data/water_quality/ammonium.csv")
# Visualize data
ammonium %>%
ggplot(aes(x = Total_Precipitation, y = Result)) +
geom_point() +
labs(x = "Total Precipitation (over previous 36 hours)",
y = "Ammonium concentration (mg/L)") +
theme_minimal()
ammonium %>%
ggplot(aes(x = Mean_Water_Temp, y = Result)) +
geom_point() +
labs(x = "Mean water temp (over previous 36 hours)",
y = "Ammonium concentration (mg/L)") +
theme_minimal()
# Summary statistics
summary(ammonium$Result)
summary(ammonium$Total_Precipitation)
summary(ammonium$Mean_Water_Temp)
# Histograms
ammonium %>% # raw - right skewed
ggplot(aes(x = Result)) +
geom_histogram(binwidth = 0.005) +
labs(x = "Ammonium concentration (mg/L)", y = "Frequency")
ammonium %>% # ln transformed - slight right skew, almost normal
ggplot(aes(x = ln_transformed)) +
geom_histogram(binwidth = 0.5) +
labs(x = "Ln transformed ammonium concentration (ln[mg/L])", y = "Frequency")
ammonium %>% # log transformed - right skew
ggplot(aes(x = log_transformed)) +
geom_histogram(binwidth = 0.5) +
labs(x = "Log transformed ammonium concentration (log[mg/L])", y = "Frequency")
ammonium %>% # sqrt transformed - normal
ggplot(aes(x = sqrt_transformed)) +
geom_histogram(binwidth = 0.05) +
labs(x = "Sqrt transformed ammonium concentration (sqrt[mg/L])", y = "Frequency")
# Check normality
shapiro_ammonium <- shapiro.test(ammonium$Result) %>% print() # non-normal, p = 5.035e-09
shapiro_ammonium_ln <- shapiro.test(ammonium$ln_transformed) %>% print() # non-normal, p = 6.557e-06
shapiro_ammonium_log <- shapiro.test(ammonium$log_transformed) %>% print() # non-normal, p = 6.557e-06
shapiro_ammonium_sqrt <- shapiro.test(ammonium$sqrt_transformed) %>% print() # borderline normal, p = 0.02306
# Regressions - raw ammonium
ammonium_model_synergy <- lm(Result ~ Total_Precipitation * Mean_Water_Temp, data = ammonium) # linear model with both predictors and "*" interaction
summary(ammonium_model_synergy)
check_normality(ammonium_model_synergy) # non-normality of residuals detected (p < .001)
ammonium_model_additive <- lm(Result ~ Total_Precipitation + Mean_Water_Temp, data = ammonium) # linear model with both predictors and "+" interaction
summary(ammonium_model_additive)
check_normality(ammonium_model_additive) # non-normality of residuals detected (p < .001)
ammonium_model_precip <- lm(Result ~ Total_Precipitation, data = ammonium) # linear model with only precip
summary(ammonium_model_precip)
check_normality(ammonium_model_precip) # non-normality of residuals detected (p < .001)
ammonium_model_temp <- lm(Result ~ Mean_Water_Temp, data = ammonium) # linear model with only water temp
summary(ammonium_model_temp)
check_normality(ammonium_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(ammonium_model_synergy, ammonium_model_additive, ammonium_model_precip, ammonium_model_temp) # temp model has lowest AIC
# Regressions - ln transformed ammonium
ln_ammonium_model_synergy <- lm(ln_transformed ~ Total_Precipitation * Mean_Water_Temp, data = ammonium) # linear model with both predictors and "*" interaction
summary(ln_ammonium_model_synergy)
check_normality(ln_ammonium_model_synergy) # non-normality of residuals detected (p < .001)
ln_ammonium_model_additive <- lm(ln_transformed ~ Total_Precipitation + Mean_Water_Temp, data = ammonium) # linear model with both predictors and "+" interaction
summary(ln_ammonium_model_additive)
check_normality(ln_ammonium_model_additive) # non-normality of residuals detected (p = 0.005)
ln_ammonium_model_precip <- lm(ln_transformed ~ Total_Precipitation, data = ammonium) # linear model with only precip
summary(ln_ammonium_model_precip)
check_normality(ln_ammonium_model_precip) # non-normality of residuals detected (p = 0.004)
ln_ammonium_model_temp <- lm(ln_transformed ~ Mean_Water_Temp, data = ammonium) # linear model with only water temp
summary(ln_ammonium_model_temp)
check_normality(ln_ammonium_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(ln_ammonium_model_synergy, ln_ammonium_model_additive, ln_ammonium_model_precip, ln_ammonium_model_temp) # synergistic model has lowest AIC
# Regressions - log transformed ammonium
log_ammonium_model_synergy <- lm(log_transformed ~ Total_Precipitation * Mean_Water_Temp, data = ammonium) # linear model with both predictors and "*" interaction
summary(log_ammonium_model_synergy)
check_normality(log_ammonium_model_synergy) # non-normality of residuals detected (p < .001)
log_ammonium_model_additive <- lm(log_transformed ~ Total_Precipitation + Mean_Water_Temp, data = ammonium) # linear model with both predictors and "+" interaction
summary(log_ammonium_model_additive)
check_normality(log_ammonium_model_additive) # non-normality of residuals detected (p = 0.005)
log_ammonium_model_precip <- lm(log_transformed ~ Total_Precipitation, data = ammonium) # linear model with only precip
summary(log_ammonium_model_precip)
check_normality(log_ammonium_model_precip) # non-normality of residuals detected (p = 0.004)
log_ammonium_model_temp <- lm(log_transformed ~ Mean_Water_Temp, data = ammonium) # linear model with only water temp
summary(log_ammonium_model_temp)
check_normality(log_ammonium_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(log_ammonium_model_synergy, log_ammonium_model_additive, log_ammonium_model_precip, log_ammonium_model_temp) # synergistic model has lowest AIC
# Regressions - sqrt transformed ammonium
sqrt_ammonium_model_synergy <- lm(sqrt_transformed ~ Total_Precipitation * Mean_Water_Temp, data = ammonium) # linear model with both predictors and "*" interaction
summary(sqrt_ammonium_model_synergy)
check_normality(sqrt_ammonium_model_synergy) # residuals appear as normally distributed (p = 0.167)
sqrt_ammonium_model_additive <- lm(sqrt_transformed ~ Total_Precipitation + Mean_Water_Temp, data = ammonium) # linear model with both predictors and "+" interaction
summary(sqrt_ammonium_model_additive)
check_normality(sqrt_ammonium_model_additive) # residuals appear as normally distributed (p = 0.342)
sqrt_ammonium_model_precip <- lm(sqrt_transformed ~ Total_Precipitation, data = ammonium) # linear model with only precip
summary(sqrt_ammonium_model_precip)
check_normality(sqrt_ammonium_model_precip) # non-normality of residuals detected (p = 0.017)
sqrt_ammonium_model_temp <- lm(sqrt_transformed ~ Mean_Water_Temp, data = ammonium) # linear model with only water temp
summary(sqrt_ammonium_model_temp)
check_normality(sqrt_ammonium_model_temp) # residuals appear as normally distributed (p = 0.420)
compare_performance(sqrt_ammonium_model_synergy, sqrt_ammonium_model_additive, sqrt_ammonium_model_precip, sqrt_ammonium_model_temp) # temp model has lowest AIC, where temp is a borderline significant predictor
par(mfrow = c(2, 2))
plot(sqrt_ammonium_model_temp)
# Plot ammonium
ggplot(ammonium, aes(x = Mean_Water_Temp, y = sqrt_transformed)) +
geom_point() +
geom_smooth(method=lm, color="darkorchid4") +
labs(x = "Mean Water Temperature (C)", y = "Square root transformed ammonium concentration (sqrt[mg/L])") +
theme_minimal()
## Chloride regression ---------------------------------------------------------
chloride <- read.csv("./data/water_quality/chloride.csv")
# Visualize data
chloride %>%
ggplot(aes(x = Total_Precipitation, y = Result)) +
geom_point() +
labs(x = "Total Precipitation (over previous 36 hours)",
y = "Chloride concentration (mg/L)") +
theme_minimal()
chloride %>%
ggplot(aes(x = Mean_Water_Temp, y = Result)) +
geom_point() +
labs(x = "Mean water temp (over previous 36 hours)",
y = "Chloride concentration (mg/L)") +
theme_minimal()
# Summary statistics
summary(chloride$Result)
summary(chloride$Total_Precipitation)
summary(chloride$Mean_Water_Temp)
# Histograms
chloride %>% # raw - multimodal
ggplot(aes(x = Result)) +
geom_histogram(binwidth = 1) +
labs(x = "Chloride concentration (mg/L)", y = "Frequency")
chloride %>% # ln transformed - normal
ggplot(aes(x = ln_transformed)) +
geom_histogram(binwidth = 0.05) +
labs(x = "Ln transformed chloride concentration (ln[mg/L])", y = "Frequency")
chloride %>% # log transformed - normal
ggplot(aes(x = log_transformed)) +
geom_histogram(binwidth = 0.05) +
labs(x = "Log transformed chloride concentration (log[mg/L])", y = "Frequency")
chloride %>% # sqrt transformed - normal
ggplot(aes(x = sqrt_transformed)) +
geom_histogram(binwidth = 0.1) +
labs(x = "Sqrt transformed chloride concentration (sqrt[mg/L])", y = "Frequency")
# Check normality
shapiro_chloride <- shapiro.test(chloride$Result) %>% print() # borderline normal, p = 0.01516
shapiro_chloride_ln <- shapiro.test(chloride$ln_transformed) %>% print() # normal, p = 0.07253
shapiro_chloride_log <- shapiro.test(chloride$log_transformed) %>% print() # normal, p = 0.07253
shapiro_chloride_sqrt <- shapiro.test(chloride$sqrt_transformed) %>% print() # borderline normal, p = 0.04934
# Regressions - raw chloride
chloride_model_synergy <- lm(Result ~ Total_Precipitation * Mean_Water_Temp, data = chloride) # linear model with both predictors and "*" interaction
summary(chloride_model_synergy)
check_normality(chloride_model_synergy) # non-normality of residuals detected (p = 0.002)
chloride_model_additive <- lm(Result ~ Total_Precipitation + Mean_Water_Temp, data = chloride) # linear model with both predictors and "+" interaction
summary(chloride_model_additive)
check_normality(chloride_model_additive) # non-normality of residuals detected (p = 0.003)
chloride_model_precip <- lm(Result ~ Total_Precipitation, data = chloride) # linear model with only precip
summary(chloride_model_precip)
check_normality(chloride_model_precip) # non-normality of residuals detected (p = 0.005)
chloride_model_temp <- lm(Result ~ Mean_Water_Temp, data = chloride) # linear model with only water temp
summary(chloride_model_temp)
check_normality(chloride_model_temp) # non-normality of residuals detected (p = 0.007)
compare_performance(chloride_model_synergy, chloride_model_additive, chloride_model_precip, chloride_model_temp) # temp model has lowest AIC
# Regressions - ln transformed chloride
ln_chloride_model_synergy <- lm(ln_transformed ~ Total_Precipitation * Mean_Water_Temp, data = chloride) # linear model with both predictors and "*" interaction
summary(ln_chloride_model_synergy)
check_normality(ln_chloride_model_synergy) # residuals appear as normally distributed (p = 0.155)
ln_chloride_model_additive <- lm(ln_transformed ~ Total_Precipitation + Mean_Water_Temp, data = chloride) # linear model with both predictors and "+" interaction
summary(ln_chloride_model_additive)
check_normality(ln_chloride_model_additive) # residuals appear as normally distributed (p = 0.201)
ln_chloride_model_precip <- lm(ln_transformed ~ Total_Precipitation, data = chloride) # linear model with only precip
summary(ln_chloride_model_precip)
check_normality(ln_chloride_model_precip) # residuals appear as normally distributed (p = 0.085)
ln_chloride_model_temp <- lm(ln_transformed ~ Mean_Water_Temp, data = chloride) # linear model with only water temp
summary(ln_chloride_model_temp)
check_normality(ln_chloride_model_temp) # residuals appear as normally distributed (p = 0.236)
compare_performance(ln_chloride_model_synergy, ln_chloride_model_additive, ln_chloride_model_precip, ln_chloride_model_temp) # precip model has lowest AIC
# Regressions - log transformed chloride
log_chloride_model_synergy <- lm(log_transformed ~ Total_Precipitation * Mean_Water_Temp, data = chloride) # linear model with both predictors and "*" interaction
summary(log_chloride_model_synergy)
check_normality(log_chloride_model_synergy) # residuals appear as normally distributed (p = 0.155)
log_chloride_model_additive <- lm(log_transformed ~ Total_Precipitation + Mean_Water_Temp, data = chloride) # linear model with both predictors and "+" interaction
summary(log_chloride_model_additive)
check_normality(log_chloride_model_additive) # residuals appear as normally distributed (p = 0.201)
log_chloride_model_precip <- lm(log_transformed ~ Total_Precipitation, data = chloride) # linear model with only precip
summary(log_chloride_model_precip)
check_normality(log_chloride_model_precip) # residuals appear as normally distributed (p = 0.085)
log_chloride_model_temp <- lm(log_transformed ~ Mean_Water_Temp, data = chloride) # linear model with only water temp
summary(log_chloride_model_temp)
check_normality(log_chloride_model_temp) # residuals appear as normally distributed (p = 0.236)
compare_performance(log_chloride_model_synergy, log_chloride_model_additive, log_chloride_model_precip, log_chloride_model_temp) # precip model has lowest AIC
# Regressions - sqrt transformed chloride
sqrt_chloride_model_synergy <- lm(sqrt_transformed ~ Total_Precipitation * Mean_Water_Temp, data = chloride) # linear model with both predictors and "*" interaction
summary(sqrt_chloride_model_synergy)
check_normality(sqrt_chloride_model_synergy) # non-normality of residuals detected (p = 0.022)
sqrt_chloride_model_additive <- lm(sqrt_transformed ~ Total_Precipitation + Mean_Water_Temp, data = chloride) # linear model with both predictors and "+" interaction
summary(sqrt_chloride_model_additive)
check_normality(sqrt_chloride_model_additive) # non-normality of residuals detected (p = 0.034)
sqrt_chloride_model_precip <- lm(sqrt_transformed ~ Total_Precipitation, data = chloride) # linear model with only precip
summary(sqrt_chloride_model_precip)
check_normality(sqrt_chloride_model_precip) # non-normality of residuals detected (p = 0.031)
sqrt_chloride_model_temp <- lm(sqrt_transformed ~ Mean_Water_Temp, data = chloride) # linear model with only water temp
summary(sqrt_chloride_model_temp)
check_normality(sqrt_chloride_model_temp) # residuals appear as normally distributed (p = 0.057)
compare_performance(sqrt_chloride_model_synergy, sqrt_chloride_model_additive, sqrt_chloride_model_precip, sqrt_chloride_model_temp) # temp model has lowest AIC
# Compare all models with normal residuals
compare_performance(ln_chloride_model_synergy, ln_chloride_model_additive, ln_chloride_model_precip, ln_chloride_model_temp, log_chloride_model_synergy, log_chloride_model_additive, log_chloride_model_precip, log_chloride_model_temp, sqrt_chloride_model_temp) # log_chloride_model_temp has the best fit
# Plot chloride
ggplot(chloride, aes(x = Mean_Water_Temp, y = log_transformed)) +
geom_point() +
geom_smooth(method=lm, color="darkorchid4") +
labs(x = "Mean Water Temperature (C)", y = "Log transformed chloride concentration (log[mg/L])") +
theme_minimal()
par(mfrow = c(2, 2))
plot(log_chloride_model_temp)
## Nitrate regression ----------------------------------------------------------
nitrate <- read.csv("./data/water_quality/nitrate.csv")
# Visualize data
nitrate %>%
ggplot(aes(x = Total_Precipitation, y = Result)) +
geom_point() +
labs(x = "Total Precipitation (over previous 36 hours)",
y = "Nitrate concentration (mg/L)") +
theme_minimal()
nitrate %>%
ggplot(aes(x = Mean_Water_Temp, y = Result)) +
geom_point() +
labs(x = "Mean water temp (over previous 36 hours)",
y = "Nitrate concentration (mg/L)") +
theme_minimal()
# Summary statistics
summary(nitrate$Result)
summary(nitrate$Total_Precipitation)
summary(nitrate$Mean_Water_Temp)
# Histograms
nitrate %>% # raw - right skew
ggplot(aes(x = Result)) +
geom_histogram(binwidth = 0.5) +
labs(x = "Nitrate concentration (mg/L)", y = "Frequency")
nitrate %>% # ln transformed - right skew
ggplot(aes(x = ln_transformed)) +
geom_histogram(binwidth = 0.5) +
labs(x = "Ln transformed nitrate concentration (ln[mg/L])", y = "Frequency")
nitrate %>% # log transformed - right skew
ggplot(aes(x = log_transformed)) +
geom_histogram(binwidth = 0.05) +
labs(x = "Log transformed nitrate concentration (log[mg/L])", y = "Frequency")
nitrate %>% # sqrt transformed - right skew
ggplot(aes(x = sqrt_transformed)) +
geom_histogram(binwidth = 0.05) +
labs(x = "Sqrt transformed nitrate concentration (sqrt[mg/L])", y = "Frequency")
# Check normality
shapiro_nitrate <- shapiro.test(nitrate$Result) %>% print() # non-normal, p = 1e-15
shapiro_nitrate_ln <- shapiro.test(nitrate$ln_transformed) %>% print() # non-normal, p = 4.227e-05
shapiro_nitrate_log <- shapiro.test(nitrate$log_transformed) %>% print() # non-normal, p = 4.227e-05
shapiro_nitrate_sqrt <- shapiro.test(nitrate$sqrt_transformed) %>% print() # non-normal, p = 1.334e-10
# Regressions - raw nitrate
nitrate_model_synergy <- lm(Result ~ Total_Precipitation * Mean_Water_Temp, data = nitrate) # linear model with both predictors and "*" interaction
summary(nitrate_model_synergy)
check_normality(nitrate_model_synergy) # non-normality of residuals detected (p < .001)
nitrate_model_additive <- lm(Result ~ Total_Precipitation + Mean_Water_Temp, data = nitrate) # linear model with both predictors and "+" interaction
summary(nitrate_model_additive)
check_normality(nitrate_model_additive) # non-normality of residuals detected (p < .001)
nitrate_model_precip <- lm(Result ~ Total_Precipitation, data = nitrate) # linear model with only precip
summary(nitrate_model_precip)
check_normality(nitrate_model_precip) # non-normality of residuals detected (p < .001)
nitrate_model_temp <- lm(Result ~ Mean_Water_Temp, data = nitrate) # linear model with only water temp
summary(nitrate_model_temp)
check_normality(nitrate_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(nitrate_model_synergy, nitrate_model_additive, nitrate_model_precip, nitrate_model_temp) # temp model has lowest AIC
# Regressions - ln transformed nitrate
ln_nitrate_model_synergy <- lm(ln_transformed ~ Total_Precipitation * Mean_Water_Temp, data = nitrate) # linear model with both predictors and "*" interaction
summary(ln_nitrate_model_synergy)
check_normality(ln_nitrate_model_synergy) # non-normality of residuals detected (p < .001)
ln_nitrate_model_additive <- lm(ln_transformed ~ Total_Precipitation + Mean_Water_Temp, data = nitrate) # linear model with both predictors and "+" interaction
summary(ln_nitrate_model_additive)
check_normality(ln_nitrate_model_additive) # non-normality of residuals detected (p < .001)
ln_nitrate_model_precip <- lm(ln_transformed ~ Total_Precipitation, data = nitrate) # linear model with only precip
summary(ln_nitrate_model_precip)
check_normality(ln_nitrate_model_precip) # non-normality of residuals detected (p < .001)
ln_nitrate_model_temp <- lm(ln_transformed ~ Mean_Water_Temp, data = nitrate) # linear model with only water temp
summary(ln_nitrate_model_temp)
check_normality(ln_nitrate_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(ln_nitrate_model_synergy, ln_nitrate_model_additive, ln_nitrate_model_precip, ln_nitrate_model_temp) # additive model has lowest AIC
# Regressions - log transformed nitrate
log_nitrate_model_synergy <- lm(log_transformed ~ Total_Precipitation * Mean_Water_Temp, data = nitrate) # linear model with both predictors and "*" interaction
summary(log_nitrate_model_synergy)
check_normality(log_nitrate_model_synergy) # non-normality of residuals detected (p < .001)
log_nitrate_model_additive <- lm(log_transformed ~ Total_Precipitation + Mean_Water_Temp, data = nitrate) # linear model with both predictors and "+" interaction
summary(log_nitrate_model_additive)
check_normality(log_nitrate_model_additive) # non-normality of residuals detected (p < .001)
log_nitrate_model_precip <- lm(log_transformed ~ Total_Precipitation, data = nitrate) # linear model with only precip
summary(log_nitrate_model_precip)
check_normality(log_nitrate_model_precip) # non-normality of residuals detected (p < .001)
log_nitrate_model_temp <- lm(log_transformed ~ Mean_Water_Temp, data = nitrate) # linear model with only water temp
summary(log_nitrate_model_temp)
check_normality(log_nitrate_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(log_nitrate_model_synergy, log_nitrate_model_additive, log_nitrate_model_precip, log_nitrate_model_temp) # additive model has lowest AIC
# Regressions - sqrt transformed nitrate
sqrt_nitrate_model_synergy <- lm(sqrt_transformed ~ Total_Precipitation * Mean_Water_Temp, data = nitrate) # linear model with both predictors and "*" interaction
summary(sqrt_nitrate_model_synergy)
check_normality(sqrt_nitrate_model_synergy) # non-normality of residuals detected (p < .001)
sqrt_nitrate_model_additive <- lm(sqrt_transformed ~ Total_Precipitation + Mean_Water_Temp, data = nitrate) # linear model with both predictors and "+" interaction
summary(sqrt_nitrate_model_additive)
check_normality(sqrt_nitrate_model_additive) # non-normality of residuals detected (p < .001)
sqrt_nitrate_model_precip <- lm(sqrt_transformed ~ Total_Precipitation, data = nitrate) # linear model with only precip
summary(sqrt_nitrate_model_precip)
check_normality(sqrt_nitrate_model_precip) # non-normality of residuals detected (p < .001)
sqrt_nitrate_model_temp <- lm(sqrt_transformed ~ Mean_Water_Temp, data = nitrate) # linear model with only water temp
summary(sqrt_nitrate_model_temp)
check_normality(sqrt_nitrate_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(sqrt_nitrate_model_synergy, sqrt_nitrate_model_additive, sqrt_nitrate_model_precip, sqrt_nitrate_model_temp) # additive model has lowest AIC
# Box cox transformation
lambda_nitrate <- BoxCox.lambda(nitrate$Result) # find the optimal lambda
transformed_nitrate <- BoxCox(nitrate$Result, lambda_nitrate) # transform raw values using lambda
nitrate$box_cox_transformed = transformed_nitrate # make new column in dataset
View(nitrate)
shapiro.test(nitrate$box_cox_transformed) # non-normal, p = 0.0001404
# Regressions - box cox transformed nitrate
box_cox_nitrate_model_synergy <- lm(box_cox_transformed ~ Total_Precipitation * Mean_Water_Temp, data = nitrate) # linear model with both predictors and "*" interaction
summary(box_cox_nitrate_model_synergy)
check_normality(box_cox_nitrate_model_synergy) # non-normality of residuals detected (p < .001)
box_cox_nitrate_model_additive <- lm(box_cox_transformed ~ Total_Precipitation + Mean_Water_Temp, data = nitrate) # linear model with both predictors and "+" interaction
summary(box_cox_nitrate_model_additive)
check_normality(box_cox_nitrate_model_additive) # non-normality of residuals detected (p < .001)
box_cox_nitrate_model_precip <- lm(box_cox_transformed ~ Total_Precipitation, data = nitrate) # linear model with only precip
summary(box_cox_nitrate_model_precip)
check_normality(box_cox_nitrate_model_precip) # non-normality of residuals detected (p < .001)
box_cox_nitrate_model_temp <- lm(box_cox_transformed ~ Mean_Water_Temp, data = nitrate) # linear model with only water temp
summary(box_cox_nitrate_model_temp)
check_normality(box_cox_nitrate_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(box_cox_nitrate_model_synergy, box_cox_nitrate_model_additive, box_cox_nitrate_model_precip, box_cox_nitrate_model_temp) # synergistic model has lowest AIC
# Robust regression
nitrate_rlm_synergy <- rlm(Result ~ Total_Precipitation * Mean_Water_Temp, data = nitrate) # rlm model with both predictors and "*" interaction
summary(nitrate_rlm_synergy)
nitrate_rlm_additive <- rlm(Result ~ Total_Precipitation + Mean_Water_Temp, data = nitrate) # rlm model with both predictors and "+" interaction
summary(nitrate_rlm_additive)
nitrate_rlm_precip <- rlm(Result ~ Total_Precipitation, data = nitrate) # rlm model with only precip
summary(nitrate_rlm_precip)
nitrate_rlm_temp <- rlm(Result ~ Mean_Water_Temp, data = nitrate) # rlm model with only temp
summary(nitrate_rlm_temp)
compare_performance(nitrate_rlm_synergy, nitrate_rlm_additive, nitrate_rlm_precip, nitrate_rlm_temp) # temp model has lowest AIC
# Plot nitrate
ggplot(nitrate, aes(x = Mean_Water_Temp, y = Result)) +
geom_point() +
geom_smooth(method=lm, color="darkorchid4") +
labs(x = "Mean Water Temperature (C)", y = "Nitrate concentration (mg/L)") +
theme_minimal()
par(mfrow = c(2, 2))
plot(nitrate_rlm_temp)
## Nitrite regression ----------------------------------------------------------
nitrite <- read.csv("./data/water_quality/nitrite.csv")
# Visualize data
nitrite %>%
ggplot(aes(x = Total_Precipitation, y = Result)) +
geom_point() +
labs(x = "Total Precipitation (over previous 36 hours)",
y = "Nitrite concentration (mg/L)") +
theme_minimal()
nitrite %>%
ggplot(aes(x = Mean_Water_Temp, y = Result)) +
geom_point() +
labs(x = "Mean water temp (over previous 36 hours)",
y = "Nitrite concentration (mg/L)") +
theme_minimal()
# Summary statistics
summary(nitrite$Result)
summary(nitrite$Total_Precipitation)
summary(nitrite$Mean_Water_Temp)
# Histograms
nitrite %>% # raw - right skew
ggplot(aes(x = Result)) +
geom_histogram(binwidth = 0.001) +
labs(x = "Nitrite concentration (mg/L)", y = "Frequency")
nitrite %>% # ln transformed - right skew
ggplot(aes(x = ln_transformed)) +
geom_histogram(binwidth = 0.5) +
labs(x = "Ln transformed nitrite concentration (ln[mg/L])", y = "Frequency")
nitrite %>% # log transformed - right skew
ggplot(aes(x = log_transformed)) +
geom_histogram(binwidth = 0.5) +
labs(x = "Log transformed nitrite concentration (log[mg/L])", y = "Frequency")
nitrite %>% # sqrt transformed - normal
ggplot(aes(x = sqrt_transformed)) +
geom_histogram(binwidth = 0.01) +
labs(x = "Sqrt transformed nitrite concentration (sqrt[mg/L])", y = "Frequency")
# Check normality
shapiro_nitrite <- shapiro.test(nitrite$Result) %>% print() # non-normal, p = 1.98e-06
shapiro_nitrite_ln <- shapiro.test(nitrite$ln_transformed) %>% print() # non-normal, p = 4.638e-13
shapiro_nitrite_log <- shapiro.test(nitrite$log_transformed) %>% print() # non-normal, p = 4.638e-13
shapiro_nitrite_sqrt <- shapiro.test(nitrite$sqrt_transformed) %>% print() # non-normal, p = 3.252e-06
# Regressions - raw nitrite
nitrite_model_synergy <- lm(Result ~ Total_Precipitation * Mean_Water_Temp, data = nitrite) # linear model with both predictors and "*" interaction
summary(nitrite_model_synergy)
check_normality(nitrite_model_synergy) # non-normality of residuals detected (p < .001)
nitrite_model_additive <- lm(Result ~ Total_Precipitation + Mean_Water_Temp, data = nitrite) # linear model with both predictors and "+" interaction
summary(nitrite_model_additive)
check_normality(nitrite_model_additive) # non-normality of residuals detected (p < .001)
nitrite_model_precip <- lm(Result ~ Total_Precipitation, data = nitrite) # linear model with only precip
summary(nitrite_model_precip)
check_normality(nitrite_model_precip) # non-normality of residuals detected (p < .001)
nitrite_model_temp <- lm(Result ~ Mean_Water_Temp, data = nitrite) # linear model with only water temp
summary(nitrite_model_temp)
check_normality(nitrite_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(nitrite_model_synergy, nitrite_model_additive, nitrite_model_precip, nitrite_model_temp) # precip model has lowest AIC
# Regressions - ln transformed nitrite
ln_nitrite_model_synergy <- lm(ln_transformed ~ Total_Precipitation * Mean_Water_Temp, data = nitrite) # linear model with both predictors and "*" interaction
summary(ln_nitrite_model_synergy)
check_normality(ln_nitrite_model_synergy) # non-normality of residuals detected (p < .001)
ln_nitrite_model_additive <- lm(ln_transformed ~ Total_Precipitation + Mean_Water_Temp, data = nitrite) # linear model with both predictors and "+" interaction
summary(ln_nitrite_model_additive)
check_normality(ln_nitrite_model_additive) # non-normality of residuals detected (p < .001)
ln_nitrite_model_precip <- lm(ln_transformed ~ Total_Precipitation, data = nitrite) # linear model with only precip
summary(ln_nitrite_model_precip)
check_normality(ln_nitrite_model_precip) # non-normality of residuals detected (p < .001)
ln_nitrite_model_temp <- lm(ln_transformed ~ Mean_Water_Temp, data = nitrite) # linear model with only water temp
summary(ln_nitrite_model_temp)
check_normality(ln_nitrite_model_temp) # non-normality of residuals detected (p < .001)
compare_performance(ln_nitrite_model_synergy, ln_nitrite_model_additive, ln_nitrite_model_precip, ln_nitrite_model_temp) # precip model has lowest AIC
# Regressions - log transformed nitrite
log_nitrite_model_synergy <- lm(log_transformed ~ Total_Precipitation * Mean_Water_Temp, data = nitrite) # linear model with both predictors and "*" interaction
summary(log_nitrite_model_synergy)
check_normality(log_nitrite_model_synergy) # non-normality of residuals detected (p < .001)
log_nitrite_model_additive <- lm(log_transformed ~ Total_Precipitation + Mean_Water_Temp, data = nitrite) # linear model with both predictors and "+" interaction
summary(log_nitrite_model_additive)
check_normality(log_nitrite_model_additive) # non-normality of residuals detected (p < .001)
log_nitrite_model_precip <- lm(log_transformed ~ Total_Precipitation, data = nitrite) # linear model with only precip
summary(log_nitrite_model_precip)
check_normality(log_nitrite_model_precip) # non-normality of residuals detected (p < .001)