Skip to content

Commit

Permalink
updating the indicator matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
CarissaGervasi-NOAA committed Oct 2, 2024
1 parent 1887d7f commit 7b2aed8
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 122 deletions.
Binary file modified indicator_data/all_indicators_matrix.rda
Binary file not shown.
235 changes: 116 additions & 119 deletions indicator_data/extracted_ind_object_names_REVISED.csv
Original file line number Diff line number Diff line change
@@ -1,119 +1,116 @@
file_name,ind_name,desc_name
ACEindex.RData,storm_index,ACE
avgLmax.RData,PR,AVGL_PR
avgLmax.RData,STT,AVGL_STT
avgLmax.RData,STX,AVGL_STX
carib_Chl.RData,ind,CHL
Carib_SST.RData,mean,SST_MEAN
Carib_SST.RData,min,SST_MIN
Carib_SST.RData,max,SST_MAX
coral_spprichness_cover.RData,X1,CRLRCH_PR
coral_spprichness_cover.RData,X2,CRLCVR_PR
coral_spprichness_cover.RData,X3,CRLRCH_VI
coral_spprichness_cover.RData,X4,CRLCVR_VI
cruise.RData,X1,CRU_PR
cruise.RData,X2,CRU_VI
DegreeHeatingWeeks.RData,X1,DHW_PR
DegreeHeatingWeeks.RData,X2,DHW_VI
disturbance.RData,final_PR.ind,DISTB_PR
disturbance.RData,V2,DISTB_STT
disturbance.RData,V3,DISTB_STX
earthquakes.RData,as.numeric.tot_num.,ETQ
fish_density.RData,X1,DEN_PR
fish_density.RData,X2,DEN_VI
fish_density.RData,X3,SLPSIZ_VI
FRsection.RData,dat.amendments,MGMT
GDP.RData,X1,GDP_PR
GDP.RData,X2,GDP_VI
gini.RData,gini_rev_pr,GINI_PR
gini.RData,gini_rev_stt,GINI_STT
gini.RData,gini_land_stx,GINI_STX
gini_landings.RData,gini_land_pr,GINI_L_PR
gini_landings.RData,gini_land_stt,GINI_L_STT
gini_landings.RData,gini_land_stx,GINI_L_STX
gini_revenue.RData,gini_rev_pr,GINI_R_PR
gini_revenue.RData,gini_rev_stt,GINI_R_STT
gini_revenue.RData,gini_rev_stx,GINI_R_STX
marine_debris.RData,Puerto.Rico,DEB_PR
marine_debris.RData,United.States.Virgin.Islands,DEB_VI
mean_Lmax.RData,X1,MLMAX_PR
mean_Lmax.RData,X2,MLMAX_STT
mean_Lmax.RData,X3,MLMAX_STX
OA.RData,dat.V3,OA
oceanNAICS.RData,GDP.PR,O_GDP_PR
oceanNAICS.RData,GDP.USVI,O_GDP_VI
oceanNAICS.RData,est.PR,EST_PR
oceanNAICS.RData,est.USVI,EST_VI
oceanNAICS.RData,emp.PR,EMP_PR
oceanNAICS.RData,emp.USVI,EMP_VI
oceanNAICS.RData,wag.PR,WAG_PR
oceanNAICS.RData,wag.USVI,WAG_VI
outreach.RData,X1,OUT_PR
outreach.RData,X2,OUT_VI
PD_ratio.RData,X1,PDR_PR
PD_ratio.RData,X2,PDR_STT
PD_ratio.RData,X3,PDR_STX
pollution.RData,PR_ind,POLL_PR
pollution.RData,USVI_ind,POLL_VI
population.RData,pop_PR,POP_PR
population.RData,pop_USVI,POP_VI
PR_Lmax_classes.RData,Lmax1,LMAX1_PR
PR_Lmax_classes.RData,Lmax2,LMAX2_PR
PR_Lmax_classes.RData,Lmax3,LMAX3_PR
PR_Lmax_classes.RData,Lmax4,LMAX4_PR
PR_Lmax_classes.RData,Lmax5,LMAX5_PR
PR_mean_Lmax.RData,lmax,MLMAX_PR2
prop_diving_trips.RData,PR,DIV_PR
prop_diving_trips.RData,STT,DIV_STT
prop_diving_trips.RData,STX,DIV_STX
prop_trips_bycatch.RData,PR,BYCAT_PR
prop_trips_bycatch.RData,STT,BYCAT_STT
prop_trips_bycatch.RData,STX,BYCAT_STX
regulations.RData,dat.amendments,REGS
RVC_PR.RData,X1,VETU_PR
RVC_PR.RData,X2,GUTT_PR
RVC_PR.RData,X3,ANAL_PR
RVC_PR.RData,X4,CHRY_PR
RVC_PR.RData,X5,AURO_PR
RVC_PR.RData,X6,VIRI_PR
RVC_STSJ.RData,X1,VETU_STT
RVC_STSJ.RData,X2,GUTT_STT
RVC_STSJ.RData,X3,ANAL_STT
RVC_STSJ.RData,X4,CHRY_STT
RVC_STSJ.RData,X5,AURO_STT
RVC_STSJ.RData,X6,VIRI_STT
RVC_STX.RData,X1,VETU_STX
RVC_STX.RData,X2,GUTT_STX
RVC_STX.RData,X3,ANAL_STX
RVC_STX.RData,X4,CHRY_STX
RVC_STX.RData,X5,AURO_STX
RVC_STX.RData,X6,VIRI_STX
Sargassum.RData,tab,SARG
STT_Lmax_classes.RData,Lmax1,LMAX1_STT
STT_Lmax_classes.RData,Lmax2,LMAX2_STT
STT_Lmax_classes.RData,Lmax3,LMAX3_STT
STT_Lmax_classes.RData,Lmax4,LMAX4_STT
STT_Lmax_classes.RData,Lmax5,LMAX5_STT
STX_Lmax_classes.RData,Lmax1,LMAX1_STX
STX_Lmax_classes.RData,Lmax2,LMAX2_STX
STX_Lmax_classes.RData,Lmax3,LMAX3_STX
STX_Lmax_classes.RData,Lmax4,LMAX4_STX
STX_Lmax_classes.RData,Lmax5,LMAX5_STX
tier3.RData,PR,TR3_PR
tier3.RData,USVI,TR3_VI
total_landings.RData,X1,LOB_PR
total_landings.RData,X2,LOB_STT
total_landings.RData,X3,LOB_STX
total_landings.RData,X4,CONC_PR
total_landings.RData,X5,CONC_STT
total_landings.RData,X6,CONC_STX
total_landings.RData,X7,OTHR_PR
total_landings.RData,X8,OTHR_STT
total_landings.RData,X9,OTHR_STX
total_rec_catch.RData,Puerto.Rico..USA.,REC_PR
total_rec_catch.RData,US.Virgin.Isl.,REC_VI
turbidity.RData,X1,TURB_PR
turbidity.RData,X2,TURB_STT
turbidity.RData,X3,TURB_STX
unemployment.RData,X1,UNEM_PR
unemployment.RData,X2,UNEM_VI
file_name,ind_name,desc_name,order
DegreeHeatingWeeks.RData,X1,DHW_PR,1
DegreeHeatingWeeks.RData,X2,DHW_VI,2
OA.RData,"annual_data[, -1]",OA,3
ACEindex.RData,storm_index,ACE,4
turbidity.RData,X1,TURB_PR,5
turbidity.RData,X2,TURB_STT,6
turbidity.RData,X3,TURB_STX,7
Carib_SST.RData,mean,SST_MEAN,8
Carib_SST.RData,min,SST_MIN,9
Carib_SST.RData,max,SST_MAX,10
marine_debris.RData,Puerto.Rico,DEB_PR,11
marine_debris.RData,United.States.Virgin.Islands,DEB_VI,12
pollution.RData,PR_ind,POLL_PR,13
pollution.RData,USVI_ind,POLL_VI,14
carib_Chl.RData,"annual_data[, -1]",CHL,15
earthquakes.RData,as.numeric.tot_num.,ETQ,16
disturbance.RData,final_PR.ind,DISTB_PR,17
disturbance.RData,V2,DISTB_STT,18
disturbance.RData,V3,DISTB_STX,19
Sargassum.RData,tab,SARG,20
cruise.RData,X1,CRU_PR,21
cruise.RData,X2,CRU_VI,22
population.RData,pop_PR,POP_PR,23
population.RData,pop_USVI,POP_VI,24
RVC_PR.RData,X1,VETU_PR,25
RVC_PR.RData,X2,GUTT_PR,26
RVC_PR.RData,X3,ANAL_PR,27
RVC_PR.RData,X4,CHRY_PR,28
RVC_PR.RData,X5,AURO_PR,29
RVC_PR.RData,X6,VIRI_PR,30
RVC_STSJ.RData,X1,VETU_STT,31
RVC_STSJ.RData,X2,GUTT_STT,32
RVC_STSJ.RData,X3,ANAL_STT,33
RVC_STSJ.RData,X4,CHRY_STT,34
RVC_STSJ.RData,X5,AURO_STT,35
RVC_STSJ.RData,X6,VIRI_STT,36
RVC_STX.RData,X1,VETU_STX,37
RVC_STX.RData,X2,GUTT_STX,38
RVC_STX.RData,X3,ANAL_STX,39
RVC_STX.RData,X4,CHRY_STX,40
RVC_STX.RData,X5,AURO_STX,41
RVC_STX.RData,X6,VIRI_STX,42
fish_density.RData,X1,DEN_PR,43
fish_density.RData,X2,DEN_VI,44
fish_density.RData,X3,SLPSIZ_VI,45
PD_ratio.RData,X1,PDR_PR,46
PD_ratio.RData,X2,PDR_STT,47
PD_ratio.RData,X3,PDR_STX,48
mean_Lmax.RData,X1,MLMAX_PR,49
mean_Lmax.RData,X2,MLMAX_STT,50
mean_Lmax.RData,X3,MLMAX_STX,51
PR_Lmax_classes.RData,Lmax1,LMAX1_PR,52
PR_Lmax_classes.RData,Lmax2,LMAX2_PR,53
PR_Lmax_classes.RData,Lmax3,LMAX3_PR,54
PR_Lmax_classes.RData,Lmax4,LMAX4_PR,55
PR_Lmax_classes.RData,Lmax5,LMAX5_PR,56
PR_mean_Lmax.RData,lmax,MLMAX_PR2,57
STT_Lmax_classes.RData,Lmax1,LMAX1_STT,58
STT_Lmax_classes.RData,Lmax2,LMAX2_STT,59
STT_Lmax_classes.RData,Lmax3,LMAX3_STT,60
STT_Lmax_classes.RData,Lmax4,LMAX4_STT,61
STT_Lmax_classes.RData,Lmax5,LMAX5_STT,62
STX_Lmax_classes.RData,Lmax1,LMAX1_STX,63
STX_Lmax_classes.RData,Lmax2,LMAX2_STX,64
STX_Lmax_classes.RData,Lmax3,LMAX3_STX,65
STX_Lmax_classes.RData,Lmax4,LMAX4_STX,66
STX_Lmax_classes.RData,Lmax5,LMAX5_STX,67
total_landings.RData,X1,LOB_PR,68
total_landings.RData,X2,LOB_STT,69
total_landings.RData,X3,LOB_STX,70
total_landings.RData,X4,CONC_PR,71
total_landings.RData,X5,CONC_STT,72
total_landings.RData,X6,CONC_STX,73
total_landings.RData,X7,OTHR_PR,74
total_landings.RData,X8,OTHR_STT,75
total_landings.RData,X9,OTHR_STX,76
oceanNAICS.RData,GDP.PR,O_GDP_PR,77
oceanNAICS.RData,GDP.USVI,O_GDP_VI,78
oceanNAICS.RData,est.PR,EST_PR,79
oceanNAICS.RData,est.USVI,EST_VI,80
oceanNAICS.RData,emp.PR,EMP_PR,81
oceanNAICS.RData,emp.USVI,EMP_VI,82
oceanNAICS.RData,wag.PR,WAG_PR,83
oceanNAICS.RData,wag.USVI,WAG_VI,84
GDP.RData,X1,GDP_PR,85
GDP.RData,X2,GDP_VI,86
unemployment.RData,X1,UNEM_PR,87
unemployment.RData,X2,UNEM_VI,88
gini.RData,gini_rev_pr,GINI_PR,89
gini.RData,gini_rev_stt,GINI_STT,90
gini.RData,gini_land_stx,GINI_STX,91
gini_landings.RData,gini_land_pr,GINI_L_PR,92
gini_landings.RData,gini_land_stt,GINI_L_STT,93
gini_landings.RData,gini_land_stx,GINI_L_STX,94
gini_revenue.RData,gini_rev_pr,GINI_R_PR,95
gini_revenue.RData,gini_rev_stt,GINI_R_STT,96
gini_revenue.RData,gini_rev_stx,GINI_R_STX,97
total_rec_catch.RData,Puerto.Rico..USA.,REC_PR,98
total_rec_catch.RData,US.Virgin.Isl.,REC_VI,99
prop_diving_trips.RData,PR,DIV_PR,100
prop_diving_trips.RData,STT,DIV_STT,101
prop_diving_trips.RData,STX,DIV_STX,102
prop_trips_bycatch.RData,PR,BYCAT_PR,103
prop_trips_bycatch.RData,STT,BYCAT_STT,104
prop_trips_bycatch.RData,STX,BYCAT_STX,105
regulations.RData,dat.amendments,REGS,106
FRsection.RData,dat.amendments,MGMT,107
tier3.RData,PR,TR3_PR,108
tier3.RData,USVI,TR3_VI,109
outreach.RData,X1,OUT_PR,110
outreach.RData,X2,OUT_VI,111
coral_spprichness_cover.RData,X1,CRLRCH_PR,112
coral_spprichness_cover.RData,X2,CRLCVR_PR,113
coral_spprichness_cover.RData,X3,CRLRCH_VI,114
coral_spprichness_cover.RData,X4,CRLCVR_VI,115
89 changes: 86 additions & 3 deletions indicator_processing/CreateIndicatorMatrix.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Script to pull all the indicators from the indicator_objects folder and merge the data into a matrix that can be used for stoplight plots.

