BCG.Level.Assignment: BCG Level Assignment

Description Usage Arguments Details Value Examples

Description

Biological Condition Gradient level assignment (1st and 2nd) given Level memberships.

Usage

1
BCG.Level.Assignment(df.level.membership)

Arguments

df.level.membership

Wide data frame with level memberships (0-1). L1 to L6.

Details

Input is L1 to L6 with membership values of 0 to 1. Result is 1st (primary) Level (Lev.1.Name) and 2nd (secondary) Level (Lev.2.Name). Also give close (Lev.Memb.close) and a proportional Level assignment ("Lev.Prop").

Add QC Checks.

Value

Returns a data frame of results in the wide format.

Examples

 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
# construct a dummy dataset
L1 <- c(rep(0, 10), 0, 0)
L2 <- c(0.4, 0, 0.4, rep(0,7), 0, 0)
L3 <- c(0.6, 0, 0.6, 0, 0.42, 0, 1, 1, 0.22, 0.33, 0.5, 0)
L4 <- c(0, 0.9, 0, 0, 0.58, 0.05, 0, 0, 0.78, 0.67, 0.5, 0)
L5 <- c(0, 0.1, 0, 1, 0, 0.95, rep(0,4), 0, 1)
L6 <- c(rep(0, 10), 0, 0)
SampleID <- LETTERS[1:length(L1)]
df.Level.Membership <- as.data.frame(SampleID, stringsAsFactors=FALSE)
df.Level.Membership[,"L1"] <- L1
df.Level.Membership[,"L2"] <- L2
df.Level.Membership[,"L3"] <- L3
df.Level.Membership[,"L4"] <- L4
df.Level.Membership[,"L5"] <- L5
df.Level.Membership[,"L6"] <- L6

# Run Function
df.Levels <- BCG.Level.Assignment(df.Level.Membership)

## Not run: 
# Show Results
View(df.Levels)

# Save Results
write.table(df.Levels, "Levels.tsv"
            , row.names=FALSE, col.names=TRUE, sep="\t")

## End(Not run)

#~~~~~~~~~~~~~~~~~~~~~~~

# Example Data

library(readxl)
library(reshape2) 
library(BioMonTools)

# Calculate Metrics
df.samps.bugs <- read_excel(system.file("./extdata/Data_BCG_PacNW.xlsx"
                                        , package="BCGcalc")
                           , guess_max = 10^6)
                                        
# Run Function
myDF <- df.samps.bugs
myCols <- c("Area_mi2", "SurfaceArea", "Density_m2", "Density_ft2", "Site_Type")
df.metric.values.bugs <- metric.values(myDF, "bugs", fun.cols2keep=myCols) 

# Import Rules
df.rules <- read_excel(system.file("./extdata/Rules.xlsx"
                             , package="BCGcalc"), sheet="BCG_PacNW_v1_500ct") 

# Calculate Metric Memberships
df.Metric.Membership <- BCG.Metric.Membership(df.metric.values.bugs, df.rules)

# Calculate Level Memberships
df.Level.Membership <- BCG.Level.Membership(df.Metric.Membership, df.rules)

# Run Function
df.Levels <- BCG.Level.Assignment(df.Level.Membership)

# QC Checks (flags)
#
# Import Checks
df.checks <- read_excel(system.file("./extdata/MetricFlags.xlsx"
                                          , package="BCGcalc"), sheet="Flags") 

# Run Function
df.flags <- qc.checks(df.metric.values.bugs, df.checks)
# Change terminology; PASS/FAIL to NA/flag
df.flags[,"FLAG"][df.flags[,"FLAG"]=="FAIL"] <- "flag"
df.flags[, "FLAG"][df.flags[,"FLAG"]=="PASS"] <- NA

# long to wide format
df.flags.wide <- dcast(df.flags, SAMPLEID ~ CHECKNAME, value.var="FLAG")
# Calc number of "flag"s by row.
df.flags.wide$NumFlags <- rowSums(df.flags.wide=="flag", na.rm=TRUE)
# Rearrange columns
NumCols <- ncol(df.flags.wide)
df.flags.wide <- df.flags.wide[, c(1, NumCols, 2:(NumCols-1))]

# Merge Levels and Flags
df.Levels.Flags <- merge(df.Levels, df.flags.wide, by="SAMPLEID", all.x=TRUE)
             
# Summarize Results
table(df.flags[,"CHECKNAME"], df.flags[,"FLAG"], useNA="ifany")

## Not run: 
# Show Results
View(df.Levels.Flags)

# Save Results
write.csv(df.Levels.Flags, "Levels.Flags.csv")

## End(Not run)

leppott/BCGcalc documentation built on June 29, 2019, 10:41 a.m.