Check and Normalize Prior Probabilities and Misclassification Costs
Source:R/utils.R
checkPriorAndMisClassCost.Rd
This function verifies and normalizes the provided prior probabilities and
misclassification cost matrix for a given response variable. It ensures that
the lengths of the prior and the dimensions of the misclassification cost
matrix match the number of levels in the response variable. If prior
or
misClassCost
are not provided, default values are used: the prior is set to
the observed frequencies of the response, and the misclassification cost
matrix is set to 1 for all misclassifications and 0 for correct
classifications.
Arguments
- prior
A numeric vector representing the prior probabilities for each class in the response variable. If
NULL
, the observed frequencies of the response are used as the default prior.- misClassCost
A square matrix representing the misclassification costs for each pair of classes in the response variable. If
NULL
, a default misclassification matrix is created where all misclassifications have a cost of 1 and correct classifications have a cost of 0.- response
A factor representing the response variable with multiple classes.
Value
A list containing:
- prior
A normalized vector of prior probabilities for each class.
- misClassCost
A square matrix representing the misclassification costs, with rows and columns labeled by the levels of the response variable.
Examples
# Example 1: Using default prior and misClassCost
response <- factor(c('A', 'B', 'A', 'B', 'C', 'A'))
checkPriorAndMisClassCost(NULL, NULL, response)
#> $prior
#> A B C
#> 0.5000000 0.3333333 0.1666667
#>
#> $misClassCost
#> A B C
#> A 0 1 1
#> B 1 0 1
#> C 1 1 0
#>
# Example 2: Providing custom prior and misClassCost
prior <- c(A = 1, B = 1, C = 2)
misClassCost <- matrix(c(0, 2, 10,
1, 0, 10,
1, 2, 0), nrow = 3, byrow = TRUE)
checkPriorAndMisClassCost(prior, misClassCost, response)
#> $prior
#> A B C
#> 0.25 0.25 0.50
#>
#> $misClassCost
#> A B C
#> A 0 2 10
#> B 1 0 10
#> C 1 2 0
#>