# I am working on turning this into a function and including the indicators with monthly data. For the monthly indicators we need to just average those into annual time steps.
# For the monthly indicators we need to just average those into annual time steps. (Not yet included)


rm(list = ls())
Expand Down Expand Up @@ -36,7 +36,69 @@ rdata_files <- list.files(indicator_dir, pattern = "\\.RData$", full.names = TRU
all_data <- lapply(rdata_files, extract_data)
all_data <- Filter(Negate(is.null), all_data)

# Extract the datelist vectors and keep only those that are of class integer


# Before we go any further, we need to pull out the indicators with monthly data and average them into annual indicators

# the monthly indicators include Carib_Chl, Carib_SST, DegreeHeatingWeeks, OA, turbidity, and unemployment

# Convert monthly data to annual by averaging values per year
convert_to_annual <- function(data_list, date_format) {
# Extract the date and indicator values
name <- data_list$file_name
dates <- data_list$datelist
values <- data_list$indicators


# Parse the date according to the specified format
if (date_format == "%m-%Y") {
years <- as.integer(format(as.Date(paste0("01-", dates), "%d-%m-%Y"), "%Y"))
} else if (date_format == "%Y%m") {
years <- as.integer(substr(dates, 1, 4)) # Extract the first 4 characters as year
} else if (date_format == "%b%Y") {
years <- as.integer(format(as.Date(paste0("01", dates), "%d%b%Y"), "%Y"))
} else {
stop("Unsupported date format")
}

# Create a dataframe for easier manipulation
df <- data.frame(year = years, values)

# Group by year and calculate the annual average
annual_data <- aggregate(. ~ year, data = df, FUN = mean)

# Update the datelist and indicators with the annual data
data_list$datelist <- annual_data$year
data_list$indicators <- as.data.frame(annual_data[, -1])

return(data_list)
}

# Apply the function to each monthly dataset
carib_Chl <- convert_to_annual(all_data[[3]], "%m-%Y")
Carib_SST <- convert_to_annual(all_data[[4]], "%m-%Y")
DegreeHeatingWeeks <- convert_to_annual(all_data[[7]], "%Y%m")
OA <- convert_to_annual(all_data[[18]], "%b%Y")
turbidity <- convert_to_annual(all_data[[38]], "%m-%Y")
unemployment <- convert_to_annual(all_data[[39]], "%Y%m")


# Ok now we have all the monthly indicators as annual indicators. We need to replace these in the all_data list.

all_data[[3]] = carib_Chl
all_data[[4]] = Carib_SST
all_data[[7]] = DegreeHeatingWeeks
all_data[[18]] = OA
all_data[[38]] = turbidity
all_data[[39]] = unemployment



########################################################

#Now back to making the indicator matrix

# Extract the datelist vectors and keep only those that are of class integer (this isn't really necessary now that we've taken the average of the monthly data, but can keep if we want to ignore the monthly data and skip the above code chunk)
datelists <- lapply(all_data, function(x) x$datelist)
datelists <- datelists[sapply(datelists, function(d) class(d) == "integer")]

Expand All @@ -47,6 +109,8 @@ year_range <- seq(min(years, na.rm = TRUE), max(years, na.rm = TRUE))
# Create an empty matrix with "year" column
matrix_data <- data.frame(year = year_range)



# Function to add indicator columns to the matrix
add_indicator_columns <- function(matrix_data, data, descriptive_names) {
datelist <- data$datelist
Expand Down Expand Up @@ -81,7 +145,26 @@ for (data in all_data) {
# Print the resulting matrix
print(matrix_data)

# re-order the columns

# Get the current column names of the matrix (excluding the first one)
current_colnames <- colnames(matrix_data)[-1]

# Match the current column names with the "desc_name" in the descriptive_names data
reorder_index <- match(current_colnames, descriptive_names$desc_name)

# Check for any unmatched columns
if (any(is.na(reorder_index))) {
print("Warning: Some columns in the matrix do not match the CSV 'desc_name'.")
}

# Remove NA values from reorder_index and sort according to the 'order' column
valid_columns <- !is.na(reorder_index)
sorted_columns <- current_colnames[valid_columns][order(descriptive_names$order[reorder_index[valid_columns]])]

# Reorganize the matrix, keeping the first column unchanged
matrix_data <- matrix_data[, c(1, match(sorted_columns, colnames(matrix_data)))]

# Save the matrix_data to an .rda file
save(matrix_data, file = "indicator_data/all_indicators_matrix.rda")


0 comments on commit 7b2aed8

Please sign in to comment.