Biological Condition Gradient Level assignment given metric memberships.
BCG.Level.Membership(
df.metric.membership,
df.rules,
col_SAMPLEID = "SAMPLEID",
col_INDEX_NAME = "INDEX_NAME",
col_INDEX_CLASS = "INDEX_CLASS",
col_LEVEL = "LEVEL",
col_METRIC_NAME = "METRIC_NAME",
col_RULE_TYPE = "RULE_TYPE",
col_EXC_RULE = "EXC_RULE",
col_MEMBERSHIP = "MEMBERSHIP",
...
)Data frame of metric memberships (long format, the same as the output of BCG.Metric.Membership).
Data frame of BCG model rules.
column name for sample id. Default = SAMPLEID
column name for index name. Default = INDEX_NAME
column name for site type.Default = INDEX_CLASS
column name for level. Default = LEVEL
column name for metric name. Default = METRIC_NAME
column name for rule type (e.g., Rule0, Rule1, or Rule2). Default = RULE_TYPE
column name for exception rules. Default = EXC_RULE
column name for metric membership. Default = MEMBERSHIP
Arguments passed to BCG.MetricMembership used internally
Returns a data frame of results in the wide format.
Input is metric memberships and a rules tables.
Output is a data frame with the membership for each row to each Level (1:6).
Minimum of:
1- sum of previous levels
Rule0 memberships
max of Rule1 (Alternate1) rules (and min of Rule2 (Alternate2) rules)
That is, perform calculations in this order:
Min of Rule2 (Alternate2) metric memberships
Max of Rule1 (Alternate1) rules and the above result.
Min of: Rule0, the above results, and 1 - the sum of previous levels.
Some exceptions exist for particular models.
| Index_Name | INDEX_CLASS |
| CT_BCG_2015 | fish02 |
| CT_BCG_2015 | fish03 |
| BCG_NMSandyRivers | bugs |
These exceptions are mostly hard coded into the function but gather some information with the parameter col_EXC_RULE from the rules table. A future update may fully automate this process.
2021 saw the introduction of Median Exception rule. For the Pacific Northwest some metrics were grouped and the 2nd of 3 values is used and the other 2 values tossed when determining level membership. This equates to using the median of the 3 values. This is handled by including "MEDIAN" in the Exc_Rule column in Rules.xlsx. Superceded by "SMALL2".
2024 added SMALL2 and SMALL3 Exception rules. For New Mexico BCG some metrics are grouped so use the 2nd or 3rd smallest value instead of the minimum. As above, this is handled by including "SMALL2" or "SMALL3" in the Exc_Rule column in Rules.xlsx.
Some Great Plains rules use multiple groupings of SMALL2. These are coded as "SMALL2A" and "SMALL2B". If additional groupings are needed the code needs to be tweaked.
Deprecated col_SITE_TYPE for col_INDEX_CLASS in v2.0.0.9001.
# library(readxl)
# library(BioMonTools)
# Calculate Metrics
df_samps_bugs <- readxl::read_excel(
system.file("extdata/Data_BCG_PugLowWilVal.xlsx"
, package="BCGcalc")
, guess_max = 10^6)
myDF <- df_samps_bugs
myCols <- c("Area_mi2", "SurfaceArea", "Density_m2", "Density_ft2")
# populate missing columns prior to metric calculation
col_missing <- c("INFRAORDER", "HABITAT", "ELEVATION_ATTR", "GRADIENT_ATTR"
, "WSAREA_ATTR", "HABSTRUCT", "UFC")
myDF[, col_missing] <- NA
df_met_val_bugs <- BioMonTools::metric.values(myDF
, "bugs"
, fun.cols2keep = myCols)
#> Joining with `by = join_by(SAMPLEID, INDEX_NAME, INDEX_CLASS)`
# Import Rules
df_rules <- readxl::read_excel(system.file("extdata/Rules.xlsx"
, package = "BCGcalc")
, sheet="Rules")
#> Warning: Expecting numeric in C1980 / R1980C3: got 'Level 2'
#> Warning: Expecting numeric in C1981 / R1981C3: got 'Level 2'
#> Warning: Expecting numeric in C1982 / R1982C3: got 'Level 2'
#> Warning: Expecting numeric in C1983 / R1983C3: got 'Level 2'
#> Warning: Expecting numeric in C1984 / R1984C3: got 'Level 2'
#> Warning: Expecting numeric in C1985 / R1985C3: got 'Level 2'
#> Warning: Expecting numeric in C1986 / R1986C3: got 'Level 2'
#> Warning: Expecting numeric in C1987 / R1987C3: got 'Level 2'
#> Warning: Expecting numeric in C1988 / R1988C3: got 'Level 2'
#> Warning: Expecting numeric in C1989 / R1989C3: got 'Level 2'
#> Warning: Expecting numeric in C1990 / R1990C3: got 'Level 3'
#> Warning: Expecting numeric in C1991 / R1991C3: got 'Level 3'
#> Warning: Expecting numeric in C1992 / R1992C3: got 'Level 3'
#> Warning: Expecting numeric in C1993 / R1993C3: got 'Level 3'
#> Warning: Expecting numeric in C1994 / R1994C3: got 'Level 3'
#> Warning: Expecting numeric in C1995 / R1995C3: got 'Level 3'
#> Warning: Expecting numeric in C1996 / R1996C3: got 'Level 3'
#> Warning: Expecting numeric in C1997 / R1997C3: got 'Level 3'
#> Warning: Expecting numeric in C1998 / R1998C3: got 'Level 4'
#> Warning: Expecting numeric in C1999 / R1999C3: got 'Level 4'
#> Warning: Expecting numeric in C2000 / R2000C3: got 'Level 4'
#> Warning: Expecting numeric in C2001 / R2001C3: got 'Level 4'
#> Warning: Expecting numeric in C2002 / R2002C3: got 'Level 4'
#> Warning: Expecting numeric in C2003 / R2003C3: got 'Level 4'
#> Warning: Expecting numeric in C2004 / R2004C3: got 'Level 4'
#> Warning: Expecting numeric in C2005 / R2005C3: got 'Level 5'
#> Warning: Expecting numeric in C2006 / R2006C3: got 'Level 5'
#> Warning: Expecting numeric in C2007 / R2007C3: got 'Level 5'
#> Warning: Expecting numeric in C2008 / R2008C3: got 'Level 2'
#> Warning: Expecting numeric in C2009 / R2009C3: got 'Level 2'
#> Warning: Expecting numeric in C2010 / R2010C3: got 'Level 2'
#> Warning: Expecting numeric in C2011 / R2011C3: got 'Level 2'
#> Warning: Expecting numeric in C2012 / R2012C3: got 'Level 2'
#> Warning: Expecting numeric in C2013 / R2013C3: got 'Level 2'
#> Warning: Expecting numeric in C2014 / R2014C3: got 'Level 2'
#> Warning: Expecting numeric in C2015 / R2015C3: got 'Level 2'
#> Warning: Expecting numeric in C2016 / R2016C3: got 'Level 2'
#> Warning: Expecting numeric in C2017 / R2017C3: got 'Level 2'
#> Warning: Expecting numeric in C2018 / R2018C3: got 'Level 3'
#> Warning: Expecting numeric in C2019 / R2019C3: got 'Level 3'
#> Warning: Expecting numeric in C2020 / R2020C3: got 'Level 3'
#> Warning: Expecting numeric in C2021 / R2021C3: got 'Level 3'
#> Warning: Expecting numeric in C2022 / R2022C3: got 'Level 3'
#> Warning: Expecting numeric in C2023 / R2023C3: got 'Level 3'
#> Warning: Expecting numeric in C2024 / R2024C3: got 'Level 3'
#> Warning: Expecting numeric in C2025 / R2025C3: got 'Level 3'
#> Warning: Expecting numeric in C2026 / R2026C3: got 'Level 4'
#> Warning: Expecting numeric in C2027 / R2027C3: got 'Level 4'
#> Warning: Expecting numeric in C2028 / R2028C3: got 'Level 4'
#> Warning: Expecting numeric in C2029 / R2029C3: got 'Level 4'
#> Warning: Expecting numeric in C2030 / R2030C3: got 'Level 4'
#> Warning: Expecting numeric in C2031 / R2031C3: got 'Level 4'
#> Warning: Expecting numeric in C2032 / R2032C3: got 'Level 4'
#> Warning: Expecting numeric in C2033 / R2033C3: got 'Level 5'
#> Warning: Expecting numeric in C2034 / R2034C3: got 'Level 5'
#> Warning: Expecting numeric in C2035 / R2035C3: got 'Level 5'
#> Warning: Expecting numeric in C2036 / R2036C3: got 'Level 2'
#> Warning: Expecting numeric in C2037 / R2037C3: got 'Level 2'
#> Warning: Expecting numeric in C2038 / R2038C3: got 'Level 2'
#> Warning: Expecting numeric in C2039 / R2039C3: got 'Level 2'
#> Warning: Expecting numeric in C2040 / R2040C3: got 'Level 2'
#> Warning: Expecting numeric in C2041 / R2041C3: got 'Level 2'
#> Warning: Expecting numeric in C2042 / R2042C3: got 'Level 2'
#> Warning: Expecting numeric in C2043 / R2043C3: got 'Level 2'
#> Warning: Expecting numeric in C2044 / R2044C3: got 'Level 2'
#> Warning: Expecting numeric in C2045 / R2045C3: got 'Level 2'
#> Warning: Expecting numeric in C2046 / R2046C3: got 'Level 3'
#> Warning: Expecting numeric in C2047 / R2047C3: got 'Level 3'
#> Warning: Expecting numeric in C2048 / R2048C3: got 'Level 3'
#> Warning: Expecting numeric in C2049 / R2049C3: got 'Level 3'
#> Warning: Expecting numeric in C2050 / R2050C3: got 'Level 3'
#> Warning: Expecting numeric in C2051 / R2051C3: got 'Level 3'
#> Warning: Expecting numeric in C2052 / R2052C3: got 'Level 3'
#> Warning: Expecting numeric in C2053 / R2053C3: got 'Level 3'
#> Warning: Expecting numeric in C2054 / R2054C3: got 'Level 3'
#> Warning: Expecting numeric in C2055 / R2055C3: got 'Level 4'
#> Warning: Expecting numeric in C2056 / R2056C3: got 'Level 4'
#> Warning: Expecting numeric in C2057 / R2057C3: got 'Level 4'
#> Warning: Expecting numeric in C2058 / R2058C3: got 'Level 4'
#> Warning: Expecting numeric in C2059 / R2059C3: got 'Level 4'
#> Warning: Expecting numeric in C2060 / R2060C3: got 'Level 4'
#> Warning: Expecting numeric in C2061 / R2061C3: got 'Level 4'
#> Warning: Expecting numeric in C2062 / R2062C3: got 'Level 4'
#> Warning: Expecting numeric in C2063 / R2063C3: got 'Level 5'
#> Warning: Expecting numeric in C2064 / R2064C3: got 'Level 5'
#> Warning: Expecting numeric in C2065 / R2065C3: got 'Level 5'
#> Warning: Expecting numeric in C2066 / R2066C3: got 'Level 2'
#> Warning: Expecting numeric in C2067 / R2067C3: got 'Level 2'
#> Warning: Expecting numeric in C2068 / R2068C3: got 'Level 2'
#> Warning: Expecting numeric in C2069 / R2069C3: got 'Level 2'
#> Warning: Expecting numeric in C2070 / R2070C3: got 'Level 2'
#> Warning: Expecting numeric in C2071 / R2071C3: got 'Level 2'
#> Warning: Expecting numeric in C2072 / R2072C3: got 'Level 2'
#> Warning: Expecting numeric in C2073 / R2073C3: got 'Level 2'
#> Warning: Expecting numeric in C2074 / R2074C3: got 'Level 2'
#> Warning: Expecting numeric in C2075 / R2075C3: got 'Level 3'
#> Warning: Expecting numeric in C2076 / R2076C3: got 'Level 3'
#> Warning: Expecting numeric in C2077 / R2077C3: got 'Level 3'
#> Warning: Expecting numeric in C2078 / R2078C3: got 'Level 3'
#> Warning: Expecting numeric in C2079 / R2079C3: got 'Level 3'
#> Warning: Expecting numeric in C2080 / R2080C3: got 'Level 3'
#> Warning: Expecting numeric in C2081 / R2081C3: got 'Level 3'
#> Warning: Expecting numeric in C2082 / R2082C3: got 'Level 4'
#> Warning: Expecting numeric in C2083 / R2083C3: got 'Level 4'
#> Warning: Expecting numeric in C2084 / R2084C3: got 'Level 4'
#> Warning: Expecting numeric in C2085 / R2085C3: got 'Level 5'
#> Warning: Expecting numeric in C2086 / R2086C3: got 'Level 2'
#> Warning: Expecting numeric in C2087 / R2087C3: got 'Level 2'
#> Warning: Expecting numeric in C2088 / R2088C3: got 'Level 2'
#> Warning: Expecting numeric in C2089 / R2089C3: got 'Level 2'
#> Warning: Expecting numeric in C2090 / R2090C3: got 'Level 2'
#> Warning: Expecting numeric in C2091 / R2091C3: got 'Level 2'
#> Warning: Expecting numeric in C2092 / R2092C3: got 'Level 2'
#> Warning: Expecting numeric in C2093 / R2093C3: got 'Level 2'
#> Warning: Expecting numeric in C2094 / R2094C3: got 'Level 3'
#> Warning: Expecting numeric in C2095 / R2095C3: got 'Level 3'
#> Warning: Expecting numeric in C2096 / R2096C3: got 'Level 3'
#> Warning: Expecting numeric in C2097 / R2097C3: got 'Level 3'
#> Warning: Expecting numeric in C2098 / R2098C3: got 'Level 3'
#> Warning: Expecting numeric in C2099 / R2099C3: got 'Level 4'
#> Warning: Expecting numeric in C2100 / R2100C3: got 'Level 4'
#> Warning: Expecting numeric in C2101 / R2101C3: got 'Level 4'
#> Warning: Expecting numeric in C2102 / R2102C3: got 'Level 5'
#> Warning: Expecting numeric in C2103 / R2103C3: got 'Level 2'
#> Warning: Expecting numeric in C2104 / R2104C3: got 'Level 2'
#> Warning: Expecting numeric in C2105 / R2105C3: got 'Level 2'
#> Warning: Expecting numeric in C2106 / R2106C3: got 'Level 2'
#> Warning: Expecting numeric in C2107 / R2107C3: got 'Level 2'
#> Warning: Expecting numeric in C2108 / R2108C3: got 'Level 2'
#> Warning: Expecting numeric in C2109 / R2109C3: got 'Level 2'
#> Warning: Expecting numeric in C2110 / R2110C3: got 'Level 2'
#> Warning: Expecting numeric in C2111 / R2111C3: got 'Level 2'
#> Warning: Expecting numeric in C2112 / R2112C3: got 'Level 2'
#> Warning: Expecting numeric in C2113 / R2113C3: got 'Level 2'
#> Warning: Expecting numeric in C2114 / R2114C3: got 'Level 2'
#> Warning: Expecting numeric in C2115 / R2115C3: got 'Level 2'
#> Warning: Expecting numeric in C2116 / R2116C3: got 'Level 3'
#> Warning: Expecting numeric in C2117 / R2117C3: got 'Level 3'
#> Warning: Expecting numeric in C2118 / R2118C3: got 'Level 3'
#> Warning: Expecting numeric in C2119 / R2119C3: got 'Level 3'
#> Warning: Expecting numeric in C2120 / R2120C3: got 'Level 3'
#> Warning: Expecting numeric in C2121 / R2121C3: got 'Level 3'
#> Warning: Expecting numeric in C2122 / R2122C3: got 'Level 3'
#> Warning: Expecting numeric in C2123 / R2123C3: got 'Level 3'
#> Warning: Expecting numeric in C2124 / R2124C3: got 'Level 3'
#> Warning: Expecting numeric in C2125 / R2125C3: got 'Level 3'
#> Warning: Expecting numeric in C2126 / R2126C3: got 'Level 3'
#> Warning: Expecting numeric in C2127 / R2127C3: got 'Level 4'
#> Warning: Expecting numeric in C2128 / R2128C3: got 'Level 4'
#> Warning: Expecting numeric in C2129 / R2129C3: got 'Level 4'
#> Warning: Expecting numeric in C2130 / R2130C3: got 'Level 4'
#> Warning: Expecting numeric in C2131 / R2131C3: got 'Level 4'
#> Warning: Expecting numeric in C2132 / R2132C3: got 'Level 4'
#> Warning: Expecting numeric in C2133 / R2133C3: got 'Level 4'
#> Warning: Expecting numeric in C2134 / R2134C3: got 'Level 4'
#> Warning: Expecting numeric in C2135 / R2135C3: got 'Level 5'
#> Warning: Expecting numeric in C2136 / R2136C3: got 'Level 5'
#> Warning: Expecting numeric in C2137 / R2137C3: got 'Level 5'
#> Warning: Expecting numeric in C2138 / R2138C3: got 'Level 5'
#> Warning: Expecting numeric in C2139 / R2139C3: got 'Level 5'
#> Warning: Expecting numeric in C2140 / R2140C3: got 'Level 2'
#> Warning: Expecting numeric in C2141 / R2141C3: got 'Level 2'
#> Warning: Expecting numeric in C2142 / R2142C3: got 'Level 2'
#> Warning: Expecting numeric in C2143 / R2143C3: got 'Level 2'
#> Warning: Expecting numeric in C2144 / R2144C3: got 'Level 2'
#> Warning: Expecting numeric in C2145 / R2145C3: got 'Level 2'
#> Warning: Expecting numeric in C2146 / R2146C3: got 'Level 2'
#> Warning: Expecting numeric in C2147 / R2147C3: got 'Level 2'
#> Warning: Expecting numeric in C2148 / R2148C3: got 'Level 2'
#> Warning: Expecting numeric in C2149 / R2149C3: got 'Level 2'
#> Warning: Expecting numeric in C2150 / R2150C3: got 'Level 3'
#> Warning: Expecting numeric in C2151 / R2151C3: got 'Level 3'
#> Warning: Expecting numeric in C2152 / R2152C3: got 'Level 3'
#> Warning: Expecting numeric in C2153 / R2153C3: got 'Level 3'
#> Warning: Expecting numeric in C2154 / R2154C3: got 'Level 3'
#> Warning: Expecting numeric in C2155 / R2155C3: got 'Level 3'
#> Warning: Expecting numeric in C2156 / R2156C3: got 'Level 3'
#> Warning: Expecting numeric in C2157 / R2157C3: got 'Level 3'
#> Warning: Expecting numeric in C2158 / R2158C3: got 'Level 3'
#> Warning: Expecting numeric in C2159 / R2159C3: got 'Level 4'
#> Warning: Expecting numeric in C2160 / R2160C3: got 'Level 4'
#> Warning: Expecting numeric in C2161 / R2161C3: got 'Level 4'
#> Warning: Expecting numeric in C2162 / R2162C3: got 'Level 4'
#> Warning: Expecting numeric in C2163 / R2163C3: got 'Level 4'
#> Warning: Expecting numeric in C2164 / R2164C3: got 'Level 4'
#> Warning: Expecting numeric in C2165 / R2165C3: got 'Level 4'
#> Warning: Expecting numeric in C2166 / R2166C3: got 'Level 4'
#> Warning: Expecting numeric in C2167 / R2167C3: got 'Level 5'
#> Warning: Expecting numeric in C2168 / R2168C3: got 'Level 5'
#> Warning: Expecting numeric in C2169 / R2169C3: got 'Level 5'
#> Warning: Expecting numeric in C2170 / R2170C3: got 'Level 5'
# Calculate Metric Memberships
df_met_memb <- BCG.Metric.Membership(df_met_val_bugs, df_rules)
# Calculate Level Memberships
df_lev_memb <- BCG.Level.Membership(df_met_memb, df_rules)
#> Warning: There were 14 warnings in `dplyr::summarize()`.
#> The first warning was:
#> ℹ In argument: `rules_lev_min = min(LEVEL, na.rm = TRUE)`.
#> ℹ In group 11: `INDEX_NAME = "BCG_ORWA_v1_Bugs500ct"`, `INDEX_CLASS =
#> "foothills_multihab"`.
#> Caused by warning in `min()`:
#> ! no non-missing arguments to min; returning Inf
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 13 remaining warnings.
# Show results
#View(df_lev_memb)
# Save Results
write.table(df_lev_memb
, file.path(tempdir(), "Level_Membership.tsv")
, row.names = FALSE
, col.names = TRUE
, sep = "\t